added translations for toast messages

This commit is contained in:
2025-01-18 15:24:16 +01:00
parent ea325e23c5
commit 558b7354ca
9 changed files with 54 additions and 57 deletions

View File

@@ -196,7 +196,7 @@ func (f *FTDIFinder) scanPeripherals(ctx context.Context) error {
// The program output is like '0:1:2' where 0 is the location, 1 is the S/N and 2 is the name
peripheralInfo := strings.Split(peripheralString, ":")
log.Debug().Str("file", "FTDIFinder").Str("peripheralName", peripheralInfo[2]).Str("peripheralSN", peripheralInfo[1]).Msg("new FTDI peripheral detected")
log.Trace().Str("file", "FTDIFinder").Str("scannedString", peripheralString).Str("peripheralName", peripheralInfo[2]).Str("peripheralSN", peripheralInfo[1]).Msg("new FTDI peripheral detected")
// Convert the location to an integer
location, err := strconv.Atoi(peripheralInfo[0])
if err != nil {
@@ -208,16 +208,13 @@ func (f *FTDIFinder) scanPeripherals(ctx context.Context) error {
if err != nil {
return fmt.Errorf("unable to create the FTDI peripheral: %v", err)
}
log.Trace().Any("periph", &peripheral).Str("file", "FTDIFinder").Str("peripheralName", peripheralInfo[2]).Msg("has been created")
ftdiPeripherals[peripheralInfo[1]] = peripheral
log.Trace().Str("file", "FTDIFinder").Str("peripheralName", peripheralInfo[2]).Msg("successfully added the FTDI peripheral to the finder")
log.Trace().Any("periph", ftdiPeripherals).Str("file", "FTDIFinder").Str("peripheralName", peripheralInfo[2]).Msg("successfully added the FTDI peripheral to the finder")
}
// Compare with the current peripherals to detect arrivals/removals
removedList, addedList := comparePeripherals(f.peripherals, ftdiPeripherals)
// Emit the events
emitPeripheralsEvents(ctx, removedList, PeripheralRemoval)
log.Info().Str("file", "FTDIFinder").Msg("FTDI remove list emitted to the front")
emitPeripheralsEvents(ctx, addedList, PeripheralArrival)
log.Info().Str("file", "FTDIFinder").Msg("FTDI add list emitted to the front")
// Emit the peripherals changes to the front
emitPeripheralsChanges(ctx, f.peripherals, ftdiPeripherals)
// Store the new peripherals list
f.peripherals = ftdiPeripherals
return nil

View File

@@ -152,11 +152,11 @@ func (f *MIDIFinder) scanPeripherals(ctx context.Context) error {
midiPeripherals[sn] = NewMIDIPeripheral(name, location, sn)
}
// Compare with the current peripherals to detect arrivals/removals
removedList, addedList := comparePeripherals(f.peripherals, midiPeripherals)
// removedList, addedList := comparePeripherals(f.peripherals, midiPeripherals)
// Emit the events
emitPeripheralsEvents(ctx, removedList, PeripheralRemoval)
// emitPeripheralsEvents(ctx, removedList, PeripheralRemoval)
log.Info().Str("file", "MIDIFinder").Msg("MIDI remove list emitted to the front")
emitPeripheralsEvents(ctx, addedList, PeripheralArrival)
// emitPeripheralsEvents(ctx, addedList, PeripheralArrival)
log.Info().Str("file", "MIDIFinder").Msg("MIDI add list emitted to the front")
// Store the new peripherals list
f.peripherals = midiPeripherals

View File

@@ -129,37 +129,23 @@ func (h *HardwareManager) Stop() error {
return nil
}
// peripheralsList emits a peripheral event
func emitPeripheralsEvents(ctx context.Context, peripheralsList map[string]Peripheral, peripheralEvent PeripheralEvent) {
for _, peripheral := range peripheralsList {
runtime.EventsEmit(ctx, string(peripheralEvent), peripheral.GetInfo())
log.Trace().Str("file", "hardware").Str("event", string(peripheralEvent)).Msg("emit peripheral event")
}
}
// emitPeripheralsChanges compares the old and new peripherals to determine which ones have been added or removed.
func emitPeripheralsChanges(ctx context.Context, oldPeripherals map[string]Peripheral, newPeripherals map[string]Peripheral) {
log.Trace().Any("oldList", oldPeripherals).Any("newList", newPeripherals).Msg("emitting peripherals changes to the front")
// comparePeripherals compares the peripherals to determine which has been inserted or removed
func comparePeripherals(oldPeripherals map[string]Peripheral, newPeripherals map[string]Peripheral) (map[string]Peripheral, map[string]Peripheral) {
// Duplicate the lists
oldList := make(map[string]Peripheral)
newList := make(map[string]Peripheral)
for key, value := range oldPeripherals {
oldList[key] = value
}
log.Trace().Str("file", "hardware").Any("oldList", oldList).Msg("peripheral oldList comparison")
for key, value := range newPeripherals {
newList[key] = value
}
log.Trace().Str("file", "hardware").Any("newList", newList).Msg("peripheral newList comparison")
// Remove in these lists all the commons peripherals
for key := range newList {
if _, exists := oldList[key]; exists {
delete(oldList, key)
delete(newList, key)
// 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].GetInfo())
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].GetInfo())
log.Trace().Str("file", "hardware").Str("event", string(PeripheralArrival)).Msg("emit peripheral arrival event")
}
}
// Now the old list contains the removed peripherals, and the new list contains the added peripherals
log.Trace().Str("file", "hardware").Any("oldList", oldList).Msg("peripheral oldList computed")
log.Trace().Str("file", "hardware").Any("newList", newList).Msg("peripheral newList computed")
return oldList, newList
}

View File

@@ -8,6 +8,7 @@ int main() {
FT_HANDLE ftHandle = nullptr;
FT_DEVICE_LIST_INFO_NODE *devInfo;
DWORD numDevs;
// create the device information list
ftStatus = FT_CreateDeviceInfoList(&numDevs);
if (ftStatus != FT_OK) {