Skip to content

Commit

Permalink
- fixed linter
Browse files Browse the repository at this point in the history
  • Loading branch information
zakharenkodmytro committed Aug 8, 2024
1 parent 1a6e5a9 commit 393ea7f
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 14 deletions.
7 changes: 4 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
})

Expand Down
4 changes: 3 additions & 1 deletion internal/gateways/identity_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion internal/network/mqtt_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 6 additions & 2 deletions internal/gateways/retry.go → internal/util/retry.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gateways
package util

import (
"time"
Expand Down Expand Up @@ -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()
}
11 changes: 6 additions & 5 deletions internal/vehicle_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package internal

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

"github.com/DIMO-Network/shared/device"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -88,15 +89,15 @@ 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 {
vt.logger.Err(updateDeviceStatusErr).Msg(fmt.Sprintf("failed to update device config status using ethAddr %s", addr.String()))
}

// 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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"flag"
"fmt"
"github.com/DIMO-Network/edge-network/internal/util"
"os"
"os/signal"
"strings"
Expand Down Expand Up @@ -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)
})

Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 393ea7f

Please sign in to comment.