Big Gotool Refactoring
- Added Event Bus - Reworked Package Parsing - Rewokred Frame Parsing
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package testrunner
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"alox.tool/api"
|
||||
"alox.tool/eventbus"
|
||||
"alox.tool/uart"
|
||||
)
|
||||
|
||||
type TestRunner struct {
|
||||
bus eventbus.EventBus
|
||||
com *uart.Com
|
||||
}
|
||||
|
||||
func New(bus eventbus.EventBus, com *uart.Com) *TestRunner {
|
||||
return &TestRunner{
|
||||
bus: bus,
|
||||
com: com,
|
||||
}
|
||||
}
|
||||
|
||||
func (tr *TestRunner) Expect(idToSend byte, payload []byte, expectedID byte, timeout time.Duration) (*api.Frame, error) {
|
||||
rxChan := tr.bus.Subscribe(api.TopicUARTRx)
|
||||
defer tr.bus.Unsubscribe(api.TopicUARTRx, rxChan)
|
||||
|
||||
if err := tr.com.Send(idToSend, payload); err != nil {
|
||||
return nil, fmt.Errorf("send error: %w", err)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil, fmt.Errorf("timeout waiting for ID 0x%02X", expectedID)
|
||||
case frame := <-rxChan:
|
||||
f := frame.(api.Frame)
|
||||
if f.ID == expectedID {
|
||||
return &f, nil
|
||||
}
|
||||
// Ignore other IDs and Messages on the Bus
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package testrunner
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"alox.tool/api"
|
||||
)
|
||||
|
||||
func (tr *TestRunner) RunVersionTest() error {
|
||||
fmt.Println("Starte Version Test...")
|
||||
|
||||
// Sende VersionRequest (0x02), erwarte VersionResponse (0x02)
|
||||
frame, err := tr.Expect(api.CmdVersion, nil, api.CmdVersion, 1*time.Second)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("Test bestanden! Hardware Version ist: %s\n", string(frame.Data))
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user