Compare commits
15
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
65f03fd4c9 | ||
|
|
20025dae1b | ||
|
|
03d0f8aefc | ||
|
|
df99994a75 | ||
|
|
80651c21cf | ||
|
|
528c62288a | ||
|
|
196e1d8916 | ||
|
|
decd91c406 | ||
|
|
b8e632d3eb | ||
|
|
d8589394f6 | ||
|
|
76f2e265d7 | ||
|
|
7c03088470 | ||
|
|
0006b649f3 | ||
|
|
3493a73a82 | ||
|
|
f4bde52f40 |
@@ -1 +0,0 @@
|
|||||||
# Streaming Flow
|
|
||||||
@@ -1,17 +1,20 @@
|
|||||||
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,6 +1,7 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"esdi/logger"
|
||||||
esdi "esdi/oldEsdi"
|
esdi "esdi/oldEsdi"
|
||||||
|
|
||||||
// "github.com/ESilva15/goirsdk"
|
// "github.com/ESilva15/goirsdk"
|
||||||
@@ -9,14 +10,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)
|
||||||
}
|
}
|
||||||
|
|||||||
+93
-87
@@ -1,97 +1,103 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"esdi/peripheral"
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
repl "github.com/ESilva15/ESgoRepl"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
func replCmdAction(cmd *cobra.Command, args []string) {
|
func replCmdAction(cmd *cobra.Command, args []string) {
|
||||||
// r := repl.NewREPL(repl.REPLCfg{
|
r := repl.NewREPL(repl.REPLCfg{
|
||||||
// PS1: "\rESDI > ",
|
PS1: "\rESDI > ",
|
||||||
// })
|
})
|
||||||
//
|
|
||||||
// perClerk := peripheral.NewPeripheralDeviceClerk()
|
perClerk := peripheral.NewPeripheralDeviceClerk()
|
||||||
//
|
|
||||||
// discoverDevicesREPLCmd := repl.Command{
|
discoverDevicesREPLCmd := repl.Command{
|
||||||
// Name: "discover",
|
Name: "discover",
|
||||||
// Usage: "discovers connected devices",
|
Usage: "discovers connected devices",
|
||||||
// Action: func(r *repl.REPL, args []string) error {
|
Action: func(r *repl.REPL, args []string) error {
|
||||||
// err := perClerk.FindDevices()
|
err := perClerk.FindDevices()
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// return err
|
return err
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// return nil
|
return nil
|
||||||
// },
|
},
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// listDevicesREPLCmd := repl.Command{
|
listDevicesREPLCmd := repl.Command{
|
||||||
// Name: "list",
|
Name: "list",
|
||||||
// Usage: "lists connected devices",
|
Usage: "lists connected devices",
|
||||||
// Action: func(r *repl.REPL, args []string) error {
|
Action: func(r *repl.REPL, args []string) error {
|
||||||
// _ = perClerk.ListDevices()
|
_ = perClerk.ListDevices()
|
||||||
//
|
|
||||||
// return nil
|
return nil
|
||||||
// },
|
},
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// listDeviceAPIREPLCmd := repl.Command{
|
listDeviceAPIREPLCmd := repl.Command{
|
||||||
// Name: "v-api",
|
Name: "v-api",
|
||||||
// Usage: "shows API of a device - pass its ID",
|
Usage: "shows API of a device - pass its ID",
|
||||||
// Action: func(r *repl.REPL, args []string) error {
|
Action: func(r *repl.REPL, args []string) error {
|
||||||
// // We should add this to the REPL instead
|
// We should add this to the REPL instead
|
||||||
// if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
// return fmt.Errorf("requires at least on argument")
|
return fmt.Errorf("requires at least on argument")
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// // First and only argument should be the ID of the device we want to use
|
// First and only argument should be the ID of the device we want to use
|
||||||
// targetID, err := strconv.ParseInt(args[0], 10, 0)
|
targetID, err := strconv.ParseInt(args[0], 10, 0)
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// return err
|
return err
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// err = perClerk.ListDeviceAPI(uint8(targetID))
|
err = perClerk.ListDeviceAPI(uint8(targetID))
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// fmt.Println("failed to view device API: ", err.Error())
|
fmt.Println("failed to view device API: ", err.Error())
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// return nil
|
return nil
|
||||||
// },
|
},
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// runDeviceAPIREPLCmd := repl.Command{
|
runDeviceAPIREPLCmd := repl.Command{
|
||||||
// Name: "v-run",
|
Name: "v-run",
|
||||||
// Usage: "runs a funcion of a device - pass its ID and function name",
|
Usage: "runs a funcion of a device - pass its ID and function name",
|
||||||
// Action: func(r *repl.REPL, args []string) error {
|
Action: func(r *repl.REPL, args []string) error {
|
||||||
// // We should add this to the REPL instead
|
// We should add this to the REPL instead
|
||||||
// if len(args) < 3 {
|
if len(args) < 3 {
|
||||||
// return fmt.Errorf("requires at least on argument")
|
return fmt.Errorf("requires at least on argument")
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// // First and only argument should be the ID of the device we want to use
|
// First and only argument should be the ID of the device we want to use
|
||||||
// targetID, err := strconv.ParseInt(args[0], 10, 0)
|
targetID, err := strconv.ParseInt(args[0], 10, 0)
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// return err
|
return err
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// fnName := args[1]
|
fnName := args[1]
|
||||||
// fnArgs := args[2:]
|
fnArgs := args[2:]
|
||||||
//
|
|
||||||
// err = perClerk.RunDeviceFunction(uint8(targetID), fnName, fnArgs)
|
err = perClerk.RunDeviceFunction(uint8(targetID), fnName, fnArgs)
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// return err
|
return err
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// return nil
|
return nil
|
||||||
// },
|
},
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// r.RegisterCMD(discoverDevicesREPLCmd)
|
r.RegisterCMD(discoverDevicesREPLCmd)
|
||||||
// r.RegisterCMD(listDevicesREPLCmd)
|
r.RegisterCMD(listDevicesREPLCmd)
|
||||||
// r.RegisterCMD(listDeviceAPIREPLCmd)
|
r.RegisterCMD(listDeviceAPIREPLCmd)
|
||||||
// r.RegisterCMD(runDeviceAPIREPLCmd)
|
r.RegisterCMD(runDeviceAPIREPLCmd)
|
||||||
//
|
|
||||||
// r.Start()
|
r.Start()
|
||||||
// r.Close()
|
r.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
// removeLabelCmd represents the removeLabel command
|
// removeLabelCmd represents the removeLabel command
|
||||||
|
|||||||
@@ -1,12 +1,10 @@
|
|||||||
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"
|
||||||
@@ -48,11 +46,11 @@ func findDisplayPort() (*communication.WalkieTalkie, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
slog.Info(fmt.Sprintf("Looking into %v", ports))
|
pLogger.Info(fmt.Sprintf("Looking into %v", ports))
|
||||||
|
|
||||||
var wt *communication.WalkieTalkie
|
var wt *communication.WalkieTalkie
|
||||||
for _, port := range ports {
|
for _, port := range ports {
|
||||||
slog.Info(fmt.Sprintf("Trying port %s", port))
|
pLogger.Info(fmt.Sprintf("Trying port %s", port))
|
||||||
|
|
||||||
wt = &communication.WalkieTalkie{
|
wt = &communication.WalkieTalkie{
|
||||||
Cfg: &serial.Config{
|
Cfg: &serial.Config{
|
||||||
@@ -62,7 +60,7 @@ func findDisplayPort() (*communication.WalkieTalkie, error) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
slog.Info(fmt.Sprintf("Started probing port %s", port))
|
pLogger.Info(fmt.Sprintf("Started probing port %s", port))
|
||||||
|
|
||||||
probeResult := make(chan error, 1)
|
probeResult := make(chan error, 1)
|
||||||
|
|
||||||
@@ -78,14 +76,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)
|
||||||
}
|
}
|
||||||
|
|
||||||
slog.Info(fmt.Sprintf("Finished probing port %s", port))
|
pLogger.Info(fmt.Sprintf("Finished probing port %s", port))
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
slog.Info(fmt.Sprintf("Success probing port %s: %+v", port, err))
|
pLogger.Info(fmt.Sprintf("Success probing port %s: %+v", port, err))
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
slog.Info(fmt.Sprintf("wasn't port %s", port))
|
pLogger.Info(fmt.Sprintf("wasn't port %s", port))
|
||||||
wt = nil
|
wt = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,6 +91,6 @@ func findDisplayPort() (*communication.WalkieTalkie, error) {
|
|||||||
return nil, fmt.Errorf("couldn't find cdashdisplay")
|
return nil, fmt.Errorf("couldn't find cdashdisplay")
|
||||||
}
|
}
|
||||||
|
|
||||||
slog.Info(fmt.Sprintf("found cdashdisplay on port: %s", wt.Cfg.Name))
|
pLogger.Info(fmt.Sprintf("found cdashdisplay on port: %s", wt.Cfg.Name))
|
||||||
return wt, nil
|
return wt, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,11 +8,9 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
helper "esdi/helpers"
|
helper "esdi/helpers"
|
||||||
"esdi/peripheral"
|
|
||||||
"esdi/peripheral/communication"
|
"esdi/peripheral/communication"
|
||||||
"esdi/peripheral/communication/packets"
|
"esdi/peripheral/communication/packets"
|
||||||
"esdi/peripheral/types"
|
"esdi/peripheral/types"
|
||||||
@@ -21,6 +19,13 @@ import (
|
|||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// TODO: remove this logger. It should be on the device struct
|
||||||
|
var pLogger *slog.Logger
|
||||||
|
|
||||||
|
func SetLogger(l *slog.Logger) {
|
||||||
|
pLogger = l
|
||||||
|
}
|
||||||
|
|
||||||
// I have to move this to some kind of configuration place
|
// I have to move this to some kind of configuration place
|
||||||
const (
|
const (
|
||||||
layoutsDir = "./layouts/"
|
layoutsDir = "./layouts/"
|
||||||
@@ -110,68 +115,26 @@ 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
|
|
||||||
failedSends int
|
|
||||||
FailedSendsConsecutiveLimit int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Connect will try to find and connect to the CDashDisplay
|
// Connect will try to find and connect to the CDashDisplay
|
||||||
func NewCDashDisplay() (*CDashDisplay, error) {
|
func Discover() (*CDashDisplay, error) {
|
||||||
// Look for the port
|
// Look for the port
|
||||||
p, err := findDisplayPort()
|
p, err := findDisplayPort()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Info("failed to find cdashdisplay port", "reason", err.Error())
|
pLogger.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
|
|
||||||
},
|
|
||||||
},
|
|
||||||
failedSends: 0,
|
|
||||||
FailedSendsConsecutiveLimit: 5,
|
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cds *CDashDisplay) Close() error {
|
|
||||||
// if cds.WT != nil {
|
|
||||||
// cds.Close()
|
|
||||||
// }
|
|
||||||
|
|
||||||
return 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 {
|
||||||
@@ -187,12 +150,9 @@ func (d *CDashDisplay) CreateWindow(win *DesktopUIWindow) (*DesktopUIWindow, err
|
|||||||
|
|
||||||
win.UIData.IDX = wID.ID
|
win.UIData.IDX = wID.ID
|
||||||
|
|
||||||
slog.Info(fmt.Sprintf("Recived ID message: %v", wID))
|
pLogger.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
|
||||||
}
|
}
|
||||||
@@ -218,14 +178,12 @@ 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?
|
||||||
slog.Debug(fmt.Sprintf("PreUpdate ID: %p", win))
|
pLogger.Debug(fmt.Sprintf("PreUpdate ID: %p", win))
|
||||||
d.State.Layout.Windows[win.UIData.IDX] = win
|
d.State.Layout.Windows[win.UIData.IDX] = win
|
||||||
slog.Debug(fmt.Sprintf("PostUpdate ID: %p", win))
|
pLogger.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
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -248,15 +206,14 @@ func (d *CDashDisplay) DestroyWindow(wID int16) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: add this
|
// NODE: 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 {
|
||||||
slog.Debug(fmt.Sprintf("UPDATE: %v", packet))
|
pLogger.Debug(fmt.Sprintf("UPDATE: %v", packet))
|
||||||
|
|
||||||
bytes, err := helper.StructToBytes(packet)
|
bytes, err := helper.StructToBytes(packet)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -270,9 +227,9 @@ func (d *CDashDisplay) updateWindowDimensions(win *UIWindow, packet UpdateDimsPa
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Nothing bad happened afaik
|
// Nothing bad happened afaik
|
||||||
slog.Debug(fmt.Sprintf("cur dims: %v", win.Dims))
|
pLogger.Debug(fmt.Sprintf("cur dims: %v", win.Dims))
|
||||||
win.Dims = packet.Dims
|
win.Dims = packet.Dims
|
||||||
slog.Debug(fmt.Sprintf("new dims: %v", win.Dims))
|
pLogger.Debug(fmt.Sprintf("new dims: %v", win.Dims))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -388,30 +345,30 @@ 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 {
|
||||||
slog.Debug(fmt.Sprintf("= Removing %d ==============================================",
|
pLogger.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 {
|
||||||
slog.Error(fmt.Sprintf("failed to destroy window: %+v", err))
|
pLogger.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
|
||||||
}
|
}
|
||||||
|
|
||||||
slog.Debug(fmt.Sprintf("= Removing %d ==============================================",
|
pLogger.Debug(fmt.Sprintf("= Removing %d ==============================================",
|
||||||
w.UIData.IDX))
|
w.UIData.IDX))
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *CDashDisplay) SendData(data *telemetry.TelemetryData) error {
|
func (d *CDashDisplay) SendData(data *telemetry.TelemetryData) {
|
||||||
packet := d.encodePacket(data)
|
packet := data.Pack()
|
||||||
|
|
||||||
bytes, err := helper.StructToBytes(packet)
|
bytes, err := helper.StructToBytes(packet)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return peripheral.ErrFailureToPackData
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
curStr := ""
|
curStr := ""
|
||||||
@@ -421,6 +378,7 @@ func (d *CDashDisplay) SendData(data *telemetry.TelemetryData) error {
|
|||||||
curStr += fmt.Sprintf("%02x ", byte)
|
curStr += fmt.Sprintf("%02x ", byte)
|
||||||
|
|
||||||
if byteCount == 8 {
|
if byteCount == 8 {
|
||||||
|
// pLogger.Debug(curStr)
|
||||||
curStr = ""
|
curStr = ""
|
||||||
byteCount = 0
|
byteCount = 0
|
||||||
}
|
}
|
||||||
@@ -429,11 +387,6 @@ func (d *CDashDisplay) SendData(data *telemetry.TelemetryData) error {
|
|||||||
// var ack packets.AckPacket
|
// var ack packets.AckPacket
|
||||||
err = d.WT.SendCommand(sendDataCMDID, bytes, nil)
|
err = d.WT.SendCommand(sendDataCMDID, bytes, nil)
|
||||||
if err != nil && err != io.EOF {
|
if err != nil && err != io.EOF {
|
||||||
if d.failedSends == d.FailedSendsConsecutiveLimit {
|
return
|
||||||
return peripheral.ErrDeviceTimedOut
|
|
||||||
}
|
}
|
||||||
d.failedSends++
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,13 +15,12 @@ func (cds *CDashDisplay) Name() string {
|
|||||||
return NAME
|
return NAME
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cds *CDashDisplay) RequiredFields() []telemetry.FieldID {
|
func (cds *CDashDisplay) RequiredFields() map[int16]telemetry.FieldID {
|
||||||
fields := make([]telemetry.FieldID, 0, len(cds.State.Layout.Windows))
|
fields := make(map[int16]telemetry.FieldID, len(cds.State.Layout.Windows))
|
||||||
|
|
||||||
for _, w := range cds.State.Layout.Windows {
|
for _, w := range cds.State.Layout.Windows {
|
||||||
if fieldID, ok := telemetry.GetFieldID(w.UIData.TelemetryField); ok {
|
fieldID, _ := telemetry.GetFieldID(w.UIData.TelemetryField)
|
||||||
fields = append(fields, fieldID)
|
fields[w.UIData.IDX] = fieldID
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return fields
|
return fields
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
package cdashdisplay
|
package cdashdisplay
|
||||||
|
|
||||||
import (
|
import "fmt"
|
||||||
"fmt"
|
|
||||||
"log/slog"
|
|
||||||
)
|
|
||||||
|
|
||||||
type LayoutTree struct {
|
type LayoutTree struct {
|
||||||
Windows map[int16]*DesktopUIWindow `yaml:"Windows"`
|
Windows map[int16]*DesktopUIWindow `yaml:"Windows"`
|
||||||
@@ -16,13 +13,13 @@ func NewLayoutTree() *LayoutTree {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *LayoutTree) AddWindow(w *DesktopUIWindow) {
|
func (l *LayoutTree) AddWindow(w *DesktopUIWindow) {
|
||||||
slog.Debug(fmt.Sprintf("adding window '%d' - %v", w.UIData.IDX))
|
pLogger.Debug(fmt.Sprintf("adding window '%d' - %v", w.UIData.IDX))
|
||||||
l.Windows[w.UIData.IDX] = w
|
l.Windows[w.UIData.IDX] = w
|
||||||
slog.Debug(fmt.Sprintf("new map - %v", l.Windows))
|
pLogger.Debug(fmt.Sprintf("new map - %v", l.Windows))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LayoutTree) RemoveWindow(idx int16) {
|
func (l *LayoutTree) RemoveWindow(idx int16) {
|
||||||
slog.Debug(fmt.Sprintf("removing window '%d'", idx))
|
pLogger.Debug(fmt.Sprintf("removing window '%d'", idx))
|
||||||
delete(l.Windows, idx)
|
delete(l.Windows, idx)
|
||||||
slog.Debug(fmt.Sprintf("new map - %v", l.Windows))
|
pLogger.Debug(fmt.Sprintf("new map - %v", l.Windows))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,5 @@
|
|||||||
package cdashdisplay
|
package cdashdisplay
|
||||||
|
|
||||||
import (
|
|
||||||
"math"
|
|
||||||
|
|
||||||
"esdi/telemetry"
|
|
||||||
)
|
|
||||||
|
|
||||||
// In this file we will place all structs that are 1:1 representation of the
|
// In this file we will place all structs that are 1:1 representation of the
|
||||||
// types in the device transport layer ->
|
// types in the device transport layer ->
|
||||||
|
|
||||||
@@ -71,71 +65,6 @@ type UIWindowUpdatePacket struct {
|
|||||||
Window UIWindow
|
Window UIWindow
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: this is here because this is the only device I use like this
|
// func getUIWindowDTO(w *DesktopWindowData) *UIWindow {
|
||||||
// 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
|
|
||||||
}
|
|
||||||
|
|||||||
+25
-8
@@ -2,6 +2,8 @@
|
|||||||
package devices
|
package devices
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
"esdi/devices/cdashdisplay"
|
"esdi/devices/cdashdisplay"
|
||||||
"esdi/devices/uidevice"
|
"esdi/devices/uidevice"
|
||||||
"esdi/peripheral"
|
"esdi/peripheral"
|
||||||
@@ -12,7 +14,7 @@ type Device struct {
|
|||||||
Discover func() (peripheral.Peripheral, error)
|
Discover func() (peripheral.Peripheral, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
var List map[string]*Device = map[string]*Device{
|
var List map[string]Device = map[string]Device{
|
||||||
uidevice.NAME: {
|
uidevice.NAME: {
|
||||||
Name: uidevice.NAME,
|
Name: uidevice.NAME,
|
||||||
Discover: DiscoverUIDevice,
|
Discover: DiscoverUIDevice,
|
||||||
@@ -33,11 +35,26 @@ func DiscoverUIDevice() (peripheral.Peripheral, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func DiscoverCDashDisplay() (peripheral.Peripheral, error) {
|
func DiscoverCDashDisplay() (peripheral.Peripheral, error) {
|
||||||
// Create a cdashdisplay
|
// // Find CDashDisplay
|
||||||
display, err := cdashdisplay.NewCDashDisplay()
|
// {
|
||||||
if err != nil {
|
// ds.Messages <- "looking for " + cdashdisplay.Name + "...\n"
|
||||||
return nil, err
|
// ds.Logger.Info("Looking for " + cdashdisplay.Name)
|
||||||
}
|
//
|
||||||
|
// cdashdisplay.SetLogger(ds.Logger.With("[device]", cdashdisplay.Name))
|
||||||
return display, nil
|
//
|
||||||
|
// // Create a cdashdisplay
|
||||||
|
// display, err := cdashdisplay.Discover()
|
||||||
|
// if err == nil {
|
||||||
|
// ds.Devices[cdashdisplay.Name] = display
|
||||||
|
// ds.Logger.Info("found " + cdashdisplay.Name + " on: " + display.WT.Cfg.Name)
|
||||||
|
// ds.Messages <- "found " + cdashdisplay.Name + " on: " + display.WT.Cfg.Name + "\n"
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// ds.Logger.Info("didn't find " + cdashdisplay.Name)
|
||||||
|
// ds.Messages <- "didn't find " + cdashdisplay.Name + "\n"
|
||||||
|
// // No CDashDisplay available for one reason or another, so we don't set the
|
||||||
|
// // key
|
||||||
|
// }
|
||||||
|
return nil, errors.New("not implemented yet")
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-14
@@ -17,17 +17,9 @@ func NewUIDevice() (peripheral.Peripheral, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (uid *UIDevice) Close() error {
|
func (uid *UIDevice) SendData(data *telemetry.TelemetryData) {
|
||||||
if uid.dataChan != nil {
|
|
||||||
close(uid.dataChan)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (uid *UIDevice) SendData(data *telemetry.TelemetryData) error {
|
|
||||||
if data == nil {
|
if data == nil {
|
||||||
return peripheral.ErrInvalidData
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
@@ -35,8 +27,6 @@ func (uid *UIDevice) SendData(data *telemetry.TelemetryData) error {
|
|||||||
default:
|
default:
|
||||||
// Drop frame if buffer is full
|
// Drop frame if buffer is full
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (uid *UIDevice) Name() string {
|
func (uid *UIDevice) Name() string {
|
||||||
@@ -47,10 +37,17 @@ func (uid *UIDevice) DataChannel() <-chan telemetry.TelemetryData {
|
|||||||
return uid.dataChan
|
return uid.dataChan
|
||||||
}
|
}
|
||||||
|
|
||||||
func (uid *UIDevice) RequiredFields() []telemetry.FieldID {
|
func (uid *UIDevice) RequiredFields() map[int16]telemetry.FieldID {
|
||||||
return []telemetry.FieldID{
|
subscribeTo := []telemetry.FieldID{
|
||||||
telemetry.Speed,
|
telemetry.Speed,
|
||||||
telemetry.Gear,
|
telemetry.Gear,
|
||||||
telemetry.RPM,
|
telemetry.RPM,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fields := make(map[int16]telemetry.FieldID, len(subscribeTo))
|
||||||
|
for k, field := range subscribeTo {
|
||||||
|
fields[int16(k)] = field
|
||||||
|
}
|
||||||
|
|
||||||
|
return fields
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package logger
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
var l *log.Logger
|
||||||
|
var once sync.Once
|
||||||
|
|
||||||
|
func createLogger() {
|
||||||
|
l = log.New(os.Stdout, "[esdi] ", log.LstdFlags | log.Lshortfile)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetInstance() *log.Logger {
|
||||||
|
once.Do(func() {
|
||||||
|
createLogger()
|
||||||
|
})
|
||||||
|
|
||||||
|
return l
|
||||||
|
}
|
||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
|
|
||||||
"esdi/cmd"
|
"esdi/cmd"
|
||||||
"esdi/config"
|
"esdi/config"
|
||||||
|
"esdi/telemetry"
|
||||||
|
|
||||||
"github.com/arl/statsviz"
|
"github.com/arl/statsviz"
|
||||||
)
|
)
|
||||||
@@ -41,6 +42,9 @@ 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 {
|
||||||
|
|||||||
@@ -108,15 +108,6 @@ func (wt *WalkieTalkie) sendPacket(cmd types.Command, data any) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// slog.Debug("# START ########################################################")
|
|
||||||
// slog.Debug(fmt.Sprintf("StartMarker: %02x", constvar.StartOfText))
|
|
||||||
// slog.Debug(fmt.Sprintf("CMD: %02x", cmd))
|
|
||||||
// slog.Debug(fmt.Sprintf("Len: %d", len(payload)))
|
|
||||||
// slog.Debug(fmt.Sprintf("Payload: %v", payload))
|
|
||||||
// slog.Debug(fmt.Sprintf("CRC: %v", CRC8(payload)))
|
|
||||||
// slog.Debug(fmt.Sprintf("EndMarker: %02x", constvar.EndOfText))
|
|
||||||
// slog.Debug("-")
|
|
||||||
|
|
||||||
packet := CMDDataPacket{
|
packet := CMDDataPacket{
|
||||||
StartMarker: constvar.StartOfText,
|
StartMarker: constvar.StartOfText,
|
||||||
CMD: cmd,
|
CMD: cmd,
|
||||||
@@ -128,9 +119,7 @@ func (wt *WalkieTalkie) sendPacket(cmd types.Command, data any) error {
|
|||||||
|
|
||||||
// Send the payload
|
// Send the payload
|
||||||
serializedPacket := packet.Serialize()
|
serializedPacket := packet.Serialize()
|
||||||
|
// fmt.Fprintf(os.Stderr, "%+v", serializedPacket)
|
||||||
// slog.Debug("Serialized packet", "packet", packet)
|
|
||||||
// slog.Debug("# END ##########################################################")
|
|
||||||
|
|
||||||
_, err = wt.Serial.Write(serializedPacket)
|
_, err = wt.Serial.Write(serializedPacket)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -187,11 +176,21 @@ func (wt *WalkieTalkie) readPacket(resp packets.Packet) error {
|
|||||||
// return nil
|
// return nil
|
||||||
// }
|
// }
|
||||||
|
|
||||||
func (wt *WalkieTalkie) SendCommand(
|
func (wt *WalkieTalkie) SendCommand(cmd types.Command, payload any,
|
||||||
cmd types.Command,
|
responseBody packets.Packet) error {
|
||||||
payload any,
|
// Prepare the header
|
||||||
responseBody packets.Packet,
|
// header := header{
|
||||||
) error {
|
// StartByte: constvar.StartOfText,
|
||||||
|
// CMD: cmd,
|
||||||
|
// EndByte: constvar.EndOfText,
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Send the header
|
||||||
|
// err := wt.sendHeader(&header)
|
||||||
|
// if err != nil {
|
||||||
|
// return err
|
||||||
|
// }
|
||||||
|
|
||||||
// Send the body
|
// Send the body
|
||||||
err := wt.sendPacket(cmd, payload)
|
err := wt.sendPacket(cmd, payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -1,9 +0,0 @@
|
|||||||
package peripheral
|
|
||||||
|
|
||||||
import "errors"
|
|
||||||
|
|
||||||
var (
|
|
||||||
ErrInvalidData = errors.New("invalid data")
|
|
||||||
ErrDeviceTimedOut = errors.New("device timed out")
|
|
||||||
ErrFailureToPackData = errors.New("failed to pack received data")
|
|
||||||
)
|
|
||||||
@@ -17,9 +17,8 @@ const (
|
|||||||
|
|
||||||
type Peripheral interface {
|
type Peripheral interface {
|
||||||
Name() string
|
Name() string
|
||||||
SendData(*telemetry.TelemetryData) error
|
SendData(*telemetry.TelemetryData)
|
||||||
RequiredFields() []telemetry.FieldID
|
RequiredFields() map[int16]telemetry.FieldID
|
||||||
Close() error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type PeripheralDeviceClerk struct {
|
type PeripheralDeviceClerk struct {
|
||||||
|
|||||||
+13
-14
@@ -28,7 +28,7 @@ type BeamNG struct {
|
|||||||
updaters [telemetry.MaxFields]func(*telemetry.TelemetryField)
|
updaters [telemetry.MaxFields]func(*telemetry.TelemetryField)
|
||||||
|
|
||||||
// stream control
|
// stream control
|
||||||
wg sync.WaitGroup
|
streamCh chan telemetry.TelemetryData
|
||||||
streamCancel context.CancelFunc
|
streamCancel context.CancelFunc
|
||||||
|
|
||||||
// timing
|
// timing
|
||||||
@@ -47,6 +47,7 @@ func NewBeamNGProvider(logger *slog.Logger, opts *bngsdk.Options) (*BeamNG, erro
|
|||||||
|
|
||||||
provider := &BeamNG{
|
provider := &BeamNG{
|
||||||
logger: logger.With("TelemetryProvider", NAME),
|
logger: logger.With("TelemetryProvider", NAME),
|
||||||
|
streamCh: make(chan telemetry.TelemetryData, 1),
|
||||||
data: telemetry.NewTelemetryData(),
|
data: telemetry.NewTelemetryData(),
|
||||||
SDK: beam,
|
SDK: beam,
|
||||||
og: &bngsdk.Outgauge{},
|
og: &bngsdk.Outgauge{},
|
||||||
@@ -109,12 +110,12 @@ func (b *BeamNG) Stream() (<-chan telemetry.TelemetryData, error) {
|
|||||||
ctx, b.streamCancel = context.WithCancel(context.Background())
|
ctx, b.streamCancel = context.WithCancel(context.Background())
|
||||||
|
|
||||||
// Start the stream
|
// Start the stream
|
||||||
ch := b.stream(ctx)
|
b.stream(ctx)
|
||||||
|
|
||||||
return ch, nil
|
return b.streamCh, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BeamNG) Subscribe(requestFields []telemetry.FieldID) {
|
func (b *BeamNG) Subscribe(requestFields map[int16]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)))
|
||||||
|
|
||||||
@@ -124,7 +125,9 @@ func (b *BeamNG) Subscribe(requestFields []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 _, id := range requestFields {
|
for winID, 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())
|
||||||
@@ -161,6 +164,7 @@ func (b *BeamNG) Subscribe(requestFields []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
|
||||||
ogSnapshot, err := b.SDK.Update()
|
ogSnapshot, err := b.SDK.Update()
|
||||||
slog.Debug("THE DATA WAS READ")
|
slog.Debug("THE DATA WAS READ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -191,15 +195,10 @@ func (b *BeamNG) readData() {
|
|||||||
b.data.LastDataPoll = time.Now()
|
b.data.LastDataPoll = time.Now()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BeamNG) stream(ctx context.Context) <-chan telemetry.TelemetryData {
|
func (b *BeamNG) stream(ctx context.Context) {
|
||||||
b.data.InitialTime = time.Now()
|
b.data.InitialTime = time.Now()
|
||||||
outCh := make(chan telemetry.TelemetryData)
|
|
||||||
b.wg.Add(1)
|
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
defer b.wg.Done()
|
|
||||||
defer close(outCh)
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
// Explicitly intercept cancellation
|
// Explicitly intercept cancellation
|
||||||
select {
|
select {
|
||||||
@@ -208,6 +207,8 @@ func (b *BeamNG) stream(ctx context.Context) <-chan telemetry.TelemetryData {
|
|||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NOTE: add a method to check if there's data available, or make this happen
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
@@ -218,7 +219,7 @@ func (b *BeamNG) stream(ctx context.Context) <-chan telemetry.TelemetryData {
|
|||||||
|
|
||||||
// Publish data
|
// Publish data
|
||||||
select {
|
select {
|
||||||
case outCh <- *b.data:
|
case b.streamCh <- *b.data:
|
||||||
slog.Debug("PUBLISHED DATA")
|
slog.Debug("PUBLISHED DATA")
|
||||||
default:
|
default:
|
||||||
// skip this data, don't allow publishers to lag behind
|
// skip this data, don't allow publishers to lag behind
|
||||||
@@ -226,6 +227,4 @@ func (b *BeamNG) stream(ctx context.Context) <-chan telemetry.TelemetryData {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return outCh
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,8 +33,7 @@ type IRacing struct {
|
|||||||
ticker *time.Ticker // ticker will keep polling intervals constant
|
ticker *time.Ticker // ticker will keep polling intervals constant
|
||||||
|
|
||||||
// Stream
|
// Stream
|
||||||
wg sync.WaitGroup
|
streamCh chan telemetry.TelemetryData
|
||||||
// streamCh chan telemetry.TelemetryData
|
|
||||||
streamCancel context.CancelFunc
|
streamCancel context.CancelFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,8 +53,10 @@ func NewIRacingProvider(
|
|||||||
logger: logger,
|
logger: logger,
|
||||||
SDK: sdk,
|
SDK: sdk,
|
||||||
data: telemetry.NewTelemetryData(),
|
data: telemetry.NewTelemetryData(),
|
||||||
|
streamCh: make(chan telemetry.TelemetryData, 1),
|
||||||
|
// NOTE: This is because I stupidly recorded a test IBT file in 240
|
||||||
// TODO: make this configurable from the user side
|
// TODO: make this configurable from the user side
|
||||||
ticker: time.NewTicker(time.Second / 60),
|
ticker: time.NewTicker(time.Second / 240),
|
||||||
}
|
}
|
||||||
|
|
||||||
provider.updaters = [telemetry.MaxFields]func(*telemetry.TelemetryField){
|
provider.updaters = [telemetry.MaxFields]func(*telemetry.TelemetryField){
|
||||||
@@ -128,14 +129,11 @@ func (i *IRacing) isDataAvailable() bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *IRacing) stream(ctx context.Context) <-chan telemetry.TelemetryData {
|
func (i *IRacing) stream(ctx context.Context) {
|
||||||
i.data.InitialTime = time.Now()
|
i.data.InitialTime = time.Now()
|
||||||
outCh := make(chan telemetry.TelemetryData)
|
|
||||||
i.wg.Add(1)
|
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
defer i.wg.Done()
|
defer close(i.streamCh)
|
||||||
defer close(outCh)
|
|
||||||
|
|
||||||
// Put this into the configuration file
|
// Put this into the configuration file
|
||||||
consecutiveTimeouts := 0
|
consecutiveTimeouts := 0
|
||||||
@@ -151,11 +149,12 @@ func (i *IRacing) stream(ctx context.Context) <-chan telemetry.TelemetryData {
|
|||||||
|
|
||||||
if i.SDK.CheckForDataEvent(time.Duration(dataEvTimeout) * time.Millisecond) {
|
if i.SDK.CheckForDataEvent(time.Duration(dataEvTimeout) * time.Millisecond) {
|
||||||
consecutiveTimeouts = 0
|
consecutiveTimeouts = 0
|
||||||
|
i.logger.Debug("sending data", "timeouts", consecutiveTimeouts)
|
||||||
i.readData()
|
i.readData()
|
||||||
|
|
||||||
// Publish data
|
// Publish data
|
||||||
select {
|
select {
|
||||||
case outCh <- *i.data:
|
case i.streamCh <- *i.data:
|
||||||
default:
|
default:
|
||||||
// skip this data, don't allow publishers to lag behind
|
// skip this data, don't allow publishers to lag behind
|
||||||
}
|
}
|
||||||
@@ -171,8 +170,6 @@ func (i *IRacing) stream(ctx context.Context) <-chan telemetry.TelemetryData {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return outCh
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *IRacing) readData() {
|
func (i *IRacing) readData() {
|
||||||
@@ -209,9 +206,9 @@ func (i *IRacing) Stream() (<-chan telemetry.TelemetryData, error) {
|
|||||||
ctx, i.streamCancel = context.WithCancel(context.Background())
|
ctx, i.streamCancel = context.WithCancel(context.Background())
|
||||||
|
|
||||||
// Start the stream
|
// Start the stream
|
||||||
ch := i.stream(ctx)
|
i.stream(ctx)
|
||||||
|
|
||||||
return ch, nil
|
return i.streamCh, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *IRacing) StopStream() {
|
func (i *IRacing) StopStream() {
|
||||||
@@ -220,11 +217,10 @@ func (i *IRacing) StopStream() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
i.streamCancel()
|
i.streamCancel()
|
||||||
i.wg.Wait()
|
|
||||||
i.streamCancel = nil
|
i.streamCancel = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *IRacing) Subscribe(requestFields []telemetry.FieldID) {
|
func (i *IRacing) Subscribe(requestFields map[int16]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))
|
||||||
@@ -233,7 +229,9 @@ func (i *IRacing) Subscribe(requestFields []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, 0, telemetry.MaxFields)
|
pendingBinds := make([]telemetry.FieldID, 0, telemetry.MaxFields)
|
||||||
|
|
||||||
for _, id := range requestFields {
|
for winID, id := range requestFields {
|
||||||
|
i.data.Values[id].IDs = append(i.data.Values[id].IDs, winID)
|
||||||
|
|
||||||
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())
|
||||||
|
|||||||
+76
-30
@@ -2,21 +2,32 @@ package services
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
"time"
|
||||||
|
|
||||||
"esdi/devices"
|
"esdi/devices"
|
||||||
"esdi/peripheral"
|
"esdi/peripheral"
|
||||||
"esdi/telemetry"
|
"esdi/telemetry"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DeviceService is the API for the peripherals
|
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 {
|
type DeviceService struct {
|
||||||
Logger *slog.Logger
|
Logger *slog.Logger
|
||||||
// Device discovery
|
// Device discovery
|
||||||
PSS *PeripheralStateStore // Store to track peripheral state
|
mu sync.RWMutex
|
||||||
ctxDiscovery context.Context
|
ctxDiscovery context.Context
|
||||||
ctxDiscoveryCancel context.CancelFunc
|
ctxDiscoveryCancel context.CancelFunc
|
||||||
|
Devices map[string]peripheral.Peripheral
|
||||||
// Strem handling
|
// Strem handling
|
||||||
streamCancel context.CancelFunc
|
streamCancel context.CancelFunc
|
||||||
TelemCh <-chan telemetry.TelemetryData
|
TelemCh <-chan telemetry.TelemetryData
|
||||||
@@ -28,7 +39,7 @@ func NewDeviceService(logger *slog.Logger) *DeviceService {
|
|||||||
sharedChannel := make(chan string, 10)
|
sharedChannel := make(chan string, 10)
|
||||||
|
|
||||||
dev := &DeviceService{
|
dev := &DeviceService{
|
||||||
PSS: NewPeripheralStateStore(logger.With("Service", "PeripheralStateStore"), devices.List),
|
Devices: make(map[string]peripheral.Peripheral),
|
||||||
Logger: logger,
|
Logger: logger,
|
||||||
Messages: sharedChannel,
|
Messages: sharedChannel,
|
||||||
}
|
}
|
||||||
@@ -41,33 +52,76 @@ func NewDeviceService(logger *slog.Logger) *DeviceService {
|
|||||||
return dev
|
return dev
|
||||||
}
|
}
|
||||||
|
|
||||||
// Getters [START] -------------------------------------------------------------
|
func (ds *DeviceService) FindDevices() {
|
||||||
func (ds *DeviceService) GetDevices() []peripheral.Peripheral {
|
// Need to define a list of devices to search for
|
||||||
snapshot := ds.PSS.GetStates()
|
// For now lets just try to find our cdashdisplay - will think about the rest later
|
||||||
peripherals := make([]peripheral.Peripheral, 0, len(snapshot))
|
ticker := time.NewTicker(2 * time.Second)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
for _, state := range snapshot {
|
for {
|
||||||
if state.State != DeviceIsConnected {
|
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
|
continue
|
||||||
}
|
}
|
||||||
peripherals = append(peripherals, state.Peripheral)
|
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
return peripherals
|
// Register the device we just found
|
||||||
|
ds.RegisterDevice(dev)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ds *DeviceService) GetPeripheral(pname string) (peripheral.Peripheral, error) {
|
// func (ds *DeviceService) SubscribeFields() error {
|
||||||
return ds.PSS.GetPeripheral(pname)
|
// 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) PeripheralExists(pname string) bool {
|
func (ds *DeviceService) GetDevice(name string) (peripheral.Peripheral, error) {
|
||||||
_, err := ds.PSS.GetPeripheral(pname)
|
val, ok := ds.Devices[name]
|
||||||
return err == nil
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("device `%s` couldn't be found", name)
|
||||||
|
}
|
||||||
|
|
||||||
|
return val, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Getters [END] ---------------------------------------------------------------
|
func (ds *DeviceService) DeviceExists(name string) bool {
|
||||||
|
if _, ok := ds.Devices[name]; !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
// Actions [START] -------------------------------------------------------------
|
|
||||||
func (ds *DeviceService) StartStream() {
|
func (ds *DeviceService) StartStream() {
|
||||||
// NOTE: i'm using this pattern a whole lot. Maybe I can create a struct to handle this
|
// NOTE: i'm using this pattern a whole lot. Maybe I can create a struct to handle this
|
||||||
var ctx context.Context
|
var ctx context.Context
|
||||||
@@ -90,8 +144,6 @@ func (ds *DeviceService) SetTelemetryChannel(ch <-chan telemetry.TelemetryData)
|
|||||||
ds.TelemCh = ch
|
ds.TelemCh = ch
|
||||||
}
|
}
|
||||||
|
|
||||||
// Actions [END] ---------------------------------------------------------------
|
|
||||||
|
|
||||||
// transmit will send the data to the devices themselves
|
// transmit will send the data to the devices themselves
|
||||||
func (ds *DeviceService) transmit(ctx context.Context) {
|
func (ds *DeviceService) transmit(ctx context.Context) {
|
||||||
var isSending atomic.Bool
|
var isSending atomic.Bool
|
||||||
@@ -113,17 +165,11 @@ func (ds *DeviceService) transmit(ctx context.Context) {
|
|||||||
|
|
||||||
// TODO: make a copy of the data and send that copy instead of keeping
|
// TODO: make a copy of the data and send that copy instead of keeping
|
||||||
// the data locked
|
// the data locked
|
||||||
for _, dev := range ds.PSS.GetStates() {
|
ds.mu.RLock()
|
||||||
if dev.State != DeviceIsConnected {
|
for _, dev := range ds.Devices {
|
||||||
continue
|
dev.SendData(&data)
|
||||||
}
|
|
||||||
|
|
||||||
err := dev.Peripheral.SendData(&data)
|
|
||||||
|
|
||||||
if err == peripheral.ErrDeviceTimedOut {
|
|
||||||
ds.onDeviceTimedOut(dev.device.Name)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
ds.mu.RUnlock()
|
||||||
|
|
||||||
isSending.Store(false)
|
isSending.Store(false)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,222 +0,0 @@
|
|||||||
package services
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"log/slog"
|
|
||||||
"maps"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"esdi/devices"
|
|
||||||
"esdi/peripheral"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
ErrPeripheralAlreadyRegistered = errors.New("peripheral is already registered")
|
|
||||||
ErrNoSuchDevice = errors.New("device doesn't exist")
|
|
||||||
ErrDeviceIsNotConnected = errors.New("device isn't connected")
|
|
||||||
)
|
|
||||||
|
|
||||||
type DeviceState = uint8
|
|
||||||
|
|
||||||
const (
|
|
||||||
DeviceTimedOut uint8 = iota
|
|
||||||
DeviceIsConnected
|
|
||||||
DeviceIsDisconnected
|
|
||||||
DeviceReconnected
|
|
||||||
)
|
|
||||||
|
|
||||||
type PeripheralState struct {
|
|
||||||
device *devices.Device
|
|
||||||
Peripheral peripheral.Peripheral
|
|
||||||
State DeviceState
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewPeripheralState(
|
|
||||||
dev *devices.Device,
|
|
||||||
peripheral peripheral.Peripheral,
|
|
||||||
state DeviceState,
|
|
||||||
) *PeripheralState {
|
|
||||||
perState := PeripheralState{
|
|
||||||
device: dev,
|
|
||||||
Peripheral: peripheral,
|
|
||||||
State: state,
|
|
||||||
}
|
|
||||||
|
|
||||||
return &perState
|
|
||||||
}
|
|
||||||
|
|
||||||
type PeripheralStateStore struct {
|
|
||||||
Logger *slog.Logger
|
|
||||||
mu sync.RWMutex
|
|
||||||
store map[string]*PeripheralState
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewPeripheralStateStore(
|
|
||||||
nLogger *slog.Logger,
|
|
||||||
devList map[string]*devices.Device,
|
|
||||||
) *PeripheralStateStore {
|
|
||||||
store := PeripheralStateStore{
|
|
||||||
Logger: nLogger,
|
|
||||||
store: make(map[string]*PeripheralState),
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, dev := range devList {
|
|
||||||
store.AddDevice(dev)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &store
|
|
||||||
}
|
|
||||||
|
|
||||||
func (pss *PeripheralStateStore) GetStates() map[string]*PeripheralState {
|
|
||||||
pss.mu.RLock()
|
|
||||||
defer pss.mu.RUnlock()
|
|
||||||
|
|
||||||
return maps.Clone(pss.store)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (pss *PeripheralStateStore) GetState(pname string) (*PeripheralState, error) {
|
|
||||||
if !pss.DeviceExists(pname) {
|
|
||||||
return nil, ErrNoSuchDevice
|
|
||||||
}
|
|
||||||
|
|
||||||
pss.mu.RLock()
|
|
||||||
defer pss.mu.RUnlock()
|
|
||||||
return pss.store[pname], nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (pss *PeripheralStateStore) GetPeripheral(pname string) (peripheral.Peripheral, error) {
|
|
||||||
state, err := pss.GetState(pname)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if state.State != DeviceIsConnected {
|
|
||||||
return nil, ErrDeviceIsNotConnected
|
|
||||||
}
|
|
||||||
|
|
||||||
return state.Peripheral, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddDevice adds a new device for tracking
|
|
||||||
func (pss *PeripheralStateStore) AddDevice(dev *devices.Device) error {
|
|
||||||
if pss.DeviceExists(dev.Name) {
|
|
||||||
return ErrPeripheralAlreadyRegistered
|
|
||||||
}
|
|
||||||
|
|
||||||
pss.mu.Lock()
|
|
||||||
defer pss.mu.Unlock()
|
|
||||||
pss.store[dev.Name] = NewPeripheralState(dev, nil, DeviceIsDisconnected)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// DeviceExists returns whether the store is already tracking `pname`
|
|
||||||
func (pss *PeripheralStateStore) DeviceExists(pname string) bool {
|
|
||||||
pss.mu.RLock()
|
|
||||||
defer pss.mu.RUnlock()
|
|
||||||
|
|
||||||
if _, ok := pss.store[pname]; ok {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// DeleteDevice deletes `pname` from tracking
|
|
||||||
func (pss *PeripheralStateStore) DeleteDevice(pname string) error {
|
|
||||||
if !pss.DeviceExists(pname) {
|
|
||||||
return ErrNoSuchDevice
|
|
||||||
}
|
|
||||||
|
|
||||||
pss.mu.Lock()
|
|
||||||
defer pss.mu.Unlock()
|
|
||||||
|
|
||||||
delete(pss.store, pname)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// "Events" [START] ------------------------------------------------------------
|
|
||||||
func (ds *DeviceService) onDeviceTimedOut(pname string) {
|
|
||||||
// We need to deregister the device
|
|
||||||
ds.PSS.setDeviceTimedOut(pname)
|
|
||||||
}
|
|
||||||
|
|
||||||
// "Events" [END] --------------------------------------------------------------
|
|
||||||
|
|
||||||
// Device State Handling [START] -----------------------------------------------
|
|
||||||
func (pss *PeripheralStateStore) setDeviceConnected(pname string, per peripheral.Peripheral) {
|
|
||||||
pss.Logger.Info("found device", "device", pname)
|
|
||||||
|
|
||||||
pss.mu.Lock()
|
|
||||||
defer pss.mu.Unlock()
|
|
||||||
pss.store[pname].Peripheral = per
|
|
||||||
pss.store[pname].State = DeviceIsConnected
|
|
||||||
}
|
|
||||||
|
|
||||||
func (pss *PeripheralStateStore) setDeviceTimedOut(pname string) {
|
|
||||||
pss.Logger.Info("device timed out", "device", pname)
|
|
||||||
|
|
||||||
pss.mu.Lock()
|
|
||||||
defer pss.mu.Unlock()
|
|
||||||
pss.store[pname].Peripheral = nil
|
|
||||||
pss.store[pname].State = DeviceTimedOut
|
|
||||||
}
|
|
||||||
|
|
||||||
func (pss *PeripheralStateStore) setDeviceReconnected(pname string, per peripheral.Peripheral) {
|
|
||||||
pss.Logger.Info("device reconnecting", "device", pname)
|
|
||||||
|
|
||||||
pss.mu.Lock()
|
|
||||||
defer pss.mu.Unlock()
|
|
||||||
pss.store[pname].Peripheral = per
|
|
||||||
pss.store[pname].State = DeviceReconnected
|
|
||||||
}
|
|
||||||
|
|
||||||
// Device State Handling [END] -------------------------------------------------
|
|
||||||
|
|
||||||
// Device Handling [START] -----------------------------------------------------
|
|
||||||
func (pss *PeripheralStateStore) HandleDeviceState() {
|
|
||||||
peripherals := pss.GetStates()
|
|
||||||
|
|
||||||
for pName, pState := range peripherals {
|
|
||||||
switch pState.State {
|
|
||||||
case DeviceIsDisconnected:
|
|
||||||
pss.Logger.Debug("looking for device", "name", pName)
|
|
||||||
dev, err := pState.device.Discover()
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Register the device we just found
|
|
||||||
pss.setDeviceConnected(pName, dev)
|
|
||||||
case DeviceIsConnected:
|
|
||||||
// Need to check if its streaming, if its not streaming than we have to do a healthcheck
|
|
||||||
pss.Logger.Debug("Device is connected. Normal", "device", pName)
|
|
||||||
case DeviceReconnected:
|
|
||||||
// If the device has reconnected we need to reset the device and then set it as connected
|
|
||||||
pss.Logger.Debug("Device has reconnected. Clearing up state", "device", pName)
|
|
||||||
case DeviceTimedOut:
|
|
||||||
// If the device has timed out we need to re-discover it or something
|
|
||||||
pss.Logger.Debug("Device is timed out. Attempting to recconect", "device", pName)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Device Handling [END] -------------------------------------------------------
|
|
||||||
|
|
||||||
// FindDevices is a routine that goes over the devices in the PeripheralStateStore
|
|
||||||
// and handles their state accordingly
|
|
||||||
func (ds *DeviceService) FindDevices() {
|
|
||||||
ticker := time.NewTicker(2 * time.Second)
|
|
||||||
defer ticker.Stop()
|
|
||||||
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-ds.ctxDiscovery.Done():
|
|
||||||
return
|
|
||||||
case <-ticker.C:
|
|
||||||
ds.PSS.HandleDeviceState()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+96
-126
@@ -7,7 +7,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"esdi/providers"
|
"esdi/providers"
|
||||||
"esdi/telemetry"
|
|
||||||
telem "esdi/telemetry"
|
telem "esdi/telemetry"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -16,8 +15,6 @@ import (
|
|||||||
type TelemetryService struct {
|
type TelemetryService struct {
|
||||||
logger *slog.Logger
|
logger *slog.Logger
|
||||||
devService *DeviceService
|
devService *DeviceService
|
||||||
// Streaming
|
|
||||||
isStreaming bool
|
|
||||||
// Concurrency protection
|
// Concurrency protection
|
||||||
mut sync.RWMutex
|
mut sync.RWMutex
|
||||||
activeProvider telem.TelemetryProvider
|
activeProvider telem.TelemetryProvider
|
||||||
@@ -68,92 +65,6 @@ func (t *TelemetryService) ProviderMonitor(ctx context.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Listener Control [START] ----------------------------------------------------
|
|
||||||
|
|
||||||
func (t *TelemetryService) SubscribeListener(id string, bufferSize int) <-chan telem.TelemetryData {
|
|
||||||
t.mut.Lock()
|
|
||||||
defer t.mut.Unlock()
|
|
||||||
|
|
||||||
// NOTE: is this truly necessary?
|
|
||||||
// return the channel if it already exists
|
|
||||||
if ch, exists := t.listeners[id]; exists {
|
|
||||||
return ch
|
|
||||||
}
|
|
||||||
|
|
||||||
ch := make(chan telem.TelemetryData, bufferSize)
|
|
||||||
t.listeners[id] = ch
|
|
||||||
|
|
||||||
t.logger.Info("New stream subscriber registered", "id", id)
|
|
||||||
return ch
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *TelemetryService) UnsubscribeListener(id string) {
|
|
||||||
t.mut.Lock()
|
|
||||||
defer t.mut.Unlock()
|
|
||||||
|
|
||||||
if ch, exists := t.listeners[id]; exists {
|
|
||||||
close(ch)
|
|
||||||
delete(t.listeners, id)
|
|
||||||
t.logger.Info("Stream subscriber removed", "id", id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *TelemetryService) SubscribeToFields() []telem.FieldID {
|
|
||||||
seen := make(map[telemetry.FieldID]struct{})
|
|
||||||
var allFields []telemetry.FieldID
|
|
||||||
|
|
||||||
for _, dev := range t.devService.GetDevices() {
|
|
||||||
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)
|
|
||||||
|
|
||||||
return allFields
|
|
||||||
}
|
|
||||||
|
|
||||||
// Listener Control [END] ------------------------------------------------------
|
|
||||||
|
|
||||||
// Provider Control [START] ----------------------------------------------------
|
|
||||||
|
|
||||||
func (t *TelemetryService) HasActiveProvider() bool {
|
|
||||||
if t.activeProvider == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *TelemetryService) dropActiveProvider() {
|
|
||||||
if t.cancelForward != nil {
|
|
||||||
t.cancelForward()
|
|
||||||
}
|
|
||||||
|
|
||||||
t.activeProvider.StopStream()
|
|
||||||
t.activeProvider.Close()
|
|
||||||
t.activeProvider = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *TelemetryService) SwitchProvider(newProvider telem.TelemetryProvider) error {
|
|
||||||
t.mut.Lock()
|
|
||||||
defer t.mut.Unlock()
|
|
||||||
|
|
||||||
// Clean up the current to be old provider
|
|
||||||
if t.activeProvider != nil {
|
|
||||||
t.dropActiveProvider()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Assign the new provider
|
|
||||||
t.activeProvider = newProvider
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *TelemetryService) onProviderHealthCheckFailed() {
|
func (t *TelemetryService) onProviderHealthCheckFailed() {
|
||||||
// Just restart the whole lookup process
|
// Just restart the whole lookup process
|
||||||
go t.FindProvider(t.CtxMonitor)
|
go t.FindProvider(t.CtxMonitor)
|
||||||
@@ -211,46 +122,19 @@ func (t *TelemetryService) FindProvider(ctx context.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Provider Control [END] ------------------------------------------------------
|
func (t *TelemetryService) SwitchProvider(newProvider telem.TelemetryProvider) error {
|
||||||
|
|
||||||
// Streaming Control [START] ---------------------------------------------------
|
|
||||||
|
|
||||||
func (t *TelemetryService) StopStream() {
|
|
||||||
t.mut.Lock()
|
t.mut.Lock()
|
||||||
defer t.mut.Unlock()
|
defer t.mut.Unlock()
|
||||||
|
|
||||||
if t.cancelForward != nil {
|
// Clean up the current to be old provider
|
||||||
t.cancelForward()
|
if t.activeProvider != nil {
|
||||||
t.cancelForward = nil
|
t.dropActiveProvider()
|
||||||
}
|
}
|
||||||
|
|
||||||
t.activeProvider.StopStream()
|
// Assign the new provider
|
||||||
t.isStreaming = false
|
t.activeProvider = newProvider
|
||||||
}
|
|
||||||
|
|
||||||
func (t *TelemetryService) StartStream() {
|
return nil
|
||||||
slog.Debug("Stream started")
|
|
||||||
|
|
||||||
// Start the new stream
|
|
||||||
if t.activeProvider == nil {
|
|
||||||
slog.Debug("there's no active provider. not starting the stream")
|
|
||||||
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
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
t.cancelForward = cancel
|
|
||||||
|
|
||||||
// Multiplex this data
|
|
||||||
go t.multiplexData(ctx, simInCh)
|
|
||||||
t.isStreaming = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TelemetryService) multiplexData(ctx context.Context, dataCh <-chan telem.TelemetryData) {
|
func (t *TelemetryService) multiplexData(ctx context.Context, dataCh <-chan telem.TelemetryData) {
|
||||||
@@ -280,8 +164,94 @@ func (t *TelemetryService) multiplexData(ctx context.Context, dataCh <-chan tele
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TelemetryService) IsStreaming() bool {
|
func (t *TelemetryService) dropActiveProvider() {
|
||||||
return t.isStreaming
|
if t.cancelForward != nil {
|
||||||
|
t.cancelForward()
|
||||||
|
}
|
||||||
|
|
||||||
|
t.activeProvider.StopStream()
|
||||||
|
t.activeProvider.Close()
|
||||||
|
t.activeProvider = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Streaming Control [END] -----------------------------------------------------
|
func (t *TelemetryService) SubscribeListener(id string, bufferSize int) <-chan telem.TelemetryData {
|
||||||
|
t.mut.Lock()
|
||||||
|
defer t.mut.Unlock()
|
||||||
|
|
||||||
|
// NOTE: is this truly necessary?
|
||||||
|
// return the channel if it already exists
|
||||||
|
if ch, exists := t.listeners[id]; exists {
|
||||||
|
return ch
|
||||||
|
}
|
||||||
|
|
||||||
|
ch := make(chan telem.TelemetryData, bufferSize)
|
||||||
|
t.listeners[id] = ch
|
||||||
|
|
||||||
|
t.logger.Info("New stream subscriber registered", "id", id)
|
||||||
|
return ch
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TelemetryService) UnsubscribeListener(id string) {
|
||||||
|
t.mut.Lock()
|
||||||
|
defer t.mut.Unlock()
|
||||||
|
|
||||||
|
if ch, exists := t.listeners[id]; exists {
|
||||||
|
close(ch)
|
||||||
|
delete(t.listeners, id)
|
||||||
|
t.logger.Info("Stream subscriber removed", "id", id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TelemetryService) SubscribeToFields() {
|
||||||
|
// _ = t.devService.SubscribeFields()
|
||||||
|
// TODO: we need to find a way of requesting devices to send all subscribed fields
|
||||||
|
// instead of going through the devices on DevService here
|
||||||
|
for _, dev := range t.devService.Devices {
|
||||||
|
fields := dev.RequiredFields()
|
||||||
|
t.activeProvider.Subscribe(fields)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TelemetryService) StartStream() {
|
||||||
|
slog.Debug("Stream started")
|
||||||
|
|
||||||
|
// Start the new stream
|
||||||
|
if t.activeProvider == nil {
|
||||||
|
slog.Debug("there's no active provider. not starting the stream")
|
||||||
|
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
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
t.cancelForward = cancel
|
||||||
|
|
||||||
|
// Multiplex this data
|
||||||
|
go t.multiplexData(ctx, simInCh)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TelemetryService) StopStream() {
|
||||||
|
t.mut.Lock()
|
||||||
|
defer t.mut.Unlock()
|
||||||
|
|
||||||
|
if t.cancelForward != nil {
|
||||||
|
t.cancelForward()
|
||||||
|
t.cancelForward = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
t.activeProvider.StopStream()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TelemetryService) HasActiveProvider() bool {
|
||||||
|
if t.activeProvider == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|||||||
+75
-8
@@ -1,18 +1,12 @@
|
|||||||
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.
|
||||||
@@ -70,7 +64,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
|
||||||
@@ -81,6 +75,49 @@ 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:
|
||||||
@@ -222,6 +259,13 @@ 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
|
||||||
@@ -241,3 +285,26 @@ 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
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import "time"
|
|||||||
type TelemetryProvider interface {
|
type TelemetryProvider interface {
|
||||||
StopStream()
|
StopStream()
|
||||||
Stream() (<-chan TelemetryData, error)
|
Stream() (<-chan TelemetryData, error)
|
||||||
Subscribe([]FieldID)
|
Subscribe(map[int16]FieldID)
|
||||||
IsAlive(time.Duration) bool
|
IsAlive(time.Duration) bool
|
||||||
Name() string
|
Name() string
|
||||||
Close()
|
Close()
|
||||||
|
|||||||
@@ -1 +1,5 @@
|
|||||||
package telemetry
|
package telemetry
|
||||||
|
|
||||||
|
func Init() {
|
||||||
|
initFieldNamesMap()
|
||||||
|
}
|
||||||
|
|||||||
@@ -211,7 +211,7 @@ func (lc *LayoutController) createWindow() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Acquire the cdashdisplay
|
// Acquire the cdashdisplay
|
||||||
displayIF, err := lc.DevService.GetPeripheral(cdashdisplay.NAME)
|
displayIF, err := lc.DevService.GetDevice(cdashdisplay.NAME)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
||||||
return
|
return
|
||||||
@@ -321,7 +321,7 @@ func (lc *LayoutController) newWindowAction() {
|
|||||||
|
|
||||||
func (lc *LayoutController) updateWindowAction(win *cdashdisplay.DesktopUIWindow) {
|
func (lc *LayoutController) updateWindowAction(win *cdashdisplay.DesktopUIWindow) {
|
||||||
// Acquire the cdashdisplay
|
// Acquire the cdashdisplay
|
||||||
displayIF, err := lc.DevService.GetPeripheral(cdashdisplay.NAME)
|
displayIF, err := lc.DevService.GetDevice(cdashdisplay.NAME)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
||||||
return
|
return
|
||||||
@@ -347,7 +347,7 @@ func (lc *LayoutController) displayLoadedLayouts() {
|
|||||||
lc.Logger.Debug("We want to view our layout!")
|
lc.Logger.Debug("We want to view our layout!")
|
||||||
|
|
||||||
// Acquire the cdashdisplay
|
// Acquire the cdashdisplay
|
||||||
displayIF, err := lc.DevService.GetPeripheral(cdashdisplay.NAME)
|
displayIF, err := lc.DevService.GetDevice(cdashdisplay.NAME)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
||||||
return
|
return
|
||||||
@@ -392,7 +392,7 @@ func (lc *LayoutController) getCurrentTreeNodeModel() (*tview.TreeNode, int16, e
|
|||||||
|
|
||||||
func (lc *LayoutController) loadLayout() {
|
func (lc *LayoutController) loadLayout() {
|
||||||
// Acquire the cdashdisplay
|
// Acquire the cdashdisplay
|
||||||
displayIF, err := lc.DevService.GetPeripheral(cdashdisplay.NAME)
|
displayIF, err := lc.DevService.GetDevice(cdashdisplay.NAME)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
||||||
return
|
return
|
||||||
@@ -416,7 +416,7 @@ func (lc *LayoutController) loadLayout() {
|
|||||||
|
|
||||||
func (lc *LayoutController) unloadLayout() {
|
func (lc *LayoutController) unloadLayout() {
|
||||||
// Acquire the cdashdisplay
|
// Acquire the cdashdisplay
|
||||||
displayIF, err := lc.DevService.GetPeripheral(cdashdisplay.NAME)
|
displayIF, err := lc.DevService.GetDevice(cdashdisplay.NAME)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
||||||
return
|
return
|
||||||
@@ -437,7 +437,7 @@ func (lc *LayoutController) unloadLayout() {
|
|||||||
|
|
||||||
func (lc *LayoutController) saveLayout() {
|
func (lc *LayoutController) saveLayout() {
|
||||||
// Acquire the cdashdisplay
|
// Acquire the cdashdisplay
|
||||||
displayIF, err := lc.DevService.GetPeripheral(cdashdisplay.NAME)
|
displayIF, err := lc.DevService.GetDevice(cdashdisplay.NAME)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
||||||
return
|
return
|
||||||
@@ -470,7 +470,7 @@ func (lc *LayoutController) deleteWindow() {
|
|||||||
wID := curNode.GetReference().(int16)
|
wID := curNode.GetReference().(int16)
|
||||||
|
|
||||||
// Acquire the cdashdisplay
|
// Acquire the cdashdisplay
|
||||||
displayIF, err := lc.DevService.GetPeripheral(cdashdisplay.NAME)
|
displayIF, err := lc.DevService.GetDevice(cdashdisplay.NAME)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ func (lc *LayoutController) handleMovementCapture(idx int16,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Acquire the cdashdisplay
|
// Acquire the cdashdisplay
|
||||||
displayIF, err := lc.DevService.GetPeripheral(cdashdisplay.NAME)
|
displayIF, err := lc.DevService.GetDevice(cdashdisplay.NAME)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
||||||
return nil
|
return nil
|
||||||
@@ -86,7 +86,7 @@ func (lc *LayoutController) handleResizeCapture(idx int16,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Acquire the cdashdisplay
|
// Acquire the cdashdisplay
|
||||||
displayIF, err := lc.DevService.GetPeripheral(cdashdisplay.NAME)
|
displayIF, err := lc.DevService.GetDevice(cdashdisplay.NAME)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ 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
|
// This CDashDisplay specific, only load if we have a CDashDisplay
|
||||||
if !mc.DevService.PeripheralExists(cdashdisplay.NAME) {
|
if !mc.DevService.DeviceExists(cdashdisplay.NAME) {
|
||||||
mc.DevService.Messages <- "CDashDisplay it not loaded yet\n"
|
mc.DevService.Messages <- "CDashDisplay it not loaded yet\n"
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ import (
|
|||||||
|
|
||||||
type StreamingCtrl struct {
|
type StreamingCtrl struct {
|
||||||
*Controller
|
*Controller
|
||||||
DevService *services.DeviceService
|
Service *services.DeviceService
|
||||||
StreamView *views.StreamToolView
|
StreamView *views.StreamToolView
|
||||||
Messages chan string
|
Messages chan string
|
||||||
Internal chan string
|
Internal chan string
|
||||||
@@ -45,16 +45,18 @@ func NewStreamingCtrl(
|
|||||||
|
|
||||||
ctrl := &StreamingCtrl{
|
ctrl := &StreamingCtrl{
|
||||||
Controller: base,
|
Controller: base,
|
||||||
DevService: devService,
|
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),
|
||||||
TelemetryCh: make(chan telemetry.TelemetryData, 1),
|
TelemetryCh: make(chan telemetry.TelemetryData, 1),
|
||||||
Run: false,
|
Run: false,
|
||||||
StreamView: streamView,
|
StreamView: streamView,
|
||||||
|
isRunning: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
ctrl.registerHooks()
|
ctrl.registerHooks()
|
||||||
|
// ctrl.subscribeListeners()
|
||||||
|
|
||||||
return ctrl
|
return ctrl
|
||||||
}
|
}
|
||||||
@@ -94,11 +96,11 @@ func (sc *StreamingCtrl) registerHooks() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (sc *StreamingCtrl) StartStop() {
|
func (sc *StreamingCtrl) StartStop() {
|
||||||
if sc.TelemServ.IsStreaming() {
|
if sc.isRunning {
|
||||||
slog.Info("stopping stream")
|
slog.Info("stopping stream")
|
||||||
|
|
||||||
sc.TelemServ.StopStream()
|
sc.TelemServ.StopStream()
|
||||||
sc.DevService.StopStream()
|
sc.Service.StopStream()
|
||||||
|
|
||||||
sc.isRunning = false
|
sc.isRunning = false
|
||||||
return
|
return
|
||||||
@@ -108,9 +110,9 @@ func (sc *StreamingCtrl) StartStop() {
|
|||||||
// 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 device servie")
|
slog.Debug("setting the data stream for device servie")
|
||||||
sc.DevService.SetTelemetryChannel(sc.TelemServ.SubscribeListener("DeviceService", 1))
|
sc.Service.SetTelemetryChannel(sc.TelemServ.SubscribeListener("DeviceService", 1))
|
||||||
|
|
||||||
dev, err := sc.DevService.GetPeripheral(uidevice.NAME)
|
dev, err := sc.Service.GetDevice(uidevice.NAME)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if uiDev, ok := dev.(*uidevice.UIDevice); ok {
|
if uiDev, ok := dev.(*uidevice.UIDevice); ok {
|
||||||
sc.TelemetryCh = uiDev.DataChannel()
|
sc.TelemetryCh = uiDev.DataChannel()
|
||||||
@@ -119,7 +121,7 @@ func (sc *StreamingCtrl) StartStop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
slog.Debug("starting services")
|
slog.Debug("starting services")
|
||||||
sc.DevService.StartStream()
|
sc.Service.StartStream()
|
||||||
sc.TelemServ.StartStream()
|
sc.TelemServ.StartStream()
|
||||||
|
|
||||||
sc.isRunning = true
|
sc.isRunning = true
|
||||||
@@ -159,11 +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 := sc.TelemServ.SubscribeToFields()
|
// 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
|
||||||
|
// }
|
||||||
|
// ---
|
||||||
|
|
||||||
|
sc.TelemServ.SubscribeToFields()
|
||||||
|
|
||||||
// sc.Messages <- fmt.Sprintf("Subscribed Fields: %+v [%d]\n", fields, len(fields))
|
// sc.Messages <- fmt.Sprintf("Subscribed Fields: %+v [%d]\n", fields, len(fields))
|
||||||
// Should I update this?
|
// Should I update this?
|
||||||
sc.Messages <- fmt.Sprintf("Subscribed to fields: %+v", fields)
|
sc.Messages <- fmt.Sprintf("Subscribed to fields\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sc *StreamingCtrl) listenToUIStream() {
|
func (sc *StreamingCtrl) listenToUIStream() {
|
||||||
@@ -175,6 +190,8 @@ func (sc *StreamingCtrl) listenToUIStream() {
|
|||||||
}
|
}
|
||||||
isDrawing.Store(true)
|
isDrawing.Store(true)
|
||||||
|
|
||||||
|
// sc.Logger.Debug("got data", "data", msg)
|
||||||
|
|
||||||
// Capture locally
|
// Capture locally
|
||||||
telemetryMsg := msg
|
telemetryMsg := msg
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user