generated from thinkode/modelRepository
24-project-life (#28)
Fix peripherals states and implements plug and reload feature. Graphics improvements. Stability improvements. Reviewed-on: #28
This commit was merged in pull request #28.
This commit is contained in:
@@ -2,8 +2,6 @@ package hardware
|
||||
|
||||
import (
|
||||
"context"
|
||||
_ "embed"
|
||||
"errors"
|
||||
"fmt"
|
||||
goRuntime "runtime"
|
||||
"sync"
|
||||
@@ -11,6 +9,7 @@ import (
|
||||
"unsafe"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/wailsapp/wails/v2/pkg/runtime"
|
||||
)
|
||||
|
||||
/*
|
||||
@@ -23,65 +22,93 @@ import "C"
|
||||
// FTDIFinder manages all the FTDI peripherals
|
||||
type FTDIFinder struct {
|
||||
wg sync.WaitGroup
|
||||
mu sync.Mutex
|
||||
|
||||
findTicker *time.Ticker // Peripherals find ticker
|
||||
foundPeripherals map[string]PeripheralInfo // The list of peripherals handled by this finder
|
||||
registeredPeripherals map[string]*FTDIPeripheral // The list of found peripherals
|
||||
scanChannel chan struct{} // The channel to trigger a scan event
|
||||
saved map[string]PeripheralInfo // Peripherals saved in the project
|
||||
detected map[string]*FTDIPeripheral // Detected peripherals
|
||||
|
||||
scanEvery time.Duration // Scans peripherals periodically
|
||||
|
||||
onArrival func(p PeripheralInfo) // When a peripheral arrives
|
||||
onRemoval func(p PeripheralInfo) // When a peripheral goes away
|
||||
}
|
||||
|
||||
// NewFTDIFinder creates a new FTDI finder
|
||||
func NewFTDIFinder(findPeriod time.Duration) *FTDIFinder {
|
||||
func NewFTDIFinder(scanEvery time.Duration) *FTDIFinder {
|
||||
log.Trace().Str("file", "FTDIFinder").Msg("FTDI finder created")
|
||||
return &FTDIFinder{
|
||||
findTicker: time.NewTicker(findPeriod),
|
||||
foundPeripherals: make(map[string]PeripheralInfo),
|
||||
registeredPeripherals: make(map[string]*FTDIPeripheral),
|
||||
scanChannel: make(chan struct{}),
|
||||
scanEvery: scanEvery,
|
||||
saved: make(map[string]PeripheralInfo),
|
||||
detected: make(map[string]*FTDIPeripheral),
|
||||
}
|
||||
}
|
||||
|
||||
// OnArrival is the callback function when a new peripheral arrives
|
||||
func (f *FTDIFinder) OnArrival(cb func(p PeripheralInfo)) {
|
||||
f.onArrival = cb
|
||||
}
|
||||
|
||||
// OnRemoval i the callback when a peripheral goes away
|
||||
func (f *FTDIFinder) OnRemoval(cb func(p PeripheralInfo)) {
|
||||
f.onRemoval = cb
|
||||
}
|
||||
|
||||
// RegisterPeripheral registers a new peripheral
|
||||
func (f *FTDIFinder) RegisterPeripheral(ctx context.Context, peripheralData PeripheralInfo) (string, error) {
|
||||
// Create a new FTDI peripheral
|
||||
ftdiPeripheral, err := NewFTDIPeripheral(peripheralData)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("unable to create the FTDI peripheral: %v", err)
|
||||
}
|
||||
// Register it in the finder
|
||||
f.registeredPeripherals[peripheralData.SerialNumber] = ftdiPeripheral
|
||||
log.Trace().Any("periph", &ftdiPeripheral).Str("file", "FTDIFinder").Str("peripheralName", peripheralData.Name).Msg("FTDI peripheral has been created")
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
|
||||
// Peripheral created, connect it
|
||||
err = ftdiPeripheral.Connect(ctx)
|
||||
if err != nil {
|
||||
log.Err(err).Str("file", "FTDIFinder").Str("peripheralSN", peripheralData.SerialNumber).Msg("unable to connect the peripheral")
|
||||
f.saved[peripheralData.SerialNumber] = peripheralData
|
||||
|
||||
// If already detected, connect it
|
||||
if peripheral, ok := f.detected[peripheralData.SerialNumber]; ok {
|
||||
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusConnecting)
|
||||
err := peripheral.Connect(ctx)
|
||||
if err != nil {
|
||||
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusDisconnected)
|
||||
log.Err(err).Str("file", "FTDIFinder").Str("peripheralSN", peripheralData.SerialNumber).Msg("unable to connect the peripheral")
|
||||
return peripheralData.SerialNumber, nil
|
||||
}
|
||||
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusDeactivated)
|
||||
// Peripheral connected, activate it
|
||||
err = peripheral.Activate(ctx)
|
||||
if err != nil {
|
||||
log.Err(err).Str("file", "FTDIFinder").Str("peripheralSN", peripheralData.SerialNumber).Msg("unable to activate the FTDI peripheral")
|
||||
return peripheralData.SerialNumber, nil
|
||||
}
|
||||
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusActivated)
|
||||
}
|
||||
// Peripheral connected, activate it
|
||||
err = ftdiPeripheral.Activate(ctx)
|
||||
if err != nil {
|
||||
log.Err(err).Str("file", "FTDIFinder").Str("peripheralSN", peripheralData.SerialNumber).Msg("unable to activate the peripheral")
|
||||
}
|
||||
// Peripheral activated
|
||||
|
||||
// Emits the event in the hardware
|
||||
runtime.EventsEmit(ctx, "LOAD_PERIPHERAL", peripheralData)
|
||||
|
||||
return peripheralData.SerialNumber, nil
|
||||
}
|
||||
|
||||
// UnregisterPeripheral unregisters an existing peripheral
|
||||
func (f *FTDIFinder) UnregisterPeripheral(ctx context.Context, peripheralID string) error {
|
||||
peripheral, registered := f.registeredPeripherals[peripheralID]
|
||||
if registered {
|
||||
func (f *FTDIFinder) UnregisterPeripheral(ctx context.Context, peripheralData PeripheralInfo) error {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
|
||||
if peripheral, detected := f.detected[peripheralData.SerialNumber]; detected {
|
||||
// Deactivating peripheral
|
||||
err := peripheral.Deactivate(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
log.Err(err).Str("sn", peripheralData.SerialNumber).Msg("unable to deactivate the peripheral")
|
||||
return nil
|
||||
}
|
||||
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusDeactivated)
|
||||
// Disconnecting peripheral
|
||||
err = peripheral.Disconnect()
|
||||
if err != nil {
|
||||
return err
|
||||
log.Err(err).Str("sn", peripheralData.SerialNumber).Msg("unable to disconnect the peripheral")
|
||||
return nil
|
||||
}
|
||||
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusDisconnected)
|
||||
}
|
||||
delete(f.registeredPeripherals, peripheralID)
|
||||
delete(f.saved, peripheralData.SerialNumber)
|
||||
runtime.EventsEmit(ctx, "UNLOAD_PERIPHERAL", peripheralData)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -100,18 +127,15 @@ func (f *FTDIFinder) Initialize() error {
|
||||
func (f *FTDIFinder) Start(ctx context.Context) error {
|
||||
f.wg.Add(1)
|
||||
go func() {
|
||||
ticker := time.NewTicker(f.scanEvery)
|
||||
defer ticker.Stop()
|
||||
defer f.wg.Done()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-f.findTicker.C:
|
||||
// Scan the peripherals
|
||||
err := f.scanPeripherals(ctx)
|
||||
if err != nil {
|
||||
log.Err(err).Str("file", "FTDIFinder").Msg("unable to scan FTDI peripherals")
|
||||
}
|
||||
case <-f.scanChannel:
|
||||
case <-ticker.C:
|
||||
// Scan the peripherals
|
||||
err := f.scanPeripherals(ctx)
|
||||
if err != nil {
|
||||
@@ -123,13 +147,13 @@ func (f *FTDIFinder) Start(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ForceScan explicily asks for scanning peripherals
|
||||
// ForceScan explicitly asks for scanning peripherals
|
||||
func (f *FTDIFinder) ForceScan() {
|
||||
select {
|
||||
case f.scanChannel <- struct{}{}:
|
||||
default:
|
||||
// Ignore if the channel is full or if it is closed
|
||||
}
|
||||
// select {
|
||||
// case f.scanChannel <- struct{}{}:
|
||||
// default:
|
||||
// // Ignore if the channel is full or if it is closed
|
||||
// }
|
||||
}
|
||||
|
||||
// GetName returns the name of the driver
|
||||
@@ -140,25 +164,27 @@ func (f *FTDIFinder) GetName() string {
|
||||
// GetPeripheralSettings gets the peripheral settings
|
||||
func (f *FTDIFinder) GetPeripheralSettings(peripheralID string) (map[string]interface{}, error) {
|
||||
// Return the specified peripheral
|
||||
peripheral, found := f.registeredPeripherals[peripheralID]
|
||||
if !found {
|
||||
log.Error().Str("file", "FTDIFinder").Str("peripheralID", peripheralID).Msg("unable to get this peripheral from the FTDI finder")
|
||||
return nil, fmt.Errorf("unable to found the peripheral")
|
||||
}
|
||||
log.Debug().Str("file", "FTDIFinder").Str("peripheralID", peripheralID).Msg("peripheral found by the FTDI finder")
|
||||
return peripheral.GetSettings(), nil
|
||||
// peripheral, found := f.registeredPeripherals[peripheralID]
|
||||
// if !found {
|
||||
// log.Error().Str("file", "FTDIFinder").Str("peripheralID", peripheralID).Msg("unable to get this peripheral from the FTDI finder")
|
||||
// return nil, fmt.Errorf("unable to found the peripheral")
|
||||
// }
|
||||
// log.Debug().Str("file", "FTDIFinder").Str("peripheralID", peripheralID).Msg("peripheral found by the FTDI finder")
|
||||
// return peripheral.GetSettings(), nil
|
||||
return make(map[string]interface{}), nil
|
||||
}
|
||||
|
||||
// SetPeripheralSettings sets the peripheral settings
|
||||
func (f *FTDIFinder) SetPeripheralSettings(peripheralID string, settings map[string]interface{}) error {
|
||||
// Return the specified peripheral
|
||||
peripheral, found := f.registeredPeripherals[peripheralID]
|
||||
if !found {
|
||||
log.Error().Str("file", "FTDIFinder").Str("peripheralID", peripheralID).Msg("unable to get this peripheral from the FTDI finder")
|
||||
return fmt.Errorf("unable to found the peripheral")
|
||||
}
|
||||
log.Debug().Str("file", "FTDIFinder").Str("peripheralID", peripheralID).Msg("peripheral found by the FTDI finder")
|
||||
return peripheral.SetSettings(settings)
|
||||
// peripheral, found := f.registeredPeripherals[peripheralID]
|
||||
// if !found {
|
||||
// log.Error().Str("file", "FTDIFinder").Str("peripheralID", peripheralID).Msg("unable to get this peripheral from the FTDI finder")
|
||||
// return fmt.Errorf("unable to found the peripheral")
|
||||
// }
|
||||
// log.Debug().Str("file", "FTDIFinder").Str("peripheralID", peripheralID).Msg("peripheral found by the FTDI finder")
|
||||
// return peripheral.SetSettings(settings)
|
||||
return nil
|
||||
}
|
||||
|
||||
// scanPeripherals scans the FTDI peripherals
|
||||
@@ -178,19 +204,19 @@ func (f *FTDIFinder) scanPeripherals(ctx context.Context) error {
|
||||
|
||||
C.get_ftdi_devices((*C.FTDIPeripheralC)(devicesPtr), C.int(count))
|
||||
|
||||
temporaryPeripherals := make(map[string]PeripheralInfo)
|
||||
currentMap := make(map[string]PeripheralInfo)
|
||||
|
||||
for i := 0; i < count; i++ {
|
||||
d := devices[i]
|
||||
|
||||
sn := C.GoString(d.serialNumber)
|
||||
desc := C.GoString(d.description)
|
||||
isOpen := d.isOpen != 0
|
||||
// isOpen := d.isOpen != 0
|
||||
|
||||
temporaryPeripherals[sn] = PeripheralInfo{
|
||||
currentMap[sn] = PeripheralInfo{
|
||||
SerialNumber: sn,
|
||||
Name: desc,
|
||||
IsOpen: isOpen,
|
||||
// IsOpen: isOpen,
|
||||
ProtocolName: "FTDI",
|
||||
}
|
||||
|
||||
@@ -198,12 +224,63 @@ func (f *FTDIFinder) scanPeripherals(ctx context.Context) error {
|
||||
C.free_ftdi_device(&d)
|
||||
}
|
||||
|
||||
log.Info().Any("peripherals", temporaryPeripherals).Msg("available FTDI peripherals")
|
||||
log.Info().Any("peripherals", currentMap).Msg("available FTDI peripherals")
|
||||
|
||||
// Emit the peripherals changes to the front
|
||||
emitPeripheralsChanges(ctx, f.foundPeripherals, temporaryPeripherals)
|
||||
// Store the new peripherals list
|
||||
f.foundPeripherals = temporaryPeripherals
|
||||
// Detect arrivals
|
||||
for sn, peripheralData := range currentMap {
|
||||
if _, known := f.detected[sn]; !known {
|
||||
peripheral := NewFTDIPeripheral(peripheralData)
|
||||
f.detected[sn] = peripheral
|
||||
|
||||
if f.onArrival != nil {
|
||||
go f.onArrival(peripheralData)
|
||||
}
|
||||
log.Info().Str("sn", sn).Str("name", peripheralData.Name).Msg("[FTDI] New peripheral detected")
|
||||
|
||||
// Disconnected by default
|
||||
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusDisconnected)
|
||||
|
||||
// If the peripheral is saved in the project => connect
|
||||
if _, saved := f.saved[sn]; saved {
|
||||
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusConnecting)
|
||||
err := peripheral.Connect(ctx)
|
||||
if err != nil {
|
||||
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusDisconnected)
|
||||
log.Err(err).Str("sn", sn).Msg("unable to connect the FTDI peripheral")
|
||||
return nil
|
||||
}
|
||||
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusDeactivated)
|
||||
err = peripheral.Activate(ctx)
|
||||
if err != nil {
|
||||
log.Err(err).Str("sn", sn).Msg("unable to activate the FTDI peripheral")
|
||||
return nil
|
||||
}
|
||||
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusActivated)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Detect removals
|
||||
for sn, oldPeripheral := range f.detected {
|
||||
if _, still := currentMap[sn]; !still {
|
||||
|
||||
// Properly clean the DMX device
|
||||
err := oldPeripheral.Disconnect()
|
||||
if err != nil {
|
||||
log.Err(err).Str("sn", sn).Msg("unable to clean the FTDI peripheral after disconnection")
|
||||
}
|
||||
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), oldPeripheral.GetInfo(), PeripheralStatusDisconnected)
|
||||
|
||||
// Delete it from the detected list
|
||||
delete(f.detected, sn)
|
||||
log.Info().Str("sn", sn).Str("name", oldPeripheral.GetInfo().Name).Msg("[FTDI] peripheral removed")
|
||||
|
||||
// Execute the removal callback
|
||||
if f.onRemoval != nil {
|
||||
go f.onRemoval(oldPeripheral.GetInfo())
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -211,29 +288,26 @@ func (f *FTDIFinder) scanPeripherals(ctx context.Context) error {
|
||||
func (f *FTDIFinder) WaitStop() error {
|
||||
log.Trace().Str("file", "FTDIFinder").Msg("stopping the FTDI finder...")
|
||||
|
||||
// Stop the ticker
|
||||
f.findTicker.Stop()
|
||||
|
||||
// Close the channel
|
||||
close(f.scanChannel)
|
||||
// close(f.scanChannel)
|
||||
|
||||
// Wait for all the peripherals to close
|
||||
log.Trace().Str("file", "FTDIFinder").Msg("closing all FTDI peripherals")
|
||||
var errs []error
|
||||
for registeredPeripheralSN, registeredPeripheral := range f.registeredPeripherals {
|
||||
err := registeredPeripheral.WaitStop()
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Errorf("%s: %w", registeredPeripheralSN, err))
|
||||
}
|
||||
}
|
||||
// log.Trace().Str("file", "FTDIFinder").Msg("closing all FTDI peripherals")
|
||||
// var errs []error
|
||||
// for registeredPeripheralSN, registeredPeripheral := range f.registeredPeripherals {
|
||||
// err := registeredPeripheral.WaitStop()
|
||||
// if err != nil {
|
||||
// errs = append(errs, fmt.Errorf("%s: %w", registeredPeripheralSN, err))
|
||||
// }
|
||||
// }
|
||||
|
||||
// Wait for goroutines to stop
|
||||
f.wg.Wait()
|
||||
|
||||
// Returning errors
|
||||
if len(errs) > 0 {
|
||||
return errors.Join(errs...)
|
||||
}
|
||||
// if len(errs) > 0 {
|
||||
// return errors.Join(errs...)
|
||||
// }
|
||||
log.Trace().Str("file", "FTDIFinder").Msg("FTDI finder stopped")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -2,14 +2,12 @@ package hardware
|
||||
|
||||
import (
|
||||
"context"
|
||||
_ "embed"
|
||||
"sync"
|
||||
|
||||
"unsafe"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/wailsapp/wails/v2/pkg/runtime"
|
||||
)
|
||||
|
||||
/*
|
||||
@@ -28,12 +26,12 @@ type FTDIPeripheral struct {
|
||||
}
|
||||
|
||||
// NewFTDIPeripheral creates a new FTDI peripheral
|
||||
func NewFTDIPeripheral(info PeripheralInfo) (*FTDIPeripheral, error) {
|
||||
func NewFTDIPeripheral(info PeripheralInfo) *FTDIPeripheral {
|
||||
log.Info().Str("file", "FTDIPeripheral").Str("name", info.Name).Str("s/n", info.SerialNumber).Msg("FTDI peripheral created")
|
||||
return &FTDIPeripheral{
|
||||
info: info,
|
||||
dmxSender: nil,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
// Connect connects the FTDI peripheral
|
||||
@@ -47,12 +45,11 @@ func (p *FTDIPeripheral) Connect(ctx context.Context) error {
|
||||
p.dmxSender = C.dmx_create()
|
||||
|
||||
// Connect the FTDI
|
||||
runtime.EventsEmit(ctx, string(PeripheralStatus), p.info, "connecting")
|
||||
serialNumber := C.CString(p.info.SerialNumber)
|
||||
defer C.free(unsafe.Pointer(serialNumber))
|
||||
if C.dmx_connect(p.dmxSender, serialNumber) != C.DMX_OK {
|
||||
log.Error().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("unable to connect the DMX device")
|
||||
return errors.Errorf("unable to connect '%s'")
|
||||
return errors.Errorf("unable to connect '%s'", p.info.SerialNumber)
|
||||
}
|
||||
|
||||
p.wg.Add(1)
|
||||
@@ -62,10 +59,7 @@ func (p *FTDIPeripheral) Connect(ctx context.Context) error {
|
||||
_ = p.Disconnect()
|
||||
}()
|
||||
|
||||
// Send deactivated state (connected but not activated for now)
|
||||
runtime.EventsEmit(ctx, string(PeripheralStatus), p.info, "deactivated")
|
||||
log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("DMX device connected successfully")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -78,6 +72,9 @@ func (p *FTDIPeripheral) Disconnect() error {
|
||||
|
||||
// Destroy the dmx sender
|
||||
C.dmx_destroy(p.dmxSender)
|
||||
|
||||
// Reset the pointer to the peripheral
|
||||
p.dmxSender = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -95,13 +92,11 @@ func (p *FTDIPeripheral) Activate(ctx context.Context) error {
|
||||
return errors.Errorf("unable to activate the DMX sender!")
|
||||
}
|
||||
|
||||
// Test only
|
||||
C.dmx_setValue(p.dmxSender, C.uint16_t(1), C.uint8_t(255))
|
||||
C.dmx_setValue(p.dmxSender, C.uint16_t(5), C.uint8_t(255))
|
||||
|
||||
runtime.EventsEmit(ctx, string(PeripheralStatus), p.info, "activated")
|
||||
|
||||
log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("DMX device activated successfully")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -120,7 +115,6 @@ func (p *FTDIPeripheral) Deactivate(ctx context.Context) error {
|
||||
}
|
||||
|
||||
log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("DMX device deactivated successfully")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -30,9 +30,9 @@ func NewOS2LPeripheral(peripheralData PeripheralInfo) (*OS2LPeripheral, error) {
|
||||
func (p *OS2LPeripheral) Connect(ctx context.Context) error {
|
||||
log.Info().Str("file", "OS2LPeripheral").Msg("OS2L peripheral connected")
|
||||
go func() {
|
||||
runtime.EventsEmit(ctx, string(PeripheralStatus), p.info, "connecting")
|
||||
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), p.info, "connecting")
|
||||
time.Sleep(5 * time.Second)
|
||||
runtime.EventsEmit(ctx, string(PeripheralStatus), p.info, "disconnected")
|
||||
runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), p.info, "disconnected")
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -7,21 +7,30 @@ import (
|
||||
"sync"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/wailsapp/wails/v2/pkg/runtime"
|
||||
)
|
||||
|
||||
// PeripheralEvent is trigger by the finders when the scan is complete
|
||||
type PeripheralEvent string
|
||||
|
||||
// PeripheralStatus is the peripheral status (DISCONNECTED => CONNECTING => DEACTIVATED => ACTIVATED)
|
||||
type PeripheralStatus string
|
||||
|
||||
const (
|
||||
// PeripheralArrival is triggerd when a peripheral has been connected to the system
|
||||
PeripheralArrival PeripheralEvent = "PERIPHERAL_ARRIVAL"
|
||||
// PeripheralRemoval is triggered when a peripheral has been disconnected from the system
|
||||
PeripheralRemoval PeripheralEvent = "PERIPHERAL_REMOVAL"
|
||||
// PeripheralStatus is triggered when a peripheral status has been updated (disconnected - connecting - connected)
|
||||
PeripheralStatus PeripheralEvent = "PERIPHERAL_STATUS"
|
||||
// debounceDuration = 500 * time.Millisecond
|
||||
// PeripheralStatusUpdated is triggered when a peripheral status has been updated (disconnected - connecting - connected)
|
||||
PeripheralStatusUpdated PeripheralEvent = "PERIPHERAL_STATUS"
|
||||
// PeripheralStatusDisconnected : peripheral is now disconnected
|
||||
PeripheralStatusDisconnected PeripheralStatus = "PERIPHERAL_DISCONNECTED"
|
||||
// PeripheralStatusConnecting : peripheral is now connecting
|
||||
PeripheralStatusConnecting PeripheralStatus = "PERIPHERAL_CONNECTING"
|
||||
// PeripheralStatusDeactivated : peripheral is now deactivated
|
||||
PeripheralStatusDeactivated PeripheralStatus = "PERIPHERAL_DEACTIVATED"
|
||||
// PeripheralStatusActivated : peripheral is now activated
|
||||
PeripheralStatusActivated PeripheralStatus = "PERIPHERAL_ACTIVATED"
|
||||
)
|
||||
|
||||
// HardwareManager is the class who manages the hardware
|
||||
@@ -45,13 +54,19 @@ func NewHardwareManager() *HardwareManager {
|
||||
|
||||
// Start starts to find new peripheral events
|
||||
func (h *HardwareManager) Start(ctx context.Context) error {
|
||||
// Initialize all the finders
|
||||
// Initialize all the finders and their callback functions
|
||||
for finderName, finder := range h.finders {
|
||||
err := finder.Initialize()
|
||||
if err != nil {
|
||||
log.Err(err).Str("file", "hardware").Str("finderName", finderName).Msg("unable to initialize finder")
|
||||
return err
|
||||
}
|
||||
finder.OnArrival(func(p PeripheralInfo) {
|
||||
runtime.EventsEmit(ctx, string(PeripheralArrival), p)
|
||||
})
|
||||
finder.OnRemoval(func(p PeripheralInfo) {
|
||||
runtime.EventsEmit(ctx, string(PeripheralRemoval), p)
|
||||
})
|
||||
err = finder.Start(ctx)
|
||||
if err != nil {
|
||||
log.Err(err).Str("file", "hardware").Str("finderName", finderName).Msg("unable to start finder")
|
||||
@@ -105,27 +120,6 @@ func (h *HardwareManager) Scan() error {
|
||||
}
|
||||
}
|
||||
|
||||
// emitPeripheralsChanges compares the old and new peripherals to determine which ones have been added or removed.
|
||||
func emitPeripheralsChanges(ctx context.Context, oldPeripherals map[string]PeripheralInfo, newPeripherals map[string]PeripheralInfo) {
|
||||
log.Trace().Any("oldList", oldPeripherals).Any("newList", newPeripherals).Msg("emitting peripherals changes to the front")
|
||||
|
||||
// Identify removed peripherals: present in the old list but not in the new list
|
||||
for oldPeriphName := range oldPeripherals {
|
||||
if _, exists := newPeripherals[oldPeriphName]; !exists {
|
||||
runtime.EventsEmit(ctx, string(PeripheralRemoval), oldPeripherals[oldPeriphName])
|
||||
log.Trace().Str("file", "hardware").Str("event", string(PeripheralRemoval)).Msg("emit peripheral removal event")
|
||||
}
|
||||
}
|
||||
|
||||
// Identify added peripherals: present in the new list but not in the old list
|
||||
for newPeriphName := range newPeripherals {
|
||||
if _, exists := oldPeripherals[newPeriphName]; !exists {
|
||||
runtime.EventsEmit(ctx, string(PeripheralArrival), newPeripherals[newPeriphName])
|
||||
log.Trace().Str("file", "hardware").Str("event", string(PeripheralArrival)).Msg("emit peripheral arrival event")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// WaitStop stops the hardware manager
|
||||
func (h *HardwareManager) WaitStop() error {
|
||||
log.Trace().Str("file", "hardware").Msg("closing the hardware manager")
|
||||
|
||||
@@ -6,7 +6,7 @@ import "context"
|
||||
type Peripheral interface {
|
||||
Connect(context.Context) error // Connect the peripheral
|
||||
IsConnected() bool // Return if the peripheral is connected or not
|
||||
Disconnect() error // Disconnect the peripheral
|
||||
Disconnect() error // Disconnect the peripheral
|
||||
Activate(context.Context) error // Activate the peripheral
|
||||
Deactivate(context.Context) error // Deactivate the peripheral
|
||||
SetSettings(map[string]interface{}) error // Set a peripheral setting
|
||||
@@ -18,21 +18,25 @@ type Peripheral interface {
|
||||
|
||||
// PeripheralInfo represents a peripheral information
|
||||
type PeripheralInfo struct {
|
||||
Name string `yaml:"name"` // Name of the peripheral
|
||||
SerialNumber string `yaml:"sn"` // S/N of the peripheral
|
||||
ProtocolName string `yaml:"protocol"` // Protocol name of the peripheral
|
||||
IsOpen bool // Open flag for peripheral connection
|
||||
Settings map[string]interface{} `yaml:"settings"` // Peripheral settings
|
||||
Name string `yaml:"name"` // Name of the peripheral
|
||||
SerialNumber string `yaml:"sn"` // S/N of the peripheral
|
||||
ProtocolName string `yaml:"protocol"` // Protocol name of the peripheral
|
||||
// IsConnected bool // If the peripheral is connected to the system
|
||||
// IsActivated bool // If the peripheral is activated in the project
|
||||
// IsDetected bool // If the peripheral is detected by the system
|
||||
Settings map[string]interface{} `yaml:"settings"` // Peripheral settings
|
||||
}
|
||||
|
||||
// PeripheralFinder represents how compatible peripheral drivers are implemented
|
||||
type PeripheralFinder interface {
|
||||
Initialize() error // Initializes the protocol
|
||||
OnArrival(cb func(p PeripheralInfo)) // Callback function when a peripheral arrives
|
||||
OnRemoval(cb func(p PeripheralInfo)) // Callback function when a peripheral goes away
|
||||
Start(context.Context) error // Start the detection
|
||||
WaitStop() error // Waiting for finder to close
|
||||
ForceScan() // Explicitly scans for peripherals
|
||||
RegisterPeripheral(context.Context, PeripheralInfo) (string, error) // Registers a new peripheral data
|
||||
UnregisterPeripheral(context.Context, string) error // Unregisters an existing peripheral
|
||||
UnregisterPeripheral(context.Context, PeripheralInfo) error // Unregisters an existing peripheral
|
||||
GetPeripheralSettings(string) (map[string]interface{}, error) // Gets the peripheral settings
|
||||
SetPeripheralSettings(string, map[string]interface{}) error // Sets the peripheral settings
|
||||
GetName() string // Get the name of the finder
|
||||
|
||||
Reference in New Issue
Block a user