Compare commits
25
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a5c465a203 | ||
|
|
174b27004f | ||
|
|
a2e14ba6a9 | ||
|
|
a6683df10b | ||
|
|
2d4875fbd0 | ||
|
|
0a8f34423c | ||
|
|
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
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
# Streaming Flow
|
||||||
@@ -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)
|
||||||
}
|
}
|
||||||
|
|||||||
+87
-93
@@ -1,103 +1,97 @@
|
|||||||
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,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,9 +8,11 @@ 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"
|
||||||
@@ -19,12 +21,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/"
|
||||||
@@ -114,25 +110,68 @@ 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
|
||||||
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", "reason", 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 {
|
||||||
@@ -148,9 +187,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 +218,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 +248,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 +270,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,30 +388,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 {
|
||||||
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))
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *CDashDisplay) SendData(data *telemetry.TelemetryData) {
|
func (d *CDashDisplay) SendData(data *telemetry.TelemetryData) error {
|
||||||
packet := data.Pack()
|
packet := d.encodePacket(data)
|
||||||
|
|
||||||
bytes, err := helper.StructToBytes(packet)
|
bytes, err := helper.StructToBytes(packet)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return peripheral.ErrFailureToPackData
|
||||||
}
|
}
|
||||||
|
|
||||||
curStr := ""
|
curStr := ""
|
||||||
@@ -376,7 +421,6 @@ 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)
|
|
||||||
curStr = ""
|
curStr = ""
|
||||||
byteCount = 0
|
byteCount = 0
|
||||||
}
|
}
|
||||||
@@ -385,6 +429,11 @@ func (d *CDashDisplay) SendData(data *telemetry.TelemetryData) {
|
|||||||
// 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 {
|
||||||
return
|
if d.failedSends == d.FailedSendsConsecutiveLimit {
|
||||||
|
return peripheral.ErrDeviceTimedOut
|
||||||
}
|
}
|
||||||
|
d.failedSends++
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
@@ -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,56 @@
|
|||||||
|
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) Close() error {
|
||||||
|
if uid.dataChan != nil {
|
||||||
|
close(uid.dataChan)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (uid *UIDevice) SendData(data *telemetry.TelemetryData) error {
|
||||||
|
if data == nil {
|
||||||
|
return peripheral.ErrInvalidData
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case uid.dataChan <- *data:
|
||||||
|
default:
|
||||||
|
// Drop frame if buffer is full
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
|||||||
@@ -108,6 +108,15 @@ 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,
|
||||||
@@ -119,7 +128,9 @@ 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 {
|
||||||
@@ -176,21 +187,11 @@ func (wt *WalkieTalkie) readPacket(resp packets.Packet) error {
|
|||||||
// return nil
|
// return nil
|
||||||
// }
|
// }
|
||||||
|
|
||||||
func (wt *WalkieTalkie) SendCommand(cmd types.Command, payload any,
|
func (wt *WalkieTalkie) SendCommand(
|
||||||
responseBody packets.Packet) error {
|
cmd types.Command,
|
||||||
// Prepare the header
|
payload any,
|
||||||
// header := header{
|
responseBody packets.Packet,
|
||||||
// StartByte: constvar.StartOfText,
|
) error {
|
||||||
// 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 {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import "esdi/peripheral/types"
|
|||||||
// themselves so we can discover them
|
// themselves so we can discover them
|
||||||
const (
|
const (
|
||||||
CDashDisplayDevID = 0x01
|
CDashDisplayDevID = 0x01
|
||||||
|
CDashDisplayDevName = "CDashDisplay"
|
||||||
ESBtnBoxDevID = 0x02
|
ESBtnBoxDevID = 0x02
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package peripheral
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrInvalidData = errors.New("invalid data")
|
||||||
|
ErrDeviceTimedOut = errors.New("device timed out")
|
||||||
|
ErrFailureToPackData = errors.New("failed to pack received data")
|
||||||
|
)
|
||||||
@@ -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,13 @@ const (
|
|||||||
DisplayPeripheral PeripheralType = "display"
|
DisplayPeripheral PeripheralType = "display"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Peripheral interface {
|
||||||
|
Name() string
|
||||||
|
SendData(*telemetry.TelemetryData) error
|
||||||
|
RequiredFields() []telemetry.FieldID
|
||||||
|
Close() error
|
||||||
|
}
|
||||||
|
|
||||||
type PeripheralDeviceClerk struct {
|
type PeripheralDeviceClerk struct {
|
||||||
// mu sync.RWMutex
|
// mu sync.RWMutex
|
||||||
Devices map[uint8]*PeripheralDevice
|
Devices map[uint8]*PeripheralDevice
|
||||||
|
|||||||
+37
-17
@@ -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
|
||||||
@@ -25,7 +28,7 @@ type BeamNG struct {
|
|||||||
updaters [telemetry.MaxFields]func(*telemetry.TelemetryField)
|
updaters [telemetry.MaxFields]func(*telemetry.TelemetryField)
|
||||||
|
|
||||||
// stream control
|
// stream control
|
||||||
streamCh chan telemetry.TelemetryData
|
wg sync.WaitGroup
|
||||||
streamCancel context.CancelFunc
|
streamCancel context.CancelFunc
|
||||||
|
|
||||||
// timing
|
// timing
|
||||||
@@ -36,16 +39,17 @@ 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{
|
||||||
streamCh: make(chan telemetry.TelemetryData, 1),
|
logger: logger.With("TelemetryProvider", NAME),
|
||||||
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 +83,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
|
||||||
@@ -92,12 +109,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
|
||||||
b.stream(ctx)
|
ch := b.stream(ctx)
|
||||||
|
|
||||||
return b.streamCh, nil
|
return ch, 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 +124,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())
|
||||||
@@ -146,8 +161,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
|
ogSnapshot, err := b.SDK.Update()
|
||||||
err := b.SDK.ReadData()
|
|
||||||
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 +169,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
|
||||||
@@ -176,10 +191,15 @@ func (b *BeamNG) readData() {
|
|||||||
b.data.LastDataPoll = time.Now()
|
b.data.LastDataPoll = time.Now()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BeamNG) stream(ctx context.Context) {
|
func (b *BeamNG) stream(ctx context.Context) <-chan telemetry.TelemetryData {
|
||||||
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 {
|
||||||
@@ -188,8 +208,6 @@ func (b *BeamNG) stream(ctx context.Context) {
|
|||||||
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
|
||||||
@@ -200,7 +218,7 @@ func (b *BeamNG) stream(ctx context.Context) {
|
|||||||
|
|
||||||
// Publish data
|
// Publish data
|
||||||
select {
|
select {
|
||||||
case b.streamCh <- *b.data:
|
case outCh <- *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
|
||||||
@@ -208,4 +226,6 @@ func (b *BeamNG) stream(ctx context.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
return outCh
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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",
|
|
||||||
}
|
|
||||||
|
|||||||
+111
-132
@@ -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"
|
||||||
@@ -35,7 +33,8 @@ type IRacing struct {
|
|||||||
ticker *time.Ticker // ticker will keep polling intervals constant
|
ticker *time.Ticker // ticker will keep polling intervals constant
|
||||||
|
|
||||||
// Stream
|
// Stream
|
||||||
streamCh chan telemetry.TelemetryData
|
wg sync.WaitGroup
|
||||||
|
// streamCh chan telemetry.TelemetryData
|
||||||
streamCancel context.CancelFunc
|
streamCancel context.CancelFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,62 +54,125 @@ func NewIRacingProvider(
|
|||||||
logger: logger,
|
logger: logger,
|
||||||
SDK: sdk,
|
SDK: sdk,
|
||||||
data: telemetry.NewTelemetryData(),
|
data: telemetry.NewTelemetryData(),
|
||||||
streamCh: make(chan telemetry.TelemetryData, 1),
|
// TODO: make this configurable from the user side
|
||||||
// NOTE: This is because I stupidly recorded a test IBT file in 240
|
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){
|
||||||
|
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if live telemetry is on
|
|
||||||
if i.SDK.IsConnected() {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *IRacing) stream(ctx context.Context) {
|
// Check if live telemetry is on
|
||||||
|
if !i.SDK.IsConnected() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *IRacing) stream(ctx context.Context) <-chan telemetry.TelemetryData {
|
||||||
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(outCh)
|
||||||
|
|
||||||
|
// 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
|
|
||||||
}
|
|
||||||
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return
|
|
||||||
case <-i.ticker.C:
|
|
||||||
i.readData()
|
i.readData()
|
||||||
|
|
||||||
// Publish data
|
// Publish data
|
||||||
select {
|
select {
|
||||||
case i.streamCh <- *i.data:
|
case outCh <- *i.data:
|
||||||
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
return outCh
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *IRacing) readData() {
|
func (i *IRacing) readData() {
|
||||||
@@ -124,18 +186,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)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -152,9 +209,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
|
||||||
i.stream(ctx)
|
ch := i.stream(ctx)
|
||||||
|
|
||||||
return i.streamCh, nil
|
return ch, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *IRacing) StopStream() {
|
func (i *IRacing) StopStream() {
|
||||||
@@ -163,21 +220,20 @@ func (i *IRacing) StopStream() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
i.streamCancel()
|
i.streamCancel()
|
||||||
|
i.wg.Wait()
|
||||||
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,102 +255,25 @@ 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)
|
||||||
boundCheck[id] = true
|
boundCheck[id] = true
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
|
}
|
||||||
+31
-9
@@ -8,6 +8,7 @@ import (
|
|||||||
"esdi/providers/iracing"
|
"esdi/providers/iracing"
|
||||||
"esdi/telemetry"
|
"esdi/telemetry"
|
||||||
|
|
||||||
|
bngsdk "github.com/ESilva15/gobngsdk"
|
||||||
"github.com/ESilva15/goirsdk"
|
"github.com/ESilva15/goirsdk"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -15,27 +16,48 @@ import (
|
|||||||
// 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,
|
||||||
return provider
|
})
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("failed to create iRacing provider", "err", err)
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBeamNGProvider(ip string, port int) telemetry.TelemetryProvider {
|
return provider, nil
|
||||||
provider, _ := beamng.NewBeamNGProvider(ip, port)
|
}
|
||||||
|
|
||||||
return provider
|
func NewBeamNGProvider(logger *slog.Logger) (telemetry.TelemetryProvider, error) {
|
||||||
|
// 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, 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,131 @@
|
|||||||
|
package services
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"log/slog"
|
||||||
|
"sync/atomic"
|
||||||
|
|
||||||
|
"esdi/devices"
|
||||||
|
"esdi/peripheral"
|
||||||
|
"esdi/telemetry"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DeviceService is the API for the peripherals
|
||||||
|
type DeviceService struct {
|
||||||
|
Logger *slog.Logger
|
||||||
|
// Device discovery
|
||||||
|
PSS *PeripheralStateStore // Store to track peripheral state
|
||||||
|
ctxDiscovery context.Context
|
||||||
|
ctxDiscoveryCancel context.CancelFunc
|
||||||
|
// 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{
|
||||||
|
PSS: NewPeripheralStateStore(logger.With("Service", "PeripheralStateStore"), devices.List),
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
// Getters [START] -------------------------------------------------------------
|
||||||
|
func (ds *DeviceService) GetDevices() []peripheral.Peripheral {
|
||||||
|
snapshot := ds.PSS.GetStates()
|
||||||
|
peripherals := make([]peripheral.Peripheral, 0, len(snapshot))
|
||||||
|
|
||||||
|
for _, state := range snapshot {
|
||||||
|
if state.State != DeviceIsConnected {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
peripherals = append(peripherals, state.Peripheral)
|
||||||
|
}
|
||||||
|
|
||||||
|
return peripherals
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ds *DeviceService) GetPeripheral(pname string) (peripheral.Peripheral, error) {
|
||||||
|
return ds.PSS.GetPeripheral(pname)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ds *DeviceService) PeripheralExists(pname string) bool {
|
||||||
|
_, err := ds.PSS.GetPeripheral(pname)
|
||||||
|
return err == nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Getters [END] ---------------------------------------------------------------
|
||||||
|
|
||||||
|
// Actions [START] -------------------------------------------------------------
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actions [END] ---------------------------------------------------------------
|
||||||
|
|
||||||
|
// 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
|
||||||
|
for _, dev := range ds.PSS.GetStates() {
|
||||||
|
if dev.State != DeviceIsConnected {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
err := dev.Peripheral.SendData(&data)
|
||||||
|
|
||||||
|
if err == peripheral.ErrDeviceTimedOut {
|
||||||
|
ds.onDeviceTimedOut(dev.device.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
isSending.Store(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,222 @@
|
|||||||
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+202
-67
@@ -4,91 +4,71 @@ 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
|
||||||
|
// Streaming
|
||||||
|
isStreaming bool
|
||||||
// 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,
|
||||||
|
devService: devServo,
|
||||||
listeners: make(map[string]chan telem.TelemetryData),
|
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) SwitchProvider(newProvider telem.TelemetryProvider) error {
|
func (t *TelemetryService) ProviderMonitor(ctx context.Context) {
|
||||||
t.mut.Lock()
|
ticker := time.NewTicker(2 * time.Second)
|
||||||
defer t.mut.Unlock()
|
defer ticker.Stop()
|
||||||
|
|
||||||
// Clean up the current to be old provider
|
|
||||||
if t.ativeProvider != nil {
|
|
||||||
t.dropActiveProvider()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Assign the new provider
|
|
||||||
t.ativeProvider = newProvider
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *TelemetryService) multiplexData(ctx context.Context, dataCh <-chan telem.TelemetryData) {
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
case data, ok := <-dataCh:
|
case <-ticker.C:
|
||||||
if !ok {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
t.mut.RLock()
|
|
||||||
for _, ch := range t.listeners {
|
|
||||||
select {
|
|
||||||
case ch <- data:
|
|
||||||
// Sends data to the subscriber
|
|
||||||
default:
|
|
||||||
// Subscriber is full, we just skip ahead. Maybe find a how to add metrics here
|
|
||||||
}
|
|
||||||
}
|
|
||||||
t.mut.RUnlock()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TelemetryService) dropActiveProvider() {
|
// Listener Control [START] ----------------------------------------------------
|
||||||
if t.cancelForward != nil {
|
|
||||||
t.cancelForward()
|
|
||||||
}
|
|
||||||
|
|
||||||
t.ativeProvider.StopStream()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *TelemetryService) SubscribeListener(id string, bufferSize int) <-chan telem.TelemetryData {
|
func (t *TelemetryService) SubscribeListener(id string, bufferSize int) <-chan telem.TelemetryData {
|
||||||
t.mut.Lock()
|
t.mut.Lock()
|
||||||
@@ -118,27 +98,123 @@ func (t *TelemetryService) UnsubscribeListener(id string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TelemetryService) SubscribeToFields(fields map[int16]telem.FieldID) {
|
func (t *TelemetryService) SubscribeToFields() []telem.FieldID {
|
||||||
t.ativeProvider.Subscribe(fields)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TelemetryService) StartStream() {
|
t.logger.Debug("requested fields", "fields", allFields)
|
||||||
slog.Debug("Stream started")
|
t.activeProvider.Subscribe(allFields)
|
||||||
|
|
||||||
// Start the new stream
|
return allFields
|
||||||
simInCh, _ := t.ativeProvider.Stream()
|
|
||||||
// if err != nil {
|
|
||||||
// // NOTE
|
|
||||||
// }
|
|
||||||
|
|
||||||
// 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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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() {
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Provider Control [END] ------------------------------------------------------
|
||||||
|
|
||||||
|
// Streaming Control [START] ---------------------------------------------------
|
||||||
|
|
||||||
func (t *TelemetryService) StopStream() {
|
func (t *TelemetryService) StopStream() {
|
||||||
t.mut.Lock()
|
t.mut.Lock()
|
||||||
defer t.mut.Unlock()
|
defer t.mut.Unlock()
|
||||||
@@ -148,5 +224,64 @@ func (t *TelemetryService) StopStream() {
|
|||||||
t.cancelForward = nil
|
t.cancelForward = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
t.ativeProvider.StopStream()
|
t.activeProvider.StopStream()
|
||||||
|
t.isStreaming = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
t.isStreaming = true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TelemetryService) multiplexData(ctx context.Context, dataCh <-chan telem.TelemetryData) {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case data, ok := <-dataCh:
|
||||||
|
if !ok {
|
||||||
|
t.logger.Debug("something happened on the provider - stream closed")
|
||||||
|
t.onProviderStopsMidStream()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
t.mut.RLock()
|
||||||
|
for _, ch := range t.listeners {
|
||||||
|
// t.logger.Debug("sending data to listener", "listener", key, "data", data)
|
||||||
|
select {
|
||||||
|
case ch <- data:
|
||||||
|
// Sends data to the subscriber
|
||||||
|
default:
|
||||||
|
// Subscriber is full, we just skip ahead. Maybe find a how to add metrics here
|
||||||
|
}
|
||||||
|
}
|
||||||
|
t.mut.RUnlock()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TelemetryService) IsStreaming() bool {
|
||||||
|
return t.isStreaming
|
||||||
|
}
|
||||||
|
|
||||||
|
// Streaming Control [END] -----------------------------------------------------
|
||||||
|
|||||||
@@ -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)
|
||||||
}
|
}
|
||||||
|
|||||||
+9
-78
@@ -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,10 +36,7 @@ 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
|
||||||
Fetch func() any // NOTE: to be deprecated
|
|
||||||
Transform func(any, *TelemetryField) // NOTE: to be deprecated
|
|
||||||
Update func(out *TelemetryField)
|
Update func(out *TelemetryField)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -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.GetPeripheral(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.GetPeripheral(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.GetPeripheral(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.GetPeripheral(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.GetPeripheral(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.GetPeripheral(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.GetPeripheral(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.GetPeripheral(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.GetPeripheral(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.PeripheralExists(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
|
DevService *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,26 +45,24 @@ func NewStreamingCtrl(
|
|||||||
|
|
||||||
ctrl := &StreamingCtrl{
|
ctrl := &StreamingCtrl{
|
||||||
Controller: base,
|
Controller: base,
|
||||||
Service: serCDash,
|
DevService: 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()
|
|
||||||
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 {
|
||||||
@@ -95,11 +94,11 @@ func (sc *StreamingCtrl) registerHooks() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (sc *StreamingCtrl) StartStop() {
|
func (sc *StreamingCtrl) StartStop() {
|
||||||
if sc.isRunning {
|
if sc.TelemServ.IsStreaming() {
|
||||||
slog.Info("stopping stream")
|
slog.Info("stopping stream")
|
||||||
|
|
||||||
sc.TelemServ.StopStream()
|
sc.TelemServ.StopStream()
|
||||||
sc.Service.StopStream()
|
sc.DevService.StopStream()
|
||||||
|
|
||||||
sc.isRunning = false
|
sc.isRunning = false
|
||||||
return
|
return
|
||||||
@@ -108,16 +107,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.DevService.SetTelemetryChannel(sc.TelemServ.SubscribeListener("DeviceService", 1))
|
||||||
|
|
||||||
slog.Debug("starting to stream data again")
|
dev, err := sc.DevService.GetPeripheral(uidevice.NAME)
|
||||||
sc.Service.StartStream()
|
if err == nil {
|
||||||
|
if uiDev, ok := dev.(*uidevice.UIDevice); ok {
|
||||||
|
sc.TelemetryCh = uiDev.DataChannel()
|
||||||
|
go sc.listenToUIStream()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
slog.Debug("starting the stream")
|
slog.Debug("starting services")
|
||||||
|
sc.DevService.StartStream()
|
||||||
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 +159,11 @@ 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))
|
fields := sc.TelemServ.SubscribeToFields()
|
||||||
|
|
||||||
for _, w := range sc.Service.CDash.State.Layout.Windows {
|
// sc.Messages <- fmt.Sprintf("Subscribed Fields: %+v [%d]\n", fields, len(fields))
|
||||||
fieldID, _ := telemetry.GetFieldID(w.UIData.TelemetryField)
|
// Should I update this?
|
||||||
fields[w.UIData.IDX] = fieldID
|
sc.Messages <- fmt.Sprintf("Subscribed to fields: %+v", fields)
|
||||||
}
|
|
||||||
|
|
||||||
sc.TelemServ.SubscribeToFields(fields)
|
|
||||||
|
|
||||||
sc.Messages <- fmt.Sprintf("Subscribed Fields: %+v [%d]\n", fields, len(fields))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sc *StreamingCtrl) listenToUIStream() {
|
func (sc *StreamingCtrl) listenToUIStream() {
|
||||||
@@ -176,8 +175,11 @@ func (sc *StreamingCtrl) listenToUIStream() {
|
|||||||
}
|
}
|
||||||
isDrawing.Store(true)
|
isDrawing.Store(true)
|
||||||
|
|
||||||
|
// 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