Namespace Parameters
    Public Class DeviceStatusFormat
        Inherits ParameterFormat

        Enum E_FieldStartIndex
            E00_Command = 2
            E01_BoardType = 3
            E02_BoardID = 4
            E03_IpAddress = 5
            E04_MacAddress = 9
            E05_FirmwareVersion = 15
            E06_DeviceName = 19
            E07_Checksum = 83
        End Enum

        Enum E_FieldLength
            E00_Command = 1
            E01_BoardType = 1
            E02_BoardID = 1
            E03_IpAddress = 4
            E04_MacAddress = 6
            E05_FirmwareVersion = 4
            E06_DeviceName = 64
            E07_Checksum = 1
        End Enum

        Private _commandCode As Byte
        Private _boardType As Byte
        Private _boardID As Byte
        Private _ipAddress As String
        Private _macAddress As String
        Private _firmwareVersion As String
        Private _deviceName As String

        ReadOnly Property DeviceName() As String
            Get
                Return _deviceName
            End Get
        End Property

        ReadOnly Property FirmwareVersion() As String
            Get
                Return _firmwareVersion
            End Get
        End Property

        ReadOnly Property MacAddress() As String
            Get
                Return _macAddress
            End Get
        End Property

        ReadOnly Property IpAddress() As String
            Get
                Return _ipAddress
            End Get
        End Property

        ReadOnly Property CommandCode() As Byte
            Get
                Return _commandCode
            End Get
        End Property

        ReadOnly Property BoardType() As Byte
            Get
                Return _boardType
            End Get
        End Property

        ReadOnly Property BoardID() As Byte
            Get
                Return _boardID
            End Get
        End Property



        Public Overloads Overrides Sub ParseParameter(ByRef parameterByteArray() As Byte)
            _errorCode = E_ErrorCodes.E01_Failed
            If parameterByteArray.Length <> 84 Then
                Throw New Exception("Device status bytes must be 84. Getting length is " & parameterByteArray.Length)
                Exit Sub
            End If

            If parameterByteArray(0) <> &HFE Then
                Throw New Exception("Invalid [Type] value (0xFE). Getting value is " & parameterByteArray(0))
                Exit Sub
            End If

            If parameterByteArray(1) <> &H54 Then
                Throw New Exception("Invalid [Length] value (0x54). Getting value is " & parameterByteArray(0))
                Exit Sub
            End If

            Dim stepIndex As Integer
            Dim errorMessage As String

            stepIndex = 0
            errorMessage = ""
            'Start parse the fields from the input data bytes 
            Do
                Select Case stepIndex
                    Case 0
                        'Get Command field
                        _commandCode = parameterByteArray(E_FieldStartIndex.E00_Command)
                    Case 1
                        'Get Board Type field
                        _boardType = parameterByteArray(E_FieldStartIndex.E01_BoardType)
                    Case 2
                        'Get Board ID field
                        _boardID = parameterByteArray(E_FieldStartIndex.E02_BoardID)
                    Case 3
                        'Get IP address
                        _ipAddress = parameterByteArray(E_FieldStartIndex.E03_IpAddress) & "." & _
                                     parameterByteArray(E_FieldStartIndex.E03_IpAddress + 1) & "." & _
                                     parameterByteArray(E_FieldStartIndex.E03_IpAddress + 2) & "." & _
                                     parameterByteArray(E_FieldStartIndex.E03_IpAddress + 3)
                    Case 4
                        'Get MAC address
                        _macAddress = Hex(parameterByteArray(E_FieldStartIndex.E04_MacAddress + 5)).PadLeft(2, "0") & "." & _
                                      Hex(parameterByteArray(E_FieldStartIndex.E04_MacAddress + 4)).PadLeft(2, "0") & "." & _
                                      Hex(parameterByteArray(E_FieldStartIndex.E04_MacAddress + 3)).PadLeft(2, "0") & "." & _
                                      Hex(parameterByteArray(E_FieldStartIndex.E04_MacAddress + 2)).PadLeft(2, "0") & "." & _
                                      Hex(parameterByteArray(E_FieldStartIndex.E04_MacAddress + 1)).PadLeft(2, "0") & "." & _
                                      Hex(parameterByteArray(E_FieldStartIndex.E04_MacAddress)).PadLeft(2, "0")
                    Case 5
                        'Get Firmware Version
                        _firmwareVersion = parameterByteArray(E_FieldStartIndex.E05_FirmwareVersion + 3).ToString & "." & _
                                           parameterByteArray(E_FieldStartIndex.E05_FirmwareVersion + 2).ToString & "." & _
                                           parameterByteArray(E_FieldStartIndex.E05_FirmwareVersion + 1).ToString & "." & _
                                           parameterByteArray(E_FieldStartIndex.E05_FirmwareVersion).ToString
                    Case 6
                        'Get Device Name
                        _deviceName = MyLib.Encoding.GetString(parameterByteArray, _
                                                               E_FieldStartIndex.E06_DeviceName, _
                                                               E_FieldLength.E06_DeviceName)
                    Case 7
                        _errorCode = E_ErrorCodes.E00_Success
                        Exit Do
                    Case Else
                        Stop
                        Exit Do
                End Select

                stepIndex += 1
            Loop
        End Sub

        Public Overloads Overrides Sub ParseParameter(ByRef parameterByteArray() As Byte, ByVal startIndex As Integer, ByVal length As Integer)
            ParseParameter(parameterByteArray, 0, parameterByteArray.Length)
        End Sub
    End Class
End Namespace


