Imports System.ComponentModel
Imports System.Reflection
Imports System.Threading
Imports _485_BurningTool.DataProcessing
'通信流程
Public MustInherit Class CommunicationFlow
    '通信类型枚举
    Public Enum CommunicationType
        None = 0
        <Description("串口")>
        SerialPort = 1
        <Description(" UDP")>
        Udp
        <Description("TCP")>
        TCP

        maximum
    End Enum
    '协议类型枚举
    Public Enum ProtocolEnum
        <Description("BLV_Bootloader")>
        BLV_Bootloader

        maximum
    End Enum
    '通信器变量
    Public m_CommunicationTypeIndex As CommunicationType = CommunicationType.SerialPort
    Public m_ProtocolEnumIndex As ProtocolEnum

    '通信发送器对象
    Public m_Transmitter As Transmitter
    '通信接协议对象
    Public m_Receiver As CommunicationProtocol
    '运行线程
    Public m_RunThread As Thread
    '线程控制标志
    Public m_RunThreadFlag As Boolean = False
    '线程 参数
    Public m_Parameterlist As Parameterlist
    '更新界面线程
    Public m_UpdateUI As Thread




    '切换通信器
    Public Sub ChangeCommunicationType(portname As String, gControl As Control)
        Dim CommunicationStr As String
        For i = CommunicationType.SerialPort To CommunicationType.maximum
            CommunicationStr = GetEnumDescription(CType(i, CommunicationType))
            If CommunicationStr = portname Then
                If Not IsNothing(m_Transmitter) AndAlso m_Transmitter.IsTransmitter Then m_Transmitter.CloseTransmitter()
                m_CommunicationTypeIndex = i
                m_Transmitter = Transmitter.CreateTransmitter(m_CommunicationTypeIndex, gControl)

                Return
            End If
        Next
    End Sub
    '切换协议
    Public Sub ChangeProtocol(Protocolstr As String)
        Dim CommunicationStr As String
        For i = ProtocolEnum.BLV_Bootloader To ProtocolEnum.maximum - 1
            CommunicationStr = GetEnumDescription(CType(i, ProtocolEnum))
            If CommunicationStr = Protocolstr Then
                m_ProtocolEnumIndex = i
                'If m_CommunicationFlow.m_Receiver.IsCreateProtocol Then m_CommunicationFlow.m_Receiver.CloseTransmitter()
                'If m_CommunicationTypeIndex = CommunicationType.None Then
                '    MsgBox("请先选择通信器通信方式!")
                '    Return
                'End If
                m_Receiver = CommunicationProtocol.CreateAbstractObject(m_CommunicationTypeIndex, m_ProtocolEnumIndex)
                If Not IsNothing(m_Transmitter) Then
                    m_Receiver.m_Transmitter = m_Transmitter
                End If
                ' m_Receiver.m_Transmitter = m_Transmitter
                Return
            End If
        Next
    End Sub
    '刷新发送器
    Public Sub RefreshTransmitter()
        If IsNothing(m_Receiver) Then Return
        m_Receiver.m_Transmitter = m_Transmitter

    End Sub
    '启动线程
    Public Sub StartThread()
        '判断线程是否启动
        If m_RunThread Is Nothing OrElse m_RunThread.ThreadState = ThreadState.Stopped Then
            m_RunThread = New Thread(AddressOf Run)
            m_RunThread.Start()
            If m_UpdateUI Is Nothing OrElse m_UpdateUI.ThreadState = ThreadState.Stopped Then
                m_UpdateUI = New Thread(AddressOf RunUpdateUI)
                m_UpdateUI.Start()
            End If
        Else
            'm_RunThread.Abort()
            'm_RunThread = New Thread(AddressOf Run)
            'm_RunThread.Start()
        End If
    End Sub
    '停止线程
    Public Sub StopThread()
        If Not IsNothing(m_RunThread) Then
            m_RunThread.Abort()
            m_RunThread = Nothing

            m_UpdateUI.Abort()
            m_UpdateUI = Nothing
        End If


    End Sub

    '更新UI线程
    Private Sub RunUpdateUI()
        While True
            Thread.Sleep(5)
            If IsNothing(m_Receiver) OrElse IsNothing(m_Receiver.UpdateUIqueue) Then Continue While
            If m_Receiver.UpdateUIqueue.Count > 0 Then
                Dim str As (Control, RuningLogConfig, Integer) = m_Receiver.UpdateUIqueue.Dequeue()
                RuningLog.OutputLogsToTheControl(str.Item1, str.Item2, str.Item3)
            End If
        End While
    End Sub
    '添加队列



    '设置线程参数
    Public Sub SetThreadParameter(Parameterlist As Parameterlist)
        Parameterlist.CommunicationTypeIndex = m_Transmitter.CommunicationTypeindex
        m_Parameterlist = Parameterlist
        m_Receiver.m_Transmitter.ClearSendData()
        SetThreadFlag(True)
        StartThread()
    End Sub

    '设置线程标志
    Public Sub SetThreadFlag(flag As Boolean)
        m_RunThreadFlag = flag
    End Sub
    '判断当前是否运行
    Public Function GetRuningNode() As Boolean
        If m_Parameterlist.EnableFlag Then

            'm_Parameterlist.Action

            Return False
        End If


    End Function

    '线程运行
    Private Sub Run()
        While m_RunThreadFlag
            If m_Parameterlist.EnableFlag Then
                m_Receiver.Runing(m_Parameterlist, m_RunThreadFlag)
            End If
            Thread.Sleep(5)
        End While
    End Sub
    Public Function IsCreateProtocol() As Boolean
        If IsNothing(m_Receiver) Then Return False
        Return True
    End Function




End Class

Public Class CommunicationProtocolEntity
    Inherits CommunicationFlow
    Public Sub New()
        StartThread()
    End Sub

End Class
Public Class Parameterlist
    '启用标记
    Public EnableFlag As Boolean
    '动作类型
    Public Action As UInt32
    '下一步动作
    Public NextAction As UInt32
    'Public Action As 
    '起始地址
    Public StartAddress As UInt32
    '接收地址
    Public ReceiveAddress As UInt32
    '接收地址范围长度
    Public ReceiveAddressRangeLength As UInt32
    '设备类型
    Public DeviceType As UInt32
    'bootloader 超时 
    Public BootloaderTimeout As UInt32
    '有效时间
    Public ValidTime As UInt32

    'boot 波特率
    Public BootBaudRate As UInt32

    'APP波特率
    Public AppBaudRate As UInt32
    '升级波特率
    Public UpgradeBaudRate As UInt32

    '当前步骤
    Public CurrentStep As Integer
    '成功跳转
    Public SuccessJump As Integer
    '失败跳转
    Public FailureJump As Integer
    '按钮
    Public btn As Button
    '表格
    Public DataGrid As DataGridView
    '富文本框
    Public RText_OutputText As RichTextBox
    '流程列表
    '通信器类型
    Public CommunicationTypeIndex As Integer
    Public ProcessList As List(Of (number As Integer, Cmdconfig))
    Public addtypeli As List(Of (Integer, Integer))
    Public addtypeliindx As Integer
    Public DevNumber As Integer
    Public NTimeOut As Integer
    Public Throughport As Integer '
    Public TransnDuion As Integer

    Sub New()
        DevNumber = 1
        BootBaudRate = 2400
        NTimeOut = 10
    End Sub
End Class