generated from thinkode/modelRepository
Closes #34 Implement MIDI peripherals Implement device concept Cleaning project Reviewed-on: #35
62 lines
2.4 KiB
Go
62 lines
2.4 KiB
Go
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
|
|
}
|