i'll got with this I guess
This commit is contained in:
+53
-2
@@ -1,8 +1,59 @@
|
||||
package telemetry
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type DataType uint8
|
||||
|
||||
const (
|
||||
DataTypeUINT8 DataType = 0
|
||||
DataTypeINT8 DataType = 1
|
||||
DataTypeUINT16 DataType = 2
|
||||
DataTypeINT16 DataType = 3
|
||||
DataTypeSTRING DataType = 4
|
||||
)
|
||||
|
||||
type TelemetryField struct {
|
||||
Parser func()
|
||||
Value any
|
||||
Type DataType
|
||||
Value any
|
||||
}
|
||||
|
||||
// Pack will pack this current TelemetryField into bytes to send over the wire
|
||||
// Format:
|
||||
// 0x00 - DataType
|
||||
// 0x01 - if its a (u)int8
|
||||
// or
|
||||
// 0x01 - if its a (u)int16 - first byte
|
||||
// 0x01 - if its a (u)int16 - second byte
|
||||
// or
|
||||
// 0x02 - str len max is 255 chars
|
||||
// [0x02] - str
|
||||
func (tf *TelemetryField) Pack() []byte {
|
||||
buf := new(bytes.Buffer)
|
||||
|
||||
buf.WriteByte(uint8(tf.Type))
|
||||
|
||||
switch tf.Type {
|
||||
case DataTypeINT8:
|
||||
buf.WriteByte(uint8(tf.Value.(int8)))
|
||||
case DataTypeUINT8:
|
||||
buf.WriteByte(uint8(tf.Value.(uint8)))
|
||||
case DataTypeINT16, DataTypeUINT16:
|
||||
binary.Write(buf, binary.LittleEndian, tf.Value)
|
||||
case DataTypeSTRING:
|
||||
str := tf.Value.(string)
|
||||
buf.WriteByte(uint8(len(str)))
|
||||
buf.WriteString(str)
|
||||
}
|
||||
|
||||
return buf.Bytes()
|
||||
}
|
||||
|
||||
func (tf *TelemetryField) String() string {
|
||||
return fmt.Sprintf("%v", tf.Value)
|
||||
}
|
||||
|
||||
// NOTE: Replace values with a more appropriate custom field approach where
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package telemetry
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"math"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_TelemetryField(t *testing.T) {
|
||||
type TFTest struct {
|
||||
name string
|
||||
tf TelemetryField
|
||||
expect []byte
|
||||
}
|
||||
|
||||
tests := []TFTest{
|
||||
{
|
||||
name: "test_max_uint8",
|
||||
tf: TelemetryField{
|
||||
Type: DataTypeUINT8,
|
||||
Value: uint8(math.MaxUint8),
|
||||
},
|
||||
expect: []byte{0x00, 0xFF},
|
||||
},
|
||||
{
|
||||
name: "test_max_int8",
|
||||
tf: TelemetryField{
|
||||
Type: DataTypeINT8,
|
||||
Value: int8(math.MaxInt8),
|
||||
},
|
||||
expect: []byte{0x01, 0x7F},
|
||||
},
|
||||
{
|
||||
name: "test_max_uint16",
|
||||
tf: TelemetryField{
|
||||
Type: DataTypeUINT16,
|
||||
Value: uint16(math.MaxUint16),
|
||||
},
|
||||
expect: []byte{0x02, 0xFF, 0xFF},
|
||||
},
|
||||
{
|
||||
name: "test_max_int16",
|
||||
tf: TelemetryField{
|
||||
Type: DataTypeINT16,
|
||||
Value: int16(math.MaxInt16),
|
||||
},
|
||||
expect: []byte{0x03, 0xFF, 0x7F},
|
||||
},
|
||||
{
|
||||
name: "test_string",
|
||||
tf: TelemetryField{
|
||||
Type: DataTypeSTRING,
|
||||
Value: "a cool string!",
|
||||
},
|
||||
expect: []byte{0x04, 0x0E, 0x61, 0x20, 0x63, 0x6F, 0x6F, 0x6C, 0x20, 0x73,
|
||||
0x74, 0x72, 0x69, 0x6E, 0x67, 0x21},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
result := test.tf.Pack()
|
||||
|
||||
if !bytes.Equal(result, test.expect) {
|
||||
t.Errorf("\nTest: %s\nExpected: %v\nGot: %v\n", test.name, test.expect, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user