Skip to content

Commit

Permalink
SM-637 delete all settings when get VIN over BLE (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesReate authored May 31, 2024
1 parent a651bda commit 3cfcaf8
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 33 deletions.
21 changes: 14 additions & 7 deletions ble_chars.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ func setupBluetoothApplication(logger zerolog.Logger, coldBoot bool, vinLogger l
vinChar.OnRead(func(_ *service.Char, _ map[string]interface{}) (resp []byte, err error) {
defer func() {
if err != nil {
logger.Err(err).Ctx(context.WithValue(context.Background(), internal.LogToMqtt, "true")).Msgf("Error retrieving VIN: %s", err)
logger.Err(err).Ctx(context.WithValue(context.Background(), internal.LogToMqtt, "true")).Msgf("error retrieving VIN via BLE: %s", err)
}
}()

Expand All @@ -427,25 +427,32 @@ func setupBluetoothApplication(logger zerolog.Logger, coldBoot bool, vinLogger l
logger.Info().Msgf("Returning cached VIN: %s", lastVIN)
return
}
// clear all settings since this is most likely a brand new pairing
errDel := lss.DeleteAllSettings()
if errDel != nil {
logger.Err(errDel).Msgf("there was one or more errors deleting settings from disk, continuing")
}

vinResp, err := vinLogger.GetVIN(unitID, nil)
if err != nil {
err = nil
logger.Err(err).Msgf("Unable to get VIN")
resp = []byte("00000000000000000")
return
}

logger.Info().Msgf("Got Protocol: %s", vinResp.Protocol) // need to do something with protocol to set right template
logger.Info().Msgf("Got VIN: %s", vinResp.VIN)
logger.Info().Msgf("Got Protocol: %s", vinResp.Protocol) // verify using protocol when requesting template
logger.Info().Ctx(context.WithValue(context.Background(), internal.LogToMqtt, "true")).Msg("Got VIN via BLE") //note we don't send the VIN to cloud logs for PII
logger.Info().Msgf(vinResp.VIN)
lastVIN = vinResp.VIN
lastProtocol = vinResp.Protocol
resp = []byte(lastVIN)
// we want to do this each time in case the device is being paired to a different vehicle
err = lss.WriteVINConfig(models.VINLoggerSettings{VINQueryName: vinResp.QueryName, VIN: lastVIN})
if err != nil {
logger.Err(err).Msgf("failed to save vin query name in settings: %s", err)
errSaveCfg := lss.WriteVINConfig(models.VINLoggerSettings{VINQueryName: vinResp.QueryName, VIN: lastVIN})
if errSaveCfg != nil {
logger.Err(errSaveCfg).Ctx(context.WithValue(context.Background(), internal.LogToMqtt, "true")).
Msgf("failed to save vin query name in settings: %s", err)
}
// todo restart the application?
return
})

Expand Down
13 changes: 7 additions & 6 deletions certificate/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ import (
"encoding/json"
"encoding/pem"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
"time"

"github.com/DIMO-Network/edge-network/commands"
"github.com/DIMO-Network/edge-network/config"
"github.com/ethereum/go-ethereum/common"
Expand All @@ -23,12 +30,6 @@ import (
"github.com/smallstep/certificates/ca"
"go.step.sm/crypto/jose"
"go.step.sm/crypto/x509util"
"io"
"net/http"
"net/url"
"os"
"strings"
"time"
)

const domain = "http://127.0.0.1:10000"
Expand Down
13 changes: 7 additions & 6 deletions certificate/certificate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@ import (
"crypto/x509"
"crypto/x509/pkix"
"fmt"
"math/big"
"net/http"
"os"
"reflect"
"testing"
"time"

dimoConfig "github.com/DIMO-Network/edge-network/config"
"github.com/google/uuid"
"github.com/jarcoal/httpmock"
"github.com/rs/zerolog"
"github.com/smallstep/certificates/api"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
"math/big"
"net/http"
"os"
"reflect"
"testing"
"time"
)

func TestCertificateService_GetOauthToken(t *testing.T) {
Expand Down
9 changes: 5 additions & 4 deletions commands/vehicle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ package commands
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"testing"

"github.com/DIMO-Network/edge-network/internal/api"
"github.com/DIMO-Network/edge-network/internal/models"
"github.com/google/uuid"
"github.com/jarcoal/httpmock"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
"io"
"net/http"
"os"
"testing"
)

func Test_isValidHex(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package config
import (
"embed"
"fmt"
"github.com/DIMO-Network/shared"
"io/fs"
"os"

"github.com/DIMO-Network/shared"
)

// Config represents the configuration for the edge-network
Expand Down
3 changes: 2 additions & 1 deletion internal/gateways/identity_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package gateways
import (
"encoding/json"
"fmt"
"github.com/DIMO-Network/edge-network/config"
"io"
"time"

"github.com/DIMO-Network/edge-network/config"

"github.com/DIMO-Network/edge-network/internal/models"
"github.com/DIMO-Network/shared"
"github.com/ethereum/go-ethereum/common"
Expand Down
3 changes: 2 additions & 1 deletion internal/gateways/vehicle_signal_decoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package gateways
import (
"encoding/json"
"fmt"
"github.com/DIMO-Network/edge-network/config"
"io"
"time"

"github.com/DIMO-Network/edge-network/config"

"github.com/rs/zerolog"

"github.com/DIMO-Network/edge-network/internal/models"
Expand Down
16 changes: 15 additions & 1 deletion internal/loggers/mocks/template_store_mock.go

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

3 changes: 2 additions & 1 deletion internal/loggers/passive_can_dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (
"encoding/base64"
"encoding/json"
"errors"
dimoConfig "github.com/DIMO-Network/edge-network/config"
"os"
"strconv"
"time"

dimoConfig "github.com/DIMO-Network/edge-network/config"

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

"github.com/DIMO-Network/edge-network/internal/network"
Expand Down
46 changes: 46 additions & 0 deletions internal/loggers/template_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import (
"encoding/json"
"fmt"
"os"
"strings"
"sync"

"github.com/pkg/errors"

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

Expand Down Expand Up @@ -33,13 +36,30 @@ type TemplateStore interface {

ReadVehicleInfo() (*models.VehicleInfo, error)
WriteVehicleInfo(settings models.VehicleInfo) error
DeleteAllSettings() error
}

// templateStore wraps reading and writing different configurations locally
type templateStore struct {
mu sync.Mutex
}

func (ts *templateStore) DeleteAllSettings() error {
var errs []error
// Call each method and collect any errors
errs = append(errs, ts.deleteConfig(VINLoggerFile))
errs = append(errs, ts.deleteConfig(PIDConfigFile))
errs = append(errs, ts.deleteConfig(DeviceSettingsFile))
errs = append(errs, ts.deleteConfig(VehicleInfoFile))
errs = append(errs, ts.deleteConfig(TemplateURLsFile))

// Combine errors and print the result
if combinedErr := combineErrors(errs); combinedErr != nil {
return combinedErr
}
return nil
}

func (ts *templateStore) ReadTemplateURLs() (*models.TemplateURLs, error) {
data, err := ts.readConfig(TemplateURLsFile)
if err != nil {
Expand Down Expand Up @@ -204,3 +224,29 @@ func (ts *templateStore) writeConfig(filePath string, settings interface{}) erro
}
return nil
}

func (ts *templateStore) deleteConfig(filePath string) error {
ts.mu.Lock()
defer ts.mu.Unlock()
// Open the file for writing (create if it doesn't exist)
err := os.Remove(filePath)
if err != nil {
return fmt.Errorf("error deleting file: %s", err)
}

return nil
}

// CombineErrors combines multiple errors into a single error
func combineErrors(errorList []error) error {
var errorMessages []string
for _, err := range errorList {
if err != nil {
errorMessages = append(errorMessages, err.Error())
}
}
if len(errorMessages) == 0 {
return nil
}
return errors.New(strings.Join(errorMessages, "; "))
}
5 changes: 3 additions & 2 deletions internal/loggers/vin_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package loggers
import (
"encoding/hex"
"fmt"
"github.com/DIMO-Network/edge-network/commands"
"github.com/DIMO-Network/edge-network/internal/models"
"regexp"
"strconv"
"sync"
"time"
"unicode"

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

"github.com/rs/zerolog"

"github.com/google/uuid"
Expand Down
3 changes: 2 additions & 1 deletion internal/models/network.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package models

import (
"github.com/DIMO-Network/edge-network/internal/api"
"time"

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

type CanDumpData struct {
Expand Down
3 changes: 2 additions & 1 deletion internal/network/data_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/DIMO-Network/edge-network/config"
"os"
"strconv"
"strings"
"time"

"github.com/DIMO-Network/edge-network/config"

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

"github.com/rs/zerolog"
Expand Down
3 changes: 2 additions & 1 deletion internal/network/data_sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
dimoConfig "github.com/DIMO-Network/edge-network/config"
"io"
"net/http"
"os"
"strings"
"testing"
"time"

dimoConfig "github.com/DIMO-Network/edge-network/config"

"github.com/DIMO-Network/edge-network/internal/models"
"github.com/stretchr/testify/assert"

Expand Down

0 comments on commit 3cfcaf8

Please sign in to comment.