Compare commits

..

2 Commits

Author SHA1 Message Date
e4392c8902 peripheral optimizations 2025-01-04 00:36:29 +01:00
556f24991e fixed saved peripherals for a new project 2024-12-29 21:22:53 +01:00
16 changed files with 613 additions and 452 deletions

49
app.go
View File

@@ -1,11 +1,14 @@
package main
import (
"changeme/hardware"
"context"
"dmxconnect/hardware"
"fmt"
"io"
"log"
"time"
"github.com/rs/zerolog/log"
"os"
"strings"
"sync"
@@ -14,21 +17,20 @@ import (
// App struct
type App struct {
ctx context.Context
cancelFunc context.CancelFunc
hardwareManager *hardware.HardwareManager // For managing all the hardware
wmiMutex sync.Mutex // Avoid some WMI operations at the same time
projectInfo ProjectInfo // The project information structure
projectSave string // The file name of the project
// FOR TESTING PURPOSE ONLY
ftdi *hardware.FTDIPeripheral
}
// NewApp creates a new App application struct
func NewApp() *App {
// Create a new hadware manager
hardwareManager := hardware.NewHardwareManager()
hardwareManager.RegisterDriver(hardware.NewMIDIDriver())
hardwareManager.RegisterDriver(hardware.NewFTDIDriver())
hardwareManager.RegisterDriver(hardware.NewOS2LDriver())
hardwareManager.RegisterDriver(hardware.NewMIDIFinder(5 * time.Second))
hardwareManager.RegisterDriver(hardware.NewFTDIFinder(5 * time.Second))
// hardwareManager.RegisterDriver(hardware.NewOS2LDriver())
return &App{
hardwareManager: hardwareManager,
projectSave: "",
@@ -40,15 +42,40 @@ func NewApp() *App {
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
err := a.hardwareManager.Start(ctx)
func (a *App) onStartup(ctx context.Context) {
a.ctx, a.cancelFunc = context.WithCancel(ctx)
err := a.hardwareManager.Start(a.ctx)
if err != nil {
log.Fatalf("Unable to start the device manager: %s", err)
log.Err(err).Str("file", "app").Msg("unable to start the hardware manager")
return
}
}
// onReady is called when the DOM is ready
// We get the current peripherals connected
func (a *App) onReady(ctx context.Context) {
// log.Debug().Str("file", "peripherals").Msg("getting peripherals...")
// err := a.hardwareManager.Scan()
// if err != nil {
// log.Err(err).Str("file", "app").Msg("unable to get the peripherals")
// }
return
}
// onShutdown is called when the app is closing
// We stop all the pending processes
func (a *App) onShutdown(ctx context.Context) {
// Close the application properly
log.Trace().Str("file", "app").Msg("app is closing")
// Explicitly close the context
a.cancelFunc()
err := a.hardwareManager.Stop()
if err != nil {
log.Err(err).Str("file", "app").Msg("unable to stop the hardware manager")
}
return
}
func formatString(input string) string {
// Convertir en minuscules
lowerCaseString := strings.ToLower(input)

View File

@@ -13,7 +13,7 @@
import { SaveProject } from '../wailsjs/go/main/App.js';
import { construct_svelte_component } from 'svelte/internal';
import { EventsOn } from '../wailsjs/runtime'
import { CreateProject, GetPeripherals } from "../wailsjs/go/main/App";
import { CreateProject } from "../wailsjs/go/main/App";
import { WindowSetTitle } from "../wailsjs/runtime/runtime"
import { get } from "svelte/store"
import ToastNotification from './components/General/ToastNotification.svelte';
@@ -83,11 +83,6 @@
$needProjectSave = true
})
// Request the list of peripherals
GetPeripherals().catch((error) => {
generateToast('danger', 'bx-error', 'Unable to get the list of peripherals: ' + error)
})
// Handle window shortcuts
document.addEventListener('keydown', function(event) {
// Check the CTRL+S keys

View File

@@ -106,6 +106,9 @@
generateToast('danger', 'bx-error', 'Unable to create the OS2L peripheral')
})
}
// Get the number of saved peripherals
$: savedPeripheralNumber = Object.values($peripherals).filter(peripheral => peripheral.isSaved).length;
</script>
<div class="hardware">
@@ -113,15 +116,15 @@
<p style="margin-bottom: 1em;">Available peripherals</p>
<div class="availableHardware">
<p style="color: var(--first-color);"><i class='bx bxs-plug'></i> Detected</p>
{#each Object.entries($peripherals) as [serialNumber, peripheral]}
{#if peripheral.isDetected}
<DeviceCard on:add={() => addPeripheral(peripheral)} on:dblclick={() => {
if(!peripheral.isSaved)
addPeripheral(peripheral)
}}
title={peripheral.Name} type={peripheral.ProtocolName} location={peripheral.Location ? peripheral.Location : ""} line1={"S/N: " + peripheral.SerialNumber} addable={!peripheral.isSaved}/>
{/if}
{/each}
{#each Object.entries($peripherals) as [serialNumber, peripheral]}
{#if peripheral.isDetected}
<DeviceCard on:add={() => addPeripheral(peripheral)} on:dblclick={() => {
if(!peripheral.isSaved)
addPeripheral(peripheral)
}}
title={peripheral.Name} type={peripheral.ProtocolName} location={peripheral.Location ? peripheral.Location : ""} line1={"S/N: " + peripheral.SerialNumber} addable={!peripheral.isSaved}/>
{/if}
{/each}
<p style="color: var(--first-color);"><i class='bx bxs-network-chart' ></i> Others</p>
<RoundedButton on:click={createOS2L} text="Add an OS2L peripheral" icon="bx-plus-circle" tooltip="Configure an OS2L connection"/>
</div>
@@ -130,12 +133,16 @@
<div style="padding: 0.5em; flex:2; width:100%;">
<p style="margin-bottom: 1em;">Project peripherals</p>
<div class="configuredHardware">
{#each Object.entries($peripherals) as [serialNumber, peripheral]}
{#if peripheral.isSaved}
<DeviceCard on:delete={() => removePeripheral(peripheral)} on:dblclick={() => removePeripheral(peripheral)}
disconnected={!peripheral.isDetected} title={peripheral.Name == "" ? "Please wait..." : peripheral.Name} type={peripheral.ProtocolName} location={peripheral.Location ? peripheral.Location : ""} line1={peripheral.SerialNumber ? "S/N: " + peripheral.SerialNumber : ""} removable signalizable/>
{/if}
{/each}
{#if savedPeripheralNumber > 0}
{#each Object.entries($peripherals) as [serialNumber, peripheral]}
{#if peripheral.isSaved}
<DeviceCard on:delete={() => removePeripheral(peripheral)} on:dblclick={() => removePeripheral(peripheral)}
disconnected={!peripheral.isDetected} title={peripheral.Name == "" ? "Please wait..." : peripheral.Name} type={peripheral.ProtocolName} location={peripheral.Location ? peripheral.Location : ""} line1={peripheral.SerialNumber ? "S/N: " + peripheral.SerialNumber : ""} removable signalizable/>
{/if}
{/each}
{:else}
<i>No hardware saved for this project.</i>
{/if}
</div>
<p style="margin-bottom: 1em;">Peripheral settings</p>
<div>

View File

@@ -27,30 +27,46 @@
})
}
// Unsave peripherals from the store and remove the disconnected peripherals
function unsavePeripherals(){
peripherals.update((storedPeripherals) => {
// Set all the isSaved keys to false and delete the disconnected peripherals
for (let peripheralID in storedPeripherals) {
storedPeripherals[peripheralID].isSaved = false
if (!storedPeripherals[peripheralID].isDetected) {
delete storedPeripherals[peripheralID]
}
}
return {...storedPeripherals}
})
}
// Load the saved peripherals into the store
function loadPeripherals(peripheralsInfo){
peripherals.update((storedPeripherals) => {
// Add the saved peripherals of the project
// If already exists pass the isSaved key to true, if not create the peripheral and set it to disconnected
for (let peripheralID in peripheralsInfo){
// Add the peripheral to the list of peripherals, with the last isDetected key and the isSaved key to true
let lastDetectedKey = storedPeripherals[peripheralID]?.isDetected
storedPeripherals[peripheralID] = peripheralsInfo[peripheralID]
storedPeripherals[peripheralID].isDetected = (lastDetectedKey === true) ? true : false
storedPeripherals[peripheralID].isSaved = true
}
return {...storedPeripherals}
})
}
// Open the selected project
function openSelectedProject(event){
let selectedOption = event.detail.key
// Open the selected project
GetProjectInfo(selectedOption).then((projectInfo) => {
$showInformation = projectInfo.ShowInfo
peripherals.update((storedPeripherals) => {
// Set all the isSaved keys to false and delete the disconnected peripherals
for (let peripheralID in storedPeripherals) {
storedPeripherals[peripheralID].isSaved = false
if (!storedPeripherals[peripheralID].isDetected) {
delete storedPeripherals[peripheralID]
}
}
// Add the saved peripherals of the project
// If already exists pass the isSaved key to true, if not create the peripheral and set it to disconnected
for (let peripheralID in projectInfo.PeripheralsInfo){
// Add the peripheral to the list of peripherals, with the last isDetected key and the isSaved key to true
let lastDetectedKey = storedPeripherals[peripheralID]?.isDetected
storedPeripherals[peripheralID] = projectInfo.PeripheralsInfo[peripheralID]
storedPeripherals[peripheralID].isDetected = (lastDetectedKey === true) ? true : false
storedPeripherals[peripheralID].isSaved = true
}
return {...storedPeripherals}
})
// Remove the saved peripherals ofthe current project
unsavePeripherals()
// Load the new project peripherals
loadPeripherals(projectInfo.PeripheralsInfo)
needProjectSave.set(false)
generateToast('info', 'bx-folder-open', 'The project <b>' + projectInfo.ShowInfo.Name + '</b> was opened')
}).catch((error) => {
@@ -63,6 +79,8 @@
// Instanciate a new project
CreateProject().then((showInfo) => {
$showInformation = showInfo
// Remove the saved peripherals ofthe current project
unsavePeripherals()
$needProjectSave = true
generateToast('info', 'bxs-folder-plus', 'The project was created')
})

2
go.mod
View File

@@ -1,4 +1,4 @@
module changeme
module dmxconnect
go 1.21

View File

@@ -1,151 +0,0 @@
package hardware
import (
"bufio"
"context"
_ "embed"
"fmt"
"os"
"os/exec"
goRuntime "runtime"
"strconv"
"strings"
"time"
"github.com/rs/zerolog/log"
)
const (
scanDelay = 4 * time.Second // Waiting delay before scanning the FTDI devices
)
// FTDIDriver represents how the protocol is defined
type FTDIDriver struct {
peripherals map[string]Peripheral
}
// NewFTDIDriver creates a new FTDI finder
func NewFTDIDriver() *FTDIDriver {
log.Trace().Str("file", "FTDIDriver").Msg("FTDI driver created")
return &FTDIDriver{
peripherals: make(map[string]Peripheral),
}
}
// Initialize initializes the FTDI driver
func (d *FTDIDriver) Initialize() error {
if goRuntime.GOOS != "windows" {
log.Error().Str("file", "FTDIDriver").Str("platform", goRuntime.GOOS).Msg("FTDI driver not compatible with your platform")
return fmt.Errorf("<!> The FTDI driver is not compatible with your platform yet (%s)", goRuntime.GOOS)
}
log.Trace().Str("file", "FTDIDriver").Msg("FTDI driver initialized")
return nil
}
// GetName returns the name of the driver
func (d *FTDIDriver) GetName() string {
return "FTDI"
}
// GetPeripheral gets the peripheral that correspond to the specified ID
func (d *FTDIDriver) GetPeripheral(peripheralID string) (Peripheral, bool) {
// Return the specified peripheral
peripheral := d.peripherals[peripheralID]
if peripheral == nil {
log.Error().Str("file", "FTDIDriver").Str("peripheralID", peripheralID).Msg("unable to get this peripheral from the FTDI driver")
return nil, false
}
log.Debug().Str("file", "FTDIDriver").Str("peripheralID", peripheralID).Msg("peripheral found by the FTDI driver")
return peripheral, true
}
//go:embed third-party/ftdi/detectFTDI.exe
var findFTDI []byte
// Scan scans the FTDI peripherals
func (d *FTDIDriver) Scan(ctx context.Context) error {
log.Trace().Str("file", "FTDIDriver").Msg("FTDI scan triggered")
time.Sleep(scanDelay)
// Create a temporary file
tempFile, err := os.CreateTemp("", "findFTDI*.exe")
if err != nil {
return err
}
defer os.Remove(tempFile.Name())
log.Trace().Str("file", "FTDIDriver").Msg("has created the FIND executable temp")
// Write the embedded executable to the temp file
if _, err := tempFile.Write(findFTDI); err != nil {
return err
}
tempFile.Close()
log.Trace().Str("file", "FTDIDriver").Msg("has written the FIND executable")
ftdiPeripherals := make(map[string]Peripheral)
finder := exec.Command(tempFile.Name())
log.Trace().Str("file", "FTDIDriver").Msg("has executed the FIND executable")
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() {
peripheralString := 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
peripheralInfo := strings.Split(peripheralString, ":")
log.Debug().Str("file", "FTDIDriver").Str("peripheralName", peripheralInfo[2]).Str("peripheralSN", peripheralInfo[1]).Msg("new FTDI peripheral detected")
// Convert the location to an integer
location, err := strconv.Atoi(peripheralInfo[0])
if err != nil {
log.Warn().Str("file", "FTDIDriver").Str("peripheralName", peripheralInfo[2]).Msg("no location provided for this FTDI peripheral")
location = -1
}
// Add the peripheral to the temporary list
peripheral, err := NewFTDIPeripheral(peripheralInfo[2], peripheralInfo[1], location)
if err != nil {
return fmt.Errorf("Unable to create the FTDI peripheral: %v", err)
}
ftdiPeripherals[peripheralInfo[1]] = peripheral
log.Trace().Str("file", "FTDIDriver").Str("peripheralName", peripheralInfo[2]).Msg("successfully added the FTDI peripheral to the driver")
}
// Compare with the current peripherals to detect arrivals/removals
removedList, addedList := comparePeripherals(d.peripherals, ftdiPeripherals)
// Emit the events
emitPeripheralsEvents(ctx, removedList, PeripheralRemoval)
log.Info().Str("file", "FTDIDriver").Msg("FTDI remove list emitted to the front")
emitPeripheralsEvents(ctx, addedList, PeripheralArrival)
log.Info().Str("file", "FTDIDriver").Msg("FTDI add list emitted to the front")
// Store the new peripherals list
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 {
return nil
}

234
hardware/FTDIFinder.go Normal file
View File

@@ -0,0 +1,234 @@
package hardware
import (
"bufio"
"context"
_ "embed"
"fmt"
"os"
"os/exec"
"path/filepath"
goRuntime "runtime"
"strconv"
"strings"
"sync"
"time"
"github.com/rs/zerolog/log"
)
const (
ftdiFinderExecutableName = "FTDI_finder.exe"
ftdiSenderExecutableName = "FTDI_sender.exe"
)
// FTDIFinder represents how the protocol is defined
type FTDIFinder struct {
findTicker time.Ticker // Peripherals find ticker
peripherals map[string]Peripheral // The list of peripherals handled by this finder
scanChannel chan struct{} // The channel to trigger a scan event
goWait sync.WaitGroup // Check goroutines execution
}
// NewFTDIFinder creates a new FTDI finder
func NewFTDIFinder(findPeriod time.Duration) *FTDIFinder {
log.Trace().Str("file", "FTDIFinder").Msg("FTDI finder created")
return &FTDIFinder{
findTicker: *time.NewTicker(findPeriod),
peripherals: make(map[string]Peripheral),
scanChannel: make(chan struct{}),
}
}
//go:embed third-party/ftdi/detectFTDI.exe
var finderExe []byte
//go:embed third-party/ftdi/dmxSender.exe
var senderExe []byte
// Initialize initializes the FTDI finder
func (f *FTDIFinder) Initialize() error {
// Check platform
if goRuntime.GOOS != "windows" {
log.Error().Str("file", "FTDIFinder").Str("platform", goRuntime.GOOS).Msg("FTDI finder not compatible with your platform")
return fmt.Errorf("the FTDI finder is not compatible with your platform yet (%s)", goRuntime.GOOS)
}
// Create the FTDI executables
err := createExecutable(ftdiFinderExecutableName, finderExe)
if err != nil {
return err
}
createExecutable(ftdiSenderExecutableName, senderExe)
if err != nil {
return err
}
log.Trace().Str("file", "FTDIFinder").Msg("FTDI finder initialized")
return nil
}
// createExecutable creates and writes an executable to the temporary directory of the system
func createExecutable(fileName string, storedFile []byte) error {
tempFile, err := os.Create(filepath.Join(os.TempDir(), fileName))
if err != nil {
log.Err(err).Str("file", "FTDIFinder").Str("fileName", fileName).Msg("unable to create an FTDI executable")
return err
}
log.Trace().Str("file", "FTDIFinder").Str("filePath", tempFile.Name()).Msg("FTDI executable created")
// Write the embedded executable to the temp file
if _, err := tempFile.Write(storedFile); err != nil {
log.Err(err).Str("file", "FTDIFinder").Str("fileName", fileName).Msg("unable to write the content to an FTDI executable")
return err
}
tempFile.Close()
log.Trace().Str("file", "FTDIPeripheral").Str("fileName", fileName).Msg("FTDI executable written")
return nil
}
// Start starts the finder and search for peripherals
func (f *FTDIFinder) Start(ctx context.Context) error {
f.goWait.Add(1)
go func() {
defer f.goWait.Done()
for {
select {
case <-ctx.Done():
return
case <-f.findTicker.C:
// Scan the peripherals
err := f.scanPeripherals(ctx)
if err != nil {
log.Err(err).Str("file", "FTDIFinder").Msg("unable to scan FTDI peripherals")
}
case <-f.scanChannel:
// Scan the peripherals
err := f.scanPeripherals(ctx)
if err != nil {
log.Err(err).Str("file", "FTDIFinder").Msg("unable to scan FTDI peripherals")
}
}
}
}()
return nil
}
// ForceScan explicily asks for scanning peripherals
func (f *FTDIFinder) ForceScan() {
f.scanChannel <- struct{}{}
}
// Stop stops the finder
func (f *FTDIFinder) Stop() error {
log.Trace().Str("file", "FTDIFinder").Msg("stopping the FTDI finder...")
// Wait for goroutines to stop
f.goWait.Wait()
// Stop the ticker
f.findTicker.Stop()
// Delete the FTDI executable files
fileToDelete := filepath.Join(os.TempDir(), ftdiFinderExecutableName)
err := os.Remove(fileToDelete)
if err != nil {
log.Warn().Str("file", "FTDIFinder").Str("fileName", fileToDelete).AnErr("error", err).Msg("unable to remove the executable file")
}
fileToDelete = filepath.Join(os.TempDir(), ftdiSenderExecutableName)
err = os.Remove(fileToDelete)
if err != nil {
log.Warn().Str("file", "FTDIFinder").Str("fileName", fileToDelete).AnErr("error", err).Msg("unable to remove the executable file")
}
log.Trace().Str("file", "FTDIFinder").Msg("FTDI finder stopped")
return nil
}
// GetName returns the name of the driver
func (f *FTDIFinder) GetName() string {
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 {
log.Error().Str("file", "FTDIFinder").Str("peripheralID", peripheralID).Msg("unable to get this peripheral from the FTDI finder")
return nil, false
}
log.Debug().Str("file", "FTDIFinder").Str("peripheralID", peripheralID).Msg("peripheral found by the FTDI finder")
return peripheral, true
}
// scanPeripherals scans the FTDI peripherals
func (f *FTDIFinder) scanPeripherals(ctx context.Context) error {
detectionCtx, cancel := context.WithCancel(ctx)
defer cancel()
log.Trace().Str("file", "FTDIFinder").Msg("FTDI scan triggered")
ftdiPeripherals := make(map[string]Peripheral)
finder := exec.CommandContext(detectionCtx, filepath.Join(os.TempDir(), ftdiFinderExecutableName))
log.Trace().Str("file", "FTDIFinder").Msg("has executed the FIND executable")
stdout, err := finder.StdoutPipe()
if err != nil {
return fmt.Errorf("unable to create the stdout pipe: %s", err)
}
defer stdout.Close()
stderr, err := finder.StderrPipe()
if err != nil {
return fmt.Errorf("unable to create the stderr pipe: %s", err)
}
defer stderr.Close()
err = finder.Start()
if err != nil {
return fmt.Errorf("unable to find FTDI peripherals: %s", err)
}
scannerErr := bufio.NewScanner(stderr)
for scannerErr.Scan() {
return fmt.Errorf("unable to find FTDI peripherals: %s", scannerErr.Text())
}
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
peripheralString := 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
peripheralInfo := strings.Split(peripheralString, ":")
log.Debug().Str("file", "FTDIFinder").Str("peripheralName", peripheralInfo[2]).Str("peripheralSN", peripheralInfo[1]).Msg("new FTDI peripheral detected")
// Convert the location to an integer
location, err := strconv.Atoi(peripheralInfo[0])
if err != nil {
log.Warn().Str("file", "FTDIFinder").Str("peripheralName", peripheralInfo[2]).Msg("no location provided for this FTDI peripheral")
location = -1
}
// Add the peripheral to the temporary list
peripheral, err := NewFTDIPeripheral(peripheralInfo[2], peripheralInfo[1], location)
if err != nil {
return fmt.Errorf("unable to create the FTDI peripheral: %v", err)
}
ftdiPeripherals[peripheralInfo[1]] = peripheral
log.Trace().Str("file", "FTDIFinder").Str("peripheralName", peripheralInfo[2]).Msg("successfully added the FTDI peripheral to the finder")
}
// Compare with the current peripherals to detect arrivals/removals
removedList, addedList := comparePeripherals(f.peripherals, ftdiPeripherals)
// Emit the events
emitPeripheralsEvents(ctx, removedList, PeripheralRemoval)
log.Info().Str("file", "FTDIFinder").Msg("FTDI remove list emitted to the front")
emitPeripheralsEvents(ctx, addedList, PeripheralArrival)
log.Info().Str("file", "FTDIFinder").Msg("FTDI add list emitted to the front")
// Store the new peripherals list
f.peripherals = ftdiPeripherals
return nil
}
// CreatePeripheral is not implemented here
func (f *FTDIFinder) CreatePeripheral(context.Context) (Peripheral, error) {
return nil, nil
}
// DeletePeripheral is not implemented here
func (f *FTDIFinder) DeletePeripheral(serialNumber string) error {
return nil
}

View File

@@ -2,6 +2,7 @@ package hardware
import (
"bufio"
"context"
_ "embed"
"fmt"
"io"
@@ -10,7 +11,6 @@ import (
"os"
"os/exec"
"sync"
)
const (
@@ -19,9 +19,6 @@ const (
setCommandString = 0x03
)
//go:embed third-party/ftdi/dmxSender.exe
var dmxSender []byte
// FTDIPeripheral contains the data of an FTDI peripheral
type FTDIPeripheral struct {
name string // The name of the peripheral
@@ -35,30 +32,14 @@ type FTDIPeripheral struct {
stderr io.ReadCloser // For reading the errors
disconnectChan chan struct{} // Channel to cancel the connection
errorsChan chan error // Channel to get the errors
wg sync.WaitGroup // Tasks management
}
// NewFTDIPeripheral creates a new FTDI peripheral
func NewFTDIPeripheral(name string, serialNumber string, location int) (*FTDIPeripheral, error) {
log.Info().Str("file", "FTDIPeripheral").Str("name", name).Str("s/n", serialNumber).Int("location", location).Msg("FTDI peripheral created")
// Create a temporary file
tempFile, err := os.CreateTemp("", "dmxSender*.exe")
if err != nil {
return nil, err
}
log.Trace().Str("file", "FTDIPeripheral").Str("s/n", serialNumber).Msg("FTDI sender temp created")
// Write the embedded executable to the temp file
if _, err := tempFile.Write(dmxSender); err != nil {
return nil, err
}
log.Trace().Str("file", "FTDIPeripheral").Str("s/n", serialNumber).Msg("FTDI sender written")
tempFile.Close()
return &FTDIPeripheral{
name: name,
programName: tempFile.Name(),
dmxSender: nil,
serialNumber: serialNumber,
location: location,
universesNumber: 1,
@@ -68,64 +49,75 @@ func NewFTDIPeripheral(name string, serialNumber string, location int) (*FTDIPer
}
// Connect connects the FTDI peripheral
func (p *FTDIPeripheral) Connect() error {
func (p *FTDIPeripheral) Connect(ctx context.Context) error {
// Connect if no connection is already running
log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("connecting FTDI peripheral...")
if p.dmxSender == nil {
log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("no instance of dmxSender for this FTDI")
// Executing the command
p.dmxSender = exec.Command(p.programName, fmt.Sprintf("%d", p.location))
log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("no instance of dmxSender for this FTDI")
// Check if the connection has already been established
if p.dmxSender != nil {
log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("dmxSender already initialized")
return nil
}
var err error
p.stdout, err = p.dmxSender.StdoutPipe()
if err != nil {
log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("unable to create the stdout pipe")
return fmt.Errorf("unable to create the stdout pipe: %v", err)
// Initialize the exec.Command for running the process
p.dmxSender = exec.Command(p.programName, fmt.Sprintf("%d", p.location))
log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("dmxSender instance created")
// Create the pipes for stdin, stdout, and stderr asynchronously without blocking
var err error
if p.stdout, err = p.dmxSender.StdoutPipe(); err != nil {
log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("unable to create stdout pipe")
return fmt.Errorf("unable to create stdout pipe: %v", err)
}
if p.stdin, err = p.dmxSender.StdinPipe(); err != nil {
log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("unable to create stdin pipe")
return fmt.Errorf("unable to create stdin pipe: %v", err)
}
if p.stderr, err = p.dmxSender.StderrPipe(); err != nil {
log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("unable to create stderr pipe")
return fmt.Errorf("unable to create stderr pipe: %v", err)
}
// Launch a goroutine to read stderr asynchronously
go func() {
scanner := bufio.NewScanner(p.stderr)
for scanner.Scan() {
// Process each line read from stderr
log.Err(fmt.Errorf(scanner.Text())).Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("error detected in dmx sender")
}
p.stdin, err = p.dmxSender.StdinPipe()
if err != nil {
log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("unable to create the stdin pipe")
return fmt.Errorf("unable to create the stdin pipe: %v", err)
if err := scanner.Err(); err != nil {
log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("error reading from stderr")
}
}()
p.stderr, err = p.dmxSender.StderrPipe()
if err != nil {
log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("unable to create the stderr pipe")
return fmt.Errorf("unable to create the stderr pipe: %v", err)
}
go func() {
scanner := bufio.NewScanner(p.stderr)
for scanner.Scan() {
// Traitez chaque ligne lue depuis stderr
log.Err(fmt.Errorf(scanner.Text())).Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("error detected in dmx sender")
}
if err := scanner.Err(); err != nil {
log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("error reading from stderr")
}
}()
p.wg.Add(1)
go func() {
defer p.wg.Done()
err = p.dmxSender.Run()
// Launch the command asynchronously in another goroutine
go func() {
// Run the command, respecting the context cancellation
err := p.dmxSender.Run()
select {
case <-ctx.Done():
// If the context is canceled, handle it gracefully
log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("dmxSender was canceled by context")
return
default:
// Handle command exit normally
if err != nil {
log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("error while execution of dmw sender")
log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("error while execution of dmx sender")
if exitError, ok := err.(*exec.ExitError); ok {
log.Warn().Str("file", "FTDIPeripheral").Int("exitCode", exitError.ExitCode()).Str("s/n", p.serialNumber).Msg("dmx sender exited with code")
}
} else {
log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("dmx sender exited successfully")
}
}()
}
}
}()
log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("dmxSender process started successfully")
return nil
}
// Disconnect disconnects the FTDI peripheral
func (p *FTDIPeripheral) Disconnect() error {
func (p *FTDIPeripheral) Disconnect(ctx context.Context) error {
log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("disconnecting FTDI peripheral...")
if p.dmxSender != nil {
log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("dmxsender is defined for this FTDI")
@@ -139,7 +131,7 @@ func (p *FTDIPeripheral) Disconnect() error {
p.dmxSender = nil
err = os.Remove(p.programName)
if err != nil {
log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("unable to delete the dmx sender temporary file")
log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Str("senderPath", p.programName).Msg("unable to delete the dmx sender temporary file")
return fmt.Errorf("unable to delete the temporary file: %v", err)
}
return nil
@@ -149,7 +141,7 @@ func (p *FTDIPeripheral) Disconnect() error {
}
// Activate activates the FTDI peripheral
func (p *FTDIPeripheral) Activate() error {
func (p *FTDIPeripheral) Activate(ctx context.Context) error {
if p.dmxSender != nil {
log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("dmxsender is defined for this FTDI")
_, err := io.WriteString(p.stdin, string([]byte{0x01, 0x00, 0x00, 0x00}))
@@ -164,7 +156,7 @@ func (p *FTDIPeripheral) Activate() error {
}
// Deactivate deactivates the FTDI peripheral
func (p *FTDIPeripheral) Deactivate() error {
func (p *FTDIPeripheral) Deactivate(ctx context.Context) error {
if p.dmxSender != nil {
log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("dmxsender is defined for this FTDI")
_, err := io.WriteString(p.stdin, string([]byte{0x02, 0x00, 0x00, 0x00}))
@@ -179,7 +171,7 @@ func (p *FTDIPeripheral) Deactivate() error {
}
// SetDeviceProperty sends a command to the specified device
func (p *FTDIPeripheral) SetDeviceProperty(uint32, channelNumber uint32, channelValue byte) error {
func (p *FTDIPeripheral) SetDeviceProperty(ctx context.Context, uint32, channelNumber uint32, channelValue byte) error {
if p.dmxSender != nil {
log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("dmxsender is defined for this FTDI")
commandString := []byte{0x03, 0x01, 0x00, 0xff, 0x03, 0x02, 0x00, channelValue}

View File

@@ -6,44 +6,88 @@ import (
"regexp"
"strconv"
"strings"
"sync"
"time"
"github.com/mattrtaylor/go-rtmidi"
"github.com/rs/zerolog/log"
)
// MIDIDriver represents how the protocol is defined
type MIDIDriver struct {
// MIDIFinder represents how the protocol is defined
type MIDIFinder struct {
findTicker time.Ticker // Peripherals find ticker
peripherals map[string]Peripheral // The list of peripherals
scanChannel chan struct{} // The channel to trigger a scan event
goWait sync.WaitGroup // Check goroutines execution
}
// NewMIDIDriver creates a new DMXUSB protocol
func NewMIDIDriver() *MIDIDriver {
log.Trace().Str("file", "MIDIDriver").Msg("MIDI driver created")
return &MIDIDriver{
// NewMIDIFinder creates a new DMXUSB protocol
func NewMIDIFinder(findPeriod time.Duration) *MIDIFinder {
log.Trace().Str("file", "MIDIFinder").Msg("MIDI finder created")
return &MIDIFinder{
findTicker: *time.NewTicker(findPeriod),
peripherals: make(map[string]Peripheral),
}
}
// Initialize initializes the MIDI driver
func (d *MIDIDriver) Initialize() error {
log.Trace().Str("file", "MIDIDriver").Msg("MIDI driver initialized")
func (f *MIDIFinder) Initialize() error {
log.Trace().Str("file", "MIDIFinder").Msg("MIDI finder initialized")
return nil
}
// Start starts the finder and search for peripherals
func (f *MIDIFinder) Start(ctx context.Context) error {
f.goWait.Add(1)
go func() {
defer f.goWait.Done()
for {
select {
case <-ctx.Done():
return
case <-f.findTicker.C:
// Scan the peripherals
err := f.scanPeripherals(ctx)
if err != nil {
log.Err(err).Str("file", "MIDIFinder").Msg("unable to scan MIDI peripherals")
}
case <-f.scanChannel:
// Scan the peripherals
err := f.scanPeripherals(ctx)
if err != nil {
log.Err(err).Str("file", "MIDIFinder").Msg("unable to scan MIDI peripherals")
}
}
}
}()
return nil
}
// Stop stops the finder
func (f *MIDIFinder) Stop() error {
log.Trace().Str("file", "MIDIFinder").Msg("stopping the MIDI finder...")
// Wait for goroutines to stop
f.goWait.Wait()
// Stop the ticker
f.findTicker.Stop()
log.Trace().Str("file", "MIDIFinder").Msg("MIDI finder stopped")
return nil
}
// GetName returns the name of the driver
func (d *MIDIDriver) GetName() string {
func (f *MIDIFinder) GetName() string {
return "MIDI"
}
// GetPeripheral gets the peripheral that correspond to the specified ID
func (d *MIDIDriver) GetPeripheral(peripheralID string) (Peripheral, bool) {
func (f *MIDIFinder) GetPeripheral(peripheralID string) (Peripheral, bool) {
// Return the specified peripheral
peripheral, found := d.peripherals[peripheralID]
peripheral, found := f.peripherals[peripheralID]
if !found {
log.Error().Str("file", "MIDIDriver").Str("peripheralID", peripheralID).Msg("unable to get this peripheral in the MIDI driver")
log.Error().Str("file", "MIDIFinder").Str("peripheralID", peripheralID).Msg("unable to get this peripheral in the MIDI finder")
return nil, false
}
log.Trace().Str("file", "MIDIDriver").Str("peripheralID", peripheralID).Msg("MIDI peripheral found in the driver")
log.Trace().Str("file", "MIDIFinder").Str("peripheralID", peripheralID).Msg("MIDI peripheral found in the driver")
return peripheral, true
}
@@ -68,23 +112,26 @@ func splitStringAndNumber(input string) (string, int, error) {
return "", 0, fmt.Errorf("no number found at the end of the string")
}
// Scan scans the interfaces compatible with the MIDI protocol
func (d *MIDIDriver) Scan(ctx context.Context) error {
// ForceScan explicily asks for scanning peripherals
func (f *MIDIFinder) ForceScan() {
f.scanChannel <- struct{}{}
}
// scanPeripherals scans the MIDI peripherals
func (f *MIDIFinder) scanPeripherals(ctx context.Context) error {
midiPeripherals := make(map[string]Peripheral)
log.Trace().Str("file", "MIDIDriver").Msg("opening MIDI scanner port...")
log.Trace().Str("file", "MIDIFinder").Msg("opening MIDI scanner port...")
midiScanner, err := rtmidi.NewMIDIInDefault()
if err != nil {
log.Err(err).Str("file", "MIDIDriver").Msg("unable to open the MIDI scanner port...")
log.Err(err).Str("file", "MIDIFinder").Msg("unable to open the MIDI scanner port...")
return fmt.Errorf("unable to open the MIDI scanner: %s", err)
}
defer midiScanner.Close()
midiScanner.SetCallback(func(m rtmidi.MIDIIn, b []byte, f float64) {
})
log.Trace().Str("file", "MIDIDriver").Msg("scanning MIDI peripherals...")
midiScanner.SetCallback(func(m rtmidi.MIDIIn, b []byte, f float64) {})
log.Trace().Str("file", "MIDIFinder").Msg("scanning MIDI peripherals...")
devicesCount, err := midiScanner.PortCount()
if err != nil {
log.Err(err).Str("file", "MIDIDriver").Msg("unable to scan MIDI peripherals...")
log.Err(err).Str("file", "MIDIFinder").Msg("unable to scan MIDI peripherals...")
return fmt.Errorf("unable to scan MIDI peripherals: %s", err)
}
for i := 0; i < devicesCount; i++ {
@@ -96,32 +143,32 @@ func (d *MIDIDriver) Scan(ctx context.Context) error {
// Separate data
name, location, err := splitStringAndNumber(portName)
if err != nil {
log.Err(err).Str("file", "MIDIDriver").Str("description", portName).Msg("invalid peripheral description")
log.Err(err).Str("file", "MIDIFinder").Str("description", portName).Msg("invalid peripheral description")
return fmt.Errorf("invalid pripheral description: %s", err)
}
log.Info().Str("file", "MIDIDriver").Str("name", name).Int("location", location).Msg("MIDI peripheral found")
log.Info().Str("file", "MIDIFinder").Str("name", name).Int("location", location).Msg("MIDI peripheral found")
// Add the peripheral to the temporary list
sn := strings.ToLower(strings.Replace(name, " ", "_", -1))
midiPeripherals[sn] = NewMIDIPeripheral(name, location, sn)
}
// Compare with the current peripherals to detect arrivals/removals
removedList, addedList := comparePeripherals(d.peripherals, midiPeripherals)
removedList, addedList := comparePeripherals(f.peripherals, midiPeripherals)
// Emit the events
emitPeripheralsEvents(ctx, removedList, PeripheralRemoval)
log.Info().Str("file", "MIDIDriver").Msg("MIDI remove list emitted to the front")
log.Info().Str("file", "MIDIFinder").Msg("MIDI remove list emitted to the front")
emitPeripheralsEvents(ctx, addedList, PeripheralArrival)
log.Info().Str("file", "MIDIDriver").Msg("MIDI add list emitted to the front")
log.Info().Str("file", "MIDIFinder").Msg("MIDI add list emitted to the front")
// Store the new peripherals list
d.peripherals = midiPeripherals
f.peripherals = midiPeripherals
return nil
}
// CreatePeripheral is not implemented here
func (d *MIDIDriver) CreatePeripheral(context.Context) (Peripheral, error) {
func (f *MIDIFinder) CreatePeripheral(context.Context) (Peripheral, error) {
return nil, nil
}
// RemovePeripheral is not implemented here
func (d *MIDIDriver) RemovePeripheral(serialNumber string) error {
// DeletePeripheral is not implemented here
func (f *MIDIFinder) DeletePeripheral(serialNumber string) error {
return nil
}

View File

@@ -1,6 +1,10 @@
package hardware
import "github.com/rs/zerolog/log"
import (
"context"
"github.com/rs/zerolog/log"
)
// MIDIPeripheral contains the data of a MIDI peripheral
type MIDIPeripheral struct {
@@ -20,27 +24,27 @@ func NewMIDIPeripheral(name string, location int, serialNumber string) *MIDIPeri
}
// Connect connects the MIDI peripheral
func (p *MIDIPeripheral) Connect() error {
func (p *MIDIPeripheral) Connect(ctx context.Context) error {
return nil
}
// Disconnect disconnects the MIDI peripheral
func (p *MIDIPeripheral) Disconnect() error {
func (p *MIDIPeripheral) Disconnect(ctx context.Context) error {
return nil
}
// Activate activates the MIDI peripheral
func (p *MIDIPeripheral) Activate() error {
func (p *MIDIPeripheral) Activate(ctx context.Context) error {
return nil
}
// Deactivate deactivates the MIDI peripheral
func (p *MIDIPeripheral) Deactivate() error {
func (p *MIDIPeripheral) Deactivate(ctx context.Context) error {
return nil
}
// SetDeviceProperty - not implemented for this kind of peripheral
func (p *MIDIPeripheral) SetDeviceProperty(uint32, uint32, byte) error {
func (p *MIDIPeripheral) SetDeviceProperty(context.Context, uint32, uint32, byte) error {
return nil
}

View File

@@ -1,6 +1,8 @@
package hardware
import (
"context"
"github.com/rs/zerolog/log"
)
@@ -20,31 +22,31 @@ func NewOS2LPeripheral(name string, serialNumber string) *OS2LPeripheral {
}
// Connect connects the MIDI peripheral
func (p *OS2LPeripheral) Connect() error {
func (p *OS2LPeripheral) Connect(ctx context.Context) error {
log.Info().Str("file", "OS2LPeripheral").Msg("OS2L peripheral connected")
return nil
}
// Disconnect disconnects the MIDI peripheral
func (p *OS2LPeripheral) Disconnect() error {
func (p *OS2LPeripheral) Disconnect(ctx context.Context) error {
log.Info().Str("file", "OS2LPeripheral").Msg("OS2L peripheral disconnected")
return nil
}
// Activate activates the MIDI peripheral
func (p *OS2LPeripheral) Activate() error {
func (p *OS2LPeripheral) Activate(ctx context.Context) error {
log.Info().Str("file", "OS2LPeripheral").Msg("OS2L peripheral activated")
return nil
}
// Deactivate deactivates the MIDI peripheral
func (p *OS2LPeripheral) Deactivate() error {
func (p *OS2LPeripheral) Deactivate(ctx context.Context) error {
log.Info().Str("file", "OS2LPeripheral").Msg("OS2L peripheral deactivated")
return nil
}
// SetDeviceProperty - not implemented for this kind of peripheral
func (p *OS2LPeripheral) SetDeviceProperty(uint32, uint32, byte) error {
func (p *OS2LPeripheral) SetDeviceProperty(context.Context, uint32, uint32, byte) error {
return nil
}

View File

@@ -3,9 +3,8 @@ package hardware
import (
"context"
"fmt"
"syscall"
"sync"
"time"
"unsafe"
"github.com/rs/zerolog/log"
@@ -31,116 +30,63 @@ var (
// HardwareManager is the class who manages the hardware
type HardwareManager struct {
drivers map[string]PeripheralDriver // 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
drivers map[string]PeripheralFinder // The map of peripherals finders
peripherals []Peripheral // The current list of peripherals
peripheralsScanTrigger chan struct{} // Trigger the peripherals scans
goWait sync.WaitGroup // Wait for goroutines to terminate
}
// NewHardwareManager creates a new HardwareManager
func NewHardwareManager() *HardwareManager {
log.Trace().Str("package", "hardware").Msg("Hardware instance created")
return &HardwareManager{
drivers: make(map[string]PeripheralDriver),
peripherals: make([]Peripheral, 0),
deviceChangedEvent: make(chan struct{}),
drivers: make(map[string]PeripheralFinder),
peripherals: make([]Peripheral, 0),
peripheralsScanTrigger: make(chan struct{}),
}
}
// Start starts to find new peripheral events
func (h *HardwareManager) Start(ctx context.Context) error {
cb := windows.NewCallback(h.wndProc)
log.Trace().Str("file", "hardware").Msg("wndProc callback set")
inst := win.GetModuleHandle(nil)
log.Trace().Str("file", "hardware").Msg("got windows API instance")
cn, err := syscall.UTF16PtrFromString("DMXConnect peripheral watcher")
if err != nil {
log.Err(err).Str("file", "hardware").Msg("failed to convert window class name to UTF16")
return fmt.Errorf("failed to convert window class name to UTF16: %w", err)
}
wc := win.WNDCLASSEX{
HInstance: inst,
LpfnWndProc: cb,
LpszClassName: cn,
}
log.Trace().Str("file", "hardware").Msg("windows API class created")
wc.CbSize = uint32(unsafe.Sizeof(wc))
if win.RegisterClassEx(&wc) == 0 {
log.Err(syscall.GetLastError()).Str("file", "hardware").Msg("failed to register window class")
return fmt.Errorf("failed to register window class: %w", syscall.GetLastError())
}
log.Trace().Str("file", "hardware").Msg("window class registered")
wName, err := syscall.UTF16PtrFromString("usbevent.exe")
if err != nil {
log.Err(err).Str("file", "hardware").Msg("failed to convert window class name to UTF16")
return fmt.Errorf("failed to convert window name to UTF16: %w", err)
}
wdw := win.CreateWindowEx(
0,
wc.LpszClassName,
wName,
win.WS_MINIMIZE|win.WS_OVERLAPPEDWINDOW,
win.CW_USEDEFAULT,
win.CW_USEDEFAULT,
100,
100,
0,
0,
wc.HInstance,
nil)
if wdw == 0 {
log.Err(syscall.GetLastError()).Str("file", "hardware").Msg("failed to create window")
return fmt.Errorf("failed to create window: %w", syscall.GetLastError())
}
log.Trace().Str("file", "hardware").Msg("window created successfully")
_ = win.ShowWindow(wdw, win.SW_HIDE)
win.UpdateWindow(wdw)
log.Trace().Str("file", "hardware").Msg("window shown and updated")
// To continuously get the devices events from Windows
go func() {
defer log.Debug().Str("file", "hardware").Msg("peripheral watcher goroutine exited")
for {
select {
case <-ctx.Done():
return
default:
var msg win.MSG
got := win.GetMessage(&msg, win.HWND(windows.HWND(wdw)), 0, 0)
if got == 0 {
win.TranslateMessage(&msg)
win.DispatchMessage(&msg)
}
}
for finderName, finder := range h.drivers {
err := finder.Initialize()
if err != nil {
log.Err(err).Str("file", "hardware").Str("finderName", finderName).Msg("unable to initialize finder")
return err
}
}()
// To handle the peripheral changed
go func() {
defer log.Debug().Str("file", "hardware").Msg("peripheral getter goroutine exited")
for {
select {
case <-ctx.Done():
return
case <-h.deviceChangedEvent:
log.Debug().Str("file", "hardware").Msg("peripheral change event, triggering scan...")
err := h.Scan(ctx)
if err != nil {
log.Err(err).Str("file", "hardware").Msg("unable to scan peripherals")
}
}
err = finder.Start(ctx)
if err != nil {
log.Err(err).Str("file", "hardware").Str("finderName", finderName).Msg("unable to start finder")
return err
}
}()
}
// n, err := detector.Register()
// if err != nil {
// log.Err(err).Str("file", "hardware").Msg("error registering the usb event")
// }
// h.detector = n
// // Run the detector
// n.Run(ctx)
// h.goWait.Add(1)
// go func() {
// defer h.goWait.Done()
// for {
// select {
// case <-ctx.Done():
// return
// case <-h.detector.EventChannel:
// // Trigger hardware scans
// log.Info().Str("file", "hardware").Msg("peripheral change event")
// case <-h.peripheralsScanTrigger:
// log.Info().Str("file", "hardware").Msg("scan triggered")
// }
// }
// }()
return nil
}
// GetDriver returns a register driver
func (h *HardwareManager) GetDriver(driverName string) (PeripheralDriver, error) {
func (h *HardwareManager) GetDriver(driverName string) (PeripheralFinder, error) {
driver, exists := h.drivers[driverName]
if !exists {
log.Error().Str("file", "hardware").Str("driverName", driverName).Msg("unable to get the driver")
@@ -151,7 +97,7 @@ func (h *HardwareManager) GetDriver(driverName string) (PeripheralDriver, error)
}
// RegisterDriver registers a new peripherals driver
func (h *HardwareManager) RegisterDriver(driver PeripheralDriver) {
func (h *HardwareManager) RegisterDriver(driver PeripheralFinder) {
h.drivers[driver.GetName()] = driver
log.Info().Str("file", "hardware").Str("driverName", driver.GetName()).Msg("driver registered")
}
@@ -171,40 +117,43 @@ func (h *HardwareManager) GetPeripheral(driverName string, peripheralID string)
}
// Scan scans all the peripherals for the registered finders
func (h *HardwareManager) Scan(ctx context.Context) error {
if len(h.drivers) == 0 {
log.Warn().Str("file", "hardware").Msg("no driver registered")
return fmt.Errorf("no driver registered")
}
for _, driver := range h.drivers {
driverCopy := driver
go func() {
err := driverCopy.Scan(ctx)
if err != nil {
log.Err(err).Str("file", "hardware").Str("driverName", driverCopy.GetName()).Msg("unable to scan peripheral")
return
}
}()
}
func (h *HardwareManager) Scan() error {
h.peripheralsScanTrigger <- struct{}{}
return nil
}
func (h *HardwareManager) wndProc(hwnd windows.HWND, msg uint32, wParam, lParam uintptr) uintptr {
switch msg {
case win.WM_DEVICECHANGE:
log.Trace().Str("file", "hardware").Uint32("msg", msg).Msg("wndProc triggered")
if msg == win.WM_DEVICECHANGE {
// Trigger the devices scan when the last DEVICE_CHANGE event is received
if debounceTimer != nil {
debounceTimer.Stop()
log.Trace().Str("file", "hardware").Msg("scan debounce timer stopped")
log.Debug().Str("file", "hardware").Msg("scan debounce timer stopped")
}
debounceTimer = time.AfterFunc(debounceDuration, func() {
log.Debug().Str("file", "hardware").Msg("peripheral changed")
h.deviceChangedEvent <- struct{}{}
h.peripheralsScanTrigger <- struct{}{}
})
}
return win.DefWindowProc(win.HWND(hwnd), msg, wParam, lParam)
}
// Stop stops the hardware manager
func (h *HardwareManager) Stop() error {
log.Trace().Str("file", "hardware").Msg("closing the hardware manager")
// Stop each finder
for finderName, finder := range h.drivers {
err := finder.Stop()
if err != nil {
log.Err(err).Str("file", "hardware").Str("finderName", finderName).Msg("unable to stop the finder")
}
}
// Wait for goroutines to finish
h.goWait.Wait()
log.Info().Str("file", "hardware").Msg("hardware manager stopped")
return nil
}
// peripheralsList emits a peripheral event
func emitPeripheralsEvents(ctx context.Context, peripheralsList map[string]Peripheral, peripheralEvent PeripheralEvent) {
for _, peripheral := range peripheralsList {

View File

@@ -4,11 +4,11 @@ import "context"
// Peripheral represents the methods used to manage a peripheral (input or output hardware)
type Peripheral interface {
Connect() error // Connect the peripheral
Disconnect() error // Disconnect the peripheral
Activate() error // Activate the peripheral
Deactivate() error // Deactivate the peripheral
SetDeviceProperty(uint32, uint32, byte) error // Update a device property
Connect(context.Context) error // Connect the peripheral
Disconnect(context.Context) error // Disconnect the peripheral
Activate(context.Context) error // Activate the peripheral
Deactivate(context.Context) error // Deactivate the peripheral
SetDeviceProperty(context.Context, uint32, uint32, byte) error // Update a device property
GetInfo() PeripheralInfo // Get the peripheral information
}
@@ -21,12 +21,14 @@ type PeripheralInfo struct {
Settings []interface{} `yaml:"settings"` // Number of DMX universes handled by the peripheral
}
// PeripheralDriver represents how compatible peripheral drivers are implemented
type PeripheralDriver interface {
// PeripheralFinder represents how compatible peripheral drivers are implemented
type PeripheralFinder interface {
Initialize() error // Initializes the protocol
Start(context.Context) error // Start the detection
Stop() error // Stop the detection
ForceScan() // Explicitly scans for peripherals
CreatePeripheral(ctx context.Context) (Peripheral, error) // Creates a new peripheral
DeletePeripheral(serialNumber string) error // Removes a peripheral
GetName() string // Get the name of the finder
GetPeripheral(string) (Peripheral, bool) // Get the peripheral
Scan(context.Context) error // Scan for peripherals
CreatePeripheral(ctx context.Context) (Peripheral, error) // Creates a new peripheral
RemovePeripheral(serialNumber string) error // Removes a peripheral
}

View File

@@ -40,7 +40,9 @@ func main() {
AssetServer: &assetserver.Options{
Assets: assets,
},
OnStartup: app.startup,
OnStartup: app.onStartup,
OnDomReady: app.onReady,
OnShutdown: app.onShutdown,
Bind: []interface{}{
app,
},

View File

@@ -1,18 +1,12 @@
package main
import (
"changeme/hardware"
"dmxconnect/hardware"
"fmt"
"github.com/rs/zerolog/log"
)
// GetPeripherals gets all the peripherals connected
func (a *App) GetPeripherals() error {
log.Debug().Str("file", "peripherals").Msg("getting peripherals...")
return a.hardwareManager.Scan(a.ctx)
}
// AddPeripheral adds a peripheral to the project
func (a *App) AddPeripheral(protocolName string, peripheralID string) error {
// Get the device from its finder
@@ -64,27 +58,66 @@ func (a *App) AddOS2LPeripheral() (hardware.PeripheralInfo, error) {
// FOR TESTING PURPOSE ONLY
func (a *App) ConnectFTDI() error {
// Create a new FTDI object
var err error
a.ftdi, err = hardware.NewFTDIPeripheral("FTDI TEST INTERFACE", "A50825I", 0)
// Connect the FTDI
driver, err := a.hardwareManager.GetDriver("FTDI")
if err != nil {
return err
}
return a.ftdi.Connect()
periph, found := driver.GetPeripheral("A50285BI")
if !found {
return fmt.Errorf("unable to find the peripheral s/n %s", "A50285BI")
}
return periph.Connect(a.ctx)
}
func (a *App) ActivateFTDI() error {
return a.ftdi.Activate()
// Connect the FTDI
driver, err := a.hardwareManager.GetDriver("FTDI")
if err != nil {
return err
}
periph, found := driver.GetPeripheral("A50285BI")
if !found {
return fmt.Errorf("unable to find the peripheral s/n %s", "A50285BI")
}
return periph.Activate(a.ctx)
}
func (a *App) SetDeviceFTDI(channelValue byte) error {
return a.ftdi.SetDeviceProperty(0, 0, channelValue)
// Connect the FTDI
driver, err := a.hardwareManager.GetDriver("FTDI")
if err != nil {
return err
}
periph, found := driver.GetPeripheral("A50285BI")
if !found {
return fmt.Errorf("unable to find the peripheral s/n %s", "A50285BI")
}
return periph.SetDeviceProperty(a.ctx, 0, 0, channelValue)
}
func (a *App) DeactivateFTDI() error {
return a.ftdi.Deactivate()
// Connect the FTDI
driver, err := a.hardwareManager.GetDriver("FTDI")
if err != nil {
return err
}
periph, found := driver.GetPeripheral("A50285BI")
if !found {
return fmt.Errorf("unable to find the peripheral s/n %s", "A50285BI")
}
return periph.Deactivate(a.ctx)
}
func (a *App) DisconnectFTDI() error {
return a.ftdi.Disconnect()
// Connect the FTDI
driver, err := a.hardwareManager.GetDriver("FTDI")
if err != nil {
return err
}
periph, found := driver.GetPeripheral("A50285BI")
if !found {
return fmt.Errorf("unable to find the peripheral s/n %s", "A50285BI")
}
return periph.Disconnect(a.ctx)
}

View File

@@ -1,7 +1,7 @@
package main
import (
"changeme/hardware"
"dmxconnect/hardware"
"fmt"
"github.com/rs/zerolog/log"