Skip to content

Commit

Permalink
formatted with go
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewI26 committed Dec 22, 2024
1 parent 05933e6 commit efa824e
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 50 deletions.
2 changes: 0 additions & 2 deletions canlink/canclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,5 +190,3 @@ func (c *CanClient) StopTracking() (map[uint32]int, error) {
func (c *CanClient) IsTracking() bool {
return c.tracking
}


6 changes: 3 additions & 3 deletions canlink/canlink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestTracer(t *testing.T) {
defer teardown(t, tracer, logger)

time.Sleep(5 * time.Second)

}

func setup(t *testing.T) (*Tracer, *zap.Logger, func(*testing.T, *Tracer, *zap.Logger)) {
Expand Down Expand Up @@ -66,7 +66,7 @@ func setup(t *testing.T) (*Tracer, *zap.Logger, func(*testing.T, *Tracer, *zap.L
logger,
conn,
WithBusName(_busName),
WithWriters(writers))
WithWriters(writers))

err = tracer.Open(ctx)
if err != nil {
Expand Down Expand Up @@ -96,4 +96,4 @@ func setup(t *testing.T) (*Tracer, *zap.Logger, func(*testing.T, *Tracer, *zap.L
}

return tracer, logger, teardown
}
}
6 changes: 3 additions & 3 deletions canlink/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"net"
"time"

"github.com/macformula/hil/utils"
"github.com/macformula/hil/canlink/writer"
"github.com/macformula/hil/utils"
"github.com/pkg/errors"
"go.einride.tech/can/pkg/socketcan"
"go.uber.org/zap"
Expand All @@ -32,8 +32,8 @@ type Tracer struct {
isRunning bool
receiver *socketcan.Receiver

traceDir string
writers []writer.WriterIface
traceDir string
writers []writer.WriterIface

canInterface string
timeout time.Duration
Expand Down
4 changes: 2 additions & 2 deletions canlink/writer/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package writer

const (
_decimal = 10

// format for 24-hour clock with minutes, seconds, and 4 digits
// of precision after decimal (period and colon delimiter)
_messageTimeFormat = "15:04:05.0000"
Expand All @@ -12,4 +12,4 @@ const (

// format for year, month and day with two digits each (period delimiter)
_filenameDateFormat = "2006-01-02"
)
)
16 changes: 8 additions & 8 deletions canlink/writer/converttostring.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package writer

import (
"strings"
"fmt"
"strconv"
"strings"

"encoding/json"
"go.uber.org/zap"
)

// Converts timestamped frames into strings for file writing
// Converts timestamped frames into strings for file writing
func convertToAscii(l *zap.Logger, timestampedFrame *TimestampedFrame) string {
var builder strings.Builder

Expand Down Expand Up @@ -43,19 +43,19 @@ func convertToAscii(l *zap.Logger, timestampedFrame *TimestampedFrame) string {
return builder.String()
}

// Converts timestamped frames into strings for file writing
// Converts timestamped frames into strings for file writing
func convertToJson(l *zap.Logger, timestampedFrame *TimestampedFrame) string {
jsonObject := map[string]interface{}{
"time": timestampedFrame.Time.Format(_messageTimeFormat),
"id": strconv.FormatUint(uint64(timestampedFrame.Frame.ID), _decimal),
"time": timestampedFrame.Time.Format(_messageTimeFormat),
"id": strconv.FormatUint(uint64(timestampedFrame.Frame.ID), _decimal),
"frameLength": strconv.FormatUint(uint64(timestampedFrame.Frame.Length), _decimal),
"bytes": timestampedFrame.Frame.Data,
"bytes": timestampedFrame.Frame.Data,
}

jsonData, err := json.Marshal(jsonObject)
if err != nil {
l.Error(err.Error())
}

return string(jsonData)
}
return string(jsonData)
}
2 changes: 1 addition & 1 deletion canlink/writer/timestampedframe.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ import (
type TimestampedFrame struct {
Frame can.Frame
Time time.Time
}
}
2 changes: 1 addition & 1 deletion canlink/writer/tracewriters.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ type WriterIface interface {
CreateTraceFile(traceDir string, busName string) error
WriteFrameToFile(frame *TimestampedFrame) error
CloseTraceFile() error
}
}
15 changes: 7 additions & 8 deletions canlink/writer/tracewriters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ func TestJsonConvertToString(t *testing.T) {
)

time := time.Date(2024, time.November, 27, 10, 30, 45, 0, time.Local)
timestampedFrame := TimestampedFrame{
timestampedFrame := TimestampedFrame{
Frame: can.Frame{
ID: 12,
ID: 12,
Length: 8,
Data: [8]byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88},
Data: [8]byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88},
},
Time: time,
}
Expand Down Expand Up @@ -68,13 +68,12 @@ func TestAsciiConvertToString(t *testing.T) {
".asc",
)


time := time.Date(2024, time.November, 27, 10, 30, 45, 0, time.Local)
timestampedFrame := TimestampedFrame{
timestampedFrame := TimestampedFrame{
Frame: can.Frame{
ID: 12,
ID: 12,
Length: 8,
Data: [8]byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88},
Data: [8]byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88},
},
Time: time,
}
Expand All @@ -95,4 +94,4 @@ func TestAsciiConvertToString(t *testing.T) {
%v; want
%v`, output, expected)
}
}
}
2 changes: 0 additions & 2 deletions canlink/writer/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,3 @@ func createEmptyTraceFile(dir string, busName string, fileSuffix string) (*os.Fi

return file, nil
}


39 changes: 19 additions & 20 deletions canlink/writer/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@ package writer

import (
"os"

"go.uber.org/zap"

"github.com/pkg/errors"
"go.uber.org/zap"
)

// Responsible for managing the trace file
type Writer struct {
traceFile *os.File
fileExtention string
traceFile *os.File
fileExtention string
convertToString func(*zap.Logger, *TimestampedFrame) string
l *zap.Logger
l *zap.Logger
}

func NewWriter(l *zap.Logger, fileExtention string) *Writer {
convertToStringMapping := map[string]func(*zap.Logger, *TimestampedFrame) string{
".jsonl": convertToJson,
".asc": convertToAscii,
}
".jsonl": convertToJson,
".asc": convertToAscii,
}
return &Writer{
l: l,
fileExtention: fileExtention,
l: l,
fileExtention: fileExtention,
convertToString: convertToStringMapping[fileExtention],
traceFile: nil,
traceFile: nil,
}
}

Expand All @@ -42,22 +42,21 @@ func (w *Writer) CreateTraceFile(traceDir string, busName string) error {
func (w *Writer) WriteFrameToFile(frame *TimestampedFrame) error {
line := w.convertToString(w.l, frame)

_, err := w.traceFile.WriteString(line)
if err != nil {
_, err := w.traceFile.WriteString(line)
if err != nil {
w.l.Info("cannot write to file")
return errors.Wrap(err, "writing to trace file")
}
return errors.Wrap(err, "writing to trace file")
}

_, err = w.traceFile.WriteString("\n")
if err != nil {
if err != nil {
w.l.Info("cannot write to file")
return errors.Wrap(err, "writing to trace file")
}
return errors.Wrap(err, "writing to trace file")
}

return nil
}


func (w *Writer) CloseTraceFile() error {
err := w.traceFile.Close()
if err != nil {
Expand All @@ -66,4 +65,4 @@ func (w *Writer) CloseTraceFile() error {
}

return nil
}
}

0 comments on commit efa824e

Please sign in to comment.