diff --git a/app.go b/app.go index 2eb3264..33abf17 100644 --- a/app.go +++ b/app.go @@ -33,7 +33,7 @@ func NewApp() *App { hardwareManager: hardwareManager, projectSave: "", projectInfo: ProjectInfo{ - PeripheralsInfo: make(map[string]hardware.PeripheralInfo), + EndpointsInfo: make(map[string]hardware.EndpointInfo), }, } } @@ -56,12 +56,12 @@ func (a *App) onStartup(ctx context.Context) { } // onReady is called when the DOM is ready -// We get the current peripherals connected +// We get the current endpoints connected func (a *App) onReady(ctx context.Context) { - // log.Debug().Str("file", "peripherals").Msg("getting peripherals...") + // log.Debug().Str("file", "endpoints").Msg("getting endpoints...") // err := a.hardwareManager.Scan() // if err != nil { - // log.Err(err).Str("file", "app").Msg("unable to get the peripherals") + // log.Err(err).Str("file", "app").Msg("unable to get the endpoints") // } return } diff --git a/endpoints.go b/endpoints.go new file mode 100644 index 0000000..cb71a35 --- /dev/null +++ b/endpoints.go @@ -0,0 +1,61 @@ +package main + +import ( + "dmxconnect/hardware" + "fmt" + + "github.com/rs/zerolog/log" +) + +// AddEndpoint adds a endpoint to the project +func (a *App) AddEndpoint(endpointData hardware.EndpointInfo) (string, error) { + // Register this new endpoint + serialNumber, err := a.hardwareManager.RegisterEndpoint(a.ctx, endpointData) + if err != nil { + return "", fmt.Errorf("unable to register the endpoint '%s': %w", serialNumber, err) + } + log.Trace().Str("file", "endpoint").Str("protocolName", endpointData.ProtocolName).Str("periphID", serialNumber).Msg("device registered to the provider") + + // Rewrite the serialnumber for virtual devices + endpointData.SerialNumber = serialNumber + + // Add the endpoint ID to the project + if a.projectInfo.EndpointsInfo == nil { + a.projectInfo.EndpointsInfo = make(map[string]hardware.EndpointInfo) + } + + a.projectInfo.EndpointsInfo[endpointData.SerialNumber] = endpointData + log.Info().Str("file", "endpoint").Str("protocolName", endpointData.ProtocolName).Str("periphID", endpointData.SerialNumber).Msg("endpoint added to project") + return endpointData.SerialNumber, nil +} + +// GetEndpointSettings gets the endpoint settings +func (a *App) GetEndpointSettings(protocolName, endpointSN string) (map[string]any, error) { + return a.hardwareManager.GetEndpointSettings(endpointSN) +} + +// UpdateEndpointSettings updates a specific setting of a endpoint +func (a *App) UpdateEndpointSettings(protocolName, endpointID string, settings map[string]any) error { + // Save the settings in the application + if a.projectInfo.EndpointsInfo == nil { + a.projectInfo.EndpointsInfo = make(map[string]hardware.EndpointInfo) + } + pInfo := a.projectInfo.EndpointsInfo[endpointID] + pInfo.Settings = settings + a.projectInfo.EndpointsInfo[endpointID] = pInfo + // Apply changes in the endpoint + return a.hardwareManager.SetEndpointSettings(a.ctx, endpointID, pInfo.Settings) +} + +// RemoveEndpoint removes a endpoint from the project +func (a *App) RemoveEndpoint(endpointData hardware.EndpointInfo) error { + // Unregister the endpoint from the provider + err := a.hardwareManager.UnregisterEndpoint(a.ctx, endpointData) + if err != nil { + return fmt.Errorf("unable to unregister this endpoint: %w", err) + } + // Remove the endpoint ID from the project + delete(a.projectInfo.EndpointsInfo, endpointData.SerialNumber) + log.Info().Str("file", "endpoint").Str("protocolName", endpointData.ProtocolName).Str("periphID", endpointData.SerialNumber).Msg("endpoint removed from project") + return nil +} diff --git a/frontend/src/components/Settings/InputsOutputsContent.svelte b/frontend/src/components/Settings/InputsOutputsContent.svelte index 7ecb948..282393f 100644 --- a/frontend/src/components/Settings/InputsOutputsContent.svelte +++ b/frontend/src/components/Settings/InputsOutputsContent.svelte @@ -2,87 +2,87 @@ import DeviceCard from "./DeviceCard.svelte"; import Input from "../General/Input.svelte"; import { t, _ } from 'svelte-i18n' - import { generateToast, needProjectSave, peripherals, colors } from "../../stores"; + import { generateToast, needProjectSave, endpoints, colors } from "../../stores"; import { get } from "svelte/store" - import { UpdatePeripheralSettings, GetPeripheralSettings, RemovePeripheral, AddPeripheral } from "../../../wailsjs/go/main/App"; + import { UpdateEndpointSettings, GetEndpointSettings, RemoveEndpoint, AddEndpoint } from "../../../wailsjs/go/main/App"; import RoundedButton from "../General/RoundedButton.svelte"; - // Create the peripheral to the project - function createPeripheral(peripheral){ - // Create the peripheral to the project (backend) - AddPeripheral(peripheral) + // Create the endpoint to the project + function createEndpoint(endpoint){ + // Create the endpoint to the project (backend) + AddEndpoint(endpoint) .catch((error) => { - console.log("Unable to create the peripheral: " + error) - generateToast('danger', 'bx-error', $_("addPeripheralErrorToast")) + console.log("Unable to create the endpoint: " + error) + generateToast('danger', 'bx-error', $_("addEndpointErrorToast")) }) } - // Add the peripheral to the project - function addPeripheral(peripheral){ - // Add the peripheral to the project (backend) - AddPeripheral(peripheral) + // Add the endpoint to the project + function addEndpoint(endpoint){ + // Add the endpoint to the project (backend) + AddEndpoint(endpoint) .catch((error) => { - console.log("Unable to add the peripheral to the project: " + error) - generateToast('danger', 'bx-error', $_("addPeripheralErrorToast")) + console.log("Unable to add the endpoint to the project: " + error) + generateToast('danger', 'bx-error', $_("addEndpointErrorToast")) }) } - // Remove the peripheral from the project - function removePeripheral(peripheral) { - // Delete the peripheral from the project (backend) - RemovePeripheral(peripheral) + // Remove the endpoint from the project + function removeEndpoint(endpoint) { + // Delete the endpoint from the project (backend) + RemoveEndpoint(endpoint) .catch((error) => { - console.log("Unable to remove the peripheral from the project: " + error) - generateToast('danger', 'bx-error', $_("removePeripheralErrorToast")) + console.log("Unable to remove the endpoint from the project: " + error) + generateToast('danger', 'bx-error', $_("removeEndpointErrorToast")) }) } - // Select the peripheral to edit its settings - let selectedPeripheralSN = null - let selectedPeripheralSettings = {} - function selectPeripheral(peripheral){ - // Load the settings array if the peripheral is detected - if (peripheral.isSaved){ - GetPeripheralSettings(peripheral.ProtocolName, peripheral.SerialNumber).then((peripheralSettings) => { - selectedPeripheralSettings = peripheralSettings - // Select the current peripheral - selectedPeripheralSN = peripheral.SerialNumber + // Select the endpoint to edit its settings + let selectedEndpointSN = null + let selectedEndpointSettings = {} + function selectEndpoint(endpoint){ + // Load the settings array if the endpoint is detected + if (endpoint.isSaved){ + GetEndpointSettings(endpoint.ProtocolName, endpoint.SerialNumber).then((endpointSettings) => { + selectedEndpointSettings = endpointSettings + // Select the current endpoint + selectedEndpointSN = endpoint.SerialNumber }).catch((error) => { - console.log("Unable to get the peripheral settings: " + error) - generateToast('danger', 'bx-error', $_("getPeripheralSettingsErrorToast")) + console.log("Unable to get the endpoint settings: " + error) + generateToast('danger', 'bx-error', $_("getEndpointSettingsErrorToast")) }) } } - // Unselect the peripheral if it is disconnected + // Unselect the endpoint if it is disconnected $: { - Object.entries($peripherals).filter(([serialNumber, peripheral]) => { - if (!peripheral.isDetected && peripheral.isSaved && selectedPeripheralSN == serialNumber) { - selectedPeripheralSN = null - selectedPeripheralSettings = {} + Object.entries($endpoints).filter(([serialNumber, endpoint]) => { + if (!endpoint.isDetected && endpoint.isSaved && selectedEndpointSN == serialNumber) { + selectedEndpointSN = null + selectedEndpointSettings = {} } }); } - // Get the number of saved peripherals - $: savedPeripheralNumber = Object.values($peripherals).filter(peripheral => peripheral.isSaved).length; + // Get the number of saved endpoints + $: savedEndpointNumber = Object.values($endpoints).filter(endpoint => endpoint.isSaved).length; - // Validate the peripheral settings + // Validate the endpoint settings function validate(settingName, settingValue){ - console.log("Peripheral setting '" + settingName + "' set to '" + settingValue + "'") + console.log("Endpoint 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) - let peripheralProtocolName = get(peripherals)[selectedPeripheralSN].ProtocolName - UpdatePeripheralSettings(peripheralProtocolName, selectedPeripheralSN, selectedPeripheralSettings).then(()=> { + }[typeof(selectedEndpointSettings[settingName])] || (x => x) + selectedEndpointSettings[settingName] = convert(settingValue) + let endpointProtocolName = get(endpoints)[selectedEndpointSN].ProtocolName + UpdateEndpointSettings(endpointProtocolName, selectedEndpointSN, selectedEndpointSettings).then(()=> { $needProjectSave = true }).catch((error) => { - console.log("Unable to save the peripheral setting: " + error) - generateToast('danger', 'bx-error', $_("peripheralSettingSaveErrorToast")) + console.log("Unable to save the endpoint setting: " + error) + generateToast('danger', 'bx-error', $_("endpointSettingSaveErrorToast")) }) } @@ -92,31 +92,31 @@
{$_("projectHardwareDetectedLabel")}
{$_("projectHardwareOthersLabel")}
{$_("projectHardwareSavedLabel")}