diff --git a/frontend/src/components/Settings/InputsOutputsContent.svelte b/frontend/src/components/Settings/InputsOutputsContent.svelte index 1979784..415e380 100644 --- a/frontend/src/components/Settings/InputsOutputsContent.svelte +++ b/frontend/src/components/Settings/InputsOutputsContent.svelte @@ -5,7 +5,7 @@ import { t, _ } from 'svelte-i18n' import { generateToast, needProjectSave, peripherals } from "../../stores"; import { get } from "svelte/store" - import { UpdatePeripheralSetting, GetPeripheralSettings, AddOS2LPeripheral, RemovePeripheral, ConnectFTDI, ActivateFTDI, DeactivateFTDI, DisconnectFTDI, SetDeviceFTDI, AddPeripheral } from "../../../wailsjs/go/main/App"; + import { UpdatePeripheralSettings, GetPeripheralSettings, AddOS2LPeripheral, RemovePeripheral, ConnectFTDI, ActivateFTDI, DeactivateFTDI, DisconnectFTDI, SetDeviceFTDI, AddPeripheral } from "../../../wailsjs/go/main/App"; import RoundedButton from "../General/RoundedButton.svelte"; function ftdiConnect(){ @@ -122,7 +122,8 @@ GetPeripheralSettings(peripheral.ProtocolName, peripheral.SerialNumber).then((peripheralSettings) => { selectedPeripheralSettings = peripheralSettings }).catch((error) => { - generateToast('error', 'bx-error', $_("getPeripheralSettingsErrorToast")) + console.log("Unable to get the peripheral settings: " + error) + generateToast('danger', 'bx-error', $_("getPeripheralSettingsErrorToast")) }) // Select the current peripheral selectedPeripheralSN = peripheral.SerialNumber @@ -145,11 +146,20 @@ // Validate the peripheral settings function validate(settingName, settingValue){ console.log("Peripheral setting '" + settingName + "' set to '" + settingValue + "'") + // Get the old setting type and convert the new setting to this type + const convert = { + number: Number, + string: String, + boolean: Boolean, + }[typeof(selectedPeripheralSettings[settingName])] || (x => x) + selectedPeripheralSettings[settingName] = convert(settingValue) + console.log(typeof(selectedPeripheralSettings[settingName])) let peripheralProtocolName = get(peripherals)[selectedPeripheralSN].ProtocolName - UpdatePeripheralSetting(peripheralProtocolName, selectedPeripheralSN, settingName, settingValue).then(()=> { + UpdatePeripheralSettings(peripheralProtocolName, selectedPeripheralSN, selectedPeripheralSettings).then(()=> { $needProjectSave = true }).catch((error) => { - generateToast('error', 'bx-error', $_("peripheralSettingSaveErrorToast")) + console.log("Unable to save the peripheral setting: " + error) + generateToast('danger', 'bx-error', $_("peripheralSettingSaveErrorToast")) }) } diff --git a/frontend/src/lang/en.json b/frontend/src/lang/en.json index fbd8db9..7e7de71 100644 --- a/frontend/src/lang/en.json +++ b/frontend/src/lang/en.json @@ -57,9 +57,9 @@ "projectOpenedToast": "The project was opened:", "projectOpenErrorToast": "Unable to open the project", "projectCreatedToast": "The project was created", - "peripheralSettingSaveErrorToast": "Unable to save the peripheral setting", + "peripheralSettingSaveErrorToast": "Unable to save the peripheral settings", - "os2lIpSetting": "OS2L server IP", - "os2lPortSetting": "OS2L server port" + "os2lIp": "OS2L server IP", + "os2lPort": "OS2L server port" } diff --git a/hardware/FTDIPeripheral.go b/hardware/FTDIPeripheral.go index 74a8b37..a005cc9 100644 --- a/hardware/FTDIPeripheral.go +++ b/hardware/FTDIPeripheral.go @@ -171,9 +171,9 @@ func (p *FTDIPeripheral) Deactivate(ctx context.Context) error { return fmt.Errorf("unable to deactivate: not connected") } -// SetPeripheralSetting sets a specific setting for this peripheral -func (p *FTDIPeripheral) SetPeripheralSetting(settingName string, settingValue interface{}) error { - p.settings[settingName] = settingValue +// SetPeripheralSettings sets a specific setting for this peripheral +func (p *FTDIPeripheral) SetPeripheralSettings(settings map[string]interface{}) error { + p.settings = settings return nil } diff --git a/hardware/MIDIPeripheral.go b/hardware/MIDIPeripheral.go index ad8134d..e3083ba 100644 --- a/hardware/MIDIPeripheral.go +++ b/hardware/MIDIPeripheral.go @@ -46,9 +46,9 @@ func (p *MIDIPeripheral) Deactivate(ctx context.Context) error { return nil } -// SetPeripheralSetting sets a specific setting for this peripheral -func (p *MIDIPeripheral) SetPeripheralSetting(settingName string, settingValue interface{}) error { - p.settings[settingName] = settingValue +// SetPeripheralSettings sets a specific setting for this peripheral +func (p *MIDIPeripheral) SetPeripheralSettings(settings map[string]interface{}) error { + p.settings = settings return nil } diff --git a/hardware/OS2LPeripheral.go b/hardware/OS2LPeripheral.go index 72b942d..e6845e5 100644 --- a/hardware/OS2LPeripheral.go +++ b/hardware/OS2LPeripheral.go @@ -2,27 +2,27 @@ package hardware import ( "context" + "fmt" "github.com/rs/zerolog/log" ) // OS2LPeripheral contains the data of an OS2L peripheral type OS2LPeripheral struct { - name string // The name of the peripheral - serialNumber string // The serial number of the peripheral - settings map[string]interface{} // The settings of the peripheral + name string // The name of the peripheral + serialNumber string // The serial number of the peripheral + serverIP string // OS2L server IP + serverPort int // OS2L server port } // NewOS2LPeripheral creates a new OS2L peripheral func NewOS2LPeripheral(name string, serialNumber string) *OS2LPeripheral { log.Trace().Str("file", "OS2LPeripheral").Str("name", name).Str("s/n", serialNumber).Msg("OS2L peripheral created") - settings := make(map[string]interface{}) - settings["os2lIpSetting"] = "192.168.1.1" - settings["os2lPortSetting"] = 9995 return &OS2LPeripheral{ name: name, + serverIP: "127.0.0.1", + serverPort: 9995, serialNumber: serialNumber, - settings: settings, } } @@ -50,9 +50,33 @@ func (p *OS2LPeripheral) Deactivate(ctx context.Context) error { return nil } -// SetPeripheralSetting sets a specific setting for this peripheral -func (p *OS2LPeripheral) SetPeripheralSetting(settingName string, settingValue interface{}) error { - p.settings[settingName] = settingValue +// SetPeripheralSettings sets a specific setting for this peripheral +func (p *OS2LPeripheral) SetPeripheralSettings(settings map[string]interface{}) error { + // Check if the IP exists + serverIP, found := settings["os2lIp"] + if !found { + return fmt.Errorf("Unable to find the OS2L server IP") + } + // Check if it is a string + ipSetting, ok := serverIP.(string) + if ok { + p.serverIP = ipSetting + + } else { + return fmt.Errorf("The specified IP is not a string") + } + // Check if the port exists + serverPort, found := settings["os2lPort"] + if !found { + return fmt.Errorf("Unable to find the OS2L server port") + } + // Check if it is a float and convert to int + portFloat, ok := serverPort.(float64) + if ok { + p.serverPort = int(portFloat) + } else { + return fmt.Errorf("The specified port is not an int") + } return nil } @@ -63,7 +87,10 @@ func (p *OS2LPeripheral) SetDeviceProperty(context.Context, uint32, uint32, byte // GetSettings gets the peripheral settings func (p *OS2LPeripheral) GetSettings() map[string]interface{} { - return p.settings + return map[string]interface{}{ + "os2lIp": p.serverIP, + "os2lPort": p.serverPort, + } } // GetInfo gets the peripheral information diff --git a/hardware/interfaces.go b/hardware/interfaces.go index 2dbbee4..213c3e3 100644 --- a/hardware/interfaces.go +++ b/hardware/interfaces.go @@ -8,7 +8,7 @@ type Peripheral interface { Disconnect(context.Context) error // Disconnect the peripheral Activate(context.Context) error // Activate the peripheral Deactivate(context.Context) error // Deactivate the peripheral - SetPeripheralSetting(string, interface{}) error // Set a peripheral setting + SetPeripheralSettings(map[string]interface{}) error // Set a peripheral setting SetDeviceProperty(context.Context, uint32, uint32, byte) error // Update a device property GetInfo() PeripheralInfo // Get the peripheral information @@ -20,7 +20,7 @@ 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 - Settings map[string]interface{} `yaml:"settings"` // Number of DMX universes handled by the peripheral + Settings map[string]interface{} `yaml:"settings"` // Peripheral settings } // PeripheralFinder represents how compatible peripheral drivers are implemented diff --git a/peripherals.go b/peripherals.go index 4794180..d98ea46 100644 --- a/peripherals.go +++ b/peripherals.go @@ -16,6 +16,9 @@ func (a *App) AddPeripheral(protocolName string, peripheralID string) error { return fmt.Errorf("unable to found the peripheral ID '%s'", peripheralID) } // Add the peripheral ID to the project + if a.projectInfo.PeripheralsInfo == nil { + a.projectInfo.PeripheralsInfo = make(map[string]hardware.PeripheralInfo) + } a.projectInfo.PeripheralsInfo[peripheralID] = p.GetInfo() log.Info().Str("file", "peripheral").Str("protocolName", protocolName).Str("periphID", peripheralID).Msg("peripheral added to project") @@ -35,26 +38,23 @@ func (a *App) GetPeripheralSettings(protocolName, peripheralID string) (map[stri return p.GetSettings(), nil } -// UpdatePeripheralSetting updates a specific setting of a peripheral -func (a *App) UpdatePeripheralSetting(protocolName, peripheralID, settingName string, settingValue interface{}) error { +// UpdatePeripheralSettings updates a specific setting of a peripheral +func (a *App) UpdatePeripheralSettings(protocolName, peripheralID string, settings map[string]interface{}) error { // Get the peripheral from its finder p, found := a.hardwareManager.GetPeripheral(protocolName, peripheralID) if !found { log.Error().Str("file", "peripheral").Str("protocolName", protocolName).Str("periphID", peripheralID).Msg("unable to found the specified peripheral") return fmt.Errorf("unable to found the peripheral ID '%s'", peripheralID) } - // Save the setting in the application + // Save the settings in the application if a.projectInfo.PeripheralsInfo == nil { a.projectInfo.PeripheralsInfo = make(map[string]hardware.PeripheralInfo) } - if a.projectInfo.PeripheralsInfo[peripheralID].Settings == nil { - a.projectInfo.PeripheralsInfo[peripheralID] = hardware.PeripheralInfo{ - Settings: make(map[string]interface{}), - } - } - a.projectInfo.PeripheralsInfo[peripheralID].Settings[settingName] = settingValue + pInfo := a.projectInfo.PeripheralsInfo[peripheralID] + pInfo.Settings = settings + a.projectInfo.PeripheralsInfo[peripheralID] = pInfo // Apply changes in the peripheral - return p.SetPeripheralSetting(settingName, settingValue) + return p.SetPeripheralSettings(pInfo.Settings) } // RemovePeripheral adds a peripheral to the project