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
|
||||
|
||||
Reference in New Issue
Block a user