save peripherals in project file

This commit is contained in:
2024-12-20 17:18:57 +01:00
parent 17b5d39fc4
commit 9964ccef7e
19 changed files with 316 additions and 228 deletions

View File

@@ -40,7 +40,20 @@ func (f *FTDIFinder) Initialize() error {
// GetName returns the name of the finder
func (f *FTDIFinder) GetName() string {
return "FTDI Finder"
return "FTDI"
}
// GetPeripheral gets the peripheral that correspond to the specified ID
func (f *FTDIFinder) GetPeripheral(peripheralID string) (Peripheral, bool) {
// Return the specified peripheral
peripheral := f.peripherals[peripheralID]
if peripheral == nil {
fmt.Println("Unable to get the peripheral in the finder")
return nil, false
}
fmt.Println("Peripheral found in the finder")
return peripheral, true
}
//go:embed third-party/ftdi/detectFTDI.exe
@@ -100,7 +113,7 @@ func (f *FTDIFinder) Scan(ctx context.Context) error {
location = -1
}
// Add the peripheral to the temporary list
peripheral, err := NewFTDIPeripheral(deviceInfo[2], deviceInfo[1], location)
peripheral, err := NewFTDIPeripheral(deviceInfo[2], deviceInfo[1], location, f.GetName())
if err != nil {
return fmt.Errorf("Unable to create the FTDI peripheral: %v", err)
}

View File

@@ -26,6 +26,7 @@ type FTDIPeripheral struct {
serialNumber string // The S/N of the FTDI peripheral
location int // The location of the peripheral
universesNumber int // The number of DMX universes handled by this peripheral
finderName string // The name of the parent finder
programName string // The temp file name of the executable
dmxSender *exec.Cmd // The command to pilot the DMX sender program
stdin io.WriteCloser // For writing in the DMX sender
@@ -37,7 +38,7 @@ type FTDIPeripheral struct {
}
// NewFTDIPeripheral creates a new FTDI peripheral
func NewFTDIPeripheral(name string, serialNumber string, location int) (*FTDIPeripheral, error) {
func NewFTDIPeripheral(name string, serialNumber string, location int, finderName string) (*FTDIPeripheral, error) {
// Create a temporary file
tempFile, err := os.CreateTemp("", "dmxSender*.exe")
if err != nil {
@@ -56,6 +57,7 @@ func NewFTDIPeripheral(name string, serialNumber string, location int) (*FTDIPer
programName: tempFile.Name(),
serialNumber: serialNumber,
location: location,
finderName: finderName,
universesNumber: 1,
disconnectChan: make(chan struct{}),
errorsChan: make(chan error, 1),
@@ -194,9 +196,8 @@ func (p *FTDIPeripheral) SetDeviceProperty(uint32, channelNumber uint32, channel
// GetInfo gets all the peripheral information
func (p *FTDIPeripheral) GetInfo() PeripheralInfo {
return PeripheralInfo{
Name: p.name,
SerialNumber: p.serialNumber,
ProtocolName: "FTDI",
UniversesNumber: p.universesNumber,
Name: p.name,
SerialNumber: p.serialNumber,
ProtocolName: "FTDI",
}
}

View File

@@ -52,6 +52,16 @@ func (f *MIDIFinder) GetName() string {
return "MIDI"
}
// GetPeripheral gets the peripheral that correspond to the specified ID
func (f *MIDIFinder) GetPeripheral(peripheralID string) (Peripheral, bool) {
// Return the specified peripheral
peripheral := f.peripherals[peripheralID]
if peripheral == nil {
return nil, false
}
return peripheral, true
}
func splitStringAndNumber(input string) (string, int, error) {
// Regular expression to match the text part and the number at the end
re := regexp.MustCompile(`^(.*?)(\d+)$`)
@@ -99,7 +109,7 @@ func (f *MIDIFinder) Scan(ctx context.Context) error {
}
fmt.Printf("New MIDI device found: %s on %i\n", name, location)
// Add the peripheral to the temporary list
midiPeripherals[portName] = NewMIDIPeripheral(name, location)
midiPeripherals[portName] = NewMIDIPeripheral(name, location, f.GetName())
}
// Compare with the current peripherals to detect arrivals/removals
removedList, addedList := comparePeripherals(f.peripherals, midiPeripherals)

View File

@@ -2,15 +2,17 @@ package hardware
// MIDIPeripheral contains the data of a MIDI peripheral
type MIDIPeripheral struct {
name string // The name of the peripheral
location int // The location of the peripheral
name string // The name of the peripheral
location int // The location of the peripheral
finderName string // The name of the parent finder
}
// NewMIDIPeripheral creates a new MIDI peripheral
func NewMIDIPeripheral(name string, location int) *MIDIPeripheral {
func NewMIDIPeripheral(name string, location int, finderName string) *MIDIPeripheral {
return &MIDIPeripheral{
name: name,
location: location,
name: name,
location: location,
finderName: finderName,
}
}

View File

@@ -29,16 +29,16 @@ var (
// HardwareManager is the class who manages the hardware
type HardwareManager struct {
finders []PeripheralFinder // The list of peripherals finders
peripherals []Peripheral // The current list of peripherals
deviceChangedEvent chan struct{} // The event when the devices list changed
finders map[string]PeripheralFinder // The map of peripherals finders
peripherals []Peripheral // The current list of peripherals
deviceChangedEvent chan struct{} // The event when the devices list changed
ctx context.Context
}
// NewHardwareManager creates a new HardwareManager
func NewHardwareManager() *HardwareManager {
return &HardwareManager{
finders: make([]PeripheralFinder, 0),
finders: make(map[string]PeripheralFinder),
peripherals: make([]Peripheral, 0),
deviceChangedEvent: make(chan struct{}),
}
@@ -120,10 +120,25 @@ func (h *HardwareManager) Start(ctx context.Context) error {
// RegisterFinder registers a new peripherals finder
func (h *HardwareManager) RegisterFinder(finder PeripheralFinder) {
h.finders = append(h.finders, finder)
h.finders[finder.GetName()] = finder
fmt.Printf("Success registered the %s finder\n", finder.GetName())
}
// GetPeripheral gets the peripheral object from the parent finder
func (h *HardwareManager) GetPeripheral(finderName string, peripheralID string) (Peripheral, bool) {
// Get the parent finder
parentFinder := h.finders[finderName]
// If no finder found, return false
if parentFinder == nil {
fmt.Println("Unable to get the finder")
return nil, false
}
fmt.Println("Finder ok, returning the peripheral")
// Contact the finder to get the device
return parentFinder.GetPeripheral(peripheralID)
}
// Scan scans all the peripherals for the registered finders
func (h *HardwareManager) Scan(ctx context.Context) error {
if len(h.finders) == 0 {

View File

@@ -15,15 +15,16 @@ type Peripheral interface {
// PeripheralInfo represents a peripheral information
type PeripheralInfo struct {
Name string // Name of the peripheral
SerialNumber string // S/N of the peripheral
ProtocolName string // Protocol name of the peripheral
UniversesNumber int // Number of DMX universes handled by the peripheral
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 []interface{} `yaml:"settings"` // Number of DMX universes handled by the peripheral
}
// PeripheralFinder represents how compatible peripheral finders are implemented
type PeripheralFinder interface {
Initialize() error // Initializes the protocol
GetName() string // Get the name of the finder
Scan(context.Context) error // Scan for peripherals
Initialize() error // Initializes the protocol
GetName() string // Get the name of the finder
GetPeripheral(string) (Peripheral, bool) // Get the peripheral
Scan(context.Context) error // Scan for peripherals
}