Created a devices service and moved the CDashDisplay service to the sahdow realm
This commit is contained in:
+119
-136
@@ -1,148 +1,131 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"sync/atomic"
|
||||
// type CDashService struct {
|
||||
// Logger *slog.Logger
|
||||
// CDash *cdashdisplay.CDashDisplay
|
||||
// // DevClerk *peripheral.PeripheralDeviceClerk
|
||||
// // Messages chan string
|
||||
// // Telemetry Channel
|
||||
// // streamCancel context.CancelFunc
|
||||
// // TelemCh <-chan telemetry.TelemetryData
|
||||
// }
|
||||
|
||||
"esdi/cdashdisplay"
|
||||
helper "esdi/helpers"
|
||||
"esdi/peripheral"
|
||||
"esdi/telemetry"
|
||||
)
|
||||
// func NewCDashService(logger *slog.Logger) *CDashService {
|
||||
// // sharedChannel := make(chan string, 10)
|
||||
// return &CDashService{
|
||||
// Logger: logger,
|
||||
// CDash: nil,
|
||||
// // DevClerk: peripheral.NewPeripheralDeviceClerk(),
|
||||
// // Messages: sharedChannel,
|
||||
// }
|
||||
// }
|
||||
|
||||
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 (ds *CDashService) FindDevice() {
|
||||
// ds.Messages <- "looking for cdash display...\n"
|
||||
// ds.Logger.Info("Looking for CDashDisplay")
|
||||
|
||||
func NewCDashService(logger *slog.Logger) *CDashService {
|
||||
sharedChannel := make(chan string, 10)
|
||||
return &CDashService{
|
||||
Logger: logger,
|
||||
CDash: nil,
|
||||
DevClerk: peripheral.NewPeripheralDeviceClerk(),
|
||||
Messages: sharedChannel,
|
||||
}
|
||||
}
|
||||
// cdashdisplay.SetLogger(ds.Logger.With("[device]", "cdashdisplay"))
|
||||
|
||||
func (cds *CDashService) FindDevice() {
|
||||
cds.Messages <- "looking for cdash display...\n"
|
||||
cds.Logger.Info("Looking for CDashDisplay")
|
||||
// display, err := cdashdisplay.Discover()
|
||||
// if err != nil {
|
||||
// ds.Logger.Info("didn't find cdashdisplay")
|
||||
// ds.Messages <- "didn't find cdash display\n"
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// ds.CDash = display
|
||||
// ds.Logger.Info("found cdashdisplay on: " + display.WT.Cfg.Name)
|
||||
// ds.Messages <- "found cdashdisplay on: " + display.WT.Cfg.Name + "\n"
|
||||
// }
|
||||
|
||||
cdashdisplay.SetLogger(cds.Logger.With("[device]", "cdashdisplay"))
|
||||
// func (ds *CDashService) CreateWindow(
|
||||
// win *cdashdisplay.DesktopUIWindow,
|
||||
// ) (*cdashdisplay.DesktopUIWindow, error) {
|
||||
// updatedWindow, err := ds.CDash.CreateWindow(win)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
//
|
||||
// return updatedWindow, nil
|
||||
// }
|
||||
//
|
||||
// func (ds *CDashService) LoadLayout(layoutPath string) error {
|
||||
// return ds.CDash.LoadLayout(layoutPath)
|
||||
// }
|
||||
//
|
||||
// func (ds *CDashService) SaveLayout(layoutPath string) error {
|
||||
// return ds.CDash.SaveLayout(layoutPath)
|
||||
// }
|
||||
//
|
||||
// func (ds *CDashService) UnloadLayout() error {
|
||||
// return ds.CDash.UnloadLayout()
|
||||
// }
|
||||
//
|
||||
// func (ds *CDashService) UpdateWindow(win *cdashdisplay.DesktopUIWindow) error {
|
||||
// ds.Messages <- fmt.Sprintf("Updating a window:\n%+v\n", win)
|
||||
// return ds.CDash.UpdateWindow(win)
|
||||
// }
|
||||
//
|
||||
// func (ds *CDashService) DeleteWindow(idx int16) error {
|
||||
// return ds.CDash.DestroyWindow(idx)
|
||||
// }
|
||||
//
|
||||
// func (ds *CDashService) ResizeWindow(idx int16, vec *helper.Vector) error {
|
||||
// err := ds.CDash.ResizeWindow(idx, vec)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
//
|
||||
// return nil
|
||||
// }
|
||||
//
|
||||
// func (ds *CDashService) MoveWindow(idx int16, vec *helper.Vector) error {
|
||||
// return ds.CDash.MoveWindow(idx, vec)
|
||||
// }
|
||||
|
||||
display, err := cdashdisplay.NewCDashDisplay()
|
||||
if err != nil {
|
||||
cds.Logger.Info("didn't find cdashdisplay")
|
||||
cds.Messages <- "didn't find cdash display\n"
|
||||
return
|
||||
}
|
||||
// func (ds *CDashService) SetTelemetryChannel(ch <-chan telemetry.TelemetryData) {
|
||||
// ds.TelemCh = ch
|
||||
// }
|
||||
|
||||
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
|
||||
}
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
// func (ds *CDashService) 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)
|
||||
//
|
||||
// ds.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,123 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"sync/atomic"
|
||||
|
||||
"esdi/devices"
|
||||
"esdi/devices/cdashdisplay"
|
||||
"esdi/telemetry"
|
||||
)
|
||||
|
||||
// DeviceService will handle sending the data from the telemetry service to the
|
||||
// actual devices
|
||||
// NOTE: create a virtual device and make it be the output window or something so
|
||||
// we can just add it as a device or whatever instead of being a custom made thing
|
||||
// that would be pretty cool I think
|
||||
type DeviceService struct {
|
||||
Logger *slog.Logger
|
||||
Devices map[string]devices.Device
|
||||
// Strem handling
|
||||
streamCancel context.CancelFunc
|
||||
TelemCh <-chan telemetry.TelemetryData
|
||||
// Output
|
||||
Messages chan string
|
||||
}
|
||||
|
||||
func NewDeviceService(logger *slog.Logger) *DeviceService {
|
||||
sharedChannel := make(chan string, 10)
|
||||
return &DeviceService{
|
||||
Devices: make(map[string]devices.Device),
|
||||
Logger: logger,
|
||||
Messages: sharedChannel,
|
||||
}
|
||||
}
|
||||
|
||||
func (ds *DeviceService) FindDevices() {
|
||||
// Need to define a list of devices to search for
|
||||
// For now lets just try to find our cdashdisplay - will think about the rest later
|
||||
|
||||
// Find CDashDisplay
|
||||
{
|
||||
ds.Messages <- "looking for " + cdashdisplay.Name + "...\n"
|
||||
ds.Logger.Info("Looking for " + cdashdisplay.Name)
|
||||
|
||||
cdashdisplay.SetLogger(ds.Logger.With("[device]", cdashdisplay.Name))
|
||||
|
||||
// Create a cdashdisplay
|
||||
display, err := cdashdisplay.Discover()
|
||||
if err == nil {
|
||||
ds.Devices[cdashdisplay.Name] = display
|
||||
ds.Logger.Info("found " + cdashdisplay.Name + " on: " + display.WT.Cfg.Name)
|
||||
ds.Messages <- "found " + cdashdisplay.Name + " on: " + display.WT.Cfg.Name + "\n"
|
||||
return
|
||||
}
|
||||
|
||||
ds.Logger.Info("didn't find " + cdashdisplay.Name)
|
||||
ds.Messages <- "didn't find " + cdashdisplay.Name + "\n"
|
||||
// No CDashDisplay available for one reason or another, so we don't set the
|
||||
// key
|
||||
}
|
||||
}
|
||||
|
||||
func (ds *DeviceService) GetDevice(name string) (devices.Device, error) {
|
||||
val, ok := ds.Devices[name]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("device `%s` couldn't be found", name)
|
||||
}
|
||||
|
||||
return val, nil
|
||||
}
|
||||
|
||||
func (ds *DeviceService) StartStream() {
|
||||
// NOTE: i'm using this pattern a whole lot. Maybe I can create a struct to handle this
|
||||
var ctx context.Context
|
||||
ctx, ds.streamCancel = context.WithCancel(context.Background())
|
||||
|
||||
go ds.transmit(ctx)
|
||||
}
|
||||
|
||||
func (ds *DeviceService) StopStream() {
|
||||
if ds.streamCancel == nil {
|
||||
return
|
||||
}
|
||||
|
||||
ds.streamCancel()
|
||||
ds.streamCancel = nil
|
||||
}
|
||||
|
||||
// SetTelemetryChannel sets the TelemCh to the passed channel
|
||||
func (ds *DeviceService) SetTelemetryChannel(ch <-chan telemetry.TelemetryData) {
|
||||
ds.TelemCh = ch
|
||||
}
|
||||
|
||||
// transmit will send the data to the devices themselves
|
||||
func (ds *DeviceService) transmit(ctx context.Context) {
|
||||
var isSending atomic.Bool
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case data, ok := <-ds.TelemCh:
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
if isSending.Load() {
|
||||
continue
|
||||
}
|
||||
|
||||
isSending.Store(true)
|
||||
|
||||
// TODO: make a copy of the data and send that copy instead of keeping
|
||||
// the data locked
|
||||
for _, dev := range ds.Devices {
|
||||
dev.SendData(&data)
|
||||
}
|
||||
isSending.Store(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,8 +14,8 @@ import (
|
||||
// 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
|
||||
type TelemetryService struct {
|
||||
logger *slog.Logger
|
||||
cdash *CDashService
|
||||
logger *slog.Logger
|
||||
devService *DeviceService
|
||||
// Concurrency protection
|
||||
mut sync.RWMutex
|
||||
ativeProvider telem.TelemetryProvider
|
||||
@@ -24,13 +24,16 @@ type TelemetryService struct {
|
||||
cancelForward context.CancelFunc
|
||||
}
|
||||
|
||||
func NewTelemetryService(logger *slog.Logger, cdash *CDashService) *TelemetryService {
|
||||
func NewTelemetryService(logger *slog.Logger, devServo *DeviceService) *TelemetryService {
|
||||
newService := &TelemetryService{
|
||||
logger: logger,
|
||||
cdash: cdash,
|
||||
listeners: make(map[string]chan telem.TelemetryData),
|
||||
logger: logger,
|
||||
devService: devServo,
|
||||
listeners: make(map[string]chan telem.TelemetryData),
|
||||
}
|
||||
|
||||
// TODO: do not create and set a provider here, a background job should be
|
||||
// detecting providers instead
|
||||
|
||||
// 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{
|
||||
|
||||
Reference in New Issue
Block a user