Skip to content

Commit 2c409a4

Browse files
committed
fix(shadow): remove asset_tracker_v2 specific properties
1 parent bb11d97 commit 2c409a4

File tree

3 files changed

+28
-116
lines changed

3 files changed

+28
-116
lines changed

src/api/DeviceShadow.spec.ts

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,10 @@ void describe('DeviceShadow type', () => {
2121
ts: 1697102116821,
2222
},
2323
},
24-
metadata: {
25-
reported: {
26-
dev: {
27-
v: {
28-
imei: {
29-
timestamp: 1697102122,
30-
},
31-
iccid: {
32-
timestamp: 1697102122,
33-
},
34-
modV: {
35-
timestamp: 1697102122,
36-
},
37-
brdV: {
38-
timestamp: 1697102122,
39-
},
40-
appV: {
41-
timestamp: 1697102122,
42-
},
43-
},
44-
ts: {
45-
timestamp: 1697102122,
46-
},
47-
},
48-
},
49-
},
24+
},
25+
$meta: {
26+
updatedAt: '2023-04-20T07:29:46.467Z',
27+
createdAt: '2023-04-19T11:49:07.370Z',
5028
},
5129
})
5230
assert.equal('errors' in res, false)

src/api/DeviceShadow.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
import { Type, type Static } from '@sinclair/typebox'
22

3-
const PropertyMetadata = Type.Union([
4-
Type.Object({ timestamp: Type.Integer({ minimum: 1, maximum: 9999999999 }) }),
5-
Type.Record(Type.String({ minLength: 1 }), Type.Unknown()),
6-
])
7-
83
/**
94
* @link https://api.nrfcloud.com/v1/#tag/All-Devices/operation/ListDevices
105
*/
116
export const DeviceShadow = Type.Object({
127
id: Type.String(),
8+
$meta: Type.Object({
9+
createdAt: Type.String({
10+
minLength: 1,
11+
examples: ['2019-08-24T14:15:22Z'],
12+
}),
13+
updatedAt: Type.String({
14+
minLength: 1,
15+
examples: ['2019-08-24T14:15:22Z'],
16+
}),
17+
}),
1318
state: Type.Object({
14-
reported: Type.Object({}),
19+
reported: Type.Record(Type.String({ minLength: 1 }), Type.Any()),
20+
desired: Type.Optional(
21+
Type.Record(Type.String({ minLength: 1 }), Type.Any()),
22+
),
1523
version: Type.Number(),
16-
metadata: Type.Object({
17-
reported: Type.Record(Type.String({ minLength: 1 }), PropertyMetadata),
18-
}),
1924
}),
2025
})
2126

2227
export type DeviceShadowType = Static<typeof DeviceShadow>
28+
;``

src/api/devices.ts

Lines changed: 9 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,11 @@
11
import { Type, type TSchema, type Static } from '@sinclair/typebox'
22
import { slashless } from './slashless.js'
33
import { ValidationError, validatedFetch } from './validatedFetch.js'
4-
5-
type Nullable<T> = { [K in keyof T]: T[K] | null }
6-
7-
export const DeviceConfig = Type.Partial(
8-
Type.Object({
9-
activeMode: Type.Boolean(), // e.g. false
10-
locationTimeout: Type.Number(), // e.g. 300
11-
activeWaitTime: Type.Number(), // e.g. 120
12-
movementResolution: Type.Number(), // e.g. 120
13-
movementTimeout: Type.Number(), // e.g. 3600
14-
accThreshAct: Type.Number(), // e.g. 4
15-
accThreshInact: Type.Number(), // e.g. 4
16-
accTimeoutInact: Type.Number(), // e.g. 60
17-
nod: Type.Array(
18-
Type.Union([
19-
Type.Literal('gnss'),
20-
Type.Literal('ncell'),
21-
Type.Literal('wifi'),
22-
]),
23-
), // e.g. ['nod']
24-
}),
25-
)
4+
import { DeviceShadow } from './DeviceShadow.js'
265

276
const Device = Type.Object({
287
id: Type.String(),
29-
state: Type.Optional(
30-
Type.Object({
31-
reported: Type.Optional(
32-
Type.Object({
33-
config: Type.Optional(DeviceConfig),
34-
connection: Type.Optional(
35-
Type.Object({
36-
status: Type.Optional(
37-
Type.Union([
38-
Type.Literal('connected'),
39-
Type.Literal('disconnected'),
40-
]),
41-
),
42-
}),
43-
),
44-
device: Type.Optional(
45-
Type.Object({
46-
deviceInfo: Type.Optional(
47-
Type.Partial(
48-
Type.Object({
49-
appVersion: Type.String(), // e.g. '1.1.0'
50-
modemFirmware: Type.String(), // e.g. 'mfw_nrf9160_1.3.4'
51-
imei: Type.String(), // e.g. '352656108602296'
52-
board: Type.String(), // e.g. 'thingy91_nrf9160'
53-
hwVer: Type.String(), // e.g. 'nRF9160 SICA B1A'
54-
}),
55-
),
56-
),
57-
}),
58-
),
59-
}),
60-
),
61-
desired: Type.Optional(
62-
Type.Object({
63-
config: Type.Optional(DeviceConfig),
64-
}),
65-
),
66-
version: Type.Number(),
67-
}),
68-
),
69-
firmware: Type.Optional(
70-
Type.Object({
71-
app: Type.Optional(
72-
Type.Object({
73-
name: Type.String({ minLength: 1 }),
74-
version: Type.String({ minLength: 1 }),
75-
}),
76-
),
77-
}),
78-
),
8+
state: Type.Optional(DeviceShadow),
799
})
8010

8111
const Page = <T extends TSchema>(Item: T) =>
@@ -118,10 +48,12 @@ export const devices = (
11848
) => Promise<
11949
{ error: Error | ValidationError } | { result: Static<typeof Device> }
12050
>
121-
updateConfig: (
51+
updateState: (
12252
id: string,
123-
config: Nullable<Omit<Static<typeof DeviceConfig>, 'nod'>> &
124-
Pick<Static<typeof DeviceConfig>, 'nod'>,
53+
state: {
54+
desired?: Record<string, any>
55+
reported?: Record<string, any>
56+
},
12557
) => Promise<{ error: Error } | { success: boolean }>
12658
register: (
12759
devices: {
@@ -156,7 +88,7 @@ export const devices = (
15688
),
15789
get: async (id) =>
15890
vf({ resource: `devices/${encodeURIComponent(id)}` }, Device),
159-
updateConfig: async (id, config) =>
91+
updateState: async (id, state) =>
16092
fetch(
16193
`${slashless(endpoint)}/v1/devices/${encodeURIComponent(id)}/state`,
16294
{
@@ -165,11 +97,7 @@ export const devices = (
16597
'Content-Type': 'application/json',
16698
},
16799
method: 'PATCH',
168-
body: JSON.stringify({
169-
desired: {
170-
config,
171-
},
172-
}),
100+
body: JSON.stringify(state),
173101
},
174102
).then((res) => {
175103
if (res.status >= 400)

0 commit comments

Comments
 (0)