Imports System.ComponentModel
Imports System.Reflection
Imports System.Threading
Imports _485_BurningTool.CommunicationFlow

Public MustInherit Class CommunicationProtocol
    ' Inherits CommunicationFlow

    '创建通信协议对象
    'Public m_CommunicationProtocol As CommunicationProtocol
    ''创建运行线程
    'Public m_RunThread As Thread
    '通信发送器对象
    Public m_Transmitter As Transmitter
    Public UpdateUIqueue As Queue(Of (Control, RuningLogConfig, lev As Integer))

    Public Shared Function CreateAbstractObject(communicationType As CommunicationType, protocolType As ProtocolEnum) As CommunicationProtocol
        Select Case protocolType
            Case ProtocolEnum.BLV_Bootloader
                Return BLV_Bootloader.CreateProtocol(communicationType)
            Case Else
                Return Nothing
        End Select

    End Function
    Public Sub AddQueue(c As Control, rl As RuningLogConfig, Optional lev As Integer = 0)
        If IsNothing(UpdateUIqueue) OrElse IsNothing(c) OrElse IsNothing(rl) Then Return
        UpdateUIqueue.Enqueue((c, rl, lev))
    End Sub
    '组装对应命令数据
    Public MustOverride Function AssembleCommandData(command As Integer, ParamArray args() As String) As Byte()
    Public MustOverride Function CheckData(data As Byte()) As Boolean
    '设置通信协议参数
    Public MustOverride Sub SetProtocolParam(ParamArray args() As String)
    Public MustOverride Sub SetSearchEquipmentProcessSwitch(iswitch As Boolean)

    'Public MustOverride Sub SendCommand(command As Cmdconfig)

    Public MustOverride Function MainProcess(g_WorkflowType As Integer, g_Cmdconfig As List(Of (number As Integer, Cmdconfig)), ParamArray args() As Object) As Boolean
    '解析接收数据
    Public MustOverride Function ParseReceiveData(gCmdconfig As Cmdconfig, data As Byte()) As Boolean
    Public MustOverride Function ParseReceiveData2(gCmdconfig As Cmdconfig, data As Byte()) As Boolean
    Public MustOverride Function Runing(Parameterlist As Parameterlist, ByRef Isruning As Boolean) As Boolean

    'Private Sub Runingthread(ParamArray args() As Object)

    'End Sub

    'Public Function RuningMainProcess(g_WorkflowType As Integer, g_Cmdconfig As List(Of (number As Integer, Cmdconfig)), ParamArray args() As Object) As Boolean
    '    '判断线程是否创建
    '    If m_RunThread Is Nothing Then
    '        '创建线程
    '        m_RunThread = New Thread(New ParameterizedThreadStart(AddressOf Runingthread))
    '        m_RunThread.Start((g_WorkflowType, g_Cmdconfig, args))

    '        '启动线程
    '        m_RunThread.Start()
    '    Else
    '        '判断线程是否运行
    '        If m_RunThread.IsAlive Then
    '            ' 关闭线程
    '            m_RunThread.Abort()

    '        End If
    '        '创建线程
    '        'm_RunThread = New Thread(AddressOf MainProcess)
    '        '启动线程
    '        m_RunThread.Start()
    '    End If
    'End Function
    'Public Sub StopRuningMainProcess()
    '    If m_RunThread Is Nothing Then
    '        Exit Sub
    '    Else
    '        If m_RunThread.IsAlive Then
    '            m_RunThread.Abort()
    '        End If
    '    End If
    'End Sub

End Class

