Skip to content

Commit

Permalink
read and check for job already done from disk
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesReate committed Oct 11, 2024
1 parent 423c255 commit 5f0afc5
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 5 deletions.
29 changes: 29 additions & 0 deletions internal/loggers/mocks/template_store_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions internal/loggers/template_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
DeviceSettingsFile = "/opt/autopi/device-settings.json"
VehicleInfoFile = "/opt/autopi/vehicle-info.json"
DBCFile = "/opt/autopi/dbc-settings.dbc"
CANDumpInfoFile = "/opt/autopi/can-dump-info.json"
)

//go:generate mockgen -source template_store.go -destination mocks/template_store_mock.go
Expand All @@ -48,6 +49,10 @@ type SettingsStore interface {
ReadVehicleInfo() (*models.VehicleInfo, error)
WriteVehicleInfo(settings models.VehicleInfo) error
DeleteAllSettings() error

ReadCANDumpInfo() (*models.CANDumpInfo, error)
// WriteCANDumpInfo sets current date on disk
WriteCANDumpInfo() error
}

// settingsStore wraps reading and writing different configurations locally
Expand All @@ -64,6 +69,7 @@ func (ts *settingsStore) DeleteAllSettings() error {
errs = append(errs, ts.deleteConfig(VehicleInfoFile))
errs = append(errs, ts.deleteConfig(TemplateURLsFile))
errs = append(errs, ts.deleteConfig(DBCFile))
errs = append(errs, ts.deleteConfig(CANDumpInfoFile))

// Combine errors and print the result
if combinedErr := combineErrors(errs); combinedErr != nil {
Expand Down Expand Up @@ -235,6 +241,33 @@ func (ts *settingsStore) WriteVehicleInfo(settings models.VehicleInfo) error {
return nil
}

func (ts *settingsStore) ReadCANDumpInfo() (*models.CANDumpInfo, error) {
data, err := ts.readConfig(CANDumpInfoFile)
if err != nil {
return nil, fmt.Errorf("error reading file: %s", err)
}
cdi := &models.CANDumpInfo{}

err = json.Unmarshal(data, cdi)
if err != nil {
return nil, fmt.Errorf("failed to unmarshall vehicleInfo: %s", err)
}

return cdi, nil
}

// WriteCANDumpInfo just sets the date to now
func (ts *settingsStore) WriteCANDumpInfo() error {
d := models.CANDumpInfo{DateExecuted: time.Now()}

err := ts.writeConfig(CANDumpInfoFile, d)
if err != nil {
return err
}

return nil
}

func (ts *settingsStore) readConfig(filePath string) ([]byte, error) {
ts.mu.Lock()
defer ts.mu.Unlock()
Expand Down
5 changes: 5 additions & 0 deletions internal/models/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package models
import (
"github.com/DIMO-Network/edge-network/internal/api"
"github.com/DIMO-Network/shared"
"time"
)

type CanDumpData struct {
Expand Down Expand Up @@ -101,6 +102,10 @@ type VehicleInfo struct {
VehicleDefinition VehicleDefinition `json:"definition"`
}

type CANDumpInfo struct {
DateExecuted time.Time `json:"dateExecuted"`
}

type VehicleDefinition struct {
Make string `json:"make"`
Model string `json:"model"`
Expand Down
20 changes: 16 additions & 4 deletions internal/signal_frame_dump_q.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package internal
import (
"encoding/json"
"fmt"
"github.com/DIMO-Network/edge-network/internal/loggers"
"sync"
"time"

Expand All @@ -21,13 +22,24 @@ type SignalFrameDumpQueue struct {
sync.RWMutex
logger zerolog.Logger
dataSender network.DataSender
lss loggers.SettingsStore
}

func NewSignalFrameDumpQueue(logger zerolog.Logger, sender network.DataSender) *SignalFrameDumpQueue {
// todo set signal dump queue `jobDone` to true if we have something on disk
// leverage the config service to read something from disk and check
func NewSignalFrameDumpQueue(logger zerolog.Logger, sender network.DataSender, lss loggers.SettingsStore) *SignalFrameDumpQueue {
// check if jobDone should just be marked to true
cdi, _ := lss.ReadCANDumpInfo()

return &SignalFrameDumpQueue{signalFrames: make(map[string][]models.SignalCanFrameDump), logger: logger,
dataSender: sender}
dataSender: sender, lss: lss, jobDone: determineJobDone(cdi)}
}

func determineJobDone(cdi *models.CANDumpInfo) bool {
jd := false
// 2024-10-01 ... now is 2024-10-15 -> false
if cdi != nil && cdi.DateExecuted.After(time.Now().Add(-31*24*time.Hour)) {
jd = true
}
return jd
}

func (scf *SignalFrameDumpQueue) Enqueue(signal models.SignalCanFrameDump) {
Expand Down
47 changes: 47 additions & 0 deletions internal/signal_frame_dump_q_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package internal

import (
"testing"
"time"

"github.com/DIMO-Network/edge-network/internal/models"
)

func TestDetermineJobDone(t *testing.T) {
now := time.Now()

testCases := []struct {
desc string
input *models.CANDumpInfo
want bool
}{
{
desc: "nil CANDumpInfo",
input: nil,
want: false,
},
{
desc: "DateExecuted is older than 30 days",
input: &models.CANDumpInfo{DateExecuted: now.Add(-31 * 24 * time.Hour)},
want: false,
},
{
desc: "DateExecuted is equal to 30 days",
input: &models.CANDumpInfo{DateExecuted: now.Add(-30 * 24 * time.Hour)},
want: true,
},
{
desc: "DateExecuted is less than 30 days",
input: &models.CANDumpInfo{DateExecuted: now.Add(-29 * 24 * time.Hour)},
want: true,
},
}

for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
if got := determineJobDone(tC.input); got != tC.want {
t.Errorf("determineJobDone() = %v, want %v for input %v", got, tC.want, tC.input)
}
})
}
}
2 changes: 1 addition & 1 deletion internal/worker_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func NewWorkerRunner(addr *common.Address, loggerSettingsSvc loggers.SettingsSto
signalsQueue := &SignalsQueue{lastTimeChecked: make(map[string]time.Time), failureCount: make(map[string]int), signals: make(map[string][]models.SignalData)}
// Interval for sending status payload to cloud. Status payload contains obd signals and non-obd signals.
interval := 20 * time.Second
sdfq := NewSignalFrameDumpQueue(logger, dataSender)
sdfq := NewSignalFrameDumpQueue(logger, dataSender, loggerSettingsSvc)
return &workerRunner{ethAddr: addr, loggerSettingsSvc: loggerSettingsSvc,
dataSender: dataSender, logger: logger, fingerprintRunner: fpRunner, pids: pids, deviceSettings: settings,
signalsQueue: signalsQueue, sendPayloadInterval: interval, device: device, vehicleInfo: vehicleInfo,
Expand Down

0 comments on commit 5f0afc5

Please sign in to comment.