2023-05-22 14:10:34 +00:00
|
|
|
package gotelem
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/kschamplin/gotelem/skylab"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CanWriter
|
|
|
|
type CanWriter struct {
|
2023-05-25 18:01:50 +00:00
|
|
|
output *os.File
|
|
|
|
cd CANDumpJSON
|
2023-05-22 14:10:34 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-05-25 18:01:50 +00:00
|
|
|
type CANDumpJSON struct {
|
2023-05-22 14:10:34 +00:00
|
|
|
Timestamp float64 `json:"ts"`
|
|
|
|
Id uint64 `json:"id"`
|
|
|
|
Data skylab.Packet `json:"data"`
|
|
|
|
}
|