has way too many things
In short: added the ability to detect when a device has disconnected. From this we have to achieve a way of: - reconnecting the device - re-subscribing to the fields it needs (it should already be subscribed since the ESDI didn't stop tho - but for the sake of it, or in case a device joins later) - start sending data to it (which should be automatic given how we are handling the devices)
This commit is contained in:
+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
|
||||||
|
|||||||
@@ -140,6 +140,14 @@ func NewCDashDisplay() (*CDashDisplay, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cds *CDashDisplay) Close() error {
|
||||||
|
// if cds.WT != nil {
|
||||||
|
// cds.Close()
|
||||||
|
// }
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (d *CDashDisplay) SendCommand() {
|
func (d *CDashDisplay) SendCommand() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -12,7 +12,7 @@ type Device struct {
|
|||||||
Discover func() (peripheral.Peripheral, error)
|
Discover func() (peripheral.Peripheral, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
var List map[string]Device = map[string]Device{
|
var List map[string]*Device = map[string]*Device{
|
||||||
uidevice.NAME: {
|
uidevice.NAME: {
|
||||||
Name: uidevice.NAME,
|
Name: uidevice.NAME,
|
||||||
Discover: DiscoverUIDevice,
|
Discover: DiscoverUIDevice,
|
||||||
|
|||||||
@@ -17,6 +17,14 @@ func NewUIDevice() (peripheral.Peripheral, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (uid *UIDevice) Close() error {
|
||||||
|
if uid.dataChan != nil {
|
||||||
|
close(uid.dataChan)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (uid *UIDevice) SendData(data *telemetry.TelemetryData) error {
|
func (uid *UIDevice) SendData(data *telemetry.TelemetryData) error {
|
||||||
if data == nil {
|
if data == nil {
|
||||||
return peripheral.ErrInvalidData
|
return peripheral.ErrInvalidData
|
||||||
|
|||||||
@@ -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 {
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ type Peripheral interface {
|
|||||||
Name() string
|
Name() string
|
||||||
SendData(*telemetry.TelemetryData) error
|
SendData(*telemetry.TelemetryData) error
|
||||||
RequiredFields() []telemetry.FieldID
|
RequiredFields() []telemetry.FieldID
|
||||||
|
Close() error
|
||||||
}
|
}
|
||||||
|
|
||||||
type PeripheralDeviceClerk struct {
|
type PeripheralDeviceClerk struct {
|
||||||
|
|||||||
@@ -54,10 +54,8 @@ func NewIRacingProvider(
|
|||||||
logger: logger,
|
logger: logger,
|
||||||
SDK: sdk,
|
SDK: sdk,
|
||||||
data: telemetry.NewTelemetryData(),
|
data: telemetry.NewTelemetryData(),
|
||||||
// streamCh: make(chan telemetry.TelemetryData, 1),
|
|
||||||
// NOTE: This is because I stupidly recorded a test IBT file in 240
|
|
||||||
// TODO: make this configurable from the user side
|
// TODO: make this configurable from the user side
|
||||||
ticker: time.NewTicker(time.Second / 240),
|
ticker: time.NewTicker(time.Second / 60),
|
||||||
}
|
}
|
||||||
|
|
||||||
provider.updaters = [telemetry.MaxFields]func(*telemetry.TelemetryField){
|
provider.updaters = [telemetry.MaxFields]func(*telemetry.TelemetryField){
|
||||||
|
|||||||
+28
-70
@@ -2,32 +2,21 @@ package services
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"sync"
|
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
|
||||||
|
|
||||||
"esdi/devices"
|
"esdi/devices"
|
||||||
"esdi/peripheral"
|
"esdi/peripheral"
|
||||||
"esdi/telemetry"
|
"esdi/telemetry"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ErrPeripheralAlreadyRegistered = errors.New("peripheral is already registered")
|
// DeviceService is the API for the peripherals
|
||||||
|
|
||||||
// DeviceService will handle sending the data from the telemetry service to the
|
|
||||||
// actual devices
|
|
||||||
// NOTE: create a virtual device and make it be the output window or something so
|
|
||||||
// we can just add it as a device or whatever instead of being a custom made thing
|
|
||||||
// that would be pretty cool I think
|
|
||||||
type DeviceService struct {
|
type DeviceService struct {
|
||||||
Logger *slog.Logger
|
Logger *slog.Logger
|
||||||
// Device discovery
|
// Device discovery
|
||||||
mu sync.RWMutex
|
PSS *PeripheralStateStore // Store to track peripheral state
|
||||||
ctxDiscovery context.Context
|
ctxDiscovery context.Context
|
||||||
ctxDiscoveryCancel context.CancelFunc
|
ctxDiscoveryCancel context.CancelFunc
|
||||||
Devices map[string]peripheral.Peripheral
|
|
||||||
// Strem handling
|
// Strem handling
|
||||||
streamCancel context.CancelFunc
|
streamCancel context.CancelFunc
|
||||||
TelemCh <-chan telemetry.TelemetryData
|
TelemCh <-chan telemetry.TelemetryData
|
||||||
@@ -39,7 +28,7 @@ func NewDeviceService(logger *slog.Logger) *DeviceService {
|
|||||||
sharedChannel := make(chan string, 10)
|
sharedChannel := make(chan string, 10)
|
||||||
|
|
||||||
dev := &DeviceService{
|
dev := &DeviceService{
|
||||||
Devices: make(map[string]peripheral.Peripheral),
|
PSS: NewPeripheralStateStore(logger.With("Service", "PeripheralStateStore"), devices.List),
|
||||||
Logger: logger,
|
Logger: logger,
|
||||||
Messages: sharedChannel,
|
Messages: sharedChannel,
|
||||||
}
|
}
|
||||||
@@ -52,68 +41,33 @@ func NewDeviceService(logger *slog.Logger) *DeviceService {
|
|||||||
return dev
|
return dev
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ds *DeviceService) FindDevices() {
|
// Getters [START] -------------------------------------------------------------
|
||||||
// Need to define a list of devices to search for
|
func (ds *DeviceService) GetDevices() []peripheral.Peripheral {
|
||||||
// For now lets just try to find our cdashdisplay - will think about the rest later
|
snapshot := ds.PSS.GetStates()
|
||||||
ticker := time.NewTicker(2 * time.Second)
|
peripherals := make([]peripheral.Peripheral, 0, len(snapshot))
|
||||||
defer ticker.Stop()
|
|
||||||
|
|
||||||
for {
|
for _, state := range snapshot {
|
||||||
select {
|
if state.State != DeviceIsConnected {
|
||||||
case <-ds.ctxDiscovery.Done():
|
|
||||||
// If requested to cancel we cancel background discovery
|
|
||||||
return
|
|
||||||
case <-ticker.C:
|
|
||||||
for pName, peripheral := range devices.List {
|
|
||||||
if ds.DeviceExists(pName) {
|
|
||||||
// We already discovered this device
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
peripherals = append(peripherals, state.Peripheral)
|
||||||
ds.Logger.Debug("looking for device", "name", pName)
|
|
||||||
dev, err := peripheral.Discover()
|
|
||||||
if err != nil {
|
|
||||||
ds.Logger.Debug("didn't find device", "name", pName)
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register the device we just found
|
return peripherals
|
||||||
ds.RegisterDevice(dev)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ds *DeviceService) RegisterDevice(dev peripheral.Peripheral) error {
|
func (ds *DeviceService) GetPeripheral(pname string) (peripheral.Peripheral, error) {
|
||||||
ds.mu.Lock()
|
return ds.PSS.GetPeripheral(pname)
|
||||||
defer ds.mu.Unlock()
|
|
||||||
|
|
||||||
if ds.DeviceExists(dev.Name()) {
|
|
||||||
return ErrPeripheralAlreadyRegistered
|
|
||||||
}
|
|
||||||
|
|
||||||
ds.Devices[dev.Name()] = dev
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ds *DeviceService) GetDevice(name string) (peripheral.Peripheral, error) {
|
func (ds *DeviceService) PeripheralExists(pname string) bool {
|
||||||
val, ok := ds.Devices[name]
|
_, err := ds.PSS.GetPeripheral(pname)
|
||||||
if !ok {
|
return err == nil
|
||||||
return nil, fmt.Errorf("device `%s` couldn't be found", name)
|
|
||||||
}
|
|
||||||
|
|
||||||
return val, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ds *DeviceService) DeviceExists(name string) bool {
|
// Getters [END] ---------------------------------------------------------------
|
||||||
if _, ok := ds.Devices[name]; !ok {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Actions [START] -------------------------------------------------------------
|
||||||
func (ds *DeviceService) StartStream() {
|
func (ds *DeviceService) StartStream() {
|
||||||
// NOTE: i'm using this pattern a whole lot. Maybe I can create a struct to handle this
|
// NOTE: i'm using this pattern a whole lot. Maybe I can create a struct to handle this
|
||||||
var ctx context.Context
|
var ctx context.Context
|
||||||
@@ -136,6 +90,8 @@ func (ds *DeviceService) SetTelemetryChannel(ch <-chan telemetry.TelemetryData)
|
|||||||
ds.TelemCh = ch
|
ds.TelemCh = ch
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Actions [END] ---------------------------------------------------------------
|
||||||
|
|
||||||
// transmit will send the data to the devices themselves
|
// transmit will send the data to the devices themselves
|
||||||
func (ds *DeviceService) transmit(ctx context.Context) {
|
func (ds *DeviceService) transmit(ctx context.Context) {
|
||||||
var isSending atomic.Bool
|
var isSending atomic.Bool
|
||||||
@@ -157,15 +113,17 @@ func (ds *DeviceService) transmit(ctx context.Context) {
|
|||||||
|
|
||||||
// TODO: make a copy of the data and send that copy instead of keeping
|
// TODO: make a copy of the data and send that copy instead of keeping
|
||||||
// the data locked
|
// the data locked
|
||||||
ds.mu.RLock()
|
for _, dev := range ds.PSS.GetStates() {
|
||||||
for _, dev := range ds.Devices {
|
if dev.State != DeviceIsConnected {
|
||||||
err := dev.SendData(&data)
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
err := dev.Peripheral.SendData(&data)
|
||||||
|
|
||||||
if err == peripheral.ErrDeviceTimedOut {
|
if err == peripheral.ErrDeviceTimedOut {
|
||||||
// What do we do here?
|
ds.onDeviceTimedOut(dev.device.Name)
|
||||||
// TODO: somehow we need to handle reconnection
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ds.mu.RUnlock()
|
|
||||||
|
|
||||||
isSending.Store(false)
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -102,7 +102,7 @@ func (t *TelemetryService) SubscribeToFields() []telem.FieldID {
|
|||||||
seen := make(map[telemetry.FieldID]struct{})
|
seen := make(map[telemetry.FieldID]struct{})
|
||||||
var allFields []telemetry.FieldID
|
var allFields []telemetry.FieldID
|
||||||
|
|
||||||
for _, dev := range t.devService.Devices {
|
for _, dev := range t.devService.GetDevices() {
|
||||||
for _, field := range dev.RequiredFields() {
|
for _, field := range dev.RequiredFields() {
|
||||||
if _, exists := seen[field]; !exists {
|
if _, exists := seen[field]; !exists {
|
||||||
seen[field] = struct{}{}
|
seen[field] = struct{}{}
|
||||||
|
|||||||
@@ -211,7 +211,7 @@ func (lc *LayoutController) createWindow() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Acquire the cdashdisplay
|
// Acquire the cdashdisplay
|
||||||
displayIF, err := lc.DevService.GetDevice(cdashdisplay.NAME)
|
displayIF, err := lc.DevService.GetPeripheral(cdashdisplay.NAME)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
||||||
return
|
return
|
||||||
@@ -321,7 +321,7 @@ func (lc *LayoutController) newWindowAction() {
|
|||||||
|
|
||||||
func (lc *LayoutController) updateWindowAction(win *cdashdisplay.DesktopUIWindow) {
|
func (lc *LayoutController) updateWindowAction(win *cdashdisplay.DesktopUIWindow) {
|
||||||
// Acquire the cdashdisplay
|
// Acquire the cdashdisplay
|
||||||
displayIF, err := lc.DevService.GetDevice(cdashdisplay.NAME)
|
displayIF, err := lc.DevService.GetPeripheral(cdashdisplay.NAME)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
||||||
return
|
return
|
||||||
@@ -347,7 +347,7 @@ func (lc *LayoutController) displayLoadedLayouts() {
|
|||||||
lc.Logger.Debug("We want to view our layout!")
|
lc.Logger.Debug("We want to view our layout!")
|
||||||
|
|
||||||
// Acquire the cdashdisplay
|
// Acquire the cdashdisplay
|
||||||
displayIF, err := lc.DevService.GetDevice(cdashdisplay.NAME)
|
displayIF, err := lc.DevService.GetPeripheral(cdashdisplay.NAME)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
||||||
return
|
return
|
||||||
@@ -392,7 +392,7 @@ func (lc *LayoutController) getCurrentTreeNodeModel() (*tview.TreeNode, int16, e
|
|||||||
|
|
||||||
func (lc *LayoutController) loadLayout() {
|
func (lc *LayoutController) loadLayout() {
|
||||||
// Acquire the cdashdisplay
|
// Acquire the cdashdisplay
|
||||||
displayIF, err := lc.DevService.GetDevice(cdashdisplay.NAME)
|
displayIF, err := lc.DevService.GetPeripheral(cdashdisplay.NAME)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
||||||
return
|
return
|
||||||
@@ -416,7 +416,7 @@ func (lc *LayoutController) loadLayout() {
|
|||||||
|
|
||||||
func (lc *LayoutController) unloadLayout() {
|
func (lc *LayoutController) unloadLayout() {
|
||||||
// Acquire the cdashdisplay
|
// Acquire the cdashdisplay
|
||||||
displayIF, err := lc.DevService.GetDevice(cdashdisplay.NAME)
|
displayIF, err := lc.DevService.GetPeripheral(cdashdisplay.NAME)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
||||||
return
|
return
|
||||||
@@ -437,7 +437,7 @@ func (lc *LayoutController) unloadLayout() {
|
|||||||
|
|
||||||
func (lc *LayoutController) saveLayout() {
|
func (lc *LayoutController) saveLayout() {
|
||||||
// Acquire the cdashdisplay
|
// Acquire the cdashdisplay
|
||||||
displayIF, err := lc.DevService.GetDevice(cdashdisplay.NAME)
|
displayIF, err := lc.DevService.GetPeripheral(cdashdisplay.NAME)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
||||||
return
|
return
|
||||||
@@ -470,7 +470,7 @@ func (lc *LayoutController) deleteWindow() {
|
|||||||
wID := curNode.GetReference().(int16)
|
wID := curNode.GetReference().(int16)
|
||||||
|
|
||||||
// Acquire the cdashdisplay
|
// Acquire the cdashdisplay
|
||||||
displayIF, err := lc.DevService.GetDevice(cdashdisplay.NAME)
|
displayIF, err := lc.DevService.GetPeripheral(cdashdisplay.NAME)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ func (lc *LayoutController) handleMovementCapture(idx int16,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Acquire the cdashdisplay
|
// Acquire the cdashdisplay
|
||||||
displayIF, err := lc.DevService.GetDevice(cdashdisplay.NAME)
|
displayIF, err := lc.DevService.GetPeripheral(cdashdisplay.NAME)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
||||||
return nil
|
return nil
|
||||||
@@ -86,7 +86,7 @@ func (lc *LayoutController) handleResizeCapture(idx int16,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Acquire the cdashdisplay
|
// Acquire the cdashdisplay
|
||||||
displayIF, err := lc.DevService.GetDevice(cdashdisplay.NAME)
|
displayIF, err := lc.DevService.GetPeripheral(cdashdisplay.NAME)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
lc.Messages <- "failed to get " + cdashdisplay.NAME
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ func (mc *DeviceController) AddDeviceAPIListItems() {
|
|||||||
mc.DeviceAPIView.DevAPIList.
|
mc.DeviceAPIView.DevAPIList.
|
||||||
AddItem("layout", "build a layout for CDashDisplay", func() {
|
AddItem("layout", "build a layout for CDashDisplay", func() {
|
||||||
// This CDashDisplay specific, only load if we have a CDashDisplay
|
// This CDashDisplay specific, only load if we have a CDashDisplay
|
||||||
if !mc.DevService.DeviceExists(cdashdisplay.NAME) {
|
if !mc.DevService.PeripheralExists(cdashdisplay.NAME) {
|
||||||
mc.DevService.Messages <- "CDashDisplay it not loaded yet\n"
|
mc.DevService.Messages <- "CDashDisplay it not loaded yet\n"
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -110,7 +110,7 @@ func (sc *StreamingCtrl) StartStop() {
|
|||||||
slog.Debug("setting the data stream for device servie")
|
slog.Debug("setting the data stream for device servie")
|
||||||
sc.DevService.SetTelemetryChannel(sc.TelemServ.SubscribeListener("DeviceService", 1))
|
sc.DevService.SetTelemetryChannel(sc.TelemServ.SubscribeListener("DeviceService", 1))
|
||||||
|
|
||||||
dev, err := sc.DevService.GetDevice(uidevice.NAME)
|
dev, err := sc.DevService.GetPeripheral(uidevice.NAME)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if uiDev, ok := dev.(*uidevice.UIDevice); ok {
|
if uiDev, ok := dev.(*uidevice.UIDevice); ok {
|
||||||
sc.TelemetryCh = uiDev.DataChannel()
|
sc.TelemetryCh = uiDev.DataChannel()
|
||||||
|
|||||||
Reference in New Issue
Block a user