From 43925d2a7d1af8c885677c6db4247aed34b5487b Mon Sep 17 00:00:00 2001 From: saji Date: Mon, 22 May 2023 09:10:34 -0500 Subject: [PATCH] move can logger and make it json --- frame.go | 33 --------------------------- skylab_logger.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 33 deletions(-) create mode 100644 skylab_logger.go diff --git a/frame.go b/frame.go index 58a94cd..20e6255 100644 --- a/frame.go +++ b/frame.go @@ -6,11 +6,6 @@ // by writing "adapters" to various devices/formats (xbee, sqlite, network socket, socketcan) package gotelem -import ( - "fmt" - "os" - "time" -) // Frame represents a protocol-agnostic CAN frame. The Id can be standard or extended, // but if it is extended, the Kind should be EFF. @@ -64,31 +59,3 @@ type CanTransciever interface { CanSource } -// CanWriter -type CanWriter struct { - output *os.File -} - -// send writes the frame to the file. -func (cw *CanWriter) Send(f *Frame) error { - ts := time.Now().Unix() - - _, err := fmt.Fprintf(cw.output, "%d %X %X\n", ts, f.Id, f.Data) - return err -} - -func (cw *CanWriter) Close() error { - return cw.output.Close() -} - -func OpenCanWriter(name string) (*CanWriter, error) { - f, err := os.Create(name) - if err != nil { - return nil, err - } - - cw := &CanWriter{ - output: f, - } - return cw, nil -} diff --git a/skylab_logger.go b/skylab_logger.go new file mode 100644 index 0000000..d622f64 --- /dev/null +++ b/skylab_logger.go @@ -0,0 +1,58 @@ +package gotelem + +import ( + "encoding/json" + "fmt" + "os" + "time" + + "github.com/kschamplin/gotelem/skylab" +) + +// CanWriter +type CanWriter struct { + output *os.File + cd candumpJSON + jsonBuf []byte +} + +// send writes the frame to the file. +func (cw *CanWriter) Send(f *Frame) (err error) { + cw.cd.Timestamp = float64(time.Now().Unix()) + + cw.cd.Id = uint64(f.Id) + + cw.cd.Data, err = skylab.FromCanFrame(f.Id, f.Data) + + if err != nil { + return + } + out, err := json.Marshal(cw.cd) + if err != nil { + return + } + fmt.Fprintln(cw.output, string(out)) + return err +} + +func (cw *CanWriter) Close() error { + return cw.output.Close() +} + +func OpenCanWriter(name string) (*CanWriter, error) { + f, err := os.Create(name) + if err != nil { + return nil, err + } + + cw := &CanWriter{ + output: f, + } + return cw, nil +} + +type candumpJSON struct { + Timestamp float64 `json:"ts"` + Id uint64 `json:"id"` + Data skylab.Packet `json:"data"` +}