Author SHA1 Message Date
esilva bd10d6b4d6 subscription fix. should subscribe too fields at once 2026-09-18 00:07:27 +01:00
esilva c888dc5436 remove commented code and made the subscription be based on a list 2026-09-18 00:05:50 +01:00
esilva dda300ce6b decoupled the winIDs from the telemetry data
hell yeah, brother!
2026-09-17 23:55:54 +01:00
esilva 1ac4ab1865 logger fixes on cdashdisplay device implementation 2026-09-17 23:08:36 +01:00
esilva a6ab64117c this is a doozy - device auto discovery and handling
The main goal was to decouple more things
We got devices being looked for in the background and the stream view
is like a device now too
2026-09-17 23:01:50 +01:00
esilva 1321977f18 updated beamng provider for the new SDK 2026-09-17 23:01:50 +01:00
esilva ffc03e9e49 added the IsAlive check for beamng 2026-09-17 23:01:50 +01:00
esilva 15b997c22f fixed some little UI bugs
- We could open the CDashDisplay specific layout before it existed -
Crash!
- We could open the stream UI before there was a provider- Crash!
2026-09-17 23:01:50 +01:00
esilva 59fa825da1 removing some unecessary logs 2026-09-17 23:01:50 +01:00
esilva dbd4086bf9 Provider loop discovery almost finished
Provider lookup will lookup on startup, do the healthcheck until the
stream starts and also restart on stream closure or provider stall
2026-09-17 23:01:50 +01:00
esilva c1366b010f remove this very verbose log 2026-09-17 23:01:50 +01:00
esilva d6362eeeb5 remove the fatals from here, but I do need to do error handling there 2026-09-17 23:01:50 +01:00
esilva 1b9a5173b6 Updated the function that checks for the sim running and how we stream data 2026-09-17 23:01:50 +01:00
esilva 7675c7e34a fixed the iracing Gear transform 2026-09-17 23:01:50 +01:00
esilva 922c2df88d Don't allow program to crash if there's no active provider on StartStream action 2026-09-17 23:01:50 +01:00
esilva 6b09eb5be0 some fixes regarding iracing provider transforms and working the auto detection 2026-09-17 23:01:50 +01:00
esilva 1d022a4fa8 update the iracing provider to be on the same page as the BeamNG one
Deprecated the Fetch and Transform methods
2026-09-17 23:01:50 +01:00
esilva a251f45859 basic loop to discover providers 2026-09-17 23:01:50 +01:00
esilva afc4b794bb Created a devices service and moved the CDashDisplay service to the sahdow realm 2026-09-17 23:01:50 +01:00
esilva 66012b6904 Merge pull request 'remove old logger. We use actual loggers now' (#10) from remove-old-logger into master
Reviewed-on: #10
2026-09-17 23:00:17 +01:00
esilva a807eb3782 remove old logger. We use actual loggers now 2026-09-17 23:00:02 +01:00
18 changed files with 192 additions and 206 deletions
+1 -4
View File
@@ -1,20 +1,17 @@
package cmd
import (
"esdi/logger"
esdi "esdi/oldEsdi"
"github.com/spf13/cobra"
)
func liveTelemetryCmdAction(cmd *cobra.Command, args []string) {
log := logger.GetInstance()
ddPort, _ := cmd.Flags().GetString("port")
outputFile, _ := cmd.Flags().GetString("out")
sessionFile, _ := cmd.Flags().GetString("session")
log.Printf("Called `live`:\nPort: '%s'\nOutFile: '%s'\n", ddPort, outputFile)
// log.Printf("Called `live`:\nPort: '%s'\nOutFile: '%s'\n", ddPort, outputFile)
esdi.RunLiveTelemetry(ddPort, outputFile, sessionFile)
}
+2 -3
View File
@@ -1,7 +1,6 @@
package cmd
import (
"esdi/logger"
esdi "esdi/oldEsdi"
// "github.com/ESilva15/goirsdk"
@@ -10,14 +9,14 @@ import (
)
func offlineTelemetryCmdAction(cmd *cobra.Command, args []string) {
log := logger.GetInstance()
// log := logger.GetInstance()
ddPort, _ := cmd.Flags().GetString("port")
inFile, _ := cmd.Flags().GetString("in")
outFile, _ := cmd.Flags().GetString("out")
sessionFile, _ := cmd.Flags().GetString("session")
log.Printf("Called `offline`:\nPort: '%s'\nSource: '%s'\nOutFile: '%s'\n", ddPort, inFile, outFile)
// log.Printf("Called `offline`:\nPort: '%s'\nSource: '%s'\nOutFile: '%s'\n", ddPort, inFile, outFile)
esdi.RunOfflineTelemetry(ddPort, inFile, outFile, sessionFile)
}
+11 -9
View File
@@ -1,10 +1,12 @@
package cdashdisplay
import (
"fmt"
"log/slog"
"time"
"esdi/peripheral/communication"
"esdi/peripheral/communication/packets"
"fmt"
"time"
"github.com/tarm/serial"
portp "go.bug.st/serial"
@@ -46,11 +48,11 @@ func findDisplayPort() (*communication.WalkieTalkie, error) {
return nil, err
}
pLogger.Info(fmt.Sprintf("Looking into %v", ports))
slog.Info(fmt.Sprintf("Looking into %v", ports))
var wt *communication.WalkieTalkie
for _, port := range ports {
pLogger.Info(fmt.Sprintf("Trying port %s", port))
slog.Info(fmt.Sprintf("Trying port %s", port))
wt = &communication.WalkieTalkie{
Cfg: &serial.Config{
@@ -60,7 +62,7 @@ func findDisplayPort() (*communication.WalkieTalkie, error) {
},
}
pLogger.Info(fmt.Sprintf("Started probing port %s", port))
slog.Info(fmt.Sprintf("Started probing port %s", port))
probeResult := make(chan error, 1)
@@ -76,14 +78,14 @@ func findDisplayPort() (*communication.WalkieTalkie, error) {
err = fmt.Errorf("probe completely hung/timed out: %s", port)
}
pLogger.Info(fmt.Sprintf("Finished probing port %s", port))
slog.Info(fmt.Sprintf("Finished probing port %s", port))
if err == nil {
pLogger.Info(fmt.Sprintf("Success probing port %s: %+v", port, err))
slog.Info(fmt.Sprintf("Success probing port %s: %+v", port, err))
break
}
pLogger.Info(fmt.Sprintf("wasn't port %s", port))
slog.Info(fmt.Sprintf("wasn't port %s", port))
wt = nil
}
@@ -91,6 +93,6 @@ func findDisplayPort() (*communication.WalkieTalkie, error) {
return nil, fmt.Errorf("couldn't find cdashdisplay")
}
pLogger.Info(fmt.Sprintf("found cdashdisplay on port: %s", wt.Cfg.Name))
slog.Info(fmt.Sprintf("found cdashdisplay on port: %s", wt.Cfg.Name))
return wt, nil
}
+55 -25
View File
@@ -8,6 +8,7 @@ import (
"log/slog"
"os"
"path"
"sync"
"time"
helper "esdi/helpers"
@@ -19,13 +20,6 @@ import (
"gopkg.in/yaml.v3"
)
// TODO: remove this logger. It should be on the device struct
var pLogger *slog.Logger
func SetLogger(l *slog.Logger) {
pLogger = l
}
// I have to move this to some kind of configuration place
const (
layoutsDir = "./layouts/"
@@ -113,28 +107,58 @@ func NewCDashState() *CDashState {
}
type CDashDisplay struct {
WT *communication.WalkieTalkie
State *CDashState
WT *communication.WalkieTalkie
State *CDashState
fieldToWindows map[telemetry.FieldID][]int16
bufPool sync.Pool
}
// Connect will try to find and connect to the CDashDisplay
func Discover() (*CDashDisplay, error) {
func NewCDashDisplay() (*CDashDisplay, error) {
// Look for the port
p, err := findDisplayPort()
if err != nil {
pLogger.Info("failed to find cdashdisplay port: %s", err.Error())
slog.Info("failed to find cdashdisplay port: %s", err.Error())
return nil, err
}
return &CDashDisplay{
WT: p,
State: NewCDashState(),
WT: p,
State: NewCDashState(),
fieldToWindows: make(map[telemetry.FieldID][]int16),
bufPool: sync.Pool{
New: func() any {
b := make([]byte, 0, telemetry.MaxFields*8)
return &b
},
},
}, nil
}
func (d *CDashDisplay) SendCommand() {
}
func (d *CDashDisplay) RegisterFieldMapping(fieldID telemetry.FieldID, winID int16) {
d.fieldToWindows[fieldID] = append(d.fieldToWindows[fieldID], winID)
}
func (d *CDashDisplay) UnregisterFieldMapping(winID int16) {
for fieldID, windows := range d.fieldToWindows {
updated := windows[:0]
for _, w := range windows {
if w != winID {
updated = append(updated, w)
}
if len(updated) == 0 {
delete(d.fieldToWindows, fieldID)
} else {
d.fieldToWindows[fieldID] = updated
}
}
}
}
func (d *CDashDisplay) CreateWindow(win *DesktopUIWindow) (*DesktopUIWindow, error) {
bytes, err := helper.StructToBytes(win.UIWindow)
if err != nil {
@@ -150,9 +174,12 @@ func (d *CDashDisplay) CreateWindow(win *DesktopUIWindow) (*DesktopUIWindow, err
win.UIData.IDX = wID.ID
pLogger.Info(fmt.Sprintf("Recived ID message: %v", wID))
slog.Info(fmt.Sprintf("Recived ID message: %v", wID))
d.State.Layout.AddWindow(win)
if fieldID, ok := telemetry.GetFieldID(win.UIData.TelemetryField); ok {
d.RegisterFieldMapping(fieldID, win.UIData.IDX)
}
return win, nil
}
@@ -178,12 +205,14 @@ func (d *CDashDisplay) UpdateWindow(win *DesktopUIWindow) error {
// I get it and update it in the controller
// I send the pointer here
// -> it should be the same pointer then right?
pLogger.Debug(fmt.Sprintf("PreUpdate ID: %p", win))
slog.Debug(fmt.Sprintf("PreUpdate ID: %p", win))
d.State.Layout.Windows[win.UIData.IDX] = win
pLogger.Debug(fmt.Sprintf("PostUpdate ID: %p", win))
slog.Debug(fmt.Sprintf("PostUpdate ID: %p", win))
// Yeah, same address as suspected
// I can't think about it right now. I'll think about that tomorrow
// TODO: need to update the field mappings here!
return nil
}
@@ -206,14 +235,15 @@ func (d *CDashDisplay) DestroyWindow(wID int16) error {
return err
}
// NODE: add this
// NOTE: add this
d.UnregisterFieldMapping(wID)
d.State.Layout.RemoveWindow(wID)
return nil
}
func (d *CDashDisplay) updateWindowDimensions(win *UIWindow, packet UpdateDimsPacket) error {
pLogger.Debug(fmt.Sprintf("UPDATE: %v", packet))
slog.Debug(fmt.Sprintf("UPDATE: %v", packet))
bytes, err := helper.StructToBytes(packet)
if err != nil {
@@ -227,9 +257,9 @@ func (d *CDashDisplay) updateWindowDimensions(win *UIWindow, packet UpdateDimsPa
}
// Nothing bad happened afaik
pLogger.Debug(fmt.Sprintf("cur dims: %v", win.Dims))
slog.Debug(fmt.Sprintf("cur dims: %v", win.Dims))
win.Dims = packet.Dims
pLogger.Debug(fmt.Sprintf("new dims: %v", win.Dims))
slog.Debug(fmt.Sprintf("new dims: %v", win.Dims))
return nil
}
@@ -345,18 +375,18 @@ func (d *CDashDisplay) LoadLayout(layoutName string) error {
func (d *CDashDisplay) UnloadLayout() error {
var err error
for _, w := range d.State.Layout.Windows {
pLogger.Debug(fmt.Sprintf("= Removing %d ==============================================",
slog.Debug(fmt.Sprintf("= Removing %d ==============================================",
w.UIData.IDX))
err = d.DestroyWindow(w.UIData.IDX)
time.Sleep(75 * time.Millisecond)
if err != nil {
pLogger.Error(fmt.Sprintf("failed to destroy window: %+v", err))
slog.Error(fmt.Sprintf("failed to destroy window: %+v", err))
// NOTE: Add a way to handle multiple errors ?
return err
}
pLogger.Debug(fmt.Sprintf("= Removing %d ==============================================",
slog.Debug(fmt.Sprintf("= Removing %d ==============================================",
w.UIData.IDX))
}
@@ -364,7 +394,7 @@ func (d *CDashDisplay) UnloadLayout() error {
}
func (d *CDashDisplay) SendData(data *telemetry.TelemetryData) {
packet := data.Pack()
packet := d.encodePacket(data)
bytes, err := helper.StructToBytes(packet)
if err != nil {
@@ -378,7 +408,7 @@ func (d *CDashDisplay) SendData(data *telemetry.TelemetryData) {
curStr += fmt.Sprintf("%02x ", byte)
if byteCount == 8 {
// pLogger.Debug(curStr)
// slog.Debug(curStr)
curStr = ""
byteCount = 0
}
+5 -4
View File
@@ -15,12 +15,13 @@ func (cds *CDashDisplay) Name() string {
return NAME
}
func (cds *CDashDisplay) RequiredFields() map[int16]telemetry.FieldID {
fields := make(map[int16]telemetry.FieldID, len(cds.State.Layout.Windows))
func (cds *CDashDisplay) RequiredFields() []telemetry.FieldID {
fields := make([]telemetry.FieldID, 0, len(cds.State.Layout.Windows))
for _, w := range cds.State.Layout.Windows {
fieldID, _ := telemetry.GetFieldID(w.UIData.TelemetryField)
fields[w.UIData.IDX] = fieldID
if fieldID, ok := telemetry.GetFieldID(w.UIData.TelemetryField); ok {
fields = append(fields, fieldID)
}
}
return fields
+8 -5
View File
@@ -1,6 +1,9 @@
package cdashdisplay
import "fmt"
import (
"fmt"
"log/slog"
)
type LayoutTree struct {
Windows map[int16]*DesktopUIWindow `yaml:"Windows"`
@@ -13,13 +16,13 @@ func NewLayoutTree() *LayoutTree {
}
func (l *LayoutTree) AddWindow(w *DesktopUIWindow) {
pLogger.Debug(fmt.Sprintf("adding window '%d' - %v", w.UIData.IDX))
slog.Debug(fmt.Sprintf("adding window '%d' - %v", w.UIData.IDX))
l.Windows[w.UIData.IDX] = w
pLogger.Debug(fmt.Sprintf("new map - %v", l.Windows))
slog.Debug(fmt.Sprintf("new map - %v", l.Windows))
}
func (l *LayoutTree) RemoveWindow(idx int16) {
pLogger.Debug(fmt.Sprintf("removing window '%d'", idx))
slog.Debug(fmt.Sprintf("removing window '%d'", idx))
delete(l.Windows, idx)
pLogger.Debug(fmt.Sprintf("new map - %v", l.Windows))
slog.Debug(fmt.Sprintf("new map - %v", l.Windows))
}
+74 -3
View File
@@ -1,5 +1,11 @@
package cdashdisplay
import (
"math"
"esdi/telemetry"
)
// In this file we will place all structs that are 1:1 representation of the
// types in the device transport layer ->
@@ -65,6 +71,71 @@ type UIWindowUpdatePacket struct {
Window UIWindow
}
// func getUIWindowDTO(w *DesktopWindowData) *UIWindow {
//
// }
// TODO: this is here because this is the only device I use like this
// once I add another serial device I will move this somewhere else that can
// be reused by all devices but still is decoupled from the telemetry package
func (cds *CDashDisplay) encodePacket(td *telemetry.TelemetryData) []byte {
bufPtr := cds.bufPool.Get().(*[]byte)
buf := (*bufPtr)[:0]
for fieldID, windowIDs := range cds.fieldToWindows {
if int(fieldID) >= len(td.Values) || len(windowIDs) == 0 {
continue
}
tf := &td.Values[fieldID]
for _, winID := range windowIDs {
buf = packField(winID, tf, buf)
}
}
// We have to copy here because we have to return the buffer
result := make([]byte, len(buf))
copy(result, buf)
cds.bufPool.Put(&buf)
return result
}
// Pack will pack this current TelemetryField into bytes to send over the wire
// Format:
// 0x00 - Field ID
// 0x00 |
// 0x01 - DataType
// 0x02 - if its a (u)int8
// or
// 0x02 - if its a (u)int16 - first byte
// 0x02 - if its a (u)int16 - second byte
// or
// 0x02 - str len max is 255 chars
// [0x02] - str
func packField(winID int16, tf *telemetry.TelemetryField, dest []byte) []byte {
// NOTE: maybe we can have a pool of these so we don't have to create them here
// or whatever
dest = append(dest, uint8(winID), uint8(winID>>8))
dest = append(dest, uint8(tf.Type))
switch tf.Type {
case telemetry.DataTypeINT8, telemetry.DataTypeUINT8, telemetry.DataTypeCHAR:
dest = append(dest, uint8(tf.Raw))
case telemetry.DataTypeINT16, telemetry.DataTypeUINT16:
dest = append(dest, uint8(tf.Raw), uint8(tf.Raw>>8))
case telemetry.DataTypeINT32, telemetry.DataTypeUINT32:
dest = append(dest, uint8(tf.Raw), uint8(tf.Raw>>8), uint8(tf.Raw>>16), uint8(tf.Raw>>24))
case telemetry.DataTypeINT64, telemetry.DataTypeUINT64:
dest = append(
dest, uint8(tf.Raw), uint8(tf.Raw>>8), uint8(tf.Raw>>16),
uint8(tf.Raw>>24), uint8(tf.Raw>>32), uint8(tf.Raw>>40), uint8(tf.Raw>>48),
uint8(tf.Raw>>56),
)
case telemetry.DataTypeSTRING:
l := min(len(tf.Str), math.MaxUint8)
dest = append(dest, uint8(l))
dest = append(dest, tf.Str[:l]...)
}
return dest
}
+7 -24
View File
@@ -2,8 +2,6 @@
package devices
import (
"errors"
"esdi/devices/cdashdisplay"
"esdi/devices/uidevice"
"esdi/peripheral"
@@ -35,26 +33,11 @@ func DiscoverUIDevice() (peripheral.Peripheral, error) {
}
func DiscoverCDashDisplay() (peripheral.Peripheral, error) {
// // Find CDashDisplay
// {
// ds.Messages <- "looking for " + cdashdisplay.Name + "...\n"
// ds.Logger.Info("Looking for " + cdashdisplay.Name)
//
// cdashdisplay.SetLogger(ds.Logger.With("[device]", cdashdisplay.Name))
//
// // Create a cdashdisplay
// display, err := cdashdisplay.Discover()
// if err == nil {
// ds.Devices[cdashdisplay.Name] = display
// ds.Logger.Info("found " + cdashdisplay.Name + " on: " + display.WT.Cfg.Name)
// ds.Messages <- "found " + cdashdisplay.Name + " on: " + display.WT.Cfg.Name + "\n"
// return
// }
//
// ds.Logger.Info("didn't find " + cdashdisplay.Name)
// ds.Messages <- "didn't find " + cdashdisplay.Name + "\n"
// // No CDashDisplay available for one reason or another, so we don't set the
// // key
// }
return nil, errors.New("not implemented yet")
// Create a cdashdisplay
display, err := cdashdisplay.NewCDashDisplay()
if err != nil {
return nil, err
}
return display, nil
}
+2 -9
View File
@@ -37,17 +37,10 @@ func (uid *UIDevice) DataChannel() <-chan telemetry.TelemetryData {
return uid.dataChan
}
func (uid *UIDevice) RequiredFields() map[int16]telemetry.FieldID {
subscribeTo := []telemetry.FieldID{
func (uid *UIDevice) RequiredFields() []telemetry.FieldID {
return []telemetry.FieldID{
telemetry.Speed,
telemetry.Gear,
telemetry.RPM,
}
fields := make(map[int16]telemetry.FieldID, len(subscribeTo))
for k, field := range subscribeTo {
fields[int16(k)] = field
}
return fields
}
-22
View File
@@ -1,22 +0,0 @@
package logger
import (
"log"
"os"
"sync"
)
var l *log.Logger
var once sync.Once
func createLogger() {
l = log.New(os.Stdout, "[esdi] ", log.LstdFlags | log.Lshortfile)
}
func GetInstance() *log.Logger {
once.Do(func() {
createLogger()
})
return l
}
-4
View File
@@ -10,7 +10,6 @@ import (
"esdi/cmd"
"esdi/config"
"esdi/telemetry"
"github.com/arl/statsviz"
)
@@ -42,9 +41,6 @@ func initApplication() {
slog.Error(fmt.Sprintf("failed to setup metrics server: %+v", err))
}
}
// Setting up some internal data structures
telemetry.Init()
}
func setupLogger() error {
+1 -1
View File
@@ -18,7 +18,7 @@ const (
type Peripheral interface {
Name() string
SendData(*telemetry.TelemetryData)
RequiredFields() map[int16]telemetry.FieldID
RequiredFields() []telemetry.FieldID
}
type PeripheralDeviceClerk struct {
+2 -4
View File
@@ -115,7 +115,7 @@ func (b *BeamNG) Stream() (<-chan telemetry.TelemetryData, error) {
return b.streamCh, nil
}
func (b *BeamNG) Subscribe(requestFields map[int16]telemetry.FieldID) {
func (b *BeamNG) Subscribe(requestFields []telemetry.FieldID) {
// NOTE: document how the Subscribe funtion works
slog.Debug(fmt.Sprintf("Len Req: %d\n", len(requestFields)))
@@ -125,9 +125,7 @@ func (b *BeamNG) Subscribe(requestFields map[int16]telemetry.FieldID) {
// we will add their dependencies and the primitives to a slice
pendingBinds := make([]telemetry.FieldID, telemetry.MaxFields)
for winID, id := range requestFields {
b.data.Values[id].IDs = append(b.data.Values[id].IDs, winID)
for _, id := range requestFields {
switch id {
case telemetry.RPMStateColour:
b.data.VirtualBinds = append(b.data.VirtualBinds, telemetry.NewRPMLights())
+2 -4
View File
@@ -220,7 +220,7 @@ func (i *IRacing) StopStream() {
i.streamCancel = nil
}
func (i *IRacing) Subscribe(requestFields map[int16]telemetry.FieldID) {
func (i *IRacing) Subscribe(requestFields []telemetry.FieldID) {
i.logger.Debug(fmt.Sprintf("Len Req: %d\n", len(requestFields)))
i.data.ActiveBinds = make([]telemetry.BoundField, 0, len(requestFields))
@@ -229,9 +229,7 @@ func (i *IRacing) Subscribe(requestFields map[int16]telemetry.FieldID) {
// we will add their dependencies and the primitives to a slice
pendingBinds := make([]telemetry.FieldID, 0, telemetry.MaxFields)
for winID, id := range requestFields {
i.data.Values[id].IDs = append(i.data.Values[id].IDs, winID)
for _, id := range requestFields {
switch id {
case telemetry.RPMStateColour:
i.data.VirtualBinds = append(i.data.VirtualBinds, telemetry.NewRPMLights())
+13 -5
View File
@@ -7,6 +7,7 @@ import (
"time"
"esdi/providers"
"esdi/telemetry"
telem "esdi/telemetry"
)
@@ -203,13 +204,20 @@ func (t *TelemetryService) UnsubscribeListener(id string) {
}
func (t *TelemetryService) SubscribeToFields() {
// _ = t.devService.SubscribeFields()
// TODO: we need to find a way of requesting devices to send all subscribed fields
// instead of going through the devices on DevService here
seen := make(map[telemetry.FieldID]struct{})
var allFields []telemetry.FieldID
for _, dev := range t.devService.Devices {
fields := dev.RequiredFields()
t.activeProvider.Subscribe(fields)
for _, field := range dev.RequiredFields() {
if _, exists := seen[field]; !exists {
seen[field] = struct{}{}
allFields = append(allFields, field)
}
}
}
t.logger.Debug("requested fields", "fields", allFields)
t.activeProvider.Subscribe(allFields)
}
func (t *TelemetryService) StartStream() {
+8 -75
View File
@@ -1,12 +1,18 @@
package telemetry
import (
"math"
"strconv"
"sync"
"time"
)
func init() {
fieldNameToID = make(map[string]FieldID, MaxFields)
for id, name := range FieldNames {
fieldNameToID[name] = FieldID(id)
}
}
// NOTE: allow the user to create custom data things. For example, iRacing provides
// multiple surface temps, but I guess the user doesn't want all of them at once.
// allow him to make something that allows some data transformation to occur.
@@ -64,7 +70,7 @@ const (
// NOTE: we can optimize this via a special command that says a given piece of data
// is for multiple targets
type TelemetryField struct {
IDs []int16 // Identification for the serial device
// IDs []int16 // Identification for the serial device
Type DataType
Raw uint64
Str string // Only to be used with DataTypeSTRING
@@ -75,49 +81,6 @@ func (tf *TelemetryField) Unused() {
tf.Raw = uint64('-')
}
// Pack will pack this current TelemetryField into bytes to send over the wire
// Format:
// 0x00 - Field ID
// 0x00 |
// 0x01 - DataType
// 0x02 - if its a (u)int8
// or
// 0x02 - if its a (u)int16 - first byte
// 0x02 - if its a (u)int16 - second byte
// or
// 0x02 - str len max is 255 chars
// [0x02] - str
func (tf *TelemetryField) Pack(dest []byte) []byte {
// NOTE: maybe we can have a pool of these so we don't have to create them here
// or whatever
for _, id := range tf.IDs {
dest = append(dest, uint8(id), uint8(id>>8))
dest = append(dest, uint8(tf.Type))
switch tf.Type {
case DataTypeINT8, DataTypeUINT8, DataTypeCHAR:
dest = append(dest, uint8(tf.Raw))
case DataTypeINT16, DataTypeUINT16:
dest = append(dest, uint8(tf.Raw), uint8(tf.Raw>>8))
case DataTypeINT32, DataTypeUINT32:
dest = append(dest, uint8(tf.Raw), uint8(tf.Raw>>8), uint8(tf.Raw>>16), uint8(tf.Raw>>24))
case DataTypeINT64, DataTypeUINT64:
dest = append(
dest, uint8(tf.Raw), uint8(tf.Raw>>8), uint8(tf.Raw>>16),
uint8(tf.Raw>>24), uint8(tf.Raw>>32), uint8(tf.Raw>>40), uint8(tf.Raw>>48),
uint8(tf.Raw>>56),
)
case DataTypeSTRING:
l := min(len(tf.Str), math.MaxUint8)
dest = append(dest, uint8(l))
dest = append(dest, tf.Str[:l]...)
}
}
return dest
}
func (tf *TelemetryField) String() string {
switch tf.Type {
case DataTypeSTRING:
@@ -259,13 +222,6 @@ func GetFieldName(id FieldID) string {
var fieldNameToID map[string]FieldID
func initFieldNamesMap() {
fieldNameToID = make(map[string]FieldID, MaxFields)
for id, name := range FieldNames {
fieldNameToID[name] = FieldID(id)
}
}
func GetFieldID(name string) (FieldID, bool) {
id, ok := fieldNameToID[name]
return id, ok
@@ -285,26 +241,3 @@ type TelemetryData struct {
func NewTelemetryData() *TelemetryData {
return &TelemetryData{}
}
func (td *TelemetryData) Pack() []byte {
bufPtr := bufferPool.Get().(*[]byte)
buf := (*bufPtr)[:0]
// for _, bind := range td.ActiveBinds {
// buf = td.Values[bind.ID].Pack(buf)
// }
for k := range td.Values {
if len(td.Values[k].IDs) > 0 {
buf = td.Values[k].Pack(buf)
}
}
// We have to copy here because we have to return the buffer
result := make([]byte, len(buf))
copy(result, buf)
bufferPool.Put(&buf)
return result
}
+1 -1
View File
@@ -6,7 +6,7 @@ import "time"
type TelemetryProvider interface {
StopStream()
Stream() (<-chan TelemetryData, error)
Subscribe(map[int16]FieldID)
Subscribe([]FieldID)
IsAlive(time.Duration) bool
Name() string
Close()
-4
View File
@@ -1,5 +1 @@
package telemetry
func Init() {
initFieldNamesMap()
}