generated from thinkode/modelRepository
implement devices
This commit is contained in:
@@ -1,5 +1,24 @@
|
||||
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 {
|
||||
@@ -10,12 +29,44 @@ type MappingInfo struct {
|
||||
Features map[string]any `yaml:"features"`
|
||||
}
|
||||
|
||||
// Device represents the logical to be controlled
|
||||
type Device struct {
|
||||
// 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
|
||||
|
||||
Endpoint Endpoint // The device endpoint which control this device
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user