Compare commits
19
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bd10d6b4d6 | ||
|
|
c888dc5436 | ||
|
|
dda300ce6b | ||
|
|
1ac4ab1865 | ||
|
|
a6ab64117c | ||
|
|
1321977f18 | ||
|
|
ffc03e9e49 | ||
|
|
15b997c22f | ||
|
|
59fa825da1 | ||
|
|
dbd4086bf9 | ||
|
|
c1366b010f | ||
|
|
d6362eeeb5 | ||
|
|
1b9a5173b6 | ||
|
|
7675c7e34a | ||
|
|
922c2df88d | ||
|
|
6b09eb5be0 | ||
|
|
1d022a4fa8 | ||
|
|
a251f45859 | ||
|
|
afc4b794bb |
@@ -0,0 +1 @@
|
|||||||
|
# Peripheral Discovery
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
# Provider Discovery
|
||||||
|
```
|
||||||
|
NewControlPanel()
|
||||||
|
↓
|
||||||
|
go telemService.FindProvider() - has a callback for onFind
|
||||||
|
|
|
||||||
|
|iterates over the known providers
|
||||||
|
|
|
||||||
|
onFind
|
||||||
|
↓
|
||||||
|
telemService.SwitchProvider() - activates the found provider
|
||||||
|
|
|
||||||
|
|-→ Create a background job while the stream hasn't initiated to listened
|
||||||
|
| for data, otherwise the connection might die before we start streaming
|
||||||
|
| ↓
|
||||||
|
| go telemService.ProviderMonitor()
|
||||||
|
↓ | |
|
||||||
|
/-→waits--\ if the provider stops we this healthcheck is stopped
|
||||||
|
\_________/ we clear the provider and once the stream starts
|
||||||
|
go back to the
|
||||||
|
↓
|
||||||
|
FindProvider()
|
||||||
|
```
|
||||||
|
The provider discovery routine starts on `tui/tui.go`.
|
||||||
|
`FindProvider` is called here and it starts a background job.
|
||||||
|
On successful discovery the background job calls the `SwitchProvider` method
|
||||||
|
and dies.
|
||||||
|
|
||||||
|
## Provider stalls
|
||||||
|
A provider stalls once there is no new data.
|
||||||
|
After stalling
|
||||||
@@ -1,70 +0,0 @@
|
|||||||
package cdashdisplay
|
|
||||||
|
|
||||||
// In this file we will place all structs that are 1:1 representation of the
|
|
||||||
// types in the device transport layer ->
|
|
||||||
|
|
||||||
const (
|
|
||||||
ShowIDFalse uint8 = 0
|
|
||||||
ShowIDTrue uint8 = 1
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
WinTypeBASE uint8 = iota
|
|
||||||
WinTypeBAR
|
|
||||||
WinTypeSTRING
|
|
||||||
WinTypeTABLE
|
|
||||||
)
|
|
||||||
|
|
||||||
var WinTYPES = []string{"BASE", "BAR", "STRING", "TABLE"}
|
|
||||||
|
|
||||||
type UIDimensions struct {
|
|
||||||
X0 uint16 `yaml:"X0"`
|
|
||||||
Y0 uint16 `yaml:"Y0"`
|
|
||||||
Width uint16 `yaml:"Width"`
|
|
||||||
Height uint16 `yaml:"Height"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type UIDecorations struct {
|
|
||||||
BGColour uint16 `yaml:"BGColour"`
|
|
||||||
FGColour uint16 `yaml:"FGColour"`
|
|
||||||
TitleColour uint16 `yaml:"TitleColour"`
|
|
||||||
BorderColour uint16 `yaml:"BorderColour"`
|
|
||||||
TitleSize uint8 `yaml:"TitleSize"`
|
|
||||||
TextSize uint8 `yaml:"TextSize"`
|
|
||||||
HasBorder uint8 `yaml:"HasBorder"`
|
|
||||||
Padding uint8 `yaml:"Padding"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// NOTE: we can't use FString32 for this - too many bytes
|
|
||||||
// NOTE: use a bit flags for this options instead
|
|
||||||
type UIWindowOpts struct {
|
|
||||||
ShowID uint8 `yaml:"ShowID"`
|
|
||||||
WinType uint8 `yaml:"WinType"`
|
|
||||||
PreviewValue FString32 `yaml:"PreviewValue"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type UIWindow struct {
|
|
||||||
Dims UIDimensions `yaml:"Dims"`
|
|
||||||
Decor UIDecorations `yaml:"Decor"`
|
|
||||||
Opts UIWindowOpts `yaml:"Opts"`
|
|
||||||
Title FString32 `yaml:"Title"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type DesktopUIWindow struct {
|
|
||||||
UIWindow
|
|
||||||
UIData DesktopUIData
|
|
||||||
}
|
|
||||||
|
|
||||||
type DesktopUIData struct {
|
|
||||||
IDX int16 `yaml:"WID"`
|
|
||||||
TelemetryField string `yaml:"TelemetryField"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type UIWindowUpdatePacket struct {
|
|
||||||
WinID int16
|
|
||||||
Window UIWindow
|
|
||||||
}
|
|
||||||
|
|
||||||
// func getUIWindowDTO(w *DesktopWindowData) *UIWindow {
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
@@ -1,20 +1,17 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"esdi/logger"
|
|
||||||
esdi "esdi/oldEsdi"
|
esdi "esdi/oldEsdi"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
func liveTelemetryCmdAction(cmd *cobra.Command, args []string) {
|
func liveTelemetryCmdAction(cmd *cobra.Command, args []string) {
|
||||||
log := logger.GetInstance()
|
|
||||||
|
|
||||||
ddPort, _ := cmd.Flags().GetString("port")
|
ddPort, _ := cmd.Flags().GetString("port")
|
||||||
outputFile, _ := cmd.Flags().GetString("out")
|
outputFile, _ := cmd.Flags().GetString("out")
|
||||||
sessionFile, _ := cmd.Flags().GetString("session")
|
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)
|
esdi.RunLiveTelemetry(ddPort, outputFile, sessionFile)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"esdi/logger"
|
|
||||||
esdi "esdi/oldEsdi"
|
esdi "esdi/oldEsdi"
|
||||||
|
|
||||||
// "github.com/ESilva15/goirsdk"
|
// "github.com/ESilva15/goirsdk"
|
||||||
@@ -10,14 +9,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func offlineTelemetryCmdAction(cmd *cobra.Command, args []string) {
|
func offlineTelemetryCmdAction(cmd *cobra.Command, args []string) {
|
||||||
log := logger.GetInstance()
|
// log := logger.GetInstance()
|
||||||
|
|
||||||
ddPort, _ := cmd.Flags().GetString("port")
|
ddPort, _ := cmd.Flags().GetString("port")
|
||||||
inFile, _ := cmd.Flags().GetString("in")
|
inFile, _ := cmd.Flags().GetString("in")
|
||||||
outFile, _ := cmd.Flags().GetString("out")
|
outFile, _ := cmd.Flags().GetString("out")
|
||||||
sessionFile, _ := cmd.Flags().GetString("session")
|
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)
|
esdi.RunOfflineTelemetry(ddPort, inFile, outFile, sessionFile)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
package cdashdisplay
|
package cdashdisplay
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
|
"time"
|
||||||
|
|
||||||
"esdi/peripheral/communication"
|
"esdi/peripheral/communication"
|
||||||
"esdi/peripheral/communication/packets"
|
"esdi/peripheral/communication/packets"
|
||||||
"fmt"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/tarm/serial"
|
"github.com/tarm/serial"
|
||||||
portp "go.bug.st/serial"
|
portp "go.bug.st/serial"
|
||||||
@@ -46,11 +48,11 @@ func findDisplayPort() (*communication.WalkieTalkie, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
pLogger.Info(fmt.Sprintf("Looking into %v", ports))
|
slog.Info(fmt.Sprintf("Looking into %v", ports))
|
||||||
|
|
||||||
var wt *communication.WalkieTalkie
|
var wt *communication.WalkieTalkie
|
||||||
for _, port := range ports {
|
for _, port := range ports {
|
||||||
pLogger.Info(fmt.Sprintf("Trying port %s", port))
|
slog.Info(fmt.Sprintf("Trying port %s", port))
|
||||||
|
|
||||||
wt = &communication.WalkieTalkie{
|
wt = &communication.WalkieTalkie{
|
||||||
Cfg: &serial.Config{
|
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)
|
probeResult := make(chan error, 1)
|
||||||
|
|
||||||
@@ -76,14 +78,14 @@ func findDisplayPort() (*communication.WalkieTalkie, error) {
|
|||||||
err = fmt.Errorf("probe completely hung/timed out: %s", port)
|
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 {
|
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
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
pLogger.Info(fmt.Sprintf("wasn't port %s", port))
|
slog.Info(fmt.Sprintf("wasn't port %s", port))
|
||||||
wt = nil
|
wt = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,6 +93,6 @@ func findDisplayPort() (*communication.WalkieTalkie, error) {
|
|||||||
return nil, fmt.Errorf("couldn't find cdashdisplay")
|
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
|
return wt, nil
|
||||||
}
|
}
|
||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
helper "esdi/helpers"
|
helper "esdi/helpers"
|
||||||
@@ -19,12 +20,6 @@ import (
|
|||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
var pLogger *slog.Logger
|
|
||||||
|
|
||||||
func SetLogger(l *slog.Logger) {
|
|
||||||
pLogger = l
|
|
||||||
}
|
|
||||||
|
|
||||||
// I have to move this to some kind of configuration place
|
// I have to move this to some kind of configuration place
|
||||||
const (
|
const (
|
||||||
layoutsDir = "./layouts/"
|
layoutsDir = "./layouts/"
|
||||||
@@ -112,27 +107,58 @@ func NewCDashState() *CDashState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type CDashDisplay struct {
|
type CDashDisplay struct {
|
||||||
WT *communication.WalkieTalkie
|
WT *communication.WalkieTalkie
|
||||||
State *CDashState
|
State *CDashState
|
||||||
|
fieldToWindows map[telemetry.FieldID][]int16
|
||||||
|
bufPool sync.Pool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Connect will try to find and connect to the CDashDisplay
|
||||||
func NewCDashDisplay() (*CDashDisplay, error) {
|
func NewCDashDisplay() (*CDashDisplay, error) {
|
||||||
// Look for the port
|
// Look for the port
|
||||||
p, err := findDisplayPort()
|
p, err := findDisplayPort()
|
||||||
if err != nil {
|
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 nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &CDashDisplay{
|
return &CDashDisplay{
|
||||||
WT: p,
|
WT: p,
|
||||||
State: NewCDashState(),
|
State: NewCDashState(),
|
||||||
|
fieldToWindows: make(map[telemetry.FieldID][]int16),
|
||||||
|
bufPool: sync.Pool{
|
||||||
|
New: func() any {
|
||||||
|
b := make([]byte, 0, telemetry.MaxFields*8)
|
||||||
|
return &b
|
||||||
|
},
|
||||||
|
},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *CDashDisplay) SendCommand() {
|
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) {
|
func (d *CDashDisplay) CreateWindow(win *DesktopUIWindow) (*DesktopUIWindow, error) {
|
||||||
bytes, err := helper.StructToBytes(win.UIWindow)
|
bytes, err := helper.StructToBytes(win.UIWindow)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -148,9 +174,12 @@ func (d *CDashDisplay) CreateWindow(win *DesktopUIWindow) (*DesktopUIWindow, err
|
|||||||
|
|
||||||
win.UIData.IDX = wID.ID
|
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)
|
d.State.Layout.AddWindow(win)
|
||||||
|
if fieldID, ok := telemetry.GetFieldID(win.UIData.TelemetryField); ok {
|
||||||
|
d.RegisterFieldMapping(fieldID, win.UIData.IDX)
|
||||||
|
}
|
||||||
|
|
||||||
return win, nil
|
return win, nil
|
||||||
}
|
}
|
||||||
@@ -176,12 +205,14 @@ func (d *CDashDisplay) UpdateWindow(win *DesktopUIWindow) error {
|
|||||||
// I get it and update it in the controller
|
// I get it and update it in the controller
|
||||||
// I send the pointer here
|
// I send the pointer here
|
||||||
// -> it should be the same pointer then right?
|
// -> 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
|
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
|
// Yeah, same address as suspected
|
||||||
// I can't think about it right now. I'll think about that tomorrow
|
// I can't think about it right now. I'll think about that tomorrow
|
||||||
|
|
||||||
|
// TODO: need to update the field mappings here!
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -204,14 +235,15 @@ func (d *CDashDisplay) DestroyWindow(wID int16) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// NODE: add this
|
// NOTE: add this
|
||||||
|
d.UnregisterFieldMapping(wID)
|
||||||
d.State.Layout.RemoveWindow(wID)
|
d.State.Layout.RemoveWindow(wID)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *CDashDisplay) updateWindowDimensions(win *UIWindow, packet UpdateDimsPacket) error {
|
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)
|
bytes, err := helper.StructToBytes(packet)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -225,9 +257,9 @@ func (d *CDashDisplay) updateWindowDimensions(win *UIWindow, packet UpdateDimsPa
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Nothing bad happened afaik
|
// 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
|
win.Dims = packet.Dims
|
||||||
pLogger.Debug(fmt.Sprintf("new dims: %v", win.Dims))
|
slog.Debug(fmt.Sprintf("new dims: %v", win.Dims))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -343,18 +375,18 @@ func (d *CDashDisplay) LoadLayout(layoutName string) error {
|
|||||||
func (d *CDashDisplay) UnloadLayout() error {
|
func (d *CDashDisplay) UnloadLayout() error {
|
||||||
var err error
|
var err error
|
||||||
for _, w := range d.State.Layout.Windows {
|
for _, w := range d.State.Layout.Windows {
|
||||||
pLogger.Debug(fmt.Sprintf("= Removing %d ==============================================",
|
slog.Debug(fmt.Sprintf("= Removing %d ==============================================",
|
||||||
w.UIData.IDX))
|
w.UIData.IDX))
|
||||||
|
|
||||||
err = d.DestroyWindow(w.UIData.IDX)
|
err = d.DestroyWindow(w.UIData.IDX)
|
||||||
time.Sleep(75 * time.Millisecond)
|
time.Sleep(75 * time.Millisecond)
|
||||||
if err != nil {
|
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 ?
|
// NOTE: Add a way to handle multiple errors ?
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
pLogger.Debug(fmt.Sprintf("= Removing %d ==============================================",
|
slog.Debug(fmt.Sprintf("= Removing %d ==============================================",
|
||||||
w.UIData.IDX))
|
w.UIData.IDX))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -362,7 +394,7 @@ func (d *CDashDisplay) UnloadLayout() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (d *CDashDisplay) SendData(data *telemetry.TelemetryData) {
|
func (d *CDashDisplay) SendData(data *telemetry.TelemetryData) {
|
||||||
packet := data.Pack()
|
packet := d.encodePacket(data)
|
||||||
|
|
||||||
bytes, err := helper.StructToBytes(packet)
|
bytes, err := helper.StructToBytes(packet)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -376,7 +408,7 @@ func (d *CDashDisplay) SendData(data *telemetry.TelemetryData) {
|
|||||||
curStr += fmt.Sprintf("%02x ", byte)
|
curStr += fmt.Sprintf("%02x ", byte)
|
||||||
|
|
||||||
if byteCount == 8 {
|
if byteCount == 8 {
|
||||||
pLogger.Debug(curStr)
|
// slog.Debug(curStr)
|
||||||
curStr = ""
|
curStr = ""
|
||||||
byteCount = 0
|
byteCount = 0
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package cdashdisplay
|
||||||
|
|
||||||
|
import (
|
||||||
|
"esdi/peripheral/devices"
|
||||||
|
"esdi/telemetry"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TODO: I believe we don't need the #esdi/peripheral/devices thing anymore
|
||||||
|
const (
|
||||||
|
ID = devices.CDashDisplayDevID
|
||||||
|
NAME = devices.CDashDisplayDevName
|
||||||
|
)
|
||||||
|
|
||||||
|
func (cds *CDashDisplay) Name() string {
|
||||||
|
return NAME
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cds *CDashDisplay) RequiredFields() []telemetry.FieldID {
|
||||||
|
fields := make([]telemetry.FieldID, 0, len(cds.State.Layout.Windows))
|
||||||
|
|
||||||
|
for _, w := range cds.State.Layout.Windows {
|
||||||
|
if fieldID, ok := telemetry.GetFieldID(w.UIData.TelemetryField); ok {
|
||||||
|
fields = append(fields, fieldID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return fields
|
||||||
|
}
|
||||||
@@ -1,6 +1,9 @@
|
|||||||
package cdashdisplay
|
package cdashdisplay
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
|
)
|
||||||
|
|
||||||
type LayoutTree struct {
|
type LayoutTree struct {
|
||||||
Windows map[int16]*DesktopUIWindow `yaml:"Windows"`
|
Windows map[int16]*DesktopUIWindow `yaml:"Windows"`
|
||||||
@@ -13,13 +16,13 @@ func NewLayoutTree() *LayoutTree {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *LayoutTree) AddWindow(w *DesktopUIWindow) {
|
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
|
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) {
|
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)
|
delete(l.Windows, idx)
|
||||||
pLogger.Debug(fmt.Sprintf("new map - %v", l.Windows))
|
slog.Debug(fmt.Sprintf("new map - %v", l.Windows))
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,141 @@
|
|||||||
|
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 ->
|
||||||
|
|
||||||
|
const (
|
||||||
|
ShowIDFalse uint8 = 0
|
||||||
|
ShowIDTrue uint8 = 1
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
WinTypeBASE uint8 = iota
|
||||||
|
WinTypeBAR
|
||||||
|
WinTypeSTRING
|
||||||
|
WinTypeTABLE
|
||||||
|
)
|
||||||
|
|
||||||
|
var WinTYPES = []string{"BASE", "BAR", "STRING", "TABLE"}
|
||||||
|
|
||||||
|
type UIDimensions struct {
|
||||||
|
X0 uint16 `yaml:"X0"`
|
||||||
|
Y0 uint16 `yaml:"Y0"`
|
||||||
|
Width uint16 `yaml:"Width"`
|
||||||
|
Height uint16 `yaml:"Height"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type UIDecorations struct {
|
||||||
|
BGColour uint16 `yaml:"BGColour"`
|
||||||
|
FGColour uint16 `yaml:"FGColour"`
|
||||||
|
TitleColour uint16 `yaml:"TitleColour"`
|
||||||
|
BorderColour uint16 `yaml:"BorderColour"`
|
||||||
|
TitleSize uint8 `yaml:"TitleSize"`
|
||||||
|
TextSize uint8 `yaml:"TextSize"`
|
||||||
|
HasBorder uint8 `yaml:"HasBorder"`
|
||||||
|
Padding uint8 `yaml:"Padding"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// NOTE: we can't use FString32 for this - too many bytes
|
||||||
|
// NOTE: use a bit flags for this options instead
|
||||||
|
type UIWindowOpts struct {
|
||||||
|
ShowID uint8 `yaml:"ShowID"`
|
||||||
|
WinType uint8 `yaml:"WinType"`
|
||||||
|
PreviewValue FString32 `yaml:"PreviewValue"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type UIWindow struct {
|
||||||
|
Dims UIDimensions `yaml:"Dims"`
|
||||||
|
Decor UIDecorations `yaml:"Decor"`
|
||||||
|
Opts UIWindowOpts `yaml:"Opts"`
|
||||||
|
Title FString32 `yaml:"Title"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type DesktopUIWindow struct {
|
||||||
|
UIWindow
|
||||||
|
UIData DesktopUIData
|
||||||
|
}
|
||||||
|
|
||||||
|
type DesktopUIData struct {
|
||||||
|
IDX int16 `yaml:"WID"`
|
||||||
|
TelemetryField string `yaml:"TelemetryField"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type UIWindowUpdatePacket struct {
|
||||||
|
WinID int16
|
||||||
|
Window 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
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
// Package devices is a peripheral factory
|
||||||
|
package devices
|
||||||
|
|
||||||
|
import (
|
||||||
|
"esdi/devices/cdashdisplay"
|
||||||
|
"esdi/devices/uidevice"
|
||||||
|
"esdi/peripheral"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Device struct {
|
||||||
|
Name string
|
||||||
|
Discover func() (peripheral.Peripheral, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
var List map[string]Device = map[string]Device{
|
||||||
|
uidevice.NAME: {
|
||||||
|
Name: uidevice.NAME,
|
||||||
|
Discover: DiscoverUIDevice,
|
||||||
|
},
|
||||||
|
cdashdisplay.NAME: {
|
||||||
|
Name: cdashdisplay.NAME,
|
||||||
|
Discover: DiscoverCDashDisplay,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func DiscoverUIDevice() (peripheral.Peripheral, error) {
|
||||||
|
uidev, err := uidevice.NewUIDevice()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return uidev, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func DiscoverCDashDisplay() (peripheral.Peripheral, error) {
|
||||||
|
// Create a cdashdisplay
|
||||||
|
display, err := cdashdisplay.NewCDashDisplay()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return display, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package uidevice
|
||||||
|
|
||||||
|
import (
|
||||||
|
"esdi/peripheral"
|
||||||
|
"esdi/telemetry"
|
||||||
|
)
|
||||||
|
|
||||||
|
type UIDevice struct {
|
||||||
|
dataChan chan telemetry.TelemetryData
|
||||||
|
}
|
||||||
|
|
||||||
|
const NAME = "UIView"
|
||||||
|
|
||||||
|
func NewUIDevice() (peripheral.Peripheral, error) {
|
||||||
|
return &UIDevice{
|
||||||
|
dataChan: make(chan telemetry.TelemetryData, 1),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (uid *UIDevice) SendData(data *telemetry.TelemetryData) {
|
||||||
|
if data == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case uid.dataChan <- *data:
|
||||||
|
default:
|
||||||
|
// Drop frame if buffer is full
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (uid *UIDevice) Name() string {
|
||||||
|
return NAME
|
||||||
|
}
|
||||||
|
|
||||||
|
func (uid *UIDevice) DataChannel() <-chan telemetry.TelemetryData {
|
||||||
|
return uid.dataChan
|
||||||
|
}
|
||||||
|
|
||||||
|
func (uid *UIDevice) RequiredFields() []telemetry.FieldID {
|
||||||
|
return []telemetry.FieldID{
|
||||||
|
telemetry.Speed,
|
||||||
|
telemetry.Gear,
|
||||||
|
telemetry.RPM,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
module esdi
|
module esdi
|
||||||
|
|
||||||
go 1.25.5
|
go 1.27.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/ESilva15/ESgoRepl v0.1.0
|
github.com/ESilva15/ESgoRepl v0.1.0
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import (
|
|||||||
|
|
||||||
"esdi/cmd"
|
"esdi/cmd"
|
||||||
"esdi/config"
|
"esdi/config"
|
||||||
"esdi/telemetry"
|
|
||||||
|
|
||||||
"github.com/arl/statsviz"
|
"github.com/arl/statsviz"
|
||||||
)
|
)
|
||||||
@@ -42,9 +41,6 @@ func initApplication() {
|
|||||||
slog.Error(fmt.Sprintf("failed to setup metrics server: %+v", err))
|
slog.Error(fmt.Sprintf("failed to setup metrics server: %+v", err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setting up some internal data structures
|
|
||||||
telemetry.Init()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupLogger() error {
|
func setupLogger() error {
|
||||||
|
|||||||
@@ -6,8 +6,9 @@ import "esdi/peripheral/types"
|
|||||||
// IDs for our devices. They need to be correctly mapped on the devices
|
// IDs for our devices. They need to be correctly mapped on the devices
|
||||||
// themselves so we can discover them
|
// themselves so we can discover them
|
||||||
const (
|
const (
|
||||||
CDashDisplayDevID = 0x01
|
CDashDisplayDevID = 0x01
|
||||||
ESBtnBoxDevID = 0x02
|
CDashDisplayDevName = "CDashDisplay"
|
||||||
|
ESBtnBoxDevID = 0x02
|
||||||
)
|
)
|
||||||
|
|
||||||
// DeviceMap maps the implemented devices
|
// DeviceMap maps the implemented devices
|
||||||
|
|||||||
@@ -2,9 +2,11 @@
|
|||||||
package peripheral
|
package peripheral
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"esdi/peripheral/devices"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
"esdi/peripheral/devices"
|
||||||
|
"esdi/telemetry"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PeripheralType string
|
type PeripheralType string
|
||||||
@@ -13,6 +15,12 @@ const (
|
|||||||
DisplayPeripheral PeripheralType = "display"
|
DisplayPeripheral PeripheralType = "display"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Peripheral interface {
|
||||||
|
Name() string
|
||||||
|
SendData(*telemetry.TelemetryData)
|
||||||
|
RequiredFields() []telemetry.FieldID
|
||||||
|
}
|
||||||
|
|
||||||
type PeripheralDeviceClerk struct {
|
type PeripheralDeviceClerk struct {
|
||||||
// mu sync.RWMutex
|
// mu sync.RWMutex
|
||||||
Devices map[uint8]*PeripheralDevice
|
Devices map[uint8]*PeripheralDevice
|
||||||
|
|||||||
@@ -17,7 +17,10 @@ import (
|
|||||||
// for BeamNG.drive
|
// for BeamNG.drive
|
||||||
// NOTE: document this please. What is a TelemetryData????
|
// NOTE: document this please. What is a TelemetryData????
|
||||||
type BeamNG struct {
|
type BeamNG struct {
|
||||||
|
logger *slog.Logger
|
||||||
|
|
||||||
SDK *bngsdk.BeamNGSDK
|
SDK *bngsdk.BeamNGSDK
|
||||||
|
og *bngsdk.Outgauge
|
||||||
|
|
||||||
// data handling
|
// data handling
|
||||||
mut sync.Mutex
|
mut sync.Mutex
|
||||||
@@ -36,16 +39,18 @@ const (
|
|||||||
NAME = "BeamNG.drive"
|
NAME = "BeamNG.drive"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewBeamNGProvider(ip string, port int) (*BeamNG, error) {
|
func NewBeamNGProvider(logger *slog.Logger, opts *bngsdk.Options) (*BeamNG, error) {
|
||||||
beam, err := bngsdk.Init(ip, port)
|
beam, err := bngsdk.NewBngSDK(*opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &BeamNG{}, err
|
return &BeamNG{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
provider := &BeamNG{
|
provider := &BeamNG{
|
||||||
|
logger: logger.With("TelemetryProvider", NAME),
|
||||||
streamCh: make(chan telemetry.TelemetryData, 1),
|
streamCh: make(chan telemetry.TelemetryData, 1),
|
||||||
data: telemetry.NewTelemetryData(),
|
data: telemetry.NewTelemetryData(),
|
||||||
SDK: &beam,
|
SDK: beam,
|
||||||
|
og: &bngsdk.Outgauge{},
|
||||||
ticker: time.NewTicker(time.Second / 60),
|
ticker: time.NewTicker(time.Second / 60),
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,6 +84,19 @@ func NewBeamNGProvider(ip string, port int) (*BeamNG, error) {
|
|||||||
return provider, nil
|
return provider, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *BeamNG) Close() {
|
||||||
|
b.SDK.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *BeamNG) Name() string {
|
||||||
|
return NAME
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *BeamNG) IsAlive(timeout time.Duration) bool {
|
||||||
|
_, err := b.SDK.Update()
|
||||||
|
return err == nil
|
||||||
|
}
|
||||||
|
|
||||||
func (b *BeamNG) StopStream() {
|
func (b *BeamNG) StopStream() {
|
||||||
if b.streamCancel == nil {
|
if b.streamCancel == nil {
|
||||||
return
|
return
|
||||||
@@ -97,7 +115,7 @@ func (b *BeamNG) Stream() (<-chan telemetry.TelemetryData, error) {
|
|||||||
return b.streamCh, nil
|
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
|
// NOTE: document how the Subscribe funtion works
|
||||||
slog.Debug(fmt.Sprintf("Len Req: %d\n", len(requestFields)))
|
slog.Debug(fmt.Sprintf("Len Req: %d\n", len(requestFields)))
|
||||||
|
|
||||||
@@ -107,9 +125,7 @@ func (b *BeamNG) Subscribe(requestFields map[int16]telemetry.FieldID) {
|
|||||||
// we will add their dependencies and the primitives to a slice
|
// we will add their dependencies and the primitives to a slice
|
||||||
pendingBinds := make([]telemetry.FieldID, telemetry.MaxFields)
|
pendingBinds := make([]telemetry.FieldID, telemetry.MaxFields)
|
||||||
|
|
||||||
for winID, id := range requestFields {
|
for _, id := range requestFields {
|
||||||
b.data.Values[id].IDs = append(b.data.Values[id].IDs, winID)
|
|
||||||
|
|
||||||
switch id {
|
switch id {
|
||||||
case telemetry.RPMStateColour:
|
case telemetry.RPMStateColour:
|
||||||
b.data.VirtualBinds = append(b.data.VirtualBinds, telemetry.NewRPMLights())
|
b.data.VirtualBinds = append(b.data.VirtualBinds, telemetry.NewRPMLights())
|
||||||
@@ -147,7 +163,7 @@ func (b *BeamNG) Subscribe(requestFields map[int16]telemetry.FieldID) {
|
|||||||
func (b *BeamNG) readData() {
|
func (b *BeamNG) readData() {
|
||||||
slog.Debug("READING THIS DATA")
|
slog.Debug("READING THIS DATA")
|
||||||
// BUG: getting stuck in here
|
// BUG: getting stuck in here
|
||||||
err := b.SDK.ReadData()
|
ogSnapshot, err := b.SDK.Update()
|
||||||
slog.Debug("THE DATA WAS READ")
|
slog.Debug("THE DATA WAS READ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Error getting data", "error", err)
|
slog.Error("Error getting data", "error", err)
|
||||||
@@ -155,6 +171,7 @@ func (b *BeamNG) readData() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
b.mut.Lock()
|
b.mut.Lock()
|
||||||
|
b.og = ogSnapshot
|
||||||
defer b.mut.Unlock()
|
defer b.mut.Unlock()
|
||||||
|
|
||||||
// Read 1 to 1 data
|
// Read 1 to 1 data
|
||||||
|
|||||||
@@ -18,34 +18,34 @@ func (b *BeamNG) unused(out *telemetry.TelemetryField) {
|
|||||||
|
|
||||||
func (b *BeamNG) updateSpeed(out *telemetry.TelemetryField) {
|
func (b *BeamNG) updateSpeed(out *telemetry.TelemetryField) {
|
||||||
out.Type = telemetry.DataTypeUINT16
|
out.Type = telemetry.DataTypeUINT16
|
||||||
out.Raw = uint64(conv.MsToKph(b.SDK.Data.Speed))
|
out.Raw = uint64(conv.MsToKph(b.og.Speed))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BeamNG) updateGear(out *telemetry.TelemetryField) {
|
func (b *BeamNG) updateGear(out *telemetry.TelemetryField) {
|
||||||
out.Type = telemetry.DataTypeSTRING
|
out.Type = telemetry.DataTypeSTRING
|
||||||
// NOTE: stupid idea but we can cache these values
|
// NOTE: stupid idea but we can cache these values
|
||||||
out.Str = strconv.Itoa(int(b.SDK.Data.Gear))
|
out.Str = strconv.Itoa(int(b.og.Gear))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BeamNG) updateRPM(out *telemetry.TelemetryField) {
|
func (b *BeamNG) updateRPM(out *telemetry.TelemetryField) {
|
||||||
out.Type = telemetry.DataTypeUINT16
|
out.Type = telemetry.DataTypeUINT16
|
||||||
out.Raw = uint64(uint16(b.SDK.Data.RPM))
|
out.Raw = uint64(uint16(b.og.RPM))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BeamNG) fuelLevel(out *telemetry.TelemetryField) {
|
func (b *BeamNG) fuelLevel(out *telemetry.TelemetryField) {
|
||||||
telemetry.FloatToStringTransform(b.SDK.Data.Fuel, out)
|
telemetry.FloatToStringTransform(b.og.Fuel, out)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BeamNG) oilPressure(out *telemetry.TelemetryField) {
|
func (b *BeamNG) oilPressure(out *telemetry.TelemetryField) {
|
||||||
telemetry.FloatToStringTransform(b.SDK.Data.OilPressure, out)
|
telemetry.FloatToStringTransform(b.og.OilPressure, out)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BeamNG) oilTemp(out *telemetry.TelemetryField) {
|
func (b *BeamNG) oilTemp(out *telemetry.TelemetryField) {
|
||||||
telemetry.FloatToStringTransform(b.SDK.Data.OilTemp, out)
|
telemetry.FloatToStringTransform(b.og.OilTemp, out)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BeamNG) engTemp(out *telemetry.TelemetryField) {
|
func (b *BeamNG) engTemp(out *telemetry.TelemetryField) {
|
||||||
telemetry.FloatToStringTransform(b.SDK.Data.EngTemp, out)
|
telemetry.FloatToStringTransform(b.og.EngTemp, out)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: find how to empty this
|
// NOTE: find how to empty this
|
||||||
@@ -54,7 +54,7 @@ func (b *BeamNG) pitSpeedLimiter(out *telemetry.TelemetryField) {
|
|||||||
|
|
||||||
func (b *BeamNG) leftIndicator(out *telemetry.TelemetryField) {
|
func (b *BeamNG) leftIndicator(out *telemetry.TelemetryField) {
|
||||||
chr := ' '
|
chr := ' '
|
||||||
if b.SDK.LeftIndicator() {
|
if b.og.LeftIndicator() {
|
||||||
chr = '<'
|
chr = '<'
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,7 +64,7 @@ func (b *BeamNG) leftIndicator(out *telemetry.TelemetryField) {
|
|||||||
|
|
||||||
func (b *BeamNG) rightIndicator(out *telemetry.TelemetryField) {
|
func (b *BeamNG) rightIndicator(out *telemetry.TelemetryField) {
|
||||||
chr := ' '
|
chr := ' '
|
||||||
if b.SDK.RightIndicator() {
|
if b.og.RightIndicator() {
|
||||||
chr = '>'
|
chr = '>'
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,7 +74,7 @@ func (b *BeamNG) rightIndicator(out *telemetry.TelemetryField) {
|
|||||||
|
|
||||||
func (b *BeamNG) absLight(out *telemetry.TelemetryField) {
|
func (b *BeamNG) absLight(out *telemetry.TelemetryField) {
|
||||||
chr := ' '
|
chr := ' '
|
||||||
if b.SDK.ABS() {
|
if b.og.ABS() {
|
||||||
chr = 'A'
|
chr = 'A'
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,7 +84,7 @@ func (b *BeamNG) absLight(out *telemetry.TelemetryField) {
|
|||||||
|
|
||||||
func (b *BeamNG) handbrakeLight(out *telemetry.TelemetryField) {
|
func (b *BeamNG) handbrakeLight(out *telemetry.TelemetryField) {
|
||||||
chr := ' '
|
chr := ' '
|
||||||
if b.SDK.Handbrake() {
|
if b.og.Handbrake() {
|
||||||
chr = 'P'
|
chr = 'P'
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,7 +94,7 @@ func (b *BeamNG) handbrakeLight(out *telemetry.TelemetryField) {
|
|||||||
|
|
||||||
func (b *BeamNG) tcLight(out *telemetry.TelemetryField) {
|
func (b *BeamNG) tcLight(out *telemetry.TelemetryField) {
|
||||||
chr := ' '
|
chr := ' '
|
||||||
if b.SDK.TractionControl() {
|
if b.og.TractionControl() {
|
||||||
chr = 'T'
|
chr = 'T'
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,7 +104,7 @@ func (b *BeamNG) tcLight(out *telemetry.TelemetryField) {
|
|||||||
|
|
||||||
func (b *BeamNG) batteryLight(out *telemetry.TelemetryField) {
|
func (b *BeamNG) batteryLight(out *telemetry.TelemetryField) {
|
||||||
chr := ' '
|
chr := ' '
|
||||||
if b.SDK.BatteryLight() {
|
if b.og.BatteryLight() {
|
||||||
chr = 'B'
|
chr = 'B'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package beamng
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log/slog"
|
||||||
|
"net"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func IsRunning() bool {
|
||||||
|
// TODO: The address should be loaded from some type of configuration
|
||||||
|
addr, err := net.ResolveUDPAddr("udp", "127.0.0.1:4444")
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, err := net.ListenUDP("udp", addr)
|
||||||
|
defer conn.Close()
|
||||||
|
if err != nil {
|
||||||
|
slog.Debug("failed to listen to udp socket: " + err.Error())
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := make([]byte, 512)
|
||||||
|
conn.SetReadDeadline(time.Now().Add(50 * time.Millisecond))
|
||||||
|
|
||||||
|
n, _, err := conn.ReadFromUDP(buf)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Think of a better number or something
|
||||||
|
return n >= 80
|
||||||
|
}
|
||||||
@@ -2,44 +2,23 @@ package iracing
|
|||||||
|
|
||||||
// This file maps the data from the desktop provider data structure to iRacing
|
// This file maps the data from the desktop provider data structure to iRacing
|
||||||
|
|
||||||
import (
|
// TODO: pass these to the new Updaters system
|
||||||
"esdi/telemetry"
|
// var internalToSDKFieldNames = map[telemetry.FieldID]string{
|
||||||
)
|
// // Tire data
|
||||||
|
// telemetry.LFtempL: "LFtempCL",
|
||||||
var internalToSDKFieldNames = map[telemetry.FieldID]string{
|
// telemetry.LFtempM: "LFtempCM",
|
||||||
telemetry.Speed: "Speed",
|
// telemetry.LFtempR: "LFtempCR",
|
||||||
telemetry.Gear: "Gear",
|
// telemetry.RFtempL: "RFtempCL",
|
||||||
telemetry.RPM: "RPM",
|
// telemetry.RFtempM: "RFtempCM",
|
||||||
telemetry.FuelLevel: "FuelLevel",
|
// telemetry.RFtempR: "RFtempCR",
|
||||||
// Engine Data
|
// telemetry.LRtempL: "LRtempCL",
|
||||||
telemetry.OilPress: "OilPress",
|
// telemetry.LRtempM: "LRtempCM",
|
||||||
telemetry.OilTemp: "OilTemp",
|
// telemetry.LRtempR: "LRtempCR",
|
||||||
telemetry.WaterTemp: "WaterTemp",
|
// telemetry.RRtempL: "RRtempCL",
|
||||||
// Engine Warnings
|
// telemetry.RRtempM: "RRtempCM",
|
||||||
telemetry.PitSpeedLimiter: "irsdk_pitSpeedLimiter",
|
// telemetry.RRtempR: "RRtempCR",
|
||||||
// Ajudstements
|
// // Session Data
|
||||||
telemetry.BrakeBias: "dcBrakeBias",
|
// telemetry.SessionTime: "SessionTime",
|
||||||
telemetry.ABSSetting: "dcABS",
|
// telemetry.ReplaySessionTime: "ReplaySessionTime",
|
||||||
telemetry.TCSetting: "dcTractionControl",
|
// telemetry.Empty: "empty",
|
||||||
telemetry.ThrottleSetting: "dcThrottleShape",
|
// }
|
||||||
// Lap Data
|
|
||||||
telemetry.LapLastLapTime: "LapLastLapTime",
|
|
||||||
telemetry.LapNumber: "Lap",
|
|
||||||
// Tire data
|
|
||||||
telemetry.LFtempL: "LFtempCL",
|
|
||||||
telemetry.LFtempM: "LFtempCM",
|
|
||||||
telemetry.LFtempR: "LFtempCR",
|
|
||||||
telemetry.RFtempL: "RFtempCL",
|
|
||||||
telemetry.RFtempM: "RFtempCM",
|
|
||||||
telemetry.RFtempR: "RFtempCR",
|
|
||||||
telemetry.LRtempL: "LRtempCL",
|
|
||||||
telemetry.LRtempM: "LRtempCM",
|
|
||||||
telemetry.LRtempR: "LRtempCR",
|
|
||||||
telemetry.RRtempL: "RRtempCL",
|
|
||||||
telemetry.RRtempM: "RRtempCM",
|
|
||||||
telemetry.RRtempR: "RRtempCR",
|
|
||||||
// Session Data
|
|
||||||
telemetry.SessionTime: "SessionTime",
|
|
||||||
telemetry.ReplaySessionTime: "ReplaySessionTime",
|
|
||||||
telemetry.Empty: "empty",
|
|
||||||
}
|
|
||||||
|
|||||||
+96
-121
@@ -7,11 +7,9 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"strconv"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
conv "esdi/conversions"
|
|
||||||
"esdi/telemetry"
|
"esdi/telemetry"
|
||||||
|
|
||||||
"github.com/ESilva15/goirsdk"
|
"github.com/ESilva15/goirsdk"
|
||||||
@@ -57,49 +55,101 @@ func NewIRacingProvider(
|
|||||||
data: telemetry.NewTelemetryData(),
|
data: telemetry.NewTelemetryData(),
|
||||||
streamCh: make(chan telemetry.TelemetryData, 1),
|
streamCh: make(chan telemetry.TelemetryData, 1),
|
||||||
// NOTE: This is because I stupidly recorded a test IBT file in 240
|
// NOTE: This is because I stupidly recorded a test IBT file in 240
|
||||||
|
// TODO: make this configurable from the user side
|
||||||
ticker: time.NewTicker(time.Second / 240),
|
ticker: time.NewTicker(time.Second / 240),
|
||||||
}
|
}
|
||||||
|
|
||||||
// provider.updaters = [telemetry.MaxFields]func(*telemetry.TelemetryField){}
|
provider.updaters = [telemetry.MaxFields]func(*telemetry.TelemetryField){
|
||||||
|
telemetry.Speed: provider.speed,
|
||||||
|
telemetry.Gear: provider.gear,
|
||||||
|
telemetry.RPM: provider.rpm,
|
||||||
|
telemetry.FuelLevel: provider.fuelLevel,
|
||||||
|
// Engine Data
|
||||||
|
telemetry.OilPress: provider.oilPress,
|
||||||
|
telemetry.OilTemp: provider.oilTemp,
|
||||||
|
telemetry.WaterTemp: provider.waterTemp,
|
||||||
|
// EngineWarnings
|
||||||
|
telemetry.PitSpeedLimiter: provider.pitSpeedLimiter,
|
||||||
|
// Adjustements
|
||||||
|
telemetry.BrakeBias: provider.brakeBias,
|
||||||
|
telemetry.ABSSetting: provider.absSetting,
|
||||||
|
telemetry.TCSetting: provider.tcSetting,
|
||||||
|
telemetry.ThrottleSetting: provider.throttleSetting,
|
||||||
|
// Lap Data
|
||||||
|
telemetry.LapLastLapTime: provider.lapTime,
|
||||||
|
telemetry.LapNumber: provider.lapNumber,
|
||||||
|
// case telemetry.LFtempM:
|
||||||
|
// binding.Transform = func(v any, out *telemetry.TelemetryField) {
|
||||||
|
// out.Type = telemetry.DataTypeSTRING
|
||||||
|
// out.Str = strconv.FormatFloat(float64(v.(float32)), 'f', 1, 32)
|
||||||
|
// }
|
||||||
|
// case telemetry.SessionTime:
|
||||||
|
// binding.Transform = func(v any, out *telemetry.TelemetryField) {
|
||||||
|
// out.Type = telemetry.DataTypeSTRING
|
||||||
|
// out.Str = strconv.FormatFloat(v.(float64), 'f', 1, 32)
|
||||||
|
// }
|
||||||
|
// case telemetry.ReplaySessionTime:
|
||||||
|
// binding.Transform = func(v any, out *telemetry.TelemetryField) {
|
||||||
|
// out.Type = telemetry.DataTypeSTRING
|
||||||
|
// out.Str = strconv.FormatFloat(v.(float64), 'f', 1, 32)
|
||||||
|
// }
|
||||||
|
// case telemetry.Empty:
|
||||||
|
// binding.Transform = telemetry.EmptyTransform
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the unset telemetry fields on the updaters as unused fields
|
||||||
|
for k := range int(telemetry.MaxFields) {
|
||||||
|
if provider.updaters[k] == nil {
|
||||||
|
provider.updaters[k] = provider.unused
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return provider, nil
|
return provider, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (i *IRacing) Close() {
|
||||||
|
// Need to find a way of gracefully closing the channel
|
||||||
|
// close(i.streamCh)
|
||||||
|
i.SDK.Close()
|
||||||
|
i.ticker.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
func (i *IRacing) isDataAvailable() bool {
|
func (i *IRacing) isDataAvailable() bool {
|
||||||
// Its offline telemetry, data must be available
|
// Its offline telemetry, data must be available
|
||||||
if i.SDK.File != nil {
|
if i.SDK.File == nil {
|
||||||
return true
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if live telemetry is on
|
// Check if live telemetry is on
|
||||||
if i.SDK.IsConnected() {
|
if !i.SDK.IsConnected() {
|
||||||
return true
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
return false
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *IRacing) stream(ctx context.Context) {
|
func (i *IRacing) stream(ctx context.Context) {
|
||||||
i.data.InitialTime = time.Now()
|
i.data.InitialTime = time.Now()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
defer close(i.streamCh)
|
||||||
|
|
||||||
|
// Put this into the configuration file
|
||||||
|
consecutiveTimeouts := 0
|
||||||
|
maxTimeouts := 30
|
||||||
|
dataEvTimeout := 100
|
||||||
for {
|
for {
|
||||||
// Explicitly intercpt cancellation
|
// Explicitly intercept cancellation
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
// We start by checking if we do or do not have data available
|
if i.SDK.CheckForDataEvent(time.Duration(dataEvTimeout) * time.Millisecond) {
|
||||||
if !i.isDataAvailable() {
|
consecutiveTimeouts = 0
|
||||||
continue
|
i.logger.Debug("sending data", "timeouts", consecutiveTimeouts)
|
||||||
}
|
|
||||||
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return
|
|
||||||
case <-i.ticker.C:
|
|
||||||
i.readData()
|
i.readData()
|
||||||
|
|
||||||
// Publish data
|
// Publish data
|
||||||
@@ -108,6 +158,15 @@ func (i *IRacing) stream(ctx context.Context) {
|
|||||||
default:
|
default:
|
||||||
// skip this data, don't allow publishers to lag behind
|
// skip this data, don't allow publishers to lag behind
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
consecutiveTimeouts++
|
||||||
|
if consecutiveTimeouts >= maxTimeouts {
|
||||||
|
i.logger.Info("Telemetry stream stalled")
|
||||||
|
if i.streamCancel != nil {
|
||||||
|
i.streamCancel()
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@@ -124,18 +183,13 @@ func (i *IRacing) readData() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read 1 to 1 data
|
// Read 1 to 1 data using our updaters
|
||||||
for _, b := range i.data.ActiveBinds {
|
for _, bind := range i.data.ActiveBinds {
|
||||||
v := i.SDK.Vars.Vars[b.Key].Value
|
i.updaters[bind.ID](&i.data.Values[bind.ID])
|
||||||
|
|
||||||
b.Transform(v, &i.data.Values[b.ID])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set up virtual binds
|
// Set up virtual binds
|
||||||
i.logger.Debug("Entering virtual binds loop")
|
|
||||||
for _, vBind := range i.data.VirtualBinds {
|
for _, vBind := range i.data.VirtualBinds {
|
||||||
// NOTE: delete the logs here, they are really bad
|
|
||||||
i.logger.Debug("Processing virtual binds")
|
|
||||||
vBind.Process(i.data)
|
vBind.Process(i.data)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -166,18 +220,16 @@ func (i *IRacing) StopStream() {
|
|||||||
i.streamCancel = nil
|
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.logger.Debug(fmt.Sprintf("Len Req: %d\n", len(requestFields)))
|
||||||
|
|
||||||
i.data.ActiveBinds = make([]telemetry.BoundField, 0, len(requestFields))
|
i.data.ActiveBinds = make([]telemetry.BoundField, 0, len(requestFields))
|
||||||
|
|
||||||
// First we must add the virtual fields
|
// First we must add the virtual fields
|
||||||
// we will add their dependencies and the primitives to a slice
|
// we will add their dependencies and the primitives to a slice
|
||||||
pendingBinds := make([]telemetry.FieldID, telemetry.MaxFields)
|
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 {
|
switch id {
|
||||||
case telemetry.RPMStateColour:
|
case telemetry.RPMStateColour:
|
||||||
i.data.VirtualBinds = append(i.data.VirtualBinds, telemetry.NewRPMLights())
|
i.data.VirtualBinds = append(i.data.VirtualBinds, telemetry.NewRPMLights())
|
||||||
@@ -199,97 +251,8 @@ func (i *IRacing) Subscribe(requestFields map[int16]telemetry.FieldID) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Translate the UI FieldIDs to this provider's field names
|
|
||||||
sdkKey, ok := internalToSDKFieldNames[id]
|
|
||||||
if !ok {
|
|
||||||
i.logger.Debug("failed to get internal id")
|
|
||||||
// Need to find a way to pass a message saying something wasn't right
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
binding := telemetry.BoundField{
|
binding := telemetry.BoundField{
|
||||||
Key: sdkKey,
|
ID: id,
|
||||||
ID: id,
|
|
||||||
}
|
|
||||||
|
|
||||||
switch id {
|
|
||||||
case telemetry.Speed:
|
|
||||||
binding.Transform = func(v any, out *telemetry.TelemetryField) {
|
|
||||||
out.Type = telemetry.DataTypeUINT16
|
|
||||||
out.Raw = uint64(conv.MsToKph(v.(float32)))
|
|
||||||
}
|
|
||||||
case telemetry.Gear:
|
|
||||||
binding.Transform = func(v any, out *telemetry.TelemetryField) {
|
|
||||||
out.Type = telemetry.DataTypeCHAR
|
|
||||||
// out.Raw = uint64(v.(int))
|
|
||||||
|
|
||||||
gear := 0
|
|
||||||
if val, ok := v.(int32); ok {
|
|
||||||
gear = int(val)
|
|
||||||
} else if val, ok := v.(int); ok {
|
|
||||||
gear = val
|
|
||||||
}
|
|
||||||
|
|
||||||
switch {
|
|
||||||
case gear == 0:
|
|
||||||
out.Raw = uint64('N') // ASCII 78
|
|
||||||
case gear < 0:
|
|
||||||
out.Raw = uint64('R') // ASCII 82
|
|
||||||
case gear > 0 && gear < 10:
|
|
||||||
// Quickest way to turn 1 into '1', 2 into '2', etc.
|
|
||||||
// ASCII '0' is 48, so 48 + 1 = 49 ('1')
|
|
||||||
out.Raw = uint64('0' + gear)
|
|
||||||
default:
|
|
||||||
out.Raw = uint64('?') // Fallback
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case telemetry.RPM:
|
|
||||||
binding.Transform = func(v any, out *telemetry.TelemetryField) {
|
|
||||||
out.Type = telemetry.DataTypeUINT16
|
|
||||||
out.Raw = uint64(uint16(v.(float32)))
|
|
||||||
}
|
|
||||||
case telemetry.FuelLevel:
|
|
||||||
binding.Transform = telemetry.FloatToStringTransformDEPRECATE
|
|
||||||
// Engine Data
|
|
||||||
case telemetry.OilPress:
|
|
||||||
binding.Transform = telemetry.FloatToStringTransformDEPRECATE
|
|
||||||
case telemetry.OilTemp:
|
|
||||||
binding.Transform = telemetry.FloatToStringTransformDEPRECATE
|
|
||||||
case telemetry.WaterTemp:
|
|
||||||
binding.Transform = telemetry.FloatToStringTransformDEPRECATE
|
|
||||||
// Something else
|
|
||||||
case telemetry.PitSpeedLimiter:
|
|
||||||
binding.Transform = PitSpeedLimiterTransform
|
|
||||||
// Adjustements
|
|
||||||
case telemetry.BrakeBias:
|
|
||||||
binding.Transform = telemetry.FloatToStringTransformDEPRECATE
|
|
||||||
case telemetry.ABSSetting:
|
|
||||||
binding.Transform = telemetry.FloatToUInt8TransformDEPRECATE
|
|
||||||
case telemetry.TCSetting:
|
|
||||||
binding.Transform = telemetry.FloatToUInt8TransformDEPRECATE
|
|
||||||
case telemetry.ThrottleSetting:
|
|
||||||
binding.Transform = telemetry.FloatToUInt8TransformDEPRECATE
|
|
||||||
case telemetry.LFtempM:
|
|
||||||
binding.Transform = func(v any, out *telemetry.TelemetryField) {
|
|
||||||
out.Type = telemetry.DataTypeSTRING
|
|
||||||
out.Str = strconv.FormatFloat(float64(v.(float32)), 'f', 1, 32)
|
|
||||||
}
|
|
||||||
case telemetry.SessionTime:
|
|
||||||
binding.Transform = func(v any, out *telemetry.TelemetryField) {
|
|
||||||
out.Type = telemetry.DataTypeSTRING
|
|
||||||
out.Str = strconv.FormatFloat(v.(float64), 'f', 1, 32)
|
|
||||||
}
|
|
||||||
case telemetry.ReplaySessionTime:
|
|
||||||
binding.Transform = func(v any, out *telemetry.TelemetryField) {
|
|
||||||
out.Type = telemetry.DataTypeSTRING
|
|
||||||
out.Str = strconv.FormatFloat(v.(float64), 'f', 1, 32)
|
|
||||||
}
|
|
||||||
case telemetry.Empty:
|
|
||||||
binding.Transform = telemetry.EmptyTransform
|
|
||||||
case telemetry.LapLastLapTime:
|
|
||||||
binding.Transform = LapTimeTransform
|
|
||||||
case telemetry.LapNumber:
|
|
||||||
binding.Transform = telemetry.UInt8Transform
|
|
||||||
}
|
}
|
||||||
|
|
||||||
i.data.ActiveBinds = append(i.data.ActiveBinds, binding)
|
i.data.ActiveBinds = append(i.data.ActiveBinds, binding)
|
||||||
@@ -298,3 +261,15 @@ func (i *IRacing) Subscribe(requestFields map[int16]telemetry.FieldID) {
|
|||||||
|
|
||||||
i.logger.Debug(fmt.Sprintf("Subscribed: %+v\n", i.data.ActiveBinds))
|
i.logger.Debug(fmt.Sprintf("Subscribed: %+v\n", i.data.ActiveBinds))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (i *IRacing) Name() string {
|
||||||
|
return NAME
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *IRacing) IsAlive(timeout time.Duration) bool {
|
||||||
|
if !i.SDK.CheckForDataEvent(timeout) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,17 +1,134 @@
|
|||||||
package iracing
|
package iracing
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log/slog"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
conv "esdi/conversions"
|
||||||
"esdi/telemetry"
|
"esdi/telemetry"
|
||||||
|
|
||||||
|
"github.com/ESilva15/goirsdk"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
LapTimeFormatStr = "04:05.000"
|
LapTimeFormatStr = "04:05.000"
|
||||||
)
|
)
|
||||||
|
|
||||||
func LapTimeTransform(v any, out *telemetry.TelemetryField) {
|
func getVar[T any](
|
||||||
lapTimeInSeconds := v.(float32)
|
vars map[string]goirsdk.Var,
|
||||||
|
key string,
|
||||||
|
fallback T,
|
||||||
|
logger *slog.Logger,
|
||||||
|
) T {
|
||||||
|
raw, ok := vars[key]
|
||||||
|
if !ok {
|
||||||
|
logger.Error("key not present in map", "key", key)
|
||||||
|
return fallback
|
||||||
|
}
|
||||||
|
|
||||||
|
val, ok := raw.Value.(T)
|
||||||
|
if !ok {
|
||||||
|
logger.Error("could not cast value", "key", key, "val", raw)
|
||||||
|
return fallback
|
||||||
|
}
|
||||||
|
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *IRacing) unused(out *telemetry.TelemetryField) {
|
||||||
|
out.Unused()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *IRacing) speed(out *telemetry.TelemetryField) {
|
||||||
|
speed := getVar(i.SDK.Vars.Vars, "Speed", float32(0), i.logger)
|
||||||
|
|
||||||
|
out.Type = telemetry.DataTypeUINT16
|
||||||
|
out.Raw = uint64(conv.MsToKph(speed))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *IRacing) gear(out *telemetry.TelemetryField) {
|
||||||
|
gear := getVar(i.SDK.Vars.Vars, "Gear", int(0), i.logger)
|
||||||
|
|
||||||
|
out.Type = telemetry.DataTypeCHAR
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case gear == 0:
|
||||||
|
out.Raw = uint64('N')
|
||||||
|
case gear == -1:
|
||||||
|
out.Raw = uint64('R')
|
||||||
|
case gear > 0:
|
||||||
|
out.Raw = uint64('0' + gear)
|
||||||
|
default:
|
||||||
|
out.Raw = uint64('?') // Fallback
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *IRacing) rpm(out *telemetry.TelemetryField) {
|
||||||
|
rpm := getVar(i.SDK.Vars.Vars, "RPM", float32(0), i.logger)
|
||||||
|
|
||||||
|
out.Type = telemetry.DataTypeUINT16
|
||||||
|
out.Raw = uint64(uint16(rpm))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *IRacing) fuelLevel(out *telemetry.TelemetryField) {
|
||||||
|
fuelLevel := getVar(i.SDK.Vars.Vars, "FuelLevel", float32(-1.0), i.logger)
|
||||||
|
telemetry.FloatToStringTransform(fuelLevel, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
// EngineWarnings
|
||||||
|
func (i *IRacing) pitSpeedLimiter(out *telemetry.TelemetryField) {
|
||||||
|
out.Type = telemetry.DataTypeSTRING
|
||||||
|
out.Str = " "
|
||||||
|
|
||||||
|
if i.SDK.PitSpeedLimiter() {
|
||||||
|
out.Str = "PIT"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// EngineData ↓
|
||||||
|
func (i *IRacing) oilPress(out *telemetry.TelemetryField) {
|
||||||
|
oilPress := getVar(i.SDK.Vars.Vars, "OilPress", float32(-1.0), i.logger)
|
||||||
|
telemetry.FloatToStringTransform(oilPress, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *IRacing) oilTemp(out *telemetry.TelemetryField) {
|
||||||
|
oilTemp := getVar(i.SDK.Vars.Vars, "OilTemp", float32(-1.0), i.logger)
|
||||||
|
telemetry.FloatToStringTransform(oilTemp, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *IRacing) waterTemp(out *telemetry.TelemetryField) {
|
||||||
|
waterTemp := getVar(i.SDK.Vars.Vars, "WaterTemp", float32(-1.0), i.logger)
|
||||||
|
telemetry.FloatToStringTransform(waterTemp, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
// EngineData ↑
|
||||||
|
|
||||||
|
// Adjustments ↓
|
||||||
|
func (i *IRacing) brakeBias(out *telemetry.TelemetryField) {
|
||||||
|
bb := getVar(i.SDK.Vars.Vars, "dcBrakeBias", float32(-1.0), i.logger)
|
||||||
|
telemetry.FloatToStringTransform(bb, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *IRacing) absSetting(out *telemetry.TelemetryField) {
|
||||||
|
abs := getVar(i.SDK.Vars.Vars, "dcABS", float32(-1.0), i.logger)
|
||||||
|
telemetry.FloatToUInt8Transform(abs, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *IRacing) tcSetting(out *telemetry.TelemetryField) {
|
||||||
|
tc := getVar(i.SDK.Vars.Vars, "dcTractionControl", float32(-1.0), i.logger)
|
||||||
|
telemetry.FloatToUInt8Transform(tc, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *IRacing) throttleSetting(out *telemetry.TelemetryField) {
|
||||||
|
throttle := getVar(i.SDK.Vars.Vars, "dcThrottleShape", float32(-1.0), i.logger)
|
||||||
|
telemetry.FloatToUInt8Transform(throttle, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Adjustments ↑
|
||||||
|
|
||||||
|
// Laps ↓
|
||||||
|
func (i *IRacing) lapTime(out *telemetry.TelemetryField) {
|
||||||
|
lapTimeInSeconds := getVar(i.SDK.Vars.Vars, "LapLastLapTime", float32(0), i.logger)
|
||||||
|
|
||||||
if lapTimeInSeconds < 0 {
|
if lapTimeInSeconds < 0 {
|
||||||
lapTimeInSeconds = 0
|
lapTimeInSeconds = 0
|
||||||
@@ -24,11 +141,9 @@ func LapTimeTransform(v any, out *telemetry.TelemetryField) {
|
|||||||
out.Str = lapTime.Format(LapTimeFormatStr)
|
out.Str = lapTime.Format(LapTimeFormatStr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func PitSpeedLimiterTransform(v any, out *telemetry.TelemetryField) {
|
func (i *IRacing) lapNumber(out *telemetry.TelemetryField) {
|
||||||
out.Type = telemetry.DataTypeSTRING
|
lapNumber := getVar(i.SDK.Vars.Vars, "Lap", int(0), i.logger)
|
||||||
if v.(bool) {
|
telemetry.UInt8Transform(lapNumber, out)
|
||||||
out.Str = "PIT"
|
|
||||||
} else {
|
|
||||||
out.Str = " "
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Laps ↑
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package iracing
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/ESilva15/goirsdk"
|
||||||
|
eventutils "github.com/ESilva15/goirsdk/eventutils"
|
||||||
|
)
|
||||||
|
|
||||||
|
func IsRunning() bool {
|
||||||
|
irUtils, err := eventutils.Init()
|
||||||
|
if err != nil {
|
||||||
|
// we need error validation or something here
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
defer irUtils.Close()
|
||||||
|
|
||||||
|
if err := irUtils.OpenEvent(goirsdk.IRSDK_DATAVALIDEVENTNAME); err != nil {
|
||||||
|
// we need error validation or something here
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// We now check for some consecutive data events
|
||||||
|
for range 3 {
|
||||||
|
if !irUtils.CheckValidDataEvent(1 * time.Second) {
|
||||||
|
// slog.Debug("Timed out waiting for DataValidEvent")
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
+33
-11
@@ -8,34 +8,56 @@ import (
|
|||||||
"esdi/providers/iracing"
|
"esdi/providers/iracing"
|
||||||
"esdi/telemetry"
|
"esdi/telemetry"
|
||||||
|
|
||||||
|
bngsdk "github.com/ESilva15/gobngsdk"
|
||||||
"github.com/ESilva15/goirsdk"
|
"github.com/ESilva15/goirsdk"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Make this be some kind of struct where we can access a function that returns
|
// Make this be some kind of struct where we can access a function that returns
|
||||||
// the selected provider by its name
|
// the selected provider by its name
|
||||||
type Provider struct {
|
type Provider struct {
|
||||||
Name string
|
Name string
|
||||||
Provider telemetry.TelemetryProvider
|
NewProvider func(*slog.Logger) (telemetry.TelemetryProvider, error)
|
||||||
|
IsRunning func() bool // To check if this provider is up and running
|
||||||
}
|
}
|
||||||
|
|
||||||
var Providers = map[string]Provider{
|
var Providers = map[string]Provider{
|
||||||
beamng.NAME: {
|
beamng.NAME: {
|
||||||
Name: beamng.NAME,
|
Name: beamng.NAME,
|
||||||
|
NewProvider: NewBeamNGProvider,
|
||||||
|
IsRunning: beamng.IsRunning,
|
||||||
},
|
},
|
||||||
iracing.NAME: {
|
iracing.NAME: {
|
||||||
Name: iracing.NAME,
|
Name: iracing.NAME,
|
||||||
|
NewProvider: NewLiveIRacingProvider,
|
||||||
|
IsRunning: iracing.IsRunning,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewIRacingProvider(logger *slog.Logger, opts goirsdk.Options,
|
func NewLiveIRacingProvider(logger *slog.Logger) (telemetry.TelemetryProvider, error) {
|
||||||
) telemetry.TelemetryProvider {
|
provider, err := iracing.NewIRacingProvider(logger, goirsdk.Options{
|
||||||
provider, _ := iracing.NewIRacingProvider(logger, opts)
|
Logger: logger,
|
||||||
|
SourceType: goirsdk.SharedMemoryFile,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("failed to create iRacing provider", "err", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
return provider
|
return provider, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBeamNGProvider(ip string, port int) telemetry.TelemetryProvider {
|
func NewBeamNGProvider(logger *slog.Logger) (telemetry.TelemetryProvider, error) {
|
||||||
provider, _ := beamng.NewBeamNGProvider(ip, port)
|
// TODO: these should come from some kind of config
|
||||||
|
provider, err := beamng.NewBeamNGProvider(logger, &bngsdk.Options{
|
||||||
|
Logger: logger.With("TelemetryProvider", beamng.NAME),
|
||||||
|
SourceType: bngsdk.UDPData,
|
||||||
|
ImportUDPAddress: "127.0.0.1",
|
||||||
|
ImportUDPPort: 4444,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("failed to create BeamNG provider", "err", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
return provider
|
return provider, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,148 +0,0 @@
|
|||||||
package services
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"log/slog"
|
|
||||||
"sync/atomic"
|
|
||||||
|
|
||||||
"esdi/cdashdisplay"
|
|
||||||
helper "esdi/helpers"
|
|
||||||
"esdi/peripheral"
|
|
||||||
"esdi/telemetry"
|
|
||||||
)
|
|
||||||
|
|
||||||
type CDashService struct {
|
|
||||||
Logger *slog.Logger
|
|
||||||
CDash *cdashdisplay.CDashDisplay
|
|
||||||
DevClerk *peripheral.PeripheralDeviceClerk
|
|
||||||
Messages chan string
|
|
||||||
// Telemetry Channel
|
|
||||||
streamCancel context.CancelFunc
|
|
||||||
TelemCh <-chan telemetry.TelemetryData
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewCDashService(logger *slog.Logger) *CDashService {
|
|
||||||
sharedChannel := make(chan string, 10)
|
|
||||||
return &CDashService{
|
|
||||||
Logger: logger,
|
|
||||||
CDash: nil,
|
|
||||||
DevClerk: peripheral.NewPeripheralDeviceClerk(),
|
|
||||||
Messages: sharedChannel,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cds *CDashService) FindDevice() {
|
|
||||||
cds.Messages <- "looking for cdash display...\n"
|
|
||||||
cds.Logger.Info("Looking for CDashDisplay")
|
|
||||||
|
|
||||||
cdashdisplay.SetLogger(cds.Logger.With("[device]", "cdashdisplay"))
|
|
||||||
|
|
||||||
display, err := cdashdisplay.NewCDashDisplay()
|
|
||||||
if err != nil {
|
|
||||||
cds.Logger.Info("didn't find cdashdisplay")
|
|
||||||
cds.Messages <- "didn't find cdash display\n"
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
cds.CDash = display
|
|
||||||
cds.Logger.Info("found cdashdisplay on: " + display.WT.Cfg.Name)
|
|
||||||
cds.Messages <- "found cdashdisplay on: " + display.WT.Cfg.Name + "\n"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cds *CDashService) CreateWindow(
|
|
||||||
win *cdashdisplay.DesktopUIWindow,
|
|
||||||
) (*cdashdisplay.DesktopUIWindow, error) {
|
|
||||||
updatedWindow, err := cds.CDash.CreateWindow(win)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return updatedWindow, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cds *CDashService) LoadLayout(layoutPath string) error {
|
|
||||||
return cds.CDash.LoadLayout(layoutPath)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cds *CDashService) SaveLayout(layoutPath string) error {
|
|
||||||
return cds.CDash.SaveLayout(layoutPath)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cds *CDashService) UnloadLayout() error {
|
|
||||||
return cds.CDash.UnloadLayout()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cds *CDashService) UpdateWindow(win *cdashdisplay.DesktopUIWindow) error {
|
|
||||||
cds.Messages <- fmt.Sprintf("Updating a window:\n%+v\n", win)
|
|
||||||
return cds.CDash.UpdateWindow(win)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cds *CDashService) DeleteWindow(idx int16) error {
|
|
||||||
return cds.CDash.DestroyWindow(idx)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cds *CDashService) ResizeWindow(idx int16, vec *helper.Vector) error {
|
|
||||||
err := cds.CDash.ResizeWindow(idx, vec)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cds *CDashService) MoveWindow(idx int16, vec *helper.Vector) error {
|
|
||||||
err := cds.CDash.MoveWindow(idx, vec)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cds *CDashService) SetTelemetryChannel(ch <-chan telemetry.TelemetryData) {
|
|
||||||
cds.TelemCh = ch
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cds *CDashService) StartStream() {
|
|
||||||
// NOTE: i'm using this pattern a whole lot. Maybe I can create a struct to handle this
|
|
||||||
var ctx context.Context
|
|
||||||
ctx, cds.streamCancel = context.WithCancel(context.Background())
|
|
||||||
|
|
||||||
go cds.transmit(ctx)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cds *CDashService) StopStream() {
|
|
||||||
if cds.streamCancel == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
cds.streamCancel()
|
|
||||||
cds.streamCancel = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// INTERNAL
|
|
||||||
|
|
||||||
func (cds *CDashService) transmit(ctx context.Context) {
|
|
||||||
var isSending atomic.Bool
|
|
||||||
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return
|
|
||||||
case data, ok := <-cds.TelemCh:
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if isSending.Load() {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
isSending.Store(true)
|
|
||||||
|
|
||||||
cds.CDash.SendData(&data)
|
|
||||||
isSending.Store(false)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
package services
|
|
||||||
|
|
||||||
// DeviceService will handle sending the data from the telemetry service to the
|
|
||||||
// actual devices
|
|
||||||
// NOTE: create a virtual device and make it be the output window or something so
|
|
||||||
// we can just add it as a device or whatever instead of being a custom made thing
|
|
||||||
// that would be pretty cool I think
|
|
||||||
type DeviceService struct {
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,177 @@
|
|||||||
|
package services
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"esdi/devices"
|
||||||
|
"esdi/peripheral"
|
||||||
|
"esdi/telemetry"
|
||||||
|
)
|
||||||
|
|
||||||
|
var ErrPeripheralAlreadyRegistered = errors.New("peripheral is already registered")
|
||||||
|
|
||||||
|
// DeviceService will handle sending the data from the telemetry service to the
|
||||||
|
// actual devices
|
||||||
|
// NOTE: create a virtual device and make it be the output window or something so
|
||||||
|
// we can just add it as a device or whatever instead of being a custom made thing
|
||||||
|
// that would be pretty cool I think
|
||||||
|
type DeviceService struct {
|
||||||
|
Logger *slog.Logger
|
||||||
|
// Device discovery
|
||||||
|
mu sync.RWMutex
|
||||||
|
ctxDiscovery context.Context
|
||||||
|
ctxDiscoveryCancel context.CancelFunc
|
||||||
|
Devices map[string]peripheral.Peripheral
|
||||||
|
// Strem handling
|
||||||
|
streamCancel context.CancelFunc
|
||||||
|
TelemCh <-chan telemetry.TelemetryData
|
||||||
|
// Output
|
||||||
|
Messages chan string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewDeviceService(logger *slog.Logger) *DeviceService {
|
||||||
|
sharedChannel := make(chan string, 10)
|
||||||
|
|
||||||
|
dev := &DeviceService{
|
||||||
|
Devices: make(map[string]peripheral.Peripheral),
|
||||||
|
Logger: logger,
|
||||||
|
Messages: sharedChannel,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start the routine that looks for devices - should always be running in the background
|
||||||
|
// Create a routine to poll this provider while we wait to start the stream or pause it
|
||||||
|
dev.ctxDiscovery, dev.ctxDiscoveryCancel = context.WithCancel(context.Background())
|
||||||
|
go dev.FindDevices()
|
||||||
|
|
||||||
|
return dev
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ds *DeviceService) FindDevices() {
|
||||||
|
// Need to define a list of devices to search for
|
||||||
|
// For now lets just try to find our cdashdisplay - will think about the rest later
|
||||||
|
ticker := time.NewTicker(2 * time.Second)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ds.ctxDiscovery.Done():
|
||||||
|
// If requested to cancel we cancel background discovery
|
||||||
|
return
|
||||||
|
case <-ticker.C:
|
||||||
|
for pName, peripheral := range devices.List {
|
||||||
|
if ds.DeviceExists(pName) {
|
||||||
|
// We already discovered this device
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
ds.Logger.Debug("looking for device", "name", pName)
|
||||||
|
dev, err := peripheral.Discover()
|
||||||
|
if err != nil {
|
||||||
|
ds.Logger.Debug("didn't find device", "name", pName)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Register the device we just found
|
||||||
|
ds.RegisterDevice(dev)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// func (ds *DeviceService) SubscribeFields() error {
|
||||||
|
// for _, dev := range ds.Devices {
|
||||||
|
// fields := dev.RequiredFields()
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// return nil
|
||||||
|
// }
|
||||||
|
|
||||||
|
func (ds *DeviceService) RegisterDevice(dev peripheral.Peripheral) error {
|
||||||
|
ds.mu.Lock()
|
||||||
|
defer ds.mu.Unlock()
|
||||||
|
|
||||||
|
if ds.DeviceExists(dev.Name()) {
|
||||||
|
return ErrPeripheralAlreadyRegistered
|
||||||
|
}
|
||||||
|
|
||||||
|
ds.Devices[dev.Name()] = dev
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ds *DeviceService) GetDevice(name string) (peripheral.Peripheral, error) {
|
||||||
|
val, ok := ds.Devices[name]
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("device `%s` couldn't be found", name)
|
||||||
|
}
|
||||||
|
|
||||||
|
return val, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ds *DeviceService) DeviceExists(name string) bool {
|
||||||
|
if _, ok := ds.Devices[name]; !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ds *DeviceService) StartStream() {
|
||||||
|
// NOTE: i'm using this pattern a whole lot. Maybe I can create a struct to handle this
|
||||||
|
var ctx context.Context
|
||||||
|
ctx, ds.streamCancel = context.WithCancel(context.Background())
|
||||||
|
|
||||||
|
go ds.transmit(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ds *DeviceService) StopStream() {
|
||||||
|
if ds.streamCancel == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ds.streamCancel()
|
||||||
|
ds.streamCancel = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetTelemetryChannel sets the TelemCh to the passed channel
|
||||||
|
func (ds *DeviceService) SetTelemetryChannel(ch <-chan telemetry.TelemetryData) {
|
||||||
|
ds.TelemCh = ch
|
||||||
|
}
|
||||||
|
|
||||||
|
// transmit will send the data to the devices themselves
|
||||||
|
func (ds *DeviceService) transmit(ctx context.Context) {
|
||||||
|
var isSending atomic.Bool
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case data, ok := <-ds.TelemCh:
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if isSending.Load() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
isSending.Store(true)
|
||||||
|
|
||||||
|
// TODO: make a copy of the data and send that copy instead of keeping
|
||||||
|
// the data locked
|
||||||
|
ds.mu.RLock()
|
||||||
|
for _, dev := range ds.Devices {
|
||||||
|
dev.SendData(&data)
|
||||||
|
}
|
||||||
|
ds.mu.RUnlock()
|
||||||
|
|
||||||
|
isSending.Store(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+142
-29
@@ -4,56 +4,136 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"esdi/providers"
|
"esdi/providers"
|
||||||
|
"esdi/telemetry"
|
||||||
telem "esdi/telemetry"
|
telem "esdi/telemetry"
|
||||||
|
|
||||||
"github.com/ESilva15/goirsdk"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// TelemetryService will be our base struct to handle telemetry data
|
// TelemetryService will be our base struct to handle telemetry data
|
||||||
// It should hook to a data sink and handle it like iRacing, BeamNG, AC and so on
|
// It should hook to a data sink and handle it like iRacing, BeamNG, AC and so on
|
||||||
type TelemetryService struct {
|
type TelemetryService struct {
|
||||||
logger *slog.Logger
|
logger *slog.Logger
|
||||||
cdash *CDashService
|
devService *DeviceService
|
||||||
// Concurrency protection
|
// Concurrency protection
|
||||||
mut sync.RWMutex
|
mut sync.RWMutex
|
||||||
ativeProvider telem.TelemetryProvider
|
activeProvider telem.TelemetryProvider
|
||||||
|
isConnected bool
|
||||||
// Channel for the UI
|
// Channel for the UI
|
||||||
listeners map[string]chan telem.TelemetryData
|
listeners map[string]chan telem.TelemetryData
|
||||||
cancelForward context.CancelFunc
|
cancelForward context.CancelFunc
|
||||||
|
// Output window
|
||||||
|
Messages chan string
|
||||||
|
// Cancel looking for providers
|
||||||
|
CtxMonitor context.Context
|
||||||
|
cancelMonitor context.CancelFunc
|
||||||
|
CtxHealthcheck context.Context
|
||||||
|
healthCheckCancel context.CancelFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTelemetryService(logger *slog.Logger, cdash *CDashService) *TelemetryService {
|
func NewTelemetryService(logger *slog.Logger, devServo *DeviceService) *TelemetryService {
|
||||||
|
sharedChannel := make(chan string, 10)
|
||||||
newService := &TelemetryService{
|
newService := &TelemetryService{
|
||||||
logger: logger,
|
logger: logger,
|
||||||
cdash: cdash,
|
isConnected: false,
|
||||||
listeners: make(map[string]chan telem.TelemetryData),
|
devService: devServo,
|
||||||
|
listeners: make(map[string]chan telem.TelemetryData),
|
||||||
|
Messages: sharedChannel,
|
||||||
}
|
}
|
||||||
|
newService.CtxMonitor, newService.cancelMonitor = context.WithCancel(context.Background())
|
||||||
// Need to instantiate a default provider here
|
|
||||||
// source := "/home/esilva/Desktop/projetos/simracing_peripherals/testTelemetry/gt3_mustang_bathurst.ibt"
|
|
||||||
firstProvider := providers.NewIRacingProvider(slog.Default(), goirsdk.Options{
|
|
||||||
SourceType: goirsdk.SharedMemoryFile,
|
|
||||||
})
|
|
||||||
// firstProvider := providers.NewBeamNGProvider("127.0.0.1", 4443)
|
|
||||||
|
|
||||||
newService.SwitchProvider(firstProvider)
|
|
||||||
|
|
||||||
return newService
|
return newService
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *TelemetryService) ProviderMonitor(ctx context.Context) {
|
||||||
|
ticker := time.NewTicker(2 * time.Second)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case <-ticker.C:
|
||||||
|
slog.Info("checking if provider is still running")
|
||||||
|
if !t.activeProvider.IsAlive(500 * time.Millisecond) {
|
||||||
|
slog.Warn("provider healthcheck failed")
|
||||||
|
t.dropActiveProvider()
|
||||||
|
t.onProviderHealthCheckFailed()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TelemetryService) onProviderHealthCheckFailed() {
|
||||||
|
// Just restart the whole lookup process
|
||||||
|
go t.FindProvider(t.CtxMonitor)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TelemetryService) onFindProvider(prov telem.TelemetryProvider) {
|
||||||
|
// Attach to the provider
|
||||||
|
t.logger.Info("found provider for " + prov.Name())
|
||||||
|
err := t.SwitchProvider(prov)
|
||||||
|
if err != nil {
|
||||||
|
t.logger.Error("failed to switch to provider onFindProvider", "err", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a routine to poll this provider while we wait to start the stream or pause it
|
||||||
|
t.CtxHealthcheck, t.healthCheckCancel = context.WithCancel(context.Background())
|
||||||
|
go t.ProviderMonitor(t.CtxHealthcheck)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TelemetryService) onProviderStopsMidStream() {
|
||||||
|
// clear the current provider
|
||||||
|
// TODO: now we need to also clear the devices to restart everything,
|
||||||
|
// if the stream stopped we have to restart the devices and everything
|
||||||
|
t.logger.Info("cleaning dropped provider and restarting lookup service")
|
||||||
|
t.dropActiveProvider()
|
||||||
|
go t.FindProvider(t.CtxMonitor)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: add some way of retriggering this. Currently it should:
|
||||||
|
// start monitoring on startup -> find provider -> stop monitoring (when game closes for example)
|
||||||
|
func (t *TelemetryService) FindProvider(ctx context.Context) {
|
||||||
|
ticker := time.NewTicker(2 * time.Second)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
t.logger.Debug("monitoring for providers")
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case <-ticker.C:
|
||||||
|
for _, prov := range providers.Providers {
|
||||||
|
// t.logger.Debug("checking provider: " + prov.Name)
|
||||||
|
provider, err := prov.NewProvider(t.logger)
|
||||||
|
if err != nil {
|
||||||
|
// Its not running
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if provider.IsAlive(500 * time.Millisecond) {
|
||||||
|
t.onFindProvider(provider)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (t *TelemetryService) SwitchProvider(newProvider telem.TelemetryProvider) error {
|
func (t *TelemetryService) SwitchProvider(newProvider telem.TelemetryProvider) error {
|
||||||
t.mut.Lock()
|
t.mut.Lock()
|
||||||
defer t.mut.Unlock()
|
defer t.mut.Unlock()
|
||||||
|
|
||||||
// Clean up the current to be old provider
|
// Clean up the current to be old provider
|
||||||
if t.ativeProvider != nil {
|
if t.activeProvider != nil {
|
||||||
t.dropActiveProvider()
|
t.dropActiveProvider()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Assign the new provider
|
// Assign the new provider
|
||||||
t.ativeProvider = newProvider
|
t.activeProvider = newProvider
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -65,11 +145,14 @@ func (t *TelemetryService) multiplexData(ctx context.Context, dataCh <-chan tele
|
|||||||
return
|
return
|
||||||
case data, ok := <-dataCh:
|
case data, ok := <-dataCh:
|
||||||
if !ok {
|
if !ok {
|
||||||
|
t.logger.Debug("something happened on the provider - stream closed")
|
||||||
|
t.onProviderStopsMidStream()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
t.mut.RLock()
|
t.mut.RLock()
|
||||||
for _, ch := range t.listeners {
|
for _, ch := range t.listeners {
|
||||||
|
// t.logger.Debug("sending data to listener", "listener", key, "data", data)
|
||||||
select {
|
select {
|
||||||
case ch <- data:
|
case ch <- data:
|
||||||
// Sends data to the subscriber
|
// Sends data to the subscriber
|
||||||
@@ -87,7 +170,9 @@ func (t *TelemetryService) dropActiveProvider() {
|
|||||||
t.cancelForward()
|
t.cancelForward()
|
||||||
}
|
}
|
||||||
|
|
||||||
t.ativeProvider.StopStream()
|
t.activeProvider.StopStream()
|
||||||
|
t.activeProvider.Close()
|
||||||
|
t.activeProvider = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TelemetryService) SubscribeListener(id string, bufferSize int) <-chan telem.TelemetryData {
|
func (t *TelemetryService) SubscribeListener(id string, bufferSize int) <-chan telem.TelemetryData {
|
||||||
@@ -118,18 +203,38 @@ func (t *TelemetryService) UnsubscribeListener(id string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TelemetryService) SubscribeToFields(fields map[int16]telem.FieldID) {
|
func (t *TelemetryService) SubscribeToFields() {
|
||||||
t.ativeProvider.Subscribe(fields)
|
seen := make(map[telemetry.FieldID]struct{})
|
||||||
|
var allFields []telemetry.FieldID
|
||||||
|
|
||||||
|
for _, dev := range t.devService.Devices {
|
||||||
|
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() {
|
func (t *TelemetryService) StartStream() {
|
||||||
slog.Debug("Stream started")
|
slog.Debug("Stream started")
|
||||||
|
|
||||||
// Start the new stream
|
// Start the new stream
|
||||||
simInCh, _ := t.ativeProvider.Stream()
|
if t.activeProvider == nil {
|
||||||
// if err != nil {
|
slog.Debug("there's no active provider. not starting the stream")
|
||||||
// // NOTE
|
return
|
||||||
// }
|
}
|
||||||
|
|
||||||
|
// Stop the provider healthcheck
|
||||||
|
t.healthCheckCancel()
|
||||||
|
|
||||||
|
simInCh, _ := t.activeProvider.Stream()
|
||||||
|
// TODO: the provider needs to be able to tell the data has stopped
|
||||||
|
// so we can restart the provider lookup routine
|
||||||
|
|
||||||
// Create the context so we can control the lifecycle
|
// Create the context so we can control the lifecycle
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
@@ -148,5 +253,13 @@ func (t *TelemetryService) StopStream() {
|
|||||||
t.cancelForward = nil
|
t.cancelForward = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
t.ativeProvider.StopStream()
|
t.activeProvider.StopStream()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TelemetryService) HasActiveProvider() bool {
|
||||||
|
if t.activeProvider == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,40 +7,35 @@ func EmptyTransform(v any, out *TelemetryField) {
|
|||||||
out.Raw = uint64('-')
|
out.Raw = uint64('-')
|
||||||
}
|
}
|
||||||
|
|
||||||
func UInt8Transform(v any, out *TelemetryField) {
|
func UInt8Transform(v int, out *TelemetryField) {
|
||||||
out.Type = DataTypeUINT8
|
out.Type = DataTypeUINT8
|
||||||
|
|
||||||
if v == nil {
|
if v < 0 {
|
||||||
out.Raw = 0
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
out.Raw = uint64(v.(int))
|
|
||||||
}
|
|
||||||
|
|
||||||
func FloatToStringTransform(v float32, out *TelemetryField) {
|
|
||||||
out.Type = DataTypeSTRING
|
|
||||||
out.Str = strconv.FormatFloat(float64(v), 'f', 1, 32)
|
|
||||||
}
|
|
||||||
|
|
||||||
func FloatToStringTransformDEPRECATE(v any, out *TelemetryField) {
|
|
||||||
out.Type = DataTypeSTRING
|
|
||||||
|
|
||||||
if v == nil {
|
|
||||||
out.Str = "inv"
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
out.Str = strconv.FormatFloat(float64(v.(float32)), 'f', 1, 32)
|
|
||||||
}
|
|
||||||
|
|
||||||
func FloatToUInt8TransformDEPRECATE(v any, out *TelemetryField) {
|
|
||||||
out.Type = DataTypeUINT8
|
|
||||||
|
|
||||||
if v == nil {
|
|
||||||
out.Raw = uint64(0)
|
out.Raw = uint64(0)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
out.Raw = uint64(v.(float32))
|
out.Raw = uint64(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func FloatToStringTransform(v float32, out *TelemetryField) {
|
||||||
|
out.Type = DataTypeSTRING
|
||||||
|
|
||||||
|
if v < 0.0 {
|
||||||
|
out.Str = "inv"
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
out.Str = strconv.FormatFloat(float64(v), 'f', 1, 32)
|
||||||
|
}
|
||||||
|
|
||||||
|
func FloatToUInt8Transform(v float32, out *TelemetryField) {
|
||||||
|
out.Type = DataTypeUINT8
|
||||||
|
|
||||||
|
if v < 0.0 {
|
||||||
|
out.Raw = uint64(0)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
out.Raw = uint64(v)
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-80
@@ -1,12 +1,18 @@
|
|||||||
package telemetry
|
package telemetry
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"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
|
// 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.
|
// 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.
|
// allow him to make something that allows some data transformation to occur.
|
||||||
@@ -30,11 +36,8 @@ type VirtualField interface {
|
|||||||
// BoundField is the data structure we use to bind the telemetry provider's data
|
// BoundField is the data structure we use to bind the telemetry provider's data
|
||||||
// to our internal telemetry fields
|
// to our internal telemetry fields
|
||||||
type BoundField struct {
|
type BoundField struct {
|
||||||
Key string // NOTE: to be deprecated
|
ID FieldID
|
||||||
ID FieldID
|
Update func(out *TelemetryField)
|
||||||
Fetch func() any // NOTE: to be deprecated
|
|
||||||
Transform func(any, *TelemetryField) // NOTE: to be deprecated
|
|
||||||
Update func(out *TelemetryField)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var bufferPool = sync.Pool{
|
var bufferPool = sync.Pool{
|
||||||
@@ -67,7 +70,7 @@ const (
|
|||||||
// NOTE: we can optimize this via a special command that says a given piece of data
|
// NOTE: we can optimize this via a special command that says a given piece of data
|
||||||
// is for multiple targets
|
// is for multiple targets
|
||||||
type TelemetryField struct {
|
type TelemetryField struct {
|
||||||
IDs []int16 // Identification for the serial device
|
// IDs []int16 // Identification for the serial device
|
||||||
Type DataType
|
Type DataType
|
||||||
Raw uint64
|
Raw uint64
|
||||||
Str string // Only to be used with DataTypeSTRING
|
Str string // Only to be used with DataTypeSTRING
|
||||||
@@ -78,48 +81,6 @@ func (tf *TelemetryField) Unused() {
|
|||||||
tf.Raw = uint64('-')
|
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 {
|
func (tf *TelemetryField) String() string {
|
||||||
switch tf.Type {
|
switch tf.Type {
|
||||||
case DataTypeSTRING:
|
case DataTypeSTRING:
|
||||||
@@ -137,7 +98,7 @@ func (tf *TelemetryField) String() string {
|
|||||||
return "NaN"
|
return "NaN"
|
||||||
}
|
}
|
||||||
|
|
||||||
type FieldID uint16
|
type FieldID = uint16
|
||||||
|
|
||||||
// We use FirstField to start the count on the fields the user can select
|
// We use FirstField to start the count on the fields the user can select
|
||||||
// the first three will be for internal use
|
// the first three will be for internal use
|
||||||
@@ -261,13 +222,6 @@ func GetFieldName(id FieldID) string {
|
|||||||
|
|
||||||
var fieldNameToID map[string]FieldID
|
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) {
|
func GetFieldID(name string) (FieldID, bool) {
|
||||||
id, ok := fieldNameToID[name]
|
id, ok := fieldNameToID[name]
|
||||||
return id, ok
|
return id, ok
|
||||||
@@ -287,26 +241,3 @@ type TelemetryData struct {
|
|||||||
func NewTelemetryData() *TelemetryData {
|
func NewTelemetryData() *TelemetryData {
|
||||||
return &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,8 +1,13 @@
|
|||||||
// Package telemetry is our interface with our data sources
|
// Package telemetry is our interface with our data sources
|
||||||
package telemetry
|
package telemetry
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
type TelemetryProvider interface {
|
type TelemetryProvider interface {
|
||||||
StopStream()
|
StopStream()
|
||||||
Stream() (<-chan TelemetryData, error)
|
Stream() (<-chan TelemetryData, error)
|
||||||
Subscribe(map[int16]FieldID)
|
Subscribe([]FieldID)
|
||||||
|
IsAlive(time.Duration) bool
|
||||||
|
Name() string
|
||||||
|
Close()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1 @@
|
|||||||
package telemetry
|
package telemetry
|
||||||
|
|
||||||
func Init() {
|
|
||||||
initFieldNamesMap()
|
|
||||||
}
|
|
||||||
|
|||||||
+105
-11
@@ -5,11 +5,12 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"esdi/cdashdisplay"
|
"esdi/devices/cdashdisplay"
|
||||||
helper "esdi/helpers"
|
|
||||||
"esdi/services"
|
"esdi/services"
|
||||||
"esdi/tui/internal/views"
|
"esdi/tui/internal/views"
|
||||||
|
|
||||||
|
helper "esdi/helpers"
|
||||||
|
|
||||||
"github.com/gdamore/tcell/v2"
|
"github.com/gdamore/tcell/v2"
|
||||||
"github.com/rivo/tview"
|
"github.com/rivo/tview"
|
||||||
)
|
)
|
||||||
@@ -19,12 +20,14 @@ type LayoutController struct {
|
|||||||
OnExit func()
|
OnExit func()
|
||||||
LayoutToolView *views.LayoutToolView
|
LayoutToolView *views.LayoutToolView
|
||||||
Messages chan string
|
Messages chan string
|
||||||
DevService *services.CDashService
|
DevService *services.DeviceService
|
||||||
MoveToolState *windowManipState
|
MoveToolState *windowManipState
|
||||||
SelectedLayout string // NOTE: This should be a struct to handle its own things
|
SelectedLayout string // NOTE: This should be a struct to handle its own things
|
||||||
|
// TODO: create a filter to select the layout
|
||||||
|
// filter telemetry provider, then filter vehicle in use and so on
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLayoutController(base *Controller, service *services.CDashService) *LayoutController {
|
func NewLayoutController(base *Controller, service *services.DeviceService) *LayoutController {
|
||||||
lc := &LayoutController{
|
lc := &LayoutController{
|
||||||
Controller: base,
|
Controller: base,
|
||||||
LayoutToolView: views.NewLayoutToolView(),
|
LayoutToolView: views.NewLayoutToolView(),
|
||||||
@@ -207,7 +210,20 @@ func (lc *LayoutController) createWindow() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
window, err = lc.DevService.CreateWindow(window)
|
// Acquire the cdashdisplay
|
||||||
|
displayIF, err := lc.DevService.GetDevice(cdashdisplay.NAME)
|
||||||
|
if err != nil {
|
||||||
|
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
||||||
|
return
|
||||||
|
}
|
||||||
|
display, ok := displayIF.(*cdashdisplay.CDashDisplay)
|
||||||
|
if !ok {
|
||||||
|
lc.Messages <- "failed to acquire " + cdashdisplay.NAME
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// ---
|
||||||
|
|
||||||
|
window, err = display.CreateWindow(window)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lc.Messages <- "failed to create window\n"
|
lc.Messages <- "failed to create window\n"
|
||||||
return
|
return
|
||||||
@@ -304,7 +320,20 @@ func (lc *LayoutController) newWindowAction() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (lc *LayoutController) updateWindowAction(win *cdashdisplay.DesktopUIWindow) {
|
func (lc *LayoutController) updateWindowAction(win *cdashdisplay.DesktopUIWindow) {
|
||||||
err := lc.DevService.UpdateWindow(win)
|
// Acquire the cdashdisplay
|
||||||
|
displayIF, err := lc.DevService.GetDevice(cdashdisplay.NAME)
|
||||||
|
if err != nil {
|
||||||
|
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
||||||
|
return
|
||||||
|
}
|
||||||
|
display, ok := displayIF.(*cdashdisplay.CDashDisplay)
|
||||||
|
if !ok {
|
||||||
|
lc.Messages <- "failed to acquire " + cdashdisplay.NAME
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// ---
|
||||||
|
|
||||||
|
err = display.UpdateWindow(win)
|
||||||
|
|
||||||
lc.Messages <- fmt.Sprintf("Window: %v\n", win)
|
lc.Messages <- fmt.Sprintf("Window: %v\n", win)
|
||||||
|
|
||||||
@@ -317,7 +346,20 @@ func (lc *LayoutController) updateWindowAction(win *cdashdisplay.DesktopUIWindow
|
|||||||
func (lc *LayoutController) displayLoadedLayouts() {
|
func (lc *LayoutController) displayLoadedLayouts() {
|
||||||
lc.Logger.Debug("We want to view our layout!")
|
lc.Logger.Debug("We want to view our layout!")
|
||||||
|
|
||||||
for _, w := range lc.DevService.CDash.State.Layout.Windows {
|
// Acquire the cdashdisplay
|
||||||
|
displayIF, err := lc.DevService.GetDevice(cdashdisplay.NAME)
|
||||||
|
if err != nil {
|
||||||
|
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
||||||
|
return
|
||||||
|
}
|
||||||
|
display, ok := displayIF.(*cdashdisplay.CDashDisplay)
|
||||||
|
if !ok {
|
||||||
|
lc.Messages <- "failed to acquire " + cdashdisplay.NAME
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// ---
|
||||||
|
|
||||||
|
for _, w := range display.State.Layout.Windows {
|
||||||
lc.Logger.Debug("==========================================================================")
|
lc.Logger.Debug("==========================================================================")
|
||||||
lc.Logger.Debug(fmt.Sprintf("updating form view for a layout: %+v", w.UIData.TelemetryField))
|
lc.Logger.Debug(fmt.Sprintf("updating form view for a layout: %+v", w.UIData.TelemetryField))
|
||||||
err := lc.updateFormView(w)
|
err := lc.updateFormView(w)
|
||||||
@@ -349,8 +391,21 @@ func (lc *LayoutController) getCurrentTreeNodeModel() (*tview.TreeNode, int16, e
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (lc *LayoutController) loadLayout() {
|
func (lc *LayoutController) loadLayout() {
|
||||||
|
// Acquire the cdashdisplay
|
||||||
|
displayIF, err := lc.DevService.GetDevice(cdashdisplay.NAME)
|
||||||
|
if err != nil {
|
||||||
|
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
||||||
|
return
|
||||||
|
}
|
||||||
|
display, ok := displayIF.(*cdashdisplay.CDashDisplay)
|
||||||
|
if !ok {
|
||||||
|
lc.Messages <- "failed to acquire " + cdashdisplay.NAME
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// ---
|
||||||
|
|
||||||
// We would get the layout path from somewhere but for nots its layout.yaml
|
// We would get the layout path from somewhere but for nots its layout.yaml
|
||||||
err := lc.DevService.LoadLayout(lc.SelectedLayout)
|
err = display.LoadLayout(lc.SelectedLayout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lc.Messages <- "failed to load layout: " + err.Error()
|
lc.Messages <- "failed to load layout: " + err.Error()
|
||||||
return
|
return
|
||||||
@@ -360,7 +415,20 @@ func (lc *LayoutController) loadLayout() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (lc *LayoutController) unloadLayout() {
|
func (lc *LayoutController) unloadLayout() {
|
||||||
err := lc.DevService.UnloadLayout()
|
// Acquire the cdashdisplay
|
||||||
|
displayIF, err := lc.DevService.GetDevice(cdashdisplay.NAME)
|
||||||
|
if err != nil {
|
||||||
|
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
||||||
|
return
|
||||||
|
}
|
||||||
|
display, ok := displayIF.(*cdashdisplay.CDashDisplay)
|
||||||
|
if !ok {
|
||||||
|
lc.Messages <- "failed to acquire " + cdashdisplay.NAME
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// ---
|
||||||
|
|
||||||
|
err = display.UnloadLayout()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lc.Logger.Error(fmt.Sprintf("Failed to unload layout: %+v", err))
|
lc.Logger.Error(fmt.Sprintf("Failed to unload layout: %+v", err))
|
||||||
return
|
return
|
||||||
@@ -368,7 +436,20 @@ func (lc *LayoutController) unloadLayout() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (lc *LayoutController) saveLayout() {
|
func (lc *LayoutController) saveLayout() {
|
||||||
err := lc.DevService.SaveLayout(lc.SelectedLayout)
|
// Acquire the cdashdisplay
|
||||||
|
displayIF, err := lc.DevService.GetDevice(cdashdisplay.NAME)
|
||||||
|
if err != nil {
|
||||||
|
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
||||||
|
return
|
||||||
|
}
|
||||||
|
display, ok := displayIF.(*cdashdisplay.CDashDisplay)
|
||||||
|
if !ok {
|
||||||
|
lc.Messages <- "failed to acquire " + cdashdisplay.NAME
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// ---
|
||||||
|
|
||||||
|
err = display.SaveLayout(lc.SelectedLayout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lc.Messages <- "failed to save layout: " + err.Error()
|
lc.Messages <- "failed to save layout: " + err.Error()
|
||||||
return
|
return
|
||||||
@@ -388,8 +469,21 @@ func (lc *LayoutController) deleteWindow() {
|
|||||||
}
|
}
|
||||||
wID := curNode.GetReference().(int16)
|
wID := curNode.GetReference().(int16)
|
||||||
|
|
||||||
|
// Acquire the cdashdisplay
|
||||||
|
displayIF, err := lc.DevService.GetDevice(cdashdisplay.NAME)
|
||||||
|
if err != nil {
|
||||||
|
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
||||||
|
return
|
||||||
|
}
|
||||||
|
display, ok := displayIF.(*cdashdisplay.CDashDisplay)
|
||||||
|
if !ok {
|
||||||
|
lc.Messages <- "failed to acquire " + cdashdisplay.NAME
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// ---
|
||||||
|
|
||||||
// Delete it
|
// Delete it
|
||||||
err := lc.DevService.DeleteWindow(wID)
|
err = display.DestroyWindow(wID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lc.Messages <- "failed to delete window: " + err.Error() + "\n"
|
lc.Messages <- "failed to delete window: " + err.Error() + "\n"
|
||||||
return
|
return
|
||||||
+35
-6
@@ -1,6 +1,7 @@
|
|||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"esdi/devices/cdashdisplay"
|
||||||
helper "esdi/helpers"
|
helper "esdi/helpers"
|
||||||
|
|
||||||
"github.com/gdamore/tcell/v2"
|
"github.com/gdamore/tcell/v2"
|
||||||
@@ -43,40 +44,68 @@ func keyToVector(r rune) (helper.Vector, bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (lc *LayoutController) handleMovementCapture(idx int16,
|
func (lc *LayoutController) handleMovementCapture(idx int16,
|
||||||
ev *tcell.EventKey) *tcell.EventKey {
|
ev *tcell.EventKey,
|
||||||
|
) *tcell.EventKey {
|
||||||
vec, ok := keyToVector(ev.Rune())
|
vec, ok := keyToVector(ev.Rune())
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
err := lc.DevService.MoveWindow(idx, &vec)
|
// Acquire the cdashdisplay
|
||||||
|
displayIF, err := lc.DevService.GetDevice(cdashdisplay.NAME)
|
||||||
|
if err != nil {
|
||||||
|
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
display, ok := displayIF.(*cdashdisplay.CDashDisplay)
|
||||||
|
if !ok {
|
||||||
|
lc.Messages <- "failed to acquire " + cdashdisplay.NAME
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
// ---
|
||||||
|
|
||||||
|
err = display.MoveWindow(idx, &vec)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lc.Messages <- "failed to move window: " + err.Error() + "\n"
|
lc.Messages <- "failed to move window: " + err.Error() + "\n"
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Success - update the form
|
// Success - update the form
|
||||||
window := lc.DevService.CDash.State.Layout.Windows[idx]
|
window := display.State.Layout.Windows[idx]
|
||||||
lc.LayoutToolView.UpdateFormView(idx, window)
|
lc.LayoutToolView.UpdateFormView(idx, window)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (lc *LayoutController) handleResizeCapture(idx int16,
|
func (lc *LayoutController) handleResizeCapture(idx int16,
|
||||||
ev *tcell.EventKey) *tcell.EventKey {
|
ev *tcell.EventKey,
|
||||||
|
) *tcell.EventKey {
|
||||||
vec, ok := keyToVector(ev.Rune())
|
vec, ok := keyToVector(ev.Rune())
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
err := lc.DevService.ResizeWindow(idx, &vec)
|
// Acquire the cdashdisplay
|
||||||
|
displayIF, err := lc.DevService.GetDevice(cdashdisplay.NAME)
|
||||||
|
if err != nil {
|
||||||
|
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
display, ok := displayIF.(*cdashdisplay.CDashDisplay)
|
||||||
|
if !ok {
|
||||||
|
lc.Messages <- "failed to acquire " + cdashdisplay.NAME
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
// ---
|
||||||
|
|
||||||
|
err = display.ResizeWindow(idx, &vec)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lc.Messages <- "failed to resize window: " + err.Error() + "\n"
|
lc.Messages <- "failed to resize window: " + err.Error() + "\n"
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Success - update the form
|
// Success - update the form
|
||||||
window := lc.DevService.CDash.State.Layout.Windows[idx]
|
window := display.State.Layout.Windows[idx]
|
||||||
lc.LayoutToolView.UpdateFormView(idx, window)
|
lc.LayoutToolView.UpdateFormView(idx, window)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -4,6 +4,7 @@ package controllers
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"esdi/devices/cdashdisplay"
|
||||||
serv "esdi/services"
|
serv "esdi/services"
|
||||||
"esdi/tui/internal/views"
|
"esdi/tui/internal/views"
|
||||||
|
|
||||||
@@ -15,12 +16,12 @@ type DeviceController struct {
|
|||||||
DeviceAPIView *views.DeviceAPIView
|
DeviceAPIView *views.DeviceAPIView
|
||||||
LayoutCtrl *LayoutController
|
LayoutCtrl *LayoutController
|
||||||
StreamCtrl *StreamingCtrl
|
StreamCtrl *StreamingCtrl
|
||||||
DevService *serv.CDashService
|
DevService *serv.DeviceService
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDeviceController(
|
func NewDeviceController(
|
||||||
base *Controller,
|
base *Controller,
|
||||||
devService *serv.CDashService,
|
devService *serv.DeviceService,
|
||||||
telemService *serv.TelemetryService,
|
telemService *serv.TelemetryService,
|
||||||
) *DeviceController {
|
) *DeviceController {
|
||||||
mc := &DeviceController{
|
mc := &DeviceController{
|
||||||
@@ -56,7 +57,7 @@ func (mc *DeviceController) setDeviceAPIViewEvents() {
|
|||||||
SetInputCapture(func(ev *tcell.EventKey) *tcell.EventKey {
|
SetInputCapture(func(ev *tcell.EventKey) *tcell.EventKey {
|
||||||
switch ev.Rune() {
|
switch ev.Rune() {
|
||||||
case 'r':
|
case 'r':
|
||||||
go mc.DevService.FindDevice()
|
go mc.DevService.FindDevices()
|
||||||
}
|
}
|
||||||
return ev
|
return ev
|
||||||
})
|
})
|
||||||
@@ -65,6 +66,12 @@ func (mc *DeviceController) setDeviceAPIViewEvents() {
|
|||||||
func (mc *DeviceController) AddDeviceAPIListItems() {
|
func (mc *DeviceController) AddDeviceAPIListItems() {
|
||||||
mc.DeviceAPIView.DevAPIList.
|
mc.DeviceAPIView.DevAPIList.
|
||||||
AddItem("layout", "build a layout for CDashDisplay", func() {
|
AddItem("layout", "build a layout for CDashDisplay", func() {
|
||||||
|
// This CDashDisplay specific, only load if we have a CDashDisplay
|
||||||
|
if !mc.DevService.DeviceExists(cdashdisplay.NAME) {
|
||||||
|
mc.DevService.Messages <- "CDashDisplay it not loaded yet\n"
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Get the api pages
|
// Get the api pages
|
||||||
views.AddAndShowPage(
|
views.AddAndShowPage(
|
||||||
mc.DeviceAPIView.DevAPIToolView.Pages,
|
mc.DeviceAPIView.DevAPIToolView.Pages,
|
||||||
@@ -75,7 +82,14 @@ func (mc *DeviceController) AddDeviceAPIListItems() {
|
|||||||
})
|
})
|
||||||
mc.DeviceAPIView.DevAPIList.
|
mc.DeviceAPIView.DevAPIList.
|
||||||
AddItem("stream", "stream data to the display", func() {
|
AddItem("stream", "stream data to the display", func() {
|
||||||
views.AddAndShowPage(mc.DeviceAPIView.DevAPIToolView.Pages,
|
// If we don't have a CDashDisplay or data source, this should be blocked
|
||||||
|
if !mc.StreamCtrl.TelemServ.HasActiveProvider() {
|
||||||
|
mc.StreamCtrl.Messages <- "no active provider present\n"
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
views.AddAndShowPage(
|
||||||
|
mc.DeviceAPIView.DevAPIToolView.Pages,
|
||||||
"streaming-tool",
|
"streaming-tool",
|
||||||
mc.StreamCtrl.StreamView.Flex,
|
mc.StreamCtrl.StreamView.Flex,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
|
||||||
"esdi/config"
|
"esdi/config"
|
||||||
|
"esdi/devices/uidevice"
|
||||||
"esdi/providers"
|
"esdi/providers"
|
||||||
"esdi/services"
|
"esdi/services"
|
||||||
"esdi/telemetry"
|
"esdi/telemetry"
|
||||||
@@ -17,7 +18,7 @@ import (
|
|||||||
|
|
||||||
type StreamingCtrl struct {
|
type StreamingCtrl struct {
|
||||||
*Controller
|
*Controller
|
||||||
Service *services.CDashService
|
Service *services.DeviceService
|
||||||
StreamView *views.StreamToolView
|
StreamView *views.StreamToolView
|
||||||
Messages chan string
|
Messages chan string
|
||||||
Internal chan string
|
Internal chan string
|
||||||
@@ -32,7 +33,7 @@ type StreamingCtrl struct {
|
|||||||
|
|
||||||
func NewStreamingCtrl(
|
func NewStreamingCtrl(
|
||||||
base *Controller,
|
base *Controller,
|
||||||
serCDash *services.CDashService,
|
devService *services.DeviceService,
|
||||||
serTelem *services.TelemetryService,
|
serTelem *services.TelemetryService,
|
||||||
) *StreamingCtrl {
|
) *StreamingCtrl {
|
||||||
// NOTE: looks sus, put this somewhere also. Not very good in here
|
// NOTE: looks sus, put this somewhere also. Not very good in here
|
||||||
@@ -44,7 +45,7 @@ func NewStreamingCtrl(
|
|||||||
|
|
||||||
ctrl := &StreamingCtrl{
|
ctrl := &StreamingCtrl{
|
||||||
Controller: base,
|
Controller: base,
|
||||||
Service: serCDash,
|
Service: devService,
|
||||||
TelemServ: serTelem,
|
TelemServ: serTelem,
|
||||||
Messages: make(chan string, 10),
|
Messages: make(chan string, 10),
|
||||||
Internal: make(chan string, 10),
|
Internal: make(chan string, 10),
|
||||||
@@ -55,15 +56,15 @@ func NewStreamingCtrl(
|
|||||||
}
|
}
|
||||||
|
|
||||||
ctrl.registerHooks()
|
ctrl.registerHooks()
|
||||||
ctrl.subscribeListeners()
|
// ctrl.subscribeListeners()
|
||||||
go ctrl.listenToUIStream()
|
|
||||||
|
|
||||||
return ctrl
|
return ctrl
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sc *StreamingCtrl) subscribeListeners() {
|
// func (sc *StreamingCtrl) subscribeListeners() {
|
||||||
sc.TelemetryCh = sc.TelemServ.SubscribeListener("UI", 1)
|
// // Here I will set a UIDevice
|
||||||
}
|
// sc.TelemetryCh = sc.TelemServ.SubscribeListener("UI", 1)
|
||||||
|
// }
|
||||||
|
|
||||||
func (sc *StreamingCtrl) registerHooks() {
|
func (sc *StreamingCtrl) registerHooks() {
|
||||||
sc.StreamView.Options.Form.SetInputCapture(func(ev *tcell.EventKey) *tcell.EventKey {
|
sc.StreamView.Options.Form.SetInputCapture(func(ev *tcell.EventKey) *tcell.EventKey {
|
||||||
@@ -108,16 +109,21 @@ func (sc *StreamingCtrl) StartStop() {
|
|||||||
// stream is not running, we have to start it now
|
// stream is not running, we have to start it now
|
||||||
// NOTE:
|
// NOTE:
|
||||||
// Subscribe the only existing device - needs to be discovered by now
|
// Subscribe the only existing device - needs to be discovered by now
|
||||||
slog.Debug("setting the data stream for cdash")
|
slog.Debug("setting the data stream for device servie")
|
||||||
sc.Service.SetTelemetryChannel(sc.TelemServ.SubscribeListener("cdash", 1))
|
sc.Service.SetTelemetryChannel(sc.TelemServ.SubscribeListener("DeviceService", 1))
|
||||||
|
|
||||||
slog.Debug("starting to stream data again")
|
dev, err := sc.Service.GetDevice(uidevice.NAME)
|
||||||
|
if err == nil {
|
||||||
|
if uiDev, ok := dev.(*uidevice.UIDevice); ok {
|
||||||
|
sc.TelemetryCh = uiDev.DataChannel()
|
||||||
|
go sc.listenToUIStream()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
slog.Debug("starting services")
|
||||||
sc.Service.StartStream()
|
sc.Service.StartStream()
|
||||||
|
|
||||||
slog.Debug("starting the stream")
|
|
||||||
sc.TelemServ.StartStream()
|
sc.TelemServ.StartStream()
|
||||||
|
|
||||||
slog.Debug("setting local control variables")
|
|
||||||
sc.isRunning = true
|
sc.isRunning = true
|
||||||
|
|
||||||
slog.Debug("starting stream")
|
slog.Debug("starting stream")
|
||||||
@@ -155,16 +161,24 @@ func (sc *StreamingCtrl) updateStream() {
|
|||||||
// Performance reasoning: this is not used during the high frequency data transmission
|
// Performance reasoning: this is not used during the high frequency data transmission
|
||||||
// so we can get away with using a map for convenience here
|
// so we can get away with using a map for convenience here
|
||||||
func (sc *StreamingCtrl) SetInternalState() {
|
func (sc *StreamingCtrl) SetInternalState() {
|
||||||
fields := make(map[int16]telemetry.FieldID, len(sc.Service.CDash.State.Layout.Windows))
|
// Acquire the cdashdisplay
|
||||||
|
// displayIF, err := sc.Service.GetDevice(cdashdisplay.NAME)
|
||||||
|
// if err != nil {
|
||||||
|
// sc.Messages <- "failed to get " + cdashdisplay.NAME
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
// display, ok := displayIF.(*cdashdisplay.CDashDisplay)
|
||||||
|
// if !ok {
|
||||||
|
// sc.Messages <- "failed to acquire " + cdashdisplay.NAME
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
// ---
|
||||||
|
|
||||||
for _, w := range sc.Service.CDash.State.Layout.Windows {
|
sc.TelemServ.SubscribeToFields()
|
||||||
fieldID, _ := telemetry.GetFieldID(w.UIData.TelemetryField)
|
|
||||||
fields[w.UIData.IDX] = fieldID
|
|
||||||
}
|
|
||||||
|
|
||||||
sc.TelemServ.SubscribeToFields(fields)
|
// sc.Messages <- fmt.Sprintf("Subscribed Fields: %+v [%d]\n", fields, len(fields))
|
||||||
|
// Should I update this?
|
||||||
sc.Messages <- fmt.Sprintf("Subscribed Fields: %+v [%d]\n", fields, len(fields))
|
sc.Messages <- fmt.Sprintf("Subscribed to fields\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sc *StreamingCtrl) listenToUIStream() {
|
func (sc *StreamingCtrl) listenToUIStream() {
|
||||||
@@ -176,8 +190,13 @@ func (sc *StreamingCtrl) listenToUIStream() {
|
|||||||
}
|
}
|
||||||
isDrawing.Store(true)
|
isDrawing.Store(true)
|
||||||
|
|
||||||
|
// sc.Logger.Debug("got data", "data", msg)
|
||||||
|
|
||||||
|
// Capture locally
|
||||||
|
telemetryMsg := msg
|
||||||
|
|
||||||
sc.App.QueueUpdateDraw(func() {
|
sc.App.QueueUpdateDraw(func() {
|
||||||
sc.StreamView.Visualizer.Update(&msg)
|
sc.StreamView.Visualizer.Update(&telemetryMsg)
|
||||||
isDrawing.Store(false)
|
isDrawing.Store(false)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
|
||||||
"esdi/cdashdisplay"
|
"esdi/devices/cdashdisplay"
|
||||||
tviewh "esdi/tui/internal/tview_helpers"
|
tviewh "esdi/tui/internal/tview_helpers"
|
||||||
|
|
||||||
"github.com/gdamore/tcell/v2"
|
"github.com/gdamore/tcell/v2"
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package views
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"esdi/cdashdisplay"
|
"esdi/devices/cdashdisplay"
|
||||||
"esdi/telemetry"
|
"esdi/telemetry"
|
||||||
|
|
||||||
"github.com/rivo/tview"
|
"github.com/rivo/tview"
|
||||||
|
|||||||
+5
-1
@@ -23,12 +23,16 @@ func NewControlPanel(logger *slog.Logger) *ControlPanel {
|
|||||||
App: tview.NewApplication(),
|
App: tview.NewApplication(),
|
||||||
}
|
}
|
||||||
|
|
||||||
devService := services.NewCDashService(logger)
|
// NOTE: create our device service here
|
||||||
|
devService := services.NewDeviceService(logger.With("service", "DeviceService"))
|
||||||
|
|
||||||
telemService := services.NewTelemetryService(logger, devService)
|
telemService := services.NewTelemetryService(logger, devService)
|
||||||
if telemService == nil {
|
if telemService == nil {
|
||||||
panic("failed to create the telemetry service")
|
panic("failed to create the telemetry service")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
go telemService.FindProvider(telemService.CtxMonitor)
|
||||||
|
|
||||||
return &ControlPanel{
|
return &ControlPanel{
|
||||||
Controller: baseController,
|
Controller: baseController,
|
||||||
DeviceController: controllers.NewDeviceController(baseController, devService, telemService),
|
DeviceController: controllers.NewDeviceController(baseController, devService, telemService),
|
||||||
|
|||||||
Reference in New Issue
Block a user