move can logger and make it json

This commit is contained in:
saji 2023-05-22 09:10:34 -05:00
parent d70ae11b82
commit 43925d2a7d
2 changed files with 58 additions and 33 deletions

View file

@ -6,11 +6,6 @@
// by writing "adapters" to various devices/formats (xbee, sqlite, network socket, socketcan) // by writing "adapters" to various devices/formats (xbee, sqlite, network socket, socketcan)
package gotelem package gotelem
import (
"fmt"
"os"
"time"
)
// Frame represents a protocol-agnostic CAN frame. The Id can be standard or extended, // Frame represents a protocol-agnostic CAN frame. The Id can be standard or extended,
// but if it is extended, the Kind should be EFF. // but if it is extended, the Kind should be EFF.
@ -64,31 +59,3 @@ type CanTransciever interface {
CanSource 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
}

58
skylab_logger.go Normal file
View file

@ -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"`
}