generated from thinkode/modelRepository
improved log system
This commit is contained in:
@@ -11,6 +11,8 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -24,6 +26,7 @@ type FTDIDriver struct {
|
||||
|
||||
// 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),
|
||||
}
|
||||
@@ -32,9 +35,10 @@ func NewFTDIDriver() *FTDIDriver {
|
||||
// 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)
|
||||
}
|
||||
fmt.Println("FTDI driver initialized")
|
||||
log.Trace().Str("file", "FTDIDriver").Msg("FTDI driver initialized")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -48,11 +52,10 @@ func (d *FTDIDriver) GetPeripheral(peripheralID string) (Peripheral, bool) {
|
||||
// Return the specified peripheral
|
||||
peripheral := d.peripherals[peripheralID]
|
||||
if peripheral == nil {
|
||||
fmt.Println("Unable to get the peripheral with the driver")
|
||||
log.Error().Str("file", "FTDIDriver").Str("peripheralID", peripheralID).Msg("unable to get this peripheral from the FTDI driver")
|
||||
return nil, false
|
||||
}
|
||||
fmt.Println("Peripheral found by the FTDI driver")
|
||||
|
||||
log.Debug().Str("file", "FTDIDriver").Str("peripheralID", peripheralID).Msg("peripheral found by the FTDI driver")
|
||||
return peripheral, true
|
||||
}
|
||||
|
||||
@@ -61,6 +64,7 @@ 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
|
||||
@@ -69,61 +73,68 @@ func (d *FTDIDriver) Scan(ctx context.Context) error {
|
||||
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)
|
||||
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)
|
||||
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)
|
||||
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())
|
||||
return fmt.Errorf("unable to find FTDI devices: %s", scannerErr.Text())
|
||||
}
|
||||
|
||||
scanner := bufio.NewScanner(stdout)
|
||||
for scanner.Scan() {
|
||||
deviceString := scanner.Text()
|
||||
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
|
||||
deviceInfo := strings.Split(deviceString, ":")
|
||||
peripheralInfo := strings.Split(peripheralString, ":")
|
||||
|
||||
fmt.Println("FTDI device: " + deviceString)
|
||||
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(deviceInfo[0])
|
||||
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(deviceInfo[2], deviceInfo[1], location)
|
||||
peripheral, err := NewFTDIPeripheral(peripheralInfo[2], peripheralInfo[1], location)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Unable to create the FTDI peripheral: %v", err)
|
||||
}
|
||||
ftdiPeripherals[deviceInfo[1]] = peripheral
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user