/
device_config_controller.go
544 lines (473 loc) · 19.9 KB
/
device_config_controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
package controllers
import (
"database/sql"
"encoding/binary"
"encoding/json"
"fmt"
"io"
"strings"
"time"
"github.com/DIMO-Network/vehicle-signal-decoding/internal/gateways"
"github.com/DIMO-Network/vehicle-signal-decoding/internal/core/appmodels"
gdata "github.com/DIMO-Network/device-data-api/pkg/grpc"
"github.com/DIMO-Network/shared"
common2 "github.com/ethereum/go-ethereum/common"
"github.com/tidwall/gjson"
"golang.org/x/mod/semver"
"github.com/DIMO-Network/vehicle-signal-decoding/internal/core/common"
"github.com/volatiletech/sqlboiler/v4/types"
pb "github.com/DIMO-Network/devices-api/pkg/grpc"
"github.com/DIMO-Network/vehicle-signal-decoding/internal/config"
_ "github.com/DIMO-Network/vehicle-signal-decoding/internal/core/appmodels" // for swagger
"github.com/DIMO-Network/vehicle-signal-decoding/internal/core/services"
"github.com/DIMO-Network/vehicle-signal-decoding/internal/infrastructure/db/models"
"github.com/DIMO-Network/vehicle-signal-decoding/pkg/grpc"
"github.com/gofiber/fiber/v2"
_ "github.com/lib/pq" //nolint
"github.com/pkg/errors"
"github.com/rs/zerolog"
"google.golang.org/protobuf/proto"
)
type DeviceConfigController struct {
settings *config.Settings
log *zerolog.Logger
db *sql.DB
userDeviceSvc services.UserDeviceService
deviceDefSvc services.DeviceDefinitionsService
deviceTemplateService services.DeviceTemplateService
fwVersionAPI shared.HTTPClientWrapper
identityAPI gateways.IdentityAPI
}
const latestFirmwareURL = "https://binaries.dimo.zone/DIMO-Network/Macaron/releases/latest"
// NewDeviceConfigController constructor
func NewDeviceConfigController(settings *config.Settings, logger *zerolog.Logger, database *sql.DB, userDeviceSvc services.UserDeviceService,
deviceDefSvc services.DeviceDefinitionsService, deviceTemplateService services.DeviceTemplateService, identityAPI gateways.IdentityAPI) DeviceConfigController {
fwVersionAPI, _ := shared.NewHTTPClientWrapper(latestFirmwareURL, "", 10*time.Second, nil, true)
return DeviceConfigController{
settings: settings,
log: logger,
db: database,
userDeviceSvc: userDeviceSvc,
deviceDefSvc: deviceDefSvc,
deviceTemplateService: deviceTemplateService,
fwVersionAPI: fwVersionAPI,
identityAPI: identityAPI,
}
}
// DeviceTemplateStatusResponse status on template and firmware versions
type DeviceTemplateStatusResponse struct {
// IsTemplateUpToDate based on information we have, based on what was set last by mobile app
IsTemplateUpToDate bool `json:"isTemplateUpToDate"`
FirmwareVersion string `json:"firmwareVersion,omitempty"`
IsFirmwareUpToDate bool `json:"isFirmwareUpToDate"`
}
func bytesToUint32(b []byte) (uint32, error) {
u := binary.BigEndian.Uint32(padByteArray(b, 4))
return u, nil
}
// GetPIDsByTemplate godoc
// @Description Retrieves a list of PID configurations from the database given a template name
// @Tags device-config
// @Produce json
// @Produce application/x-protobuf
// @Success 200 {object} grpc.PIDRequests "Successfully retrieved PID Configurations"
// @Failure 404 "No PID Config data found for the given template name."
// @Param templateName path string true "template name"
// @Router /device-config/pids/{templateName} [get]
func (d *DeviceConfigController) GetPIDsByTemplate(c *fiber.Ctx) error {
templateNameWithVersion := c.Params("templateName")
// split out version
templateName, _ := parseOutTemplateAndVersion(templateNameWithVersion)
// ignore version for now since we're not really using it
template, err := models.FindTemplate(c.Context(), d.db, templateName)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("No template with name: %s found", templateName))
}
return errors.Wrapf(err, "Failed to retrieve Template %s", templateName)
}
pidConfigs, err := models.PidConfigs(
models.PidConfigWhere.TemplateName.EQ(templateName),
).All(c.Context(), d.db)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return fiber.NewError(fiber.StatusNotFound, "No PID data found for the given template name.")
}
return errors.Wrap(err, "Failed to retrieve PID Configs")
}
// Check if template has a parent and retrieve its PID configs
if template.ParentTemplateName.Valid {
pidConfigsParent, err := models.PidConfigs(
models.PidConfigWhere.TemplateName.EQ(template.ParentTemplateName.String),
).All(c.Context(), d.db)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return errors.Wrap(err, "Failed to retrieve Parent PID Configs")
}
// Append the parent pidConfigs to the original pidConfigs
pidConfigs = append(pidConfigs, pidConfigsParent...)
}
protoPIDs := &grpc.PIDRequests{
TemplateName: templateName,
}
if template != nil {
protoPIDs.Version = template.Version
}
for _, pidConfig := range pidConfigs {
headerUint32, err := bytesToUint32(pidConfig.Header)
if err != nil {
d.log.Err(err).Send()
return fiber.NewError(fiber.StatusInternalServerError, "invalid header bytes configuration: "+err.Error())
}
modeUint32, err := bytesToUint32(pidConfig.Mode)
if err != nil {
d.log.Err(err).Send()
return fiber.NewError(fiber.StatusInternalServerError, "invalid mode bytes configuration: "+err.Error())
}
pidUint32, err := bytesToUint32(pidConfig.Pid)
if err != nil {
d.log.Err(err).Send()
return fiber.NewError(fiber.StatusInternalServerError, "invalid pid bytes configuration: "+err.Error())
}
pid := &grpc.PIDConfig{
Name: pidConfig.SignalName,
Header: headerUint32,
Mode: modeUint32,
Pid: pidUint32,
Formula: common.PrependFormulaTypeDefault(pidConfig.Formula),
IntervalSeconds: uint32(pidConfig.IntervalSeconds),
}
// the pid can override the protocol, otherwise use one at template level.
if pidConfig.Protocol.Valid {
pid.Protocol = pidConfig.Protocol.String
} else {
pid.Protocol = template.Protocol
}
if pidConfig.CanFlowControlClear.Valid {
pid.CanFlowControlClear = pidConfig.CanFlowControlClear.Bool
}
if pidConfig.CanFlowControlIDPair.Valid {
pid.CanFlowControlIdPair = pidConfig.CanFlowControlIDPair.String
}
protoPIDs.Requests = append(protoPIDs.Requests, pid)
}
acceptHeader := c.Get("Accept", "application/json")
if acceptHeader == "application/x-protobuf" {
out, err := proto.Marshal(protoPIDs)
if err != nil {
return errors.Wrap(err, "Failed to serialize to protobuf")
}
c.Set("Content-Type", "application/x-protobuf")
return c.Send(out)
}
return c.JSON(protoPIDs)
}
// GetDeviceSettingsByName godoc
// @Description Fetches the device settings configurations from device_settings table given a name. Note that device settings mostly only vary by powertrain and
// @Description may or may not be attached to a specific template. To return protobuf: "application/x-protobuf"
// @Description Note that the templateName returned here is actually the device setting name
// @Tags device-config
// @Produce json
// @Produce application/x-protobuf
// @Success 200 {object} grpc.DeviceSetting "Successfully retrieved Device Settings"
// @Failure 404 "No Device Settings data found for the given name."
// @Param name path string true "name"
// @Router /device-config/settings/{name} [get]
func (d *DeviceConfigController) GetDeviceSettingsByName(c *fiber.Ctx) error {
nameWithVersion := c.Params("name")
if len(nameWithVersion) == 0 {
return fiber.NewError(fiber.StatusBadRequest, "name for settings empty")
}
name, _ := parseOutTemplateAndVersion(nameWithVersion)
// ignore version for now since we're not really using it
dbDeviceSettings, err := models.FindDeviceSetting(c.Context(), d.db, name)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return fiber.NewError(fiber.StatusNotFound, "No Device Settings data found with name: "+name)
}
return errors.Wrap(err, "Failed to retrieve Device Settings")
}
// Deserialize the settings JSONB into the SettingsData struct
var settings appmodels.SettingsData
if dbDeviceSettings.Settings.Valid {
jsonBytes, err := dbDeviceSettings.Settings.MarshalJSON()
if err != nil {
return errors.Wrap(err, "Failed to marshal settings JSON")
}
if err = json.Unmarshal(jsonBytes, &settings); err != nil {
return errors.Wrap(err, "Failed to deserialize settings data")
}
} else {
return fiber.NewError(fiber.StatusNotFound, "Settings data is null")
}
protoDeviceSettings := &grpc.DeviceSetting{
TemplateName: dbDeviceSettings.Name, // in future add a Name field, once safe to change proto
SafetyCutOutVoltage: float32(settings.SafetyCutOutVoltage),
SleepTimerEventDrivenPeriodSecs: float32(settings.SleepTimerEventDrivenPeriodSecs),
WakeTriggerVoltageLevel: float32(settings.WakeTriggerVoltageLevel),
SleepTimerEventDrivenIntervalSecs: float32(3600), // not used by Macaron
SleepTimerInactivityAfterSleepSecs: float32(21600),
SleepTimerInactivityFallbackIntervalSecs: float32(21600),
//TemplateName: dbDeviceSettings.TemplateName.String, // in future we could do this, could be empty
//Version: "v1.0.1", // for future - once safe to change proto file
}
acceptHeader := c.Get("Accept", "application/json")
if acceptHeader == "application/x-protobuf" {
out, err := proto.Marshal(protoDeviceSettings)
if err != nil {
return errors.Wrap(err, "Failed to serialize to protobuf")
}
c.Set("Content-Type", "application/x-protobuf")
return c.Send(out)
}
return c.JSON(protoDeviceSettings)
}
// GetDBCFileByTemplateName godoc
// @Description Fetches the DBC file from the dbc_files table given a template name
// @Tags device-config
// @Produce plain
// @Success 200 {string} string "Successfully retrieved DBC file"
// @Failure 404 "No DBC file found for the given template name."
// @Param templateName path string true "template name"
// @Router /device-config/dbc/{templateName} [get]
func (d *DeviceConfigController) GetDBCFileByTemplateName(c *fiber.Ctx) error {
templateNameWithVersion := c.Params("templateName")
templateName, _ := parseOutTemplateAndVersion(templateNameWithVersion)
// ignore version since not really using right now
dbResult, err := models.DBCFiles(
models.DBCFileWhere.TemplateName.EQ(templateName)).One(c.Context(), d.db)
// Error handling
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return c.Status(fiber.StatusNotFound).SendString(fmt.Sprintf("No DBC file found for template name: %s", templateName))
}
return errors.Wrap(err, "Failed to retrieve DBC File")
}
// Return the DBC file itself
if c.Accepts("text/plain") == "text/plain" {
c.Status(fiber.StatusOK).Set("Content-Type", "text/plain")
return c.SendString(dbResult.DBCFile)
}
return c.Status(fiber.StatusNotAcceptable).SendString("Not Acceptable")
}
// GetConfigURLsFromVIN godoc
// @Description Retrieve the URLs for PID, DeviceSettings, and DBC configuration based on a given VIN. These could be empty if not configs available
// @Tags device-config
// @Produce json
// @Success 200 {object} appmodels.DeviceConfigResponse "Successfully retrieved configuration URLs"
// @Failure 404 "Not Found - No templates available for the given parameters"
// @Param vin path string true "vehicle identification number (VIN)"
// @Param protocol query string false "CAN Protocol, '6' or '7', 8,9,66,77,88,99"
// @Router /device-config/vin/{vin}/urls [get]
func (d *DeviceConfigController) GetConfigURLsFromVIN(c *fiber.Ctx) error {
vin := c.Params("vin")
protocol := c.Query("protocol", "")
ud, err := d.userDeviceSvc.GetUserDeviceByVIN(c.Context(), vin)
if err != nil {
definitionResp, err := d.deviceDefSvc.DecodeVIN(c.Context(), vin)
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": fmt.Sprintf("could not decode VIN, contact support if you're sure this is valid VIN: %s", vin)})
}
ud = &pb.UserDevice{
DeviceDefinitionId: definitionResp.DeviceDefinitionId,
PowerTrainType: definitionResp.Powertrain,
CANProtocol: protocol,
Vin: &vin,
}
if len(definitionResp.DeviceStyleId) > 0 {
ud.DeviceStyleId = &definitionResp.DeviceStyleId
}
}
response, err := d.deviceTemplateService.ResolveDeviceConfiguration(c, ud, nil)
if err != nil {
return err
}
return c.JSON(response)
}
// GetConfigURLsFromEthAddr godoc
// @Description Retrieve the URLs for PID, DeviceSettings, and DBC configuration based on device's Ethereum Address. These could be empty if not configs available
// @Tags device-config
// @Produce json
// @Success 200 {object} appmodels.DeviceConfigResponse "Successfully retrieved configuration URLs"
// @Failure 404 "Not Found - No templates available for the given parameters"
// @Failure 400 "incorrect eth addr format"
// @Param ethAddr path string true "Ethereum Address"
// @Param protocol query string false "CAN Protocol, '6' or '7'"
// @Router /device-config/eth-addr/{ethAddr}/urls [get]
func (d *DeviceConfigController) GetConfigURLsFromEthAddr(c *fiber.Ctx) error {
ethAddr := c.Params("ethAddr")
protocol := c.Query("protocol", "")
// todo query database for device eth addr to template mapping
vehicle, err := d.identityAPI.QueryIdentityAPIForVehicle(common2.HexToAddress(ethAddr))
if err != nil {
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": fmt.Sprintf("no minted vehicle for device EthAddr: %s", ethAddr)})
}
// we still need this to get the powertrain
ud, err := d.userDeviceSvc.GetUserDeviceByEthAddr(c.Context(), common2.HexToAddress(ethAddr))
if err != nil {
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": fmt.Sprintf("no connected user device found for EthAddr: %s", ethAddr)})
}
if protocol != "" {
ud.CANProtocol = protocol
}
response, err := d.deviceTemplateService.ResolveDeviceConfiguration(c, ud, vehicle)
if err != nil {
return err
}
return c.JSON(response)
}
// GetConfigStatusByEthAddr godoc
// @Description Helps client determine if template (pids, dbc, settings) are up to date or not for the device with the given eth addr.
// @Tags device-config
// @Produce json
// @Success 200 {object} DeviceTemplateStatusResponse "Successfully retrieved configuration URLs"
// @Failure 404 "Not Found - we haven't seen this device yet, assume template not up to date"
// @Failure 400 "incorrect eth addr format"
// @Param ethAddr path string true "Ethereum Address"
// @Router /device-config/eth-addr/{ethAddr}/status [get]
func (d *DeviceConfigController) GetConfigStatusByEthAddr(c *fiber.Ctx) error {
ethAddr := c.Params("ethAddr")
addr := common2.HexToAddress(ethAddr)
// we use this to know what the config should be
ud, err := d.userDeviceSvc.GetUserDeviceByEthAddr(c.Context(), addr)
if err != nil {
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": fmt.Sprintf("no connected user device found for EthAddr: %s", ethAddr)})
}
dts, err := models.DeviceTemplateStatuses(models.DeviceTemplateStatusWhere.DeviceEthAddr.EQ(addr.Bytes())).One(c.Context(), d.db)
if err != nil {
if !errors.Is(err, sql.ErrNoRows) {
return err
}
}
deviceFWVers := ""
isTemplateUpdated := false
if dts != nil {
deviceFWVers = dts.FirmwareVersion.String
// figure out what the config should be
deviceConfiguration, err := d.deviceTemplateService.ResolveDeviceConfiguration(c, ud, nil)
if err != nil {
return err
}
// if all this is true then we know we're up to date
if dts.TemplateDBCURL.String == deviceConfiguration.DbcURL &&
dts.TemplatePidURL.String == deviceConfiguration.PidURL &&
dts.TemplateSettingsURL.String == deviceConfiguration.DeviceSettingURL {
isTemplateUpdated = true
}
}
if deviceFWVers == "" {
// get fw version from device if any
deviceData, err := d.userDeviceSvc.GetRawDeviceData(c.Context(), ud.Id)
if err != nil {
d.log.Err(err).Str("device_address", ethAddr).Msg("failed to get device data")
if dts == nil {
// if don't get anything from device-data-api and dts is nil, nothing we can do
return fiber.NewError(fiber.StatusNotFound, "haven't seen device with eth addr yet: "+ethAddr)
}
}
deviceFWVers = parseOutFWVersion(deviceData)
}
latestFirmwareStr, err := d.getLatestFWVersion()
if err != nil {
return err
}
return c.JSON(DeviceTemplateStatusResponse{
IsTemplateUpToDate: isTemplateUpdated,
IsFirmwareUpToDate: isFwUpToDate(latestFirmwareStr, deviceFWVers),
FirmwareVersion: deviceFWVers,
})
}
// PatchConfigStatusByEthAddr godoc
// @Description Set what template and/or firmware was applied. None of the properties are required. Will not be set if not passed in.
// @Tags device-config
// @Produce json
// @Success 200 "Successfully updated"
// @Failure 500 "unable to parse request or storage failure"
// @Param ethAddr path string true "Ethereum Address"
// @Param config body DeviceTemplateStatusPatch true "set any properties that were updated on the device"
// @Security BearerAuth
// @Router /device-config/eth-addr/{ethAddr}/status [patch]
func (d *DeviceConfigController) PatchConfigStatusByEthAddr(c *fiber.Ctx) error {
ethAddr := c.Params("ethAddr")
addr := common2.HexToAddress(ethAddr)
payload := DeviceTemplateStatusPatch{}
err := c.BodyParser(&payload)
if err != nil {
return err
}
_, err = d.deviceTemplateService.StoreDeviceConfigUsed(c.Context(), addr, payload.DbcURL, payload.PidsURL, payload.SettingsURL, payload.FirmwareVersionApplied)
if err != nil {
return err
}
return c.SendStatus(fiber.StatusOK)
}
func parseOutFWVersion(data *gdata.RawDeviceDataResponse) string {
for _, item := range data.Items {
v := gjson.GetBytes(item.SignalsJsonData, "fwVersion.value").Str
if len(v) > 1 {
if v[0:1] != "v" {
return "v" + v
}
return v
}
}
return ""
}
func parseOutTemplateAndVersion(templateNameWithVersion string) (string, string) {
parts := strings.Split(templateNameWithVersion, "@")
if len(parts) == 2 {
return parts[0], parts[1]
}
return parts[0], ""
}
type DeviceTemplateStatusPatch struct {
// SettingsURL template settings url with version as returned from api
SettingsURL *string `json:"settingsUrl"`
// PidsURL template pids url with version as returned from api
PidsURL *string `json:"pidsUrl"`
// DBCFileURL template dbc file url with version as returned from api
DbcURL *string `json:"dbcUrl"`
// FirmwareVersionApplied version of firmware that was confirmed installed on device
FirmwareVersionApplied *string `json:"firmwareVersionApplied"`
}
func padByteArray(input []byte, targetLength int) []byte {
if len(input) >= targetLength {
return input // No need to pad if the input is already longer or equal to the target length
}
padded := make([]byte, targetLength-len(input))
return append(padded, input...)
}
// modelMatch simply returns if the modelName is in the model List
func modelMatch(modelList types.StringArray, modelName string) bool {
for _, m := range modelList {
if strings.EqualFold(m, modelName) {
return true
}
}
return false
}
func isFwUpToDate(latest, current string) bool {
if len(latest) > 1 && len(current) > 1 {
if latest[0:1] != "v" {
latest = "v" + latest
}
if current[0:1] != "v" {
current = "v" + current
}
if semver.Compare(latest, current) == 0 {
return true
}
}
return false
}
// calls well known dimo URL to get latest Macaron fw version
func (d *DeviceConfigController) getLatestFWVersion() (string, error) {
// get latest fw version. at some point will need to know device hw type to know this better
res, err := d.fwVersionAPI.ExecuteRequest("", "GET", nil)
if err != nil {
return "", errors.Wrap(err, "unable to get latest macaron firmware")
}
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
latestFirmwareStr := gjson.GetBytes(body, "name").Str
return latestFirmwareStr, nil
}