Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Big Tesla merge #330

Merged
merged 27 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
25ef73f
Add Telemetry/Subscribe command to Tesla Commands (#305)
0xdev22 Apr 19, 2024
b02fefd
Feature/si 2557 create endpoint to fetch available commands (#308)
0xdev22 Apr 22, 2024
89fc42f
Add tesla virtual key status to integration status endpoint (#311)
0xdev22 May 1, 2024
2e4bad9
Register device for tesla telemetry (#315)
0xdev22 May 1, 2024
43947eb
Merge branch 'main' into feature/tesla-telemetry-main
elffjs May 1, 2024
42f119a
Try to cut down on big import diff
elffjs May 1, 2024
8dd5db0
More import formatting
elffjs May 1, 2024
ef226b6
More imports
elffjs May 1, 2024
6fd4fce
Machine comment
elffjs May 1, 2024
c164b6f
Multiline string for CA
elffjs May 1, 2024
2000836
Remove some Fiber middleware we didn't use
elffjs May 1, 2024
218e74e
Weird comment formatting
elffjs May 1, 2024
b356dd5
Comment for machine
elffjs May 1, 2024
5243bf1
Swap order of set difference. I feel like this is more normal
elffjs May 1, 2024
e8adb02
Misspelling
elffjs May 1, 2024
2a05c64
More import
elffjs May 1, 2024
180d595
More encryption
elffjs May 1, 2024
cb0f18a
Another machine comment
elffjs May 1, 2024
f7910f9
Nolint
elffjs May 1, 2024
a1a6134
Get rid of the global integration command list for now
elffjs May 1, 2024
732e667
Clean up tests
elffjs May 1, 2024
52e5c4f
Kill struct
elffjs May 1, 2024
c628ace
Regen
elffjs May 1, 2024
b7c5260
More Joins, some variable names
elffjs May 1, 2024
aa5e85a
Response wrapper
elffjs May 1, 2024
b71abf4
More unmarshal cleanup
elffjs May 1, 2024
fea6b89
URL join
elffjs May 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 1 addition & 23 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,8 @@ linters:
- staticcheck
- typecheck
- unused
# - zerologlint

issues:
fix: true

linters-settings:
gci:
# DEPRECATED: use `sections` and `prefix(github.com/org/project)` instead.
local-prefixes: github.com/org/project
# Section configuration to compare against.
# Section names are case-insensitive and may contain parameters in ().
# The default order of sections is `standard > default > custom > blank > dot`,
# If `custom-order` is `true`, it follows the order of `sections` option.
# Default: ["standard", "default"]
sections:
- standard # Standard section: captures all standard packages.
- default # Default section: contains all imports that could not be matched to another section type.
- prefix(github.com/DIMO-Network/devices-api) # Custom section: groups all imports with the specified Prefix.
- blank # Blank section: contains all blank imports. This section is not present unless explicitly enabled.
- dot # Dot section: contains all dot imports. This section is not present unless explicitly enabled.
# Skip generated files.
# Default: true
skip-generated: true
# Enable custom order of sections.
# If `true`, make the section order the same as the order of `sections`.
# Default: false
custom-order: false
linters-settings: {}
27 changes: 27 additions & 0 deletions charts/devices-api/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,33 @@ env:
TESLA_TOKEN_URL: https://auth.tesla.com/oauth2/v3/token
TESLA_FLEET_URL: https://fleet-api.prd.%s.vn.cloud.tesla.com
META_TRANSACTION_PROCESSOR_GRPC_ADDR: meta-transaction-processor-dev:8086
TESLA_TELEMETRY_HOST_NAME: ingest-tesla.dev.dimo.zone
TESLA_TELEMETRY_PORT: 443
TESLA_TELEMETRY_CA_CERTIFICATE: |
-----BEGIN CERTIFICATE-----
MIIBvDCCAWKgAwIBAgIRAL6QCUcK/8jy48V7ElERABowCgYIKoZIzj0EAwIwIzEh
MB8GA1UEAxMYRElNTyBDQSBEZXZlbG9wbWVudCBSb290MCAXDTIyMDQyMzExMTEw
M1oYDzIwNzIwNDEwMTExMTAzWjAyMTAwLgYDVQQDEydESU1PIENBIERldmVsb3Bt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ports:
Expand Down
14 changes: 5 additions & 9 deletions cmd/devices-api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import (
jwtware "github.com/gofiber/contrib/jwt"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cache"
"github.com/gofiber/fiber/v2/middleware/cors"
fiberrecover "github.com/gofiber/fiber/v2/middleware/recover"
"github.com/gofiber/swagger"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
Expand Down Expand Up @@ -138,21 +137,17 @@ func startWebAPI(logger zerolog.Logger, settings *config.Settings, pdb db.Store,
countriesController := controllers.NewCountriesController()
userIntegrationAuthController := controllers.NewUserIntegrationAuthController(settings, pdb.DBS, &logger, ddSvc, teslaFleetAPISvc, redisCache, cipher, usersClient)

// commenting this out b/c the library includes the path in the metrics which saturates prometheus queries - need to fork / make our own
//prometheus := fiberprometheus.New("devices-api")
//app.Use(prometheus.Middleware)
app.Use(metrics.HTTPMetricsMiddleware)

app.Use(fiberrecover.New(fiberrecover.Config{
Next: nil,
EnableStackTrace: true,
StackTraceHandler: nil,
}))
//cors
app.Use(cors.New())
// request logging
app.Use(zflogger.New(logger, nil))
//cache

// Request logging.
app.Use(zflogger.New(logger, nil)) // TODO(elffjs): Is this even printing?

cacheHandler := cache.New(cache.Config{
Next: func(c *fiber.Ctx) bool {
return c.Query("refresh") == "true"
Expand Down Expand Up @@ -308,6 +303,7 @@ func startWebAPI(logger zerolog.Logger, settings *config.Settings, pdb db.Store,
udOwner.Post("/integrations/:integrationID/commands/trunk/open", userDeviceController.OpenTrunk)
udOwner.Post("/integrations/:integrationID/commands/frunk/open", userDeviceController.OpenFrunk)
udOwner.Get("/integrations/:integrationID/commands/:requestID", userDeviceController.GetCommandRequestStatus)
udOwner.Post("/integrations/:integrationID/commands/telemetry/subscribe", userDeviceController.TelemetrySubscribe)

udOwner.Post("/commands/opt-in", userDeviceController.DeviceOptIn)

Expand Down
4 changes: 2 additions & 2 deletions cmd/devices-api/set_command_compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (p *setCommandCompatibilityCmd) Execute(ctx context.Context, _ *flag.FlagSe
return subcommands.ExitSuccess
}

var teslaEnabledCommands = []string{"doors/lock", "doors/unlock", "trunk/open", "frunk/open", "charge/limit"}
var teslaEnabledCommands = []string{constants.DoorsLock, constants.DoorsUnlock, constants.TrunkOpen, constants.FrunkOpen, constants.ChargeLimit}

func setCommandCompatibility(ctx context.Context, settings *config.Settings, pdb db.Store, ddSvc services.DeviceDefinitionService) error {

Expand Down Expand Up @@ -146,7 +146,7 @@ func setCommandCompatSmartcar(ctx context.Context, settings *config.Settings, pd
continue
}

md.Commands.Capable = []string{"doors/lock", "doors/unlock"}
md.Commands.Capable = []string{constants.DoorsLock, constants.DoorsUnlock}

if err := su.Metadata.Marshal(md); err != nil {
return err
Expand Down
49 changes: 49 additions & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1555,6 +1555,38 @@ const docTemplate = `{
}
}
},
"/user/devices/{userDeviceID}/integrations/{integrationID}/commands/telemetry/subscribe": {
"post": {
"description": "Subscribe vehicle for Telemetry Data. Currently, this only works for Teslas connected through Tesla.",
"produces": [
"application/json"
],
"tags": [
"device",
"integration",
"command"
],
"summary": "Subscribe vehicle for Telemetry Data",
"operationId": "telemetry-subscribe",
"parameters": [
{
"type": "string",
"description": "Device ID",
"name": "userDeviceID",
"in": "path",
"required": true
},
{
"type": "string",
"description": "Integration ID",
"name": "integrationID",
"in": "path",
"required": true
}
],
"responses": {}
}
},
"/user/devices/{userDeviceID}/integrations/{integrationID}/commands/trunk/open": {
"post": {
"description": "Open the device's front trunk. Currently, this only works for Teslas connected through Tesla.",
Expand Down Expand Up @@ -3082,6 +3114,14 @@ const docTemplate = `{
"status": {
"description": "Status is one of \"Pending\", \"PendingFirstData\", \"Active\", \"Failed\", \"DuplicateIntegration\".",
"type": "string"
},
"tesla": {
"description": "Contains further details about tesla integration status",
"allOf": [
{
"$ref": "#/definitions/internal_controllers.TeslaConnectionStatus"
}
]
}
}
},
Expand Down Expand Up @@ -3397,6 +3437,15 @@ const docTemplate = `{
}
}
},
"internal_controllers.TeslaConnectionStatus": {
"type": "object",
"properties": {
"isVirtualKeyConnected": {
"description": "Status of the virtual key connection",
"type": "boolean"
}
}
},
"internal_controllers.UpdateCountryCodeReq": {
"type": "object",
"properties": {
Expand Down
49 changes: 49 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1547,6 +1547,38 @@
}
}
},
"/user/devices/{userDeviceID}/integrations/{integrationID}/commands/telemetry/subscribe": {
"post": {
"description": "Subscribe vehicle for Telemetry Data. Currently, this only works for Teslas connected through Tesla.",
"produces": [
"application/json"
],
"tags": [
"device",
"integration",
"command"
],
"summary": "Subscribe vehicle for Telemetry Data",
"operationId": "telemetry-subscribe",
"parameters": [
{
"type": "string",
"description": "Device ID",
"name": "userDeviceID",
"in": "path",
"required": true
},
{
"type": "string",
"description": "Integration ID",
"name": "integrationID",
"in": "path",
"required": true
}
],
"responses": {}
}
},
"/user/devices/{userDeviceID}/integrations/{integrationID}/commands/trunk/open": {
"post": {
"description": "Open the device's front trunk. Currently, this only works for Teslas connected through Tesla.",
Expand Down Expand Up @@ -3074,6 +3106,14 @@
"status": {
"description": "Status is one of \"Pending\", \"PendingFirstData\", \"Active\", \"Failed\", \"DuplicateIntegration\".",
"type": "string"
},
"tesla": {
"description": "Contains further details about tesla integration status",
"allOf": [
{
"$ref": "#/definitions/internal_controllers.TeslaConnectionStatus"
}
]
}
}
},
Expand Down Expand Up @@ -3389,6 +3429,15 @@
}
}
},
"internal_controllers.TeslaConnectionStatus": {
"type": "object",
"properties": {
"isVirtualKeyConnected": {
"description": "Status of the virtual key connection",
"type": "boolean"
}
}
},
"internal_controllers.UpdateCountryCodeReq": {
"type": "object",
"properties": {
Expand Down
34 changes: 34 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,10 @@ definitions:
description: Status is one of "Pending", "PendingFirstData", "Active", "Failed",
"DuplicateIntegration".
type: string
tesla:
allOf:
- $ref: '#/definitions/internal_controllers.TeslaConnectionStatus'
description: Contains further details about tesla integration status
type: object
internal_controllers.ManufacturerInfo:
properties:
Expand Down Expand Up @@ -805,6 +809,12 @@ definitions:
example: 0x30bce3da6985897224b29a0fe064fd2b426bb85a394cc09efe823b5c83326a8e
type: string
type: object
internal_controllers.TeslaConnectionStatus:
properties:
isVirtualKeyConnected:
description: Status of the virtual key connection
type: boolean
type: object
internal_controllers.UpdateCountryCodeReq:
properties:
countryCode:
Expand Down Expand Up @@ -1852,6 +1862,30 @@ paths:
description: No Content
tags:
- integrations
/user/devices/{userDeviceID}/integrations/{integrationID}/commands/telemetry/subscribe:
post:
description: Subscribe vehicle for Telemetry Data. Currently, this only works
for Teslas connected through Tesla.
operationId: telemetry-subscribe
parameters:
- description: Device ID
in: path
name: userDeviceID
required: true
type: string
- description: Integration ID
in: path
name: integrationID
required: true
type: string
produces:
- application/json
responses: {}
summary: Subscribe vehicle for Telemetry Data
tags:
- device
- integration
- command
/user/devices/{userDeviceID}/integrations/{integrationID}/commands/trunk/open:
post:
description: Open the device's front trunk. Currently, this only works for Teslas
Expand Down
3 changes: 3 additions & 0 deletions internal/config/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ type Settings struct {
TeslaClientSecret string `yaml:"TESLA_CLIENT_SECRET"`
TeslaTokenURL string `yaml:"TESLA_TOKEN_URL"`
TeslaFleetURL string `yaml:"TESLA_FLEET_URL"`
TeslaTelemetryHostName string `yaml:"TESLA_TELEMETRY_HOST_NAME"`
TeslaTelemetryPort int `yaml:"TESLA_TELEMETRY_PORT"`
TeslaTelemetryCACertificate string `yaml:"TESLA_TELEMETRY_CA_CERTIFICATE"`
}

func (s *Settings) IsProduction() bool {
Expand Down
9 changes: 9 additions & 0 deletions internal/constants/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,12 @@ const (
func (r AutoPiSubStatusEnum) String() string {
return string(r)
}

const (
ChargeLimit string = "charge/limit"
FrunkOpen string = "frunk/open"
TrunkOpen string = "trunk/open"
DoorsLock string = "doors/lock"
DoorsUnlock string = "doors/unlock"
TelemetrySubscribe string = "telemetry/subscribe"
)
8 changes: 4 additions & 4 deletions internal/controllers/nft_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ func (nc *NFTController) GetVehicleStatus(c *fiber.Ctx) error {
// @Param tokenID path string true "Token ID"
// @Router /vehicle/{tokenID}/commands/doors/unlock [post]
func (nc *NFTController) UnlockDoors(c *fiber.Ctx) error {
return nc.handleEnqueueCommand(c, "doors/unlock")
return nc.handleEnqueueCommand(c, constants.DoorsUnlock)
}

// LockDoors godoc
Expand All @@ -485,7 +485,7 @@ func (nc *NFTController) UnlockDoors(c *fiber.Ctx) error {
// @Param tokenID path string true "Token ID"
// @Router /vehicle/{tokenID}/commands/doors/lock [post]
func (nc *NFTController) LockDoors(c *fiber.Ctx) error {
return nc.handleEnqueueCommand(c, "doors/lock")
return nc.handleEnqueueCommand(c, constants.DoorsLock)
}

// OpenTrunk godoc
Expand All @@ -497,7 +497,7 @@ func (nc *NFTController) LockDoors(c *fiber.Ctx) error {
// @Param tokenID path string true "Token ID"
// @Router /vehicle/{tokenID}/commands/trunk/open [post]
func (nc *NFTController) OpenTrunk(c *fiber.Ctx) error {
return nc.handleEnqueueCommand(c, "trunk/open")
return nc.handleEnqueueCommand(c, constants.TrunkOpen)
}

// OpenFrunk godoc
Expand All @@ -509,7 +509,7 @@ func (nc *NFTController) OpenTrunk(c *fiber.Ctx) error {
// @Param tokenID path string true "Token ID"
// @Router /vehicle/{tokenID}/commands/frunk/open [post]
func (nc *NFTController) OpenFrunk(c *fiber.Ctx) error {
return nc.handleEnqueueCommand(c, "frunk/open")
return nc.handleEnqueueCommand(c, constants.FrunkOpen)
}

// handleEnqueueCommand enqueues the command specified by commandPath with the
Expand Down
Loading
Loading