Compare commits
No commits in common. "d5381a3c33a85f2873a38be0e233759e0a287d7c" and "c8034066c9a48477ab25b2f67b1ac6b88abba665" have entirely different histories.
d5381a3c33
...
c8034066c9
|
@ -9,8 +9,6 @@ import (
|
|||
"github.com/kschamplin/gotelem/skylab"
|
||||
)
|
||||
|
||||
// Broker is a Bus Event broadcast system. You can subscribe to events,
|
||||
// and send events.
|
||||
type Broker struct {
|
||||
subs map[string]chan skylab.BusEvent // contains the channel for each subsciber
|
||||
|
||||
|
@ -19,7 +17,6 @@ type Broker struct {
|
|||
bufsize int // size of chan buffer in elements.
|
||||
}
|
||||
|
||||
// NewBroker creates a new broker with a given logger.
|
||||
func NewBroker(bufsize int, logger *slog.Logger) *Broker {
|
||||
return &Broker{
|
||||
subs: make(map[string]chan skylab.BusEvent),
|
||||
|
@ -28,7 +25,6 @@ func NewBroker(bufsize int, logger *slog.Logger) *Broker {
|
|||
}
|
||||
}
|
||||
|
||||
// Subscribe joins the broker with the given name. The name must be unique.
|
||||
func (b *Broker) Subscribe(name string) (ch chan skylab.BusEvent, err error) {
|
||||
// get rw lock.
|
||||
b.lock.Lock()
|
||||
|
@ -44,9 +40,6 @@ func (b *Broker) Subscribe(name string) (ch chan skylab.BusEvent, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
|
||||
// Unsubscribe removes a subscriber matching the name. It doesn't do anything
|
||||
// if there's nobody subscribed with that name
|
||||
func (b *Broker) Unsubscribe(name string) {
|
||||
// remove the channel from the map. We don't need to close it.
|
||||
b.lock.Lock()
|
||||
|
@ -54,8 +47,6 @@ func (b *Broker) Unsubscribe(name string) {
|
|||
delete(b.subs, name)
|
||||
}
|
||||
|
||||
// Publish sends a bus event to all subscribers. It includes a sender
|
||||
// string which prevents loopback.
|
||||
func (b *Broker) Publish(sender string, message skylab.BusEvent) {
|
||||
b.lock.RLock()
|
||||
defer b.lock.RUnlock()
|
||||
|
|
|
@ -92,7 +92,7 @@ func run(ctx *cli.Context) (err error) {
|
|||
|
||||
fileReader := bufio.NewReader(istream)
|
||||
|
||||
var pfun logparsers.BusEventParser
|
||||
var pfun logparsers.BusParserFunc
|
||||
|
||||
pfun, ok := logparsers.ParsersMap[ctx.String("format")]
|
||||
if !ok {
|
||||
|
|
|
@ -2,7 +2,6 @@ package logparsers
|
|||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
|
@ -36,10 +35,10 @@ func NewFormatError(msg string, err error) error {
|
|||
return &FormatError{msg: msg, err: err}
|
||||
}
|
||||
|
||||
// type CanFrameParser is a function that takes a string
|
||||
// type LineParserFunc is a function that takes a string
|
||||
// and returns a can frame. This is useful for common
|
||||
// can dump formats.
|
||||
type CanFrameParser func(string) (can.Frame, time.Time, error)
|
||||
type LineParserFunc func(string) (can.Frame, time.Time, error)
|
||||
|
||||
var candumpRegex = regexp.MustCompile(`^\((\d+)\.(\d{6})\) \w+ (\w+)#(\w+)$`)
|
||||
|
||||
|
@ -158,27 +157,20 @@ func parseTelemLogLine(line string) (frame can.Frame, ts time.Time, err error) {
|
|||
|
||||
}
|
||||
|
||||
// BusEventParser is a function that takes a string and returns a busevent.
|
||||
type BusEventParser func(string) (skylab.BusEvent, error)
|
||||
// BusParserFunc is a function that takes a string and returns a busevent.
|
||||
type BusParserFunc func(string) (skylab.BusEvent, error)
|
||||
|
||||
// skylabify JSON parser.
|
||||
func parseSkylabifyLogLine(input string) (skylab.BusEvent, error) {
|
||||
var b = skylab.BusEvent{}
|
||||
err := json.Unmarshal([]byte(input), &b)
|
||||
return b, err
|
||||
}
|
||||
|
||||
// frameParseToBusEvent takes a line parser (that returns a can frame)
|
||||
// parserBusEventMapper takes a line parser (that returns a can frame)
|
||||
// and makes it return a busEvent instead.
|
||||
func frameParseToBusEvent(fun CanFrameParser) BusEventParser {
|
||||
func parserBusEventMapper(f LineParserFunc) BusParserFunc {
|
||||
return func(s string) (skylab.BusEvent, error) {
|
||||
var b = skylab.BusEvent{}
|
||||
frame, ts, err := fun(s)
|
||||
f, ts, err := f(s)
|
||||
if err != nil {
|
||||
return b, err
|
||||
}
|
||||
b.Timestamp = ts
|
||||
b.Data, err = skylab.FromCanFrame(frame)
|
||||
b.Data, err = skylab.FromCanFrame(f)
|
||||
if err != nil {
|
||||
return b, err
|
||||
}
|
||||
|
@ -187,8 +179,7 @@ func frameParseToBusEvent(fun CanFrameParser) BusEventParser {
|
|||
}
|
||||
}
|
||||
|
||||
var ParsersMap = map[string]BusEventParser{
|
||||
"telem": frameParseToBusEvent(parseTelemLogLine),
|
||||
"candump": frameParseToBusEvent(parseCanDumpLine),
|
||||
"json": parseSkylabifyLogLine,
|
||||
var ParsersMap = map[string]BusParserFunc{
|
||||
"telem": parserBusEventMapper(parseTelemLogLine),
|
||||
"candump": parserBusEventMapper(parseCanDumpLine),
|
||||
}
|
||||
|
|
|
@ -6,7 +6,6 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/kschamplin/gotelem/internal/can"
|
||||
"github.com/kschamplin/gotelem/skylab"
|
||||
)
|
||||
|
||||
func Test_parseCanDumpLine(t *testing.T) {
|
||||
|
@ -171,42 +170,3 @@ func Test_parseTelemLogLine_errors(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_parseSkylabifyLogLine(t *testing.T) {
|
||||
type args struct {
|
||||
input string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want skylab.BusEvent
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "basic test",
|
||||
args: args{
|
||||
input: `{"ts":1685141873612,"id":259,"name":"wsl_velocity","data":{"motor_velocity":89.97547,"vehicle_velocity":2.38853}}`},
|
||||
want: skylab.BusEvent{
|
||||
Timestamp: time.UnixMilli(1685141873612),
|
||||
Name: "wsl_velocity",
|
||||
Data: &skylab.WslVelocity{
|
||||
MotorVelocity: 89.97547,
|
||||
VehicleVelocity: 2.38853,
|
||||
},
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := parseSkylabifyLogLine(tt.args.input)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("parseSkylabifyLogLine() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("parseSkylabifyLogLine() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
1267
skylab/skylab_gen.go
1267
skylab/skylab_gen.go
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load diff
|
@ -3,7 +3,6 @@ package skylab
|
|||
|
||||
import (
|
||||
"testing"
|
||||
"reflect"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
|
@ -45,20 +44,4 @@ func TestJSON{{$structName}}(t *testing.T) {
|
|||
|
||||
}
|
||||
|
||||
func TestCanFrame{{$structName}}(t *testing.T) {
|
||||
v := &{{$structName}}{}
|
||||
frame, err := ToCanFrame(v)
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
|
||||
retpkt, err := FromCanFrame(frame)
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(v, retpkt) {
|
||||
t.Fatalf("decoded packet did not match sent %v got %v", v, retpkt)
|
||||
}
|
||||
}
|
||||
|
||||
{{- end }}
|
||||
|
|
|
@ -29,7 +29,7 @@ func TestCanSocket(t *testing.T) {
|
|||
}
|
||||
})
|
||||
|
||||
t.Run("test interface name", func(t *testing.T) {
|
||||
t.Run("test name", func(t *testing.T) {
|
||||
sock, _ := NewCanSocket("vcan0")
|
||||
defer sock.Close()
|
||||
|
||||
|
|
Loading…
Reference in a new issue