diff --git a/hardware/FTDIFinder.go b/hardware/FTDIFinder.go index 7e3e0cb..bad2ef9 100644 --- a/hardware/FTDIFinder.go +++ b/hardware/FTDIFinder.go @@ -60,27 +60,24 @@ func (f *FTDIFinder) RegisterPeripheral(ctx context.Context, peripheralData Peri defer f.mu.Unlock() f.saved[peripheralData.SerialNumber] = peripheralData + runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusDisconnected) // If already detected, connect it if peripheral, ok := f.detected[peripheralData.SerialNumber]; ok { f.wg.Add(1) go func() { defer f.wg.Done() - 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 } - 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 } - runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusActivated) }() } @@ -102,14 +99,12 @@ func (f *FTDIFinder) UnregisterPeripheral(ctx context.Context, peripheralData Pe 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() + err = peripheral.Disconnect(ctx) if err != nil { log.Err(err).Str("sn", peripheralData.SerialNumber).Msg("unable to disconnect the peripheral") return nil } - runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), peripheralData, PeripheralStatusDisconnected) } delete(f.saved, peripheralData.SerialNumber) runtime.EventsEmit(ctx, "UNLOAD_PERIPHERAL", peripheralData) @@ -169,27 +164,25 @@ 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 - return make(map[string]interface{}), nil + peripheral, found := f.detected[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 } // SetPeripheralSettings sets the peripheral settings func (f *FTDIFinder) SetPeripheralSettings(ctx context.Context, peripheralID string, settings map[string]any) 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) - return nil + peripheral, found := f.detected[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) } // scanPeripherals scans the FTDI peripherals @@ -242,28 +235,21 @@ func (f *FTDIFinder) scanPeripherals(ctx context.Context) error { } 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 { f.wg.Add(1) go func(p PeripheralInfo) { defer f.wg.Done() - runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), p, PeripheralStatusConnecting) err := peripheral.Connect(ctx) if err != nil { - runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), p, PeripheralStatusDisconnected) log.Err(err).Str("sn", p.SerialNumber).Msg("unable to connect the FTDI peripheral") return } - runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), p, PeripheralStatusDeactivated) err = peripheral.Activate(ctx) if err != nil { log.Err(err).Str("sn", p.SerialNumber).Msg("unable to activate the FTDI peripheral") return } - runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), p, PeripheralStatusActivated) }(peripheralData) } } @@ -278,12 +264,10 @@ func (f *FTDIFinder) scanPeripherals(ctx context.Context) error { if err != nil { log.Err(err).Str("sn", sn).Msg("unable to deactivate the FTDI peripheral after disconnection") } - runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), oldPeripheral.GetInfo(), PeripheralStatusDeactivated) - err = oldPeripheral.Disconnect() + err = oldPeripheral.Disconnect(ctx) if err != nil { log.Err(err).Str("sn", sn).Msg("unable to disconnect the FTDI peripheral after disconnection") } - runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), oldPeripheral.GetInfo(), PeripheralStatusDisconnected) // Delete it from the detected list delete(f.detected, sn) diff --git a/hardware/FTDIPeripheral.go b/hardware/FTDIPeripheral.go index 8debace..ac9a088 100644 --- a/hardware/FTDIPeripheral.go +++ b/hardware/FTDIPeripheral.go @@ -8,6 +8,7 @@ import ( "github.com/pkg/errors" "github.com/rs/zerolog/log" + "github.com/wailsapp/wails/v2/pkg/runtime" ) /* @@ -41,6 +42,8 @@ func (p *FTDIPeripheral) Connect(ctx context.Context) error { return errors.Errorf("the DMX device has already been created!") } + runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), p.GetInfo(), PeripheralStatusConnecting) + // Create the DMX sender p.dmxSender = C.dmx_create() @@ -48,6 +51,7 @@ func (p *FTDIPeripheral) Connect(ctx context.Context) error { serialNumber := C.CString(p.info.SerialNumber) defer C.free(unsafe.Pointer(serialNumber)) if C.dmx_connect(p.dmxSender, serialNumber) != C.DMX_OK { + runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), p.GetInfo(), PeripheralStatusDisconnected) 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'", p.info.SerialNumber) } @@ -56,15 +60,16 @@ func (p *FTDIPeripheral) Connect(ctx context.Context) error { go func() { defer p.wg.Done() <-ctx.Done() - _ = p.Disconnect() + _ = p.Disconnect(ctx) }() + runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), p.GetInfo(), PeripheralStatusDeactivated) log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("DMX device connected successfully") return nil } // Disconnect disconnects the FTDI peripheral -func (p *FTDIPeripheral) Disconnect() error { +func (p *FTDIPeripheral) Disconnect(ctx context.Context) error { // Check if the device has already been created if p.dmxSender == nil { return errors.Errorf("the DMX device has not been connected!") @@ -75,6 +80,8 @@ func (p *FTDIPeripheral) Disconnect() error { // Reset the pointer to the peripheral p.dmxSender = nil + + runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), p.GetInfo(), PeripheralStatusDisconnected) return nil } @@ -96,6 +103,8 @@ func (p *FTDIPeripheral) Activate(ctx context.Context) error { 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(PeripheralStatusUpdated), p.GetInfo(), PeripheralStatusActivated) + log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("DMX device activated successfully") return nil } @@ -114,6 +123,7 @@ func (p *FTDIPeripheral) Deactivate(ctx context.Context) error { return errors.Errorf("unable to deactivate the DMX sender!") } + runtime.EventsEmit(ctx, string(PeripheralStatusUpdated), p.GetInfo(), PeripheralStatusDeactivated) log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("DMX device deactivated successfully") return nil }