Skip to content

Commit

Permalink
feat: filter can messages via command line "--filter"
Browse files Browse the repository at this point in the history
  • Loading branch information
faryon93 committed Aug 26, 2020
1 parent 5fd3c95 commit dcba738
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
61 changes: 61 additions & 0 deletions filter/filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package filter

// ---------------------------------------------------------------------------------------
// imports
// ---------------------------------------------------------------------------------------

import (
"github.com/robertkrimen/otto"
)

// ---------------------------------------------------------------------------------------
// constants
// ---------------------------------------------------------------------------------------

const (
InputVariableName = "f"
)

// ---------------------------------------------------------------------------------------
// types
// ---------------------------------------------------------------------------------------

type Filter struct {
Expr string
}

// ---------------------------------------------------------------------------------------
// public functions
// ---------------------------------------------------------------------------------------

func NewFilter(expr string) *Filter {
if expr == "" {
expr = "true"
}

return &Filter{Expr: expr}
}

// ---------------------------------------------------------------------------------------
// public members
// ---------------------------------------------------------------------------------------

func (f *Filter) Eval(v interface{}) (bool, error) {
vm := otto.New()
v, err := vm.ToValue(v)
if err != nil {
return false, err
}

err = vm.Set(InputVariableName, v)
if err != nil {
return false, err
}

ret, err := vm.Run(f.Expr)
if err != nil {
return false, err
}

return ret.ToBoolean()
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module canlisten
go 1.14

require (
github.com/robertkrimen/otto v0.0.0-20191219234010-c382bd3c16ff
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae // indirect
gopkg.in/sourcemap.v1 v1.0.5 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
github.com/robertkrimen/otto v0.0.0-20191219234010-c382bd3c16ff h1:+6NUiITWwE5q1KO6SAfUX918c+Tab0+tGAM/mtdlUyA=
github.com/robertkrimen/otto v0.0.0-20191219234010-c382bd3c16ff/go.mod h1:xvqspoSXJTIpemEonrMDFq6XzwHYYgToXWj5eRX1OtY=
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07 h1:UyzmZLoiDWMRywV4DUYb9Fbt8uiOSooupjTq10vpvnU=
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA=
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae h1:Ih9Yo4hSPImZOpfGuA4bR/ORKTAbhZo2AbWNRCnevdo=
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/sourcemap.v1 v1.0.5 h1:inv58fC9f9J3TK2Y2R1NPntXEn3/wjWHkonhIUODNTI=
gopkg.in/sourcemap.v1 v1.0.5/go.mod h1:2RlvNNSMglmRrcvhfuzp4hQHwOtjxlbjX7UPY/GXb78=
21 changes: 19 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"time"

"canlisten/can"
"canlisten/filter"

"github.com/tarm/serial"
)
Expand All @@ -35,8 +36,9 @@ const (
// ---------------------------------------------------------------------------------------

var (
Device string
Running = true
Device string
FilterExpr string
Running = true
)

// ---------------------------------------------------------------------------------------
Expand Down Expand Up @@ -85,6 +87,7 @@ func GetPrintableCmd(cmd string) string {

func main() {
flag.StringVar(&Device, "dev", "/dev/ttyACM0", "")
flag.StringVar(&FilterExpr, "filter", "", "")
flag.Parse()

c := &serial.Config{Name: Device, Baud: 115200, ReadTimeout: 50 * time.Millisecond}
Expand All @@ -109,6 +112,9 @@ func main() {
log.Println("closing application")
}()

log.Printf("Using filter: \"%s\"", FilterExpr)
filterVm := filter.NewFilter(FilterExpr)

// print all can messages to stdout
stdout := log.New(os.Stdout, "", log.LstdFlags|log.Lmicroseconds)
canCounter := 0
Expand All @@ -132,6 +138,17 @@ func main() {
}
frame.Timestamp = time.Now()

// apply the filterVm
show, err := filterVm.Eval(frame)
if err != nil {
log.Println("failed to evaluate filterVm:", err.Error())
continue
}

if !show {
continue
}

// count message and print to stdout
canCounter++
stdout.Printf("%s %s", filepath.Base(Device), frame.String())
Expand Down

0 comments on commit dcba738

Please sign in to comment.