Skip to content

Commit

Permalink
template device definition logic in GetConfigURLs function
Browse files Browse the repository at this point in the history
  • Loading branch information
lauradeng23 committed Nov 14, 2023
1 parent c47fc4a commit a0595de
Show file tree
Hide file tree
Showing 17 changed files with 1,609 additions and 164 deletions.
97 changes: 79 additions & 18 deletions internal/controllers/device_config_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"encoding/binary"
"fmt"
"strconv"

p_grpc "github.com/DIMO-Network/device-definitions-api/pkg/grpc"
pb "github.com/DIMO-Network/devices-api/pkg/grpc"
Expand Down Expand Up @@ -244,15 +245,18 @@ func (d *DeviceConfigController) GetConfigURLs(c *fiber.Ctx, ud *pb.UserDevice)
ud.CANProtocol = models.CanProtocolTypeCAN11_500
}

// Device Definitions
// Retrieve Device Definition
var ddResponse *p_grpc.GetDeviceDefinitionResponse
deviceDefinitionID := ud.DeviceDefinitionId
ddResponse, err := d.deviceDefSvc.GetDeviceDefinitionByID(c.Context(), deviceDefinitionID)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": fmt.Sprintf("Failed to retrieve device definition for deviceDefinitionId: %s", deviceDefinitionID)})
}
vehicleYear := int(ddResponse.DeviceDefinitions[0].Type.Year)
vehicleMake := ddResponse.DeviceDefinitions[0].Type.MakeSlug
vehicleModel := ddResponse.DeviceDefinitions[0].Type.ModelSlug

// Determine Powertrain
var powerTrainType string
for _, attribute := range ddResponse.DeviceDefinitions[0].DeviceAttributes {
if attribute.Name == "powertrain_type" {
Expand All @@ -267,34 +271,91 @@ func (d *DeviceConfigController) GetConfigURLs(c *fiber.Ctx, ud *pb.UserDevice)
}
}

// Query templates, filter by protocol and powertrain
templates, err := models.Templates(
models.TemplateWhere.Protocol.EQ(ud.CANProtocol),
models.TemplateWhere.Powertrain.EQ(ud.PowerTrainType),
qm.Load(models.TemplateRels.TemplateNameDBCFile),
qm.Load(models.TemplateRels.TemplateNameTemplateVehicles),
qm.Load(models.TemplateRels.TemplateNameDeviceSetting),
deviceDefinitionIDInt64, err := strconv.ParseInt(deviceDefinitionID, 10, 64)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Invalid device definition ID"})
}

var matchedTemplateName string
var matchedDeviceDefinition *models.TemplateDeviceDefinition

// Try and find a template from template_device_definitions
deviceDefinitions, err := models.TemplateDeviceDefinitions(
models.TemplateDeviceDefinitionWhere.DeviceDefinitionID.EQ(deviceDefinitionIDInt64),
models.TemplateDeviceDefinitionWhere.YearStart.LTE(vehicleYear),
models.TemplateDeviceDefinitionWhere.YearEnd.GTE(vehicleYear),
models.TemplateDeviceDefinitionWhere.MakeSlug.EQ(vehicleMake),
).All(context.Background(), d.db)

if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": fmt.Sprintf("Failed to query templates for protocol: %s and powertrain: %s", ud.CANProtocol, ud.PowerTrainType)})
if err != nil && err != sql.ErrNoRows {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Failed to query template device definitions"})
}

// Filter templates by vehicle year range
var matchedTemplate *models.Template
for _, template := range templates {
for _, tv := range template.R.TemplateNameTemplateVehicles {
if vehicleYear >= tv.YearStart && vehicleYear <= tv.YearEnd {
matchedTemplate = template
for _, dd := range deviceDefinitions {
for _, model := range dd.ModelWhitelist {
if model == vehicleModel {
matchedDeviceDefinition = dd
break
}
}
if matchedTemplate != nil {
if matchedDeviceDefinition != nil {
break
}
}

if matchedDeviceDefinition != nil {
matchedTemplateName = matchedDeviceDefinition.TemplateName
}

var matchedTemplate *models.Template

// Fetch the template object if a name was found
if matchedTemplateName != "" {
matchedTemplate, err = models.Templates(
models.TemplateWhere.TemplateName.EQ(matchedTemplateName),
).One(context.Background(), d.db)

if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Failed to fetch the template by name"})
}
}

var templates []*models.Template

// If no specific template match is found, query by protocol and powertrain
if matchedTemplate == nil {
// Query templates, filter by protocol and powertrain
templates, err := models.Templates(
models.TemplateWhere.Protocol.EQ(ud.CANProtocol),
models.TemplateWhere.Powertrain.EQ(ud.PowerTrainType),
qm.Load(models.TemplateRels.TemplateNameDBCFile),
qm.Load(models.TemplateRels.TemplateNameTemplateVehicles),
qm.Load(models.TemplateRels.TemplateNameDeviceSetting),
).All(context.Background(), d.db)

if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": fmt.Sprintf("Failed to query templates for protocol: %s and powertrain: %s", ud.CANProtocol, ud.PowerTrainType)})
}

// Filter templates by vehicle year range
for _, template := range templates {
for _, tv := range template.R.TemplateNameTemplateVehicles {
if vehicleYear >= tv.YearStart && vehicleYear <= tv.YearEnd {
matchedTemplate = template
break
}
}
if matchedTemplate != nil {
break
}
}
if matchedTemplate == nil {
matchedTemplate = templates[0]
}
}

if matchedTemplate == nil {
matchedTemplate = templates[0]
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": "No matching template found"})
}

templateName := matchedTemplate.TemplateName
Expand Down
1 change: 0 additions & 1 deletion internal/core/services/device_definitions_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
type DeviceDefinitionsService interface {
GetDeviceDefinitionByID(ctx context.Context, id string) (*p_grpc.GetDeviceDefinitionResponse, error)
DecodeVIN(ctx context.Context, vin string) (*p_grpc.DecodeVinResponse, error)
// Add other methods as required.
}

type deviceDefinitionsService struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/infrastructure/db/models/boil_queries.go

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

4 changes: 3 additions & 1 deletion internal/infrastructure/db/models/boil_table_names.go

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

2 changes: 1 addition & 1 deletion internal/infrastructure/db/models/boil_types.go

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

2 changes: 1 addition & 1 deletion internal/infrastructure/db/models/boil_view_names.go

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

30 changes: 23 additions & 7 deletions internal/infrastructure/db/models/dbc_codes.go

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

2 changes: 1 addition & 1 deletion internal/infrastructure/db/models/dbc_files.go

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

2 changes: 1 addition & 1 deletion internal/infrastructure/db/models/device_settings.go

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

Loading

0 comments on commit a0595de

Please sign in to comment.