48 lines
1.0 KiB
Go
48 lines
1.0 KiB
Go
package proto
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strconv"
|
|
)
|
|
|
|
type HexByte byte
|
|
|
|
type ProtoComplete struct {
|
|
Header ProtocolHeader `json:"protocol"`
|
|
Protocols map[string][]ProtoMessage `json:"protocols"`
|
|
}
|
|
|
|
type ProtocolHeader struct {
|
|
StartByte HexByte `json:"start_byte"`
|
|
MessageLength int `json:"message_length"`
|
|
MaxPayload int `json:"max_payload"`
|
|
Checksum string `json:"checksum"`
|
|
}
|
|
|
|
type ProtoMessage struct {
|
|
Name string `json:"name"`
|
|
ID HexByte `json:"id"`
|
|
Payload []ProtoMessagePayload `json:"payload,omitempty"` // optional falls kein payload
|
|
}
|
|
|
|
type ProtoMessagePayload struct {
|
|
Name string `json:"name"`
|
|
DataType string `json:"type"`
|
|
ArrayLength int `json:"array,omitempty"` // optional falls kein array
|
|
}
|
|
|
|
func (h *HexByte) UnmarshalJSON(data []byte) error {
|
|
var s string
|
|
if err := json.Unmarshal(data, &s); err != nil { // turn into go string
|
|
return err
|
|
}
|
|
|
|
i, err := strconv.ParseUint(s, 0, 8)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
*h = HexByte(i)
|
|
return nil
|
|
}
|