Files
dmxconnect/hardware/FTDIDriver.go

141 lines
3.6 KiB
Go
Raw Normal View History

2024-12-15 13:45:46 +01:00
package hardware
import (
"bufio"
"context"
_ "embed"
"fmt"
"os"
"os/exec"
goRuntime "runtime"
"strconv"
"strings"
"time"
)
const (
scanDelay = 4 * time.Second // Waiting delay before scanning the FTDI devices
)
2024-12-23 17:22:37 +01:00
// FTDIDriver represents how the protocol is defined
type FTDIDriver struct {
2024-12-15 13:45:46 +01:00
peripherals map[string]Peripheral
}
2024-12-23 17:22:37 +01:00
// NewFTDIDriver creates a new FTDI finder
func NewFTDIDriver() *FTDIDriver {
return &FTDIDriver{
2024-12-15 13:45:46 +01:00
peripherals: make(map[string]Peripheral),
}
}
2024-12-23 17:22:37 +01:00
// Initialize initializes the FTDI driver
func (d *FTDIDriver) Initialize() error {
2024-12-15 13:45:46 +01:00
if goRuntime.GOOS != "windows" {
2024-12-23 17:22:37 +01:00
return fmt.Errorf("<!> The FTDI driver is not compatible with your platform yet (%s)", goRuntime.GOOS)
2024-12-15 13:45:46 +01:00
}
2024-12-23 17:22:37 +01:00
fmt.Println("FTDI driver initialized")
2024-12-15 13:45:46 +01:00
return nil
}
2024-12-23 17:22:37 +01:00
// GetName returns the name of the driver
func (d *FTDIDriver) GetName() string {
2024-12-20 17:18:57 +01:00
return "FTDI"
}
// GetPeripheral gets the peripheral that correspond to the specified ID
2024-12-23 17:22:37 +01:00
func (d *FTDIDriver) GetPeripheral(peripheralID string) (Peripheral, bool) {
2024-12-20 17:18:57 +01:00
// Return the specified peripheral
2024-12-23 17:22:37 +01:00
peripheral := d.peripherals[peripheralID]
2024-12-20 17:18:57 +01:00
if peripheral == nil {
2024-12-23 17:22:37 +01:00
fmt.Println("Unable to get the peripheral with the driver")
2024-12-20 17:18:57 +01:00
return nil, false
}
2024-12-23 17:22:37 +01:00
fmt.Println("Peripheral found by the FTDI driver")
2024-12-20 17:18:57 +01:00
return peripheral, true
2024-12-15 13:45:46 +01:00
}
//go:embed third-party/ftdi/detectFTDI.exe
var findFTDI []byte
// Scan scans the FTDI peripherals
2024-12-23 17:22:37 +01:00
func (d *FTDIDriver) Scan(ctx context.Context) error {
2024-12-15 13:45:46 +01:00
time.Sleep(scanDelay)
// Create a temporary file
tempFile, err := os.CreateTemp("", "findFTDI*.exe")
if err != nil {
return err
}
defer os.Remove(tempFile.Name())
// Write the embedded executable to the temp file
if _, err := tempFile.Write(findFTDI); err != nil {
return err
}
tempFile.Close()
ftdiPeripherals := make(map[string]Peripheral)
finder := exec.Command(tempFile.Name())
stdout, err := finder.StdoutPipe()
if err != nil {
return fmt.Errorf("Unable to create the stdout pipe: %s", err)
}
stderr, err := finder.StderrPipe()
if err != nil {
return fmt.Errorf("Unable to create the stderr pipe: %s", err)
}
err = finder.Start()
if err != nil {
return fmt.Errorf("Unable to find FTDI devices: %s", err)
}
scannerErr := bufio.NewScanner(stderr)
for scannerErr.Scan() {
return fmt.Errorf("Unable to find FTDI devices: %s", scannerErr.Text())
}
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
deviceString := scanner.Text()
// The program output is like '0:1:2' where 0 is the location, 1 is the S/N and 2 is the name
deviceInfo := strings.Split(deviceString, ":")
fmt.Println("FTDI device: " + deviceString)
// Convert the location to an integer
location, err := strconv.Atoi(deviceInfo[0])
if err != nil {
location = -1
}
// Add the peripheral to the temporary list
2024-12-23 17:22:37 +01:00
peripheral, err := NewFTDIPeripheral(deviceInfo[2], deviceInfo[1], location)
2024-12-15 13:45:46 +01:00
if err != nil {
return fmt.Errorf("Unable to create the FTDI peripheral: %v", err)
}
ftdiPeripherals[deviceInfo[1]] = peripheral
}
// Compare with the current peripherals to detect arrivals/removals
2024-12-23 17:22:37 +01:00
removedList, addedList := comparePeripherals(d.peripherals, ftdiPeripherals)
2024-12-15 13:45:46 +01:00
// Emit the events
emitPeripheralsEvents(ctx, removedList, PeripheralRemoval)
emitPeripheralsEvents(ctx, addedList, PeripheralArrival)
// Store the new peripherals list
2024-12-23 17:22:37 +01:00
d.peripherals = ftdiPeripherals
return nil
}
// CreatePeripheral is not implemented here
func (d *FTDIDriver) CreatePeripheral(context.Context) (Peripheral, error) {
return nil, nil
}
// RemovePeripheral is not implemented here
func (d *FTDIDriver) RemovePeripheral(serialNumber string) error {
2024-12-15 13:45:46 +01:00
return nil
}