From 393ea7f2dda805018f49c2d37cd078b48c7a95ce Mon Sep 17 00:00:00 2001 From: dmytrozakharenko Date: Thu, 8 Aug 2024 17:26:03 -0400 Subject: [PATCH] - fixed linter --- config/config.go | 7 ++++--- internal/gateways/identity_api.go | 4 +++- internal/network/mqtt_store.go | 3 ++- internal/{gateways => util}/retry.go | 8 ++++++-- internal/vehicle_template.go | 11 ++++++----- main.go | 5 +++-- 6 files changed, 24 insertions(+), 14 deletions(-) rename internal/{gateways => util}/retry.go (97%) diff --git a/config/config.go b/config/config.go index 861bcf5..8e107d4 100644 --- a/config/config.go +++ b/config/config.go @@ -3,14 +3,15 @@ package config import ( "embed" "fmt" - "github.com/DIMO-Network/edge-network/internal/gateways" - "github.com/rs/zerolog" "io" "io/fs" "net/http" "os" "time" + "github.com/DIMO-Network/edge-network/internal/util" + "github.com/rs/zerolog" + "github.com/DIMO-Network/shared" ) @@ -107,7 +108,7 @@ func ReadConfig(logger zerolog.Logger, configFiles embed.FS, configURL, configFi // Get secrets from remote config remoteConfigPathOnDisk := "/opt/autopi/remote-config.json" - remoteConfig, err := gateways.Retry[Config](3, 1*time.Second, logger, func() (interface{}, error) { + remoteConfig, err := util.Retry[Config](3, 1*time.Second, logger, func() (interface{}, error) { return GetRemoteConfig(configURL, remoteConfigPathOnDisk) }) diff --git a/internal/gateways/identity_api.go b/internal/gateways/identity_api.go index 7ab35c8..1635110 100644 --- a/internal/gateways/identity_api.go +++ b/internal/gateways/identity_api.go @@ -6,6 +6,8 @@ import ( "io" "time" + "github.com/DIMO-Network/edge-network/internal/util" + "github.com/DIMO-Network/edge-network/config" "github.com/DIMO-Network/edge-network/internal/models" @@ -99,7 +101,7 @@ func (i *identityAPIService) fetchVehicleWithQuery(query string) (*models.Vehicl } if vehicleResponse.Data.AfterMarketDevice.Vehicle.TokenID == 0 { - return nil, Stop{fmt.Errorf(ErrNotFound.Error())} + return nil, util.Stop{Err: fmt.Errorf(ErrNotFound.Error())} } return &vehicleResponse.Data.AfterMarketDevice.Vehicle, nil diff --git a/internal/network/mqtt_store.go b/internal/network/mqtt_store.go index 8a5ccf1..e100aeb 100644 --- a/internal/network/mqtt_store.go +++ b/internal/network/mqtt_store.go @@ -2,9 +2,10 @@ package network import ( "fmt" + "sync" + mqtt "github.com/eclipse/paho.mqtt.golang" "github.com/eclipse/paho.mqtt.golang/packets" - "sync" ) // CustomFileStore is a custom implementation of the mqtt.FileStore interface. diff --git a/internal/gateways/retry.go b/internal/util/retry.go similarity index 97% rename from internal/gateways/retry.go rename to internal/util/retry.go index d5722e7..e306ab6 100644 --- a/internal/gateways/retry.go +++ b/internal/util/retry.go @@ -1,4 +1,4 @@ -package gateways +package util import ( "time" @@ -75,5 +75,9 @@ func RetryErrorOnly(attempts int, sleep time.Duration, logger zerolog.Logger, fn // Stop is an error that wraps an error and is used to indicate that we should not retry type Stop struct { - error + Err error +} + +func (s Stop) Error() string { + return s.Err.Error() } diff --git a/internal/vehicle_template.go b/internal/vehicle_template.go index 0be1170..5126b74 100644 --- a/internal/vehicle_template.go +++ b/internal/vehicle_template.go @@ -2,6 +2,7 @@ package internal import ( "fmt" + "github.com/DIMO-Network/edge-network/internal/util" "time" "github.com/DIMO-Network/shared/device" @@ -56,7 +57,7 @@ func (vt *vehicleTemplates) GetTemplateSettings(addr *common.Address, fwVersion } } - templateURLsRemote, err := gateways.Retry[device.ConfigResponse](3, 1*time.Second, vt.logger, func() (interface{}, error) { + templateURLsRemote, err := util.Retry[device.ConfigResponse](3, 1*time.Second, vt.logger, func() (interface{}, error) { return vt.vsd.GetUrlsByEthAddr(addr) }) if err != nil || templateURLsRemote == nil { @@ -88,7 +89,7 @@ func (vt *vehicleTemplates) GetTemplateSettings(addr *common.Address, fwVersion } // if we downloaded new template from remote, we need to update device config status by calling vehicle-signal-decoding-api - updateDeviceStatusErr := gateways.RetryErrorOnly(3, 1*time.Second, vt.logger, func() error { + updateDeviceStatusErr := util.RetryErrorOnly(3, 1*time.Second, vt.logger, func() error { return vt.vsd.UpdateDeviceConfigStatus(addr, fwVersion, unitID, templateURLsRemote) }) if updateDeviceStatusErr != nil { @@ -96,7 +97,7 @@ func (vt *vehicleTemplates) GetTemplateSettings(addr *common.Address, fwVersion } // PIDs, device settings, DBC (leave for later). If we can't get any of them, return what we have locally - remotePids, err := gateways.Retry[models.TemplatePIDs](3, 1*time.Second, vt.logger, func() (interface{}, error) { + remotePids, err := util.Retry[models.TemplatePIDs](3, 1*time.Second, vt.logger, func() (interface{}, error) { return vt.vsd.GetPIDs(templateURLsRemote.PidURL) }) if err != nil { @@ -109,7 +110,7 @@ func (vt *vehicleTemplates) GetTemplateSettings(addr *common.Address, fwVersion } } // get device settings - deviceSettings, err = gateways.Retry[models.TemplateDeviceSettings](3, 1*time.Second, vt.logger, func() (interface{}, error) { + deviceSettings, err = util.Retry[models.TemplateDeviceSettings](3, 1*time.Second, vt.logger, func() (interface{}, error) { return vt.vsd.GetDeviceSettings(templateURLsRemote.DeviceSettingURL) }) if err != nil { @@ -124,7 +125,7 @@ func (vt *vehicleTemplates) GetTemplateSettings(addr *common.Address, fwVersion } // get dbc file if templateURLsRemote.DbcURL != "" { - dbcFile, err = gateways.Retry[string](3, 1*time.Second, vt.logger, func() (interface{}, error) { + dbcFile, err = util.Retry[string](3, 1*time.Second, vt.logger, func() (interface{}, error) { return vt.vsd.GetDBC(templateURLsRemote.DbcURL) }) if err != nil { diff --git a/main.go b/main.go index 67fbe43..df2223c 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "encoding/json" "flag" "fmt" + "github.com/DIMO-Network/edge-network/internal/util" "os" "os/signal" "strings" @@ -75,7 +76,7 @@ func main() { logger.Info().Msgf("hardware version found: %s", hwRevision) // retry logic for getting ethereum address - ethAddr, ethErr := gateways.Retry[common.Address](3, 5*time.Second, logger, func() (interface{}, error) { + ethAddr, ethErr := util.Retry[common.Address](3, 5*time.Second, logger, func() (interface{}, error) { return commands.GetEthereumAddress(unitID) }) @@ -270,7 +271,7 @@ func setupBluez(name string) error { // getVehicleInfo queries identity-api with 3 retries logic, to get vehicle to device pairing info (vehicle NFT) func getVehicleInfo(logger zerolog.Logger, ethAddr *common.Address, conf dimoConfig.Config) (*models.VehicleInfo, error) { identityAPIService := gateways.NewIdentityAPIService(logger, conf) - vehicleDefinition, err := gateways.Retry[models.VehicleInfo](3, 1*time.Second, logger, func() (interface{}, error) { + vehicleDefinition, err := util.Retry[models.VehicleInfo](3, 1*time.Second, logger, func() (interface{}, error) { v, err := identityAPIService.QueryIdentityAPIForVehicle(*ethAddr) if v != nil && v.TokenID == 0 { return nil, fmt.Errorf("failed to query identity api for vehicle info - tokenId is zero")