package hardware import ( "context" "fmt" "math/rand" "strings" "github.com/wailsapp/wails/v2/pkg/runtime" ) // DeviceEvent is triggered when a device event changes type DeviceEvent string const ( // DeviceArrival is triggered when a device arrival DeviceArrival DeviceEvent = "DEVICE_ARRIVAL" // DeviceRemoval is triggered when a device removal DeviceRemoval EndpointEvent = "DEVICE_REMOVAL" ) // MappingInfo is the configuration for each device type MappingInfo struct { DeviceInfo struct { Name string `yaml:"name"` Manufacturer string `yaml:"manufacturer"` Type string `yaml:"type"` } `yaml:"device"` Features map[string]any `yaml:"features"` } // DeviceInfo represents the device data type DeviceInfo struct { SerialNumber string // The device s/n Name string // The device name Manufacturer string // The device manufacturer Version string // The device version } // Device represents the logical to be controlled type Device struct { DeviceInfo DeviceInfo // The device base information Endpoint Endpoint // The device endpoint which control this device } // AddDevice adds a new device to the manager func (h *Manager) AddDevice(ctx context.Context, device DeviceInfo, endpoint Endpoint) error { // If the SerialNumber is empty, generate another one if device.SerialNumber == "" { device.SerialNumber = strings.ToUpper(fmt.Sprintf("%08x", rand.Intn(1<<32))) } // Add or replace the device to the manager h.devices[device.SerialNumber] = &Device{device, endpoint} // Send the event to the front runtime.EventsEmit(ctx, string(DeviceArrival), device) return nil } // RemoveDevice removes a device from the manager func (h *Manager) RemoveDevice(ctx context.Context, serialNumber string) error { // Delete the device from the manager if serialNumber == "" { return fmt.Errorf("the device s/n is empty") } delete(h.devices, serialNumber) // Send the event to the front runtime.EventsEmit(ctx, string(DeviceRemoval), serialNumber) return nil }