Skip to content

Commit

Permalink
SM-5 - persist protocol (#212)
Browse files Browse the repository at this point in the history
* persist protocol

* Fix comment

---------

Co-authored-by: ricardo <[email protected]>
  • Loading branch information
rhvivancoeffio and ricardovivanco authored Oct 11, 2023
1 parent bf14809 commit 13e4173
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion internal/services/fingerprint/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"encoding/json"
"fmt"
"github.com/DIMO-Network/devices-api/internal/services"
"regexp"
"strings"
"time"
Expand Down Expand Up @@ -122,6 +123,32 @@ func (c *Consumer) HandleDeviceFingerprint(ctx context.Context, event *Event) er
return err
}

// Save Protocol
if ad.R.VehicleToken.R.UserDevice != nil {
md := services.UserDeviceMetadata{}
if err = ad.R.VehicleToken.R.UserDevice.Metadata.Unmarshal(&md); err != nil {
c.logger.Error().Msgf("Could not unmarshal userdevice metadata for device: %s", ad.R.VehicleToken.R.UserDevice.ID)
return err
}

if md.CANProtocol == nil {
protocol, err := ExtractProtocol(event.Data)
if err != nil {
c.logger.Error().Err(err)
}

if err == nil && protocol != nil {
md.CANProtocol = protocol
}
}

err = ad.R.VehicleToken.R.UserDevice.Metadata.Marshal(&md)
if err != nil {
c.logger.Error().Msgf("could not marshal userdevice metadata for device: %s", ad.R.VehicleToken.R.UserDevice.ID)
return err
}
}

c.logger.Info().Str("device-addr", event.Subject).Msg("issued vin credential")

return nil
Expand Down Expand Up @@ -192,7 +219,6 @@ func NumToWeekEnd(n int) time.Time {
}

var ErrNoVIN = errors.New("no VIN field")

var basicVINExp = regexp.MustCompile(`^[A-Z0-9]{17}$`)

// ExtractVIN extracts the vin field from a status update's data object.
Expand Down Expand Up @@ -221,3 +247,15 @@ func ExtractVIN(data []byte) (string, error) {

return vin, nil
}

func ExtractProtocol(data []byte) (*string, error) {
partialData := new(struct {
Protocol *string `json:"protocol"`
})

if err := json.Unmarshal(data, partialData); err != nil {
return nil, fmt.Errorf("failed parsing data field: %w", err)
}

return partialData.Protocol, nil
}

0 comments on commit 13e4173

Please sign in to comment.