Skip to content

Commit

Permalink
fix(shadow): remove asset_tracker_v2 specific properties
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Apr 23, 2024
1 parent bb11d97 commit 2c409a4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 116 deletions.
30 changes: 4 additions & 26 deletions src/api/DeviceShadow.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,10 @@ void describe('DeviceShadow type', () => {
ts: 1697102116821,
},
},
metadata: {
reported: {
dev: {
v: {
imei: {
timestamp: 1697102122,
},
iccid: {
timestamp: 1697102122,
},
modV: {
timestamp: 1697102122,
},
brdV: {
timestamp: 1697102122,
},
appV: {
timestamp: 1697102122,
},
},
ts: {
timestamp: 1697102122,
},
},
},
},
},
$meta: {
updatedAt: '2023-04-20T07:29:46.467Z',
createdAt: '2023-04-19T11:49:07.370Z',
},
})
assert.equal('errors' in res, false)
Expand Down
24 changes: 15 additions & 9 deletions src/api/DeviceShadow.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
import { Type, type Static } from '@sinclair/typebox'

const PropertyMetadata = Type.Union([
Type.Object({ timestamp: Type.Integer({ minimum: 1, maximum: 9999999999 }) }),
Type.Record(Type.String({ minLength: 1 }), Type.Unknown()),
])

/**
* @link https://api.nrfcloud.com/v1/#tag/All-Devices/operation/ListDevices
*/
export const DeviceShadow = Type.Object({
id: Type.String(),
$meta: Type.Object({
createdAt: Type.String({
minLength: 1,
examples: ['2019-08-24T14:15:22Z'],
}),
updatedAt: Type.String({
minLength: 1,
examples: ['2019-08-24T14:15:22Z'],
}),
}),
state: Type.Object({
reported: Type.Object({}),
reported: Type.Record(Type.String({ minLength: 1 }), Type.Any()),
desired: Type.Optional(
Type.Record(Type.String({ minLength: 1 }), Type.Any()),
),
version: Type.Number(),
metadata: Type.Object({
reported: Type.Record(Type.String({ minLength: 1 }), PropertyMetadata),
}),
}),
})

export type DeviceShadowType = Static<typeof DeviceShadow>
;``
90 changes: 9 additions & 81 deletions src/api/devices.ts
Original file line number Diff line number Diff line change
@@ -1,81 +1,11 @@
import { Type, type TSchema, type Static } from '@sinclair/typebox'
import { slashless } from './slashless.js'
import { ValidationError, validatedFetch } from './validatedFetch.js'

type Nullable<T> = { [K in keyof T]: T[K] | null }

export const DeviceConfig = Type.Partial(
Type.Object({
activeMode: Type.Boolean(), // e.g. false
locationTimeout: Type.Number(), // e.g. 300
activeWaitTime: Type.Number(), // e.g. 120
movementResolution: Type.Number(), // e.g. 120
movementTimeout: Type.Number(), // e.g. 3600
accThreshAct: Type.Number(), // e.g. 4
accThreshInact: Type.Number(), // e.g. 4
accTimeoutInact: Type.Number(), // e.g. 60
nod: Type.Array(
Type.Union([
Type.Literal('gnss'),
Type.Literal('ncell'),
Type.Literal('wifi'),
]),
), // e.g. ['nod']
}),
)
import { DeviceShadow } from './DeviceShadow.js'

const Device = Type.Object({
id: Type.String(),
state: Type.Optional(
Type.Object({
reported: Type.Optional(
Type.Object({
config: Type.Optional(DeviceConfig),
connection: Type.Optional(
Type.Object({
status: Type.Optional(
Type.Union([
Type.Literal('connected'),
Type.Literal('disconnected'),
]),
),
}),
),
device: Type.Optional(
Type.Object({
deviceInfo: Type.Optional(
Type.Partial(
Type.Object({
appVersion: Type.String(), // e.g. '1.1.0'
modemFirmware: Type.String(), // e.g. 'mfw_nrf9160_1.3.4'
imei: Type.String(), // e.g. '352656108602296'
board: Type.String(), // e.g. 'thingy91_nrf9160'
hwVer: Type.String(), // e.g. 'nRF9160 SICA B1A'
}),
),
),
}),
),
}),
),
desired: Type.Optional(
Type.Object({
config: Type.Optional(DeviceConfig),
}),
),
version: Type.Number(),
}),
),
firmware: Type.Optional(
Type.Object({
app: Type.Optional(
Type.Object({
name: Type.String({ minLength: 1 }),
version: Type.String({ minLength: 1 }),
}),
),
}),
),
state: Type.Optional(DeviceShadow),
})

const Page = <T extends TSchema>(Item: T) =>
Expand Down Expand Up @@ -118,10 +48,12 @@ export const devices = (
) => Promise<
{ error: Error | ValidationError } | { result: Static<typeof Device> }
>
updateConfig: (
updateState: (
id: string,
config: Nullable<Omit<Static<typeof DeviceConfig>, 'nod'>> &
Pick<Static<typeof DeviceConfig>, 'nod'>,
state: {
desired?: Record<string, any>
reported?: Record<string, any>
},
) => Promise<{ error: Error } | { success: boolean }>
register: (
devices: {
Expand Down Expand Up @@ -156,7 +88,7 @@ export const devices = (
),
get: async (id) =>
vf({ resource: `devices/${encodeURIComponent(id)}` }, Device),
updateConfig: async (id, config) =>
updateState: async (id, state) =>
fetch(
`${slashless(endpoint)}/v1/devices/${encodeURIComponent(id)}/state`,
{
Expand All @@ -165,11 +97,7 @@ export const devices = (
'Content-Type': 'application/json',
},
method: 'PATCH',
body: JSON.stringify({
desired: {
config,
},
}),
body: JSON.stringify(state),
},
).then((res) => {
if (res.status >= 400)
Expand Down

0 comments on commit 2c409a4

Please sign in to comment.