Imports System.Net

Public Class UtsWebPacket
    Implements IDataPacket

    Private _packetSn As Integer

    Sub New()
        _packetSn = 0
    End Sub


    Public Function FillPacket(cmd As Byte) As Byte()
        Dim packet(PacketBits.Command) As Byte
        packet(PacketBits.Head) = &HAA
        packet(PacketBits.SerialNumber) = CByte(_packetSn)
        packet(PacketBits.ParamLength) = &H0
        packet(PacketBits.ParamLength + 1) = &H0
        packet(PacketBits.CheckValue) = &H0
        packet(PacketBits.Command) = cmd
        packet(PacketBits.CheckValue) = CheckSum.GetPacketCheck(packet)

        _packetSn += 1
        If _packetSn > &HFF Then _packetSn = 0
        Return packet
    End Function


    ''' <summary>
    ''' 用于填充发送包
    ''' </summary>
    ''' <param name="cmd">发送包命令</param>
    ''' <param name="param">发送包参数</param>
    ''' <returns></returns>
    Public Function FillPacket(cmd As Byte, param() As Byte) As Byte() Implements IDataPacket.FillPacket
        Dim packet(PacketBits.Command + param.Length) As Byte
        packet(PacketBits.Head) = &HAA
        packet(PacketBits.SerialNumber) = CByte(_packetSn)
        Array.Copy(BitConverter.GetBytes(CShort(param.Length)), 0, packet, PacketBits.ParamLength, 2)
        packet(PacketBits.CheckValue) = &H0
        packet(PacketBits.Command) = cmd

        Array.Copy(param, 0, packet, PacketBits.Param, param.Length)
        packet(PacketBits.CheckValue) = CheckSum.GetPacketCheck(packet)

        _packetSn += 1
        If _packetSn > &HFF Then _packetSn = 0
        Return packet
    End Function

    ''' <summary>
    ''' 一般用于填充回复包
    ''' </summary>
    ''' <param name="sn">接收包序号</param>
    ''' <param name="cmd">接收包命令</param>
    ''' <param name="param">回复包参数</param>
    ''' <returns></returns>
    Public Function FillPacket(sn As Byte, cmd As Byte, param() As Byte) As Byte()
        Dim packet(PacketBits.Command + param.Length) As Byte
        packet(PacketBits.Head) = &HAA
        packet(PacketBits.SerialNumber) = sn
        Array.Copy(BitConverter.GetBytes(CShort(param.Length)), 0, packet, PacketBits.ParamLength, 2)
        packet(PacketBits.CheckValue) = &H0
        packet(PacketBits.Command) = cmd

        Array.Copy(param, 0, packet, PacketBits.Param, param.Length)

        packet(PacketBits.CheckValue) = CheckSum.GetPacketCheck(packet)

        Return packet
    End Function


    Public Function CheckPacket(packet() As Byte) As Boolean Implements IDataPacket.CheckPacket
        If packet(PacketBits.Head) <> &HAA Then
            Throw New Exception($"Invalid Packet Head!Src:{packet(PacketBits.Head)} Dest:{&HAA}")
        End If

        'If packet(PacketBits.SerialNumber) <> _packetSn Then
        '    Throw New Exception($"Invalid Packet Sn!Src: {packet(PacketBits.SerialNumber)} Dest:{_packetSn}")
        'End If

        Dim destLength As Integer = packet.Length - PacketBits.Param

        If BitConverter.ToInt16(packet, PacketBits.ParamLength) <> destLength Then
            Throw New Exception($"Invalid Packet Lengnt!Src:{packet(PacketBits.ParamLength)} Dest:{destLength}")
        End If

        If CheckSum.GetPacketCheck(packet) <> &H0 Then
            Throw New Exception($"Invalid Packet CheckValue!Src:{BitConverter.ToString(packet)}")
        End If

        Return True
    End Function


    Enum PacketBits
        ''' <summary>包头</summary>
        Head

        ''' <summary>序号</summary>
        SerialNumber

        ''' <summary>包长,两位,小端模式</summary>
        ParamLength

        ''' <summary>校验</summary>
        CheckValue = ParamLength + 2

        ''' <summary>命令</summary>
        Command

        ''' <summary>参数</summary>
        Param
    End Enum


    <Flags>
    Enum Commands
        ''' <summary>心跳包</summary>
        Heartbeat = &H1

        ''' <summary>日志上报</summary>
        SetLogType = &H2

        ''' <summary>日志上报</summary>
        GetLogType = &H3


        ''' <summary>增加服务任务</summary>
        AddServiceTask = &H11

        ''' <summary>删除服务任务</summary>
        DelServiceTask = &H12

        ''' <summary>获取服务任务</summary>
        GetServiceTask = &H13

        ''' <summary>设置服务任务</summary>
        SetServiceTask = &H14

        ''' <summary>开启服务任务</summary>
        StartServiceTask = &H15

        ''' <summary>停止服务任务</summary>
        StopServiceTask = &H16

        ''' <summary>重启服务任务</summary>
        RestartServiceTask = &H17

        ''' <summary>上传文件</summary>
        UploadFile = &H21

        ''' <summary>读取指定文件大小</summary>
        ReadFileSize = &H22
    End Enum


    Public Class PublicIpChangedEventArgs
        Inherits EventArgs

        Sub New(ip As IPAddress)
            PublicIP = ip
        End Sub

        Public Property PublicIP As IPAddress
    End Class

    Public Class AddTaskEventArgs
        Inherits EventArgs

        Sub New(jsonString As String)
            Me.JsonString = jsonString
        End Sub

        Public Property JsonString As String
    End Class
End Class