'命令发送信息类
Public Class Cmdconfig

    '命令类型
    Public CmdValue As Integer
    '命令参数数组
    Public CmdArgs As List(Of Object)
    '发送SN号
    Public CmdSN As Integer
    '发送重发次数 
    Public CmdSendCount As Integer
    '发送次数
    Public CmdSendCountNow As Integer
    '持续发生时间
    Public CmdSendcontinueTime As Integer
    '发送间隔时间
    Public CmdSendInterval As Integer
    '发送命令
    Public CmdSendData As Byte()
    '接收命令集合  时间+数据
    Public CmdReceiveData As List(Of (DateTime, Byte()))
    '接收命令超时 秒
    Public CmdReceiveTimeout As Integer
    '接收命令校验
    Public CmdReceiveCheck As Boolean
    '接收命令状态
    Public CmdReceiveStatus As Boolean

    '接收命令错误信息
    Public CmdReceiveError As String
    '发送命令时间
    Public CmdSendTime As DateTime

    '针对升级写入的序号
    Public CmdWriteIndex As Integer

    '协议对象
    Public Protocol As CommunicationProtocol
    '节点创建时间
    Public NodeCreateTime As DateTime

    '附加其他协议列表
    Public OtherProtocol As UDPCommunicationnode



    '创建对象并设置对应类中变量   

    Sub New(args() As Integer)
        CmdValue = Integer.Parse(args(0))
        CmdSN = Integer.Parse(args(1))
        CmdSendCount = Integer.Parse(args(2))
        CmdSendInterval = Integer.Parse(args(3))
        CmdReceiveData = New List(Of (DateTime, Byte()))
        CmdReceiveTimeout = Integer.Parse(args(4))
        CmdArgs = New List(Of Object)
        CmdSendTime = DateTime.Now
        NodeCreateTime = DateTime.Now
        CmdWriteIndex = 0
        CmdSendcontinueTime = 0
        CmdSendCountNow = 0
        OtherProtocol = New UDPCommunicationnode
    End Sub
    '设置发送持续时间
    Public Sub setCmdSendcontinueTime(iswitch As Integer)
        CmdSendcontinueTime = iswitch
    End Sub
    Public Sub setCmdReceiveStatus(iswitch As Boolean)
        CmdReceiveStatus = iswitch
    End Sub
    Public Sub SetProtocol(nProtocol As CommunicationProtocol)
        Protocol = nProtocol
    End Sub
    Sub New()
        CmdReceiveData = New List(Of (DateTime, Byte()))
        CmdArgs = New List(Of Object)
    End Sub

    Public Sub setUDPCommunicationnode()
        OtherProtocol.UDP_TRANSPARENT_MODE_COMMAND_REPLY_STATUS = 0
        OtherProtocol.UDP_TRANSPARENT_MODE_COMMAND_REPLY_RESULT = 0
        OtherProtocol.UDP_QUERY_HOST_CURRENT_MODE_COMMAND_REPLY_STATUS = 0
        OtherProtocol.UDP_QUERY_HOST_CURRENT_MODE_COMMAND_REPLY_RESULT = 0
    End Sub

End Class

Public Class UDPCommunicationnode
    '透传模式命令回复状态
    Public UDP_TRANSPARENT_MODE_COMMAND_REPLY_STATUS As Integer = 0
    '透传模式命令回复结果
    Public UDP_TRANSPARENT_MODE_COMMAND_REPLY_RESULT As Integer = 0
    '查询主机当前模式命令回复状态
    Public UDP_QUERY_HOST_CURRENT_MODE_COMMAND_REPLY_STATUS As Integer = 0
    '查询主机当前模式命令回复结果
    Public UDP_QUERY_HOST_CURRENT_MODE_COMMAND_REPLY_RESULT As Integer = 0
    Sub New()
        UDP_TRANSPARENT_MODE_COMMAND_REPLY_STATUS = 0
        UDP_TRANSPARENT_MODE_COMMAND_REPLY_RESULT = 0
        UDP_QUERY_HOST_CURRENT_MODE_COMMAND_REPLY_STATUS = 0
        UDP_QUERY_HOST_CURRENT_MODE_COMMAND_REPLY_RESULT = 0
    End Sub
End Class