@@ -15,6 +15,7 @@ import { getDevice as getDeviceFromIndex } from '../../devices/getDevice.js'
15
15
import { getAttributesForDevice } from '../../devices/getAttributesForDevice.js'
16
16
import { registerDevice } from '../../devices/registerDevice.js'
17
17
import type { getAllAccountsSettings } from '../../nrfcloud/allAccounts.js'
18
+ import { registerUnsupportedDevice } from '../../devices/registerUnsupportedDevice.js'
18
19
19
20
const createDeviceForModel = ( {
20
21
db,
@@ -55,42 +56,108 @@ const createDeviceForModel = ({
55
56
account,
56
57
} )
57
58
58
- await pRetry (
59
- async ( ) => {
60
- const res = await getDeviceFromIndex ( {
61
- db,
62
- devicesTableName : devicesTable ,
63
- devicesIndexName : devicesTableFingerprintIndexName ,
64
- } ) ( { fingerprint } )
65
- if ( 'error' in res )
66
- throw new Error ( `Failed to resolve fingerprint ${ fingerprint } !` )
67
- } ,
68
- {
69
- retries : 5 ,
70
- minTimeout : 500 ,
71
- maxTimeout : 1000 ,
72
- } ,
73
- )
59
+ await waitForDeviceToBeAvailable ( {
60
+ db,
61
+ devicesTable,
62
+ devicesTableFingerprintIndexName,
63
+ } ) ( fingerprint )
64
+ await waitForDeviceAttributesToBeAvailable ( {
65
+ db,
66
+ devicesTable,
67
+ } ) ( id )
68
+
69
+ context [ storageName ] = fingerprint
70
+ context [ `${ storageName } _deviceId` ] = id
71
+ progress ( `Device registered: ${ fingerprint } (${ id } )` )
72
+ } ,
73
+ )
74
+
75
+ const waitForDeviceToBeAvailable =
76
+ ( {
77
+ db,
78
+ devicesTable,
79
+ devicesTableFingerprintIndexName,
80
+ } : {
81
+ db : DynamoDBClient
82
+ devicesTable : string
83
+ devicesTableFingerprintIndexName : string
84
+ } ) =>
85
+ async ( fingerprint : string ) => {
86
+ await pRetry (
87
+ async ( ) => {
88
+ const res = await getDeviceFromIndex ( {
89
+ db,
90
+ devicesTableName : devicesTable ,
91
+ devicesIndexName : devicesTableFingerprintIndexName ,
92
+ } ) ( { fingerprint } )
93
+ if ( 'error' in res )
94
+ throw new Error ( `Failed to resolve fingerprint ${ fingerprint } !` )
95
+ } ,
96
+ {
97
+ retries : 5 ,
98
+ minTimeout : 500 ,
99
+ maxTimeout : 1000 ,
100
+ } ,
101
+ )
102
+ }
74
103
75
- await pRetry (
76
- async ( ) => {
77
- const res = await getAttributesForDevice ( {
78
- db,
79
- DevicesTableName : devicesTable ,
80
- } ) ( id )
81
- if ( 'error' in res )
82
- throw new Error ( `Failed to get model for device ${ id } !` )
83
- } ,
84
- {
85
- retries : 5 ,
86
- minTimeout : 500 ,
87
- maxTimeout : 1000 ,
88
- } ,
104
+ const waitForDeviceAttributesToBeAvailable =
105
+ ( { db, devicesTable } : { db : DynamoDBClient ; devicesTable : string } ) =>
106
+ async ( id : string ) => {
107
+ await pRetry (
108
+ async ( ) => {
109
+ const res = await getAttributesForDevice ( {
110
+ db,
111
+ DevicesTableName : devicesTable ,
112
+ } ) ( id )
113
+ if ( 'error' in res )
114
+ throw new Error ( `Failed to get model for device ${ id } !` )
115
+ } ,
116
+ {
117
+ retries : 5 ,
118
+ minTimeout : 500 ,
119
+ maxTimeout : 1000 ,
120
+ } ,
121
+ )
122
+ }
123
+
124
+ const createUnsupportedDevice = ( {
125
+ db,
126
+ devicesTable,
127
+ devicesTableFingerprintIndexName,
128
+ } : {
129
+ db : DynamoDBClient
130
+
131
+ devicesTable : string
132
+ devicesTableFingerprintIndexName : string
133
+ } ) =>
134
+ regExpMatchedStep (
135
+ {
136
+ regExp :
137
+ / ^ I h a v e t h e f i n g e r p r i n t f o r a n u n s u p p o r t e d d e v i c e i n ` (?< storageName > [ ^ ` ] + ) ` $ / ,
138
+ schema : Type . Object ( {
139
+ storageName : Type . String ( ) ,
140
+ } ) ,
141
+ } ,
142
+ async ( { match : { storageName } , log : { progress } , context } ) => {
143
+ const fingerprint = `92b.${ generateCode ( ) } `
144
+ const id = randomUUID ( )
145
+
146
+ progress (
147
+ `Registering unsupported device ${ id } into table ${ devicesTable } ` ,
89
148
)
149
+ await registerUnsupportedDevice ( { db, devicesTableName : devicesTable } ) ( {
150
+ id,
151
+ fingerprint,
152
+ } )
153
+ await waitForDeviceToBeAvailable ( {
154
+ db,
155
+ devicesTable,
156
+ devicesTableFingerprintIndexName,
157
+ } ) ( fingerprint )
90
158
91
159
context [ storageName ] = fingerprint
92
160
context [ `${ storageName } _deviceId` ] = id
93
-
94
161
progress ( `Device registered: ${ fingerprint } (${ id } )` )
95
162
} ,
96
163
)
@@ -164,5 +231,10 @@ export const steps = (
164
231
} : { devicesTableFingerprintIndexName : string ; devicesTable : string } ,
165
232
) : StepRunner < Record < string , string > > [ ] => [
166
233
createDeviceForModel ( { db, devicesTableFingerprintIndexName, devicesTable } ) ,
234
+ createUnsupportedDevice ( {
235
+ db,
236
+ devicesTableFingerprintIndexName,
237
+ devicesTable,
238
+ } ) ,
167
239
publishDeviceMessage ( allAccountSettings ) ,
168
240
]
0 commit comments