Files
esdi/tui/internal/services/cdashdisplay.go
T

108 lines
2.6 KiB
Go

package services
import (
"esdi/cdashdisplay"
helper "esdi/helpers"
"esdi/peripheral"
"esdi/tui/internal/models"
"log/slog"
)
type CDashService struct {
Logger *slog.Logger
CDash *cdashdisplay.CDashDisplay
iRacingTelemetry *IRacingService
DevClerk *peripheral.PeripheralDeviceClerk
Messages chan string
}
func NewCDashService(logger *slog.Logger) *CDashService {
sharedChannel := make(chan string, 10)
return &CDashService{
Logger: logger,
CDash: nil,
DevClerk: peripheral.NewPeripheralDeviceClerk(),
Messages: sharedChannel,
iRacingTelemetry: NewIRacingService(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.UIWindow) (int16, error) {
wID, err := cds.CDash.CreateWindow(*win)
if err != nil {
return -1, err
}
return wID, 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) UpdateWindow(idx int16, win *cdashdisplay.UIWindow) error {
return cds.CDash.UpdateWindow(idx, win)
}
func (cds *CDashService) DeleteWindow(idx int16) error {
return cds.CDash.DestroyWindow(idx)
}
func (cds *CDashService) ResizeWindow(win *models.UIWindow, vec *helper.Vector) error {
err := cds.CDash.ResizeWindow(win.IDX, vec)
if err != nil {
return err
}
// Update the UI window data
win.Window = *cds.CDash.State.Layout.Windows[win.IDX]
return nil
}
func (cds *CDashService) MoveWindow(win *models.UIWindow, vec *helper.Vector) error {
err := cds.CDash.MoveWindow(win.IDX, vec)
if err != nil {
return err
}
// Update the UI window data
win.Window = *cds.CDash.State.Layout.Windows[win.IDX]
return nil
}
func (cds *CDashService) StartStream() {
cds.iRacingTelemetry.StartStream()
}
func (cds *CDashService) GetStream() <-chan string {
return cds.iRacingTelemetry.GetStream()
}
func (cds *CDashService) StopStream() {
cds.iRacingTelemetry.StopStream()
}