@@ -5,6 +5,7 @@ import { getAPISettings } from '@hello.nrfcloud.com/nrfcloud-api-helpers/setting
55import chalk from 'chalk'
66import { chunk } from 'lodash-es'
77import { table } from 'table'
8+ import { getByDeviceIds } from '../../devices/getByDeviceIds.js'
89import { compareLists } from '../../devices/import/compareLists.js'
910import { readDeviceCertificates } from '../../devices/import/readDeviceCertificates.js'
1011import { readDevicesList } from '../../devices/import/readDevicesList.js'
@@ -79,20 +80,37 @@ export const importDevicesCommand = ({
7980 process . exit ( 1 )
8081 }
8182
83+ const byIds = getByDeviceIds ( { db, DevicesTableName : devicesTableName } )
84+ const existing = await byIds (
85+ Array . from ( devices . keys ( ) ) . map ( ( imei ) => `oob-${ imei } ` ) ,
86+ )
87+ const existingDevices = Object . keys ( existing )
88+
8289 console . log (
8390 table ( [
8491 [ 'Fingerprint' , 'Device ID' , 'Model' , 'HW version' ] ,
85- ...Array . from ( devices . entries ( ) ) . map (
86- ( [ imei , { fingerprint, hwVersion } ] ) => [
92+ ...Array . from ( devices . entries ( ) )
93+ . filter ( ( [ imei ] ) => ! existingDevices . includes ( `oob-${ imei } ` ) )
94+ . map ( ( [ imei , { fingerprint, hwVersion } ] ) => [
8795 chalk . green ( fingerprint ) ,
8896 chalk . blue ( imei ) ,
8997 chalk . blue ( model ) ,
9098 chalk . blue ( hwVersion ) ,
91- ] ,
92- ) ,
99+ ] ) ,
93100 ] ) ,
94101 )
95102
103+ if ( existingDevices . length > 0 ) {
104+ console . warn (
105+ chalk . yellow (
106+ `Skipping` ,
107+ existingDevices . length ,
108+ `devices which are already registered.` ,
109+ ) ,
110+ )
111+ console . warn ( '' )
112+ }
113+
96114 if ( dryRun === true ) {
97115 console . log ( chalk . gray ( `Dry run, not registering devices.` ) )
98116 process . exit ( 2 )
@@ -112,15 +130,17 @@ export const importDevicesCommand = ({
112130 for ( const page of chunk ( [ ...deviceCertificates . entries ( ) ] , 1000 ) ) {
113131 // Bulk-ops API allows max 1,000 devices per request
114132 const registration = await client . register (
115- Array . from ( page ) . map ( ( [ imei , { certificate : certPem } ] ) => {
116- const deviceId = `oob-${ imei } `
117- return {
118- deviceId,
119- subType : model . replace ( / [ ^ 0 - 9 a - z - ] / gi, '-' ) ,
120- tags : [ model . replace ( / [ ^ 0 - 9 a - z - ] / gi, ':' ) ] ,
121- certPem,
122- }
123- } ) ,
133+ Array . from ( page )
134+ . filter ( ( [ imei ] ) => ! existingDevices . includes ( `oob-${ imei } ` ) )
135+ . map ( ( [ imei , { certificate : certPem } ] ) => {
136+ const deviceId = `oob-${ imei } `
137+ return {
138+ deviceId,
139+ subType : model . replace ( / [ ^ 0 - 9 a - z - ] / gi, '-' ) ,
140+ tags : [ model . replace ( / [ ^ 0 - 9 a - z - ] / gi, ':' ) ] ,
141+ certPem,
142+ }
143+ } ) ,
124144 )
125145
126146 if ( 'error' in registration ) {
@@ -140,7 +160,9 @@ export const importDevicesCommand = ({
140160 devicesTableName,
141161 } )
142162
143- for ( const [ imei , { fingerprint, hwVersion } ] of devices . entries ( ) ) {
163+ for ( const [ imei , { fingerprint, hwVersion } ] of devices
164+ . entries ( )
165+ . filter ( ( [ imei ] ) => ! existingDevices . includes ( `oob-${ imei } ` ) ) ) {
144166 const deviceId = `oob-${ imei } `
145167
146168 const res = await r ( {
0 commit comments