@@ -35,6 +35,13 @@ var config = require('../config'),
35
35
service : 'tester' ,
36
36
subservice : '/test'
37
37
} ,
38
+ configIot = {
39
+ host : 'localhost' ,
40
+ port : 4041 ,
41
+ name : 'default' ,
42
+ service : 'tester' ,
43
+ subservice : '/test'
44
+ } ,
38
45
separator = '\n\n\t' ;
39
46
40
47
function queryContext ( commands ) {
@@ -177,6 +184,27 @@ function configure(commands) {
177
184
config . subservice = commands [ 3 ] ;
178
185
}
179
186
187
+ function showConfig ( commands ) {
188
+ console . log ( '\nCurrent configuration:\n\n' ) ;
189
+ console . log ( JSON . stringify ( config , null , 4 ) ) ;
190
+ console . log ( '\n' ) ;
191
+ clUtils . prompt ( ) ;
192
+ }
193
+
194
+ function configureIot ( commands ) {
195
+ configIot . host = commands [ 0 ] ;
196
+ configIot . port = commands [ 1 ] ;
197
+ config . service = commands [ 2 ] ;
198
+ config . subservice = commands [ 3 ] ;
199
+ }
200
+
201
+ function showConfigIot ( commands ) {
202
+ console . log ( '\nCurrent configuration:\n\n' ) ;
203
+ console . log ( JSON . stringify ( configIot , null , 4 ) ) ;
204
+ console . log ( '\n' ) ;
205
+ clUtils . prompt ( ) ;
206
+ }
207
+
180
208
function discoverContext ( commands ) {
181
209
var options = {
182
210
url : 'http://' + config . host + ':' + config . port + '/v1/registry/discoverContextAvailability' ,
@@ -212,7 +240,7 @@ function discoverContext(commands) {
212
240
function provisionDevice ( commands ) {
213
241
function generateOptions ( deviceConfig , callback ) {
214
242
var options = {
215
- uri : 'http://' + commands [ 0 ] + ':' + commands [ 1 ] + '/iot/devices' ,
243
+ uri : 'http://' + configIot . host + ':' + configIot . port + '/iot/devices' ,
216
244
method : 'POST'
217
245
} ;
218
246
@@ -246,7 +274,7 @@ function provisionDevice(commands) {
246
274
clUtils . prompt ( ) ;
247
275
}
248
276
249
- fs . readFile ( commands [ 2 ] , 'utf8' , function ( error , deviceConfig ) {
277
+ fs . readFile ( commands [ 0 ] , 'utf8' , function ( error , deviceConfig ) {
250
278
if ( error && error . code === 'ENOENT' ) {
251
279
console . error ( 'File not found' ) ;
252
280
clUtils . prompt ( ) ;
@@ -259,11 +287,157 @@ function provisionDevice(commands) {
259
287
} ) ;
260
288
}
261
289
262
- function showConfig ( commands ) {
263
- console . log ( '\nCurrent configuration:\n\n' ) ;
264
- console . log ( JSON . stringify ( config , null , 4 ) ) ;
265
- console . log ( '\n' ) ;
266
- clUtils . prompt ( ) ;
290
+ function listProvisioned ( commands ) {
291
+ var options = {
292
+ uri : 'http://' + configIot . host + ':' + configIot . port + '/iot/devices' ,
293
+ method : 'GET'
294
+ } ;
295
+
296
+ console . log ( 'Devices provisioned in host [%s:%s]' , configIot . host , configIot . port ) ;
297
+ console . log ( '----------------------------------------------------------------' ) ;
298
+
299
+ request ( options , function ( error , result , body ) {
300
+ if ( error ) {
301
+ console . log ( 'Couldn\'t connect with the provisioning server: ' + error . toString ( ) ) ;
302
+ } else if ( result . statusCode === 200 && body ) {
303
+ var parsedBody = JSON . parse ( body ) ;
304
+ console . log ( JSON . stringify ( parsedBody , null , 4 ) ) ;
305
+ } else {
306
+ console . log ( 'Unexpected application error. Status: ' + result . statusCode ) ;
307
+ }
308
+ clUtils . prompt ( ) ;
309
+ } ) ;
310
+ }
311
+
312
+ function removeProvisioned ( commands ) {
313
+ var options = {
314
+ uri : 'http://' + configIot . host + ':' + configIot . port + '/iot/devices/' + commands [ 0 ] ,
315
+ method : 'DELETE'
316
+ } ;
317
+
318
+ console . log ( 'Removing device [%s] [%s:%s]' , commands [ 0 ] , configIot . host , configIot . port ) ;
319
+ console . log ( '----------------------------------------------------------------' ) ;
320
+
321
+ request ( options , function ( error , result , body ) {
322
+ if ( error ) {
323
+ console . log ( 'Couldn\'t connect with the provisioning server: ' + error . toString ( ) ) ;
324
+ } else if ( result . statusCode === 200 && body ) {
325
+ var parsedBody = JSON . parse ( body ) ;
326
+ console . log ( 'Device [%s] removed successfully' , commands [ 0 ] ) ;
327
+ } else {
328
+ console . log ( 'Unexpected application error. Status: ' + result . statusCode ) ;
329
+ }
330
+ clUtils . prompt ( ) ;
331
+ } ) ;
332
+ }
333
+
334
+ function addGroup ( commands ) {
335
+ console . log ( 'Adding device groups to host [%s:%s] from file [%s]' , configIot . host , configIot . port , commands [ 0 ] ) ;
336
+
337
+ function generateOptions ( deviceConfig , callback ) {
338
+ var options = {
339
+ uri : 'http://' + configIot . host + ':' + configIot . port + '/iot/agents/' + configIot . name + '/services' ,
340
+ method : 'POST' ,
341
+ headers : {
342
+ 'fiware-service' : configIot . service ,
343
+ 'fiware-servicepath' : configIot . subservice
344
+ }
345
+ } ;
346
+
347
+ try {
348
+ var payload = JSON . parse ( deviceConfig ) ;
349
+ options . json = payload ;
350
+ callback ( null , options ) ;
351
+ } catch ( e ) {
352
+ callback ( 'Wrong JSON. Couldn\'t parse' ) ;
353
+ }
354
+ }
355
+
356
+ function sendProvisionRequest ( options , callback ) {
357
+ request ( options , function ( error , result , body ) {
358
+ if ( error ) {
359
+ callback ( 'Couldn\'t connect with the provisioning server: ' + error . toString ( ) ) ;
360
+ } else if ( result . statusCode === 200 && body ) {
361
+ callback ( null , 'Device group successfully provisioned' ) ;
362
+ } else {
363
+ callback ( 'Unexpected application error. Status: ' + result . statusCode ) ;
364
+ }
365
+ } ) ;
366
+ }
367
+
368
+ function handleOut ( error , msg ) {
369
+ if ( error ) {
370
+ console . error ( error ) ;
371
+ } else {
372
+ console . log ( msg ) ;
373
+ }
374
+ clUtils . prompt ( ) ;
375
+ }
376
+
377
+ fs . readFile ( commands [ 0 ] , 'utf8' , function ( error , deviceConfig ) {
378
+ if ( error && error . code === 'ENOENT' ) {
379
+ console . error ( 'File not found' ) ;
380
+ clUtils . prompt ( ) ;
381
+ } else {
382
+ async . waterfall ( [
383
+ async . apply ( generateOptions , deviceConfig ) ,
384
+ sendProvisionRequest
385
+ ] , handleOut ) ;
386
+ }
387
+ } ) ;
388
+ }
389
+
390
+ function removeGroup ( commands ) {
391
+ var options = {
392
+ uri : 'http://' + configIot . host + ':' + configIot . port + '/iot/agents/' + configIot . name + '/services' ,
393
+ method : 'DELETE' ,
394
+ headers : {
395
+ 'fiware-service' : configIot . service ,
396
+ 'fiware-servicepath' : configIot . subservice
397
+ }
398
+ } ;
399
+
400
+ console . log ( 'Removing device group for subservice [%s] from [%s:%s]' ,
401
+ configIot . subservice , configIot . host , configIot . port ) ;
402
+
403
+ console . log ( '----------------------------------------------------------------' ) ;
404
+
405
+ request ( options , function ( error , result , body ) {
406
+ if ( error ) {
407
+ console . log ( 'Couldn\'t connect with the provisioning server: ' + error . toString ( ) ) ;
408
+ } else if ( result . statusCode === 200 && body ) {
409
+ console . log ( 'Device group for subservice [%s] removed successfully' , configIot . subservice ) ;
410
+ } else {
411
+ console . log ( 'Unexpected application error. Status: ' + result . statusCode ) ;
412
+ }
413
+ clUtils . prompt ( ) ;
414
+ } ) ;
415
+ }
416
+
417
+ function listGroups ( commands ) {
418
+ console . log ( 'Devices groups provisioned in host [%s:%s]' , configIot . host , configIot . port ) ;
419
+ console . log ( '----------------------------------------------------------------' ) ;
420
+ var options = {
421
+ uri : 'http://' + configIot . host + ':' + configIot . port + '/iot/agents/' + configIot . name + '/services' ,
422
+ method : 'GET' ,
423
+ headers : {
424
+ 'fiware-service' : configIot . service ,
425
+ 'fiware-servicepath' : '/*'
426
+ }
427
+ } ;
428
+
429
+ request ( options , function ( error , result , body ) {
430
+ console . log ( JSON . stringify ( result . headers ) ) ;
431
+ if ( error ) {
432
+ console . log ( 'Couldn\'t connect with the provisioning server: ' + error . toString ( ) ) ;
433
+ } else if ( result . statusCode === 200 && body ) {
434
+ var parsedBody = JSON . parse ( body ) ;
435
+ console . log ( JSON . stringify ( parsedBody , null , 4 ) ) ;
436
+ } else {
437
+ console . log ( 'Unexpected application error. Status: ' + result . statusCode ) ;
438
+ }
439
+ clUtils . prompt ( ) ;
440
+ } ) ;
267
441
}
268
442
269
443
var commands = {
@@ -294,21 +468,57 @@ var commands = {
294
468
description : '\tGet all the context providers for a entity and type.' ,
295
469
handler : discoverContext
296
470
} ,
297
- 'config ' : {
471
+ 'configCb ' : {
298
472
parameters : [ 'host' , 'port' , 'service' , 'subservice' ] ,
299
473
description : '\tConfig a new host and port for the remote Context Broker.' ,
300
474
handler : configure
301
475
} ,
302
- 'showConfig ' : {
476
+ 'showConfigCb ' : {
303
477
parameters : [ ] ,
304
- description : '\tShow the current configuration of the client.' ,
478
+ description : '\tShow the current configuration of the client for the Context Broker .' ,
305
479
handler : showConfig
306
480
} ,
481
+ 'configIot' : {
482
+ parameters : [ 'host' , 'port' , 'service' , 'subservice' ] ,
483
+ description : '\tConfig a new host and port for the remote IoT Agent.' ,
484
+ handler : configureIot
485
+ } ,
486
+ 'showConfigIot' : {
487
+ parameters : [ ] ,
488
+ description : '\tShow the current configuration of the client for the IoT Agent.' ,
489
+ handler : showConfigIot
490
+ } ,
307
491
'provision' : {
308
- parameters : [ 'host' , 'port' , ' filename'] ,
492
+ parameters : [ 'filename' ] ,
309
493
description : '\tProvision a new device using the Device Provisioning API. The device configuration is \n' +
310
- '\tread from the script location .' ,
494
+ '\tread from the file specified in the "filename" parameter .' ,
311
495
handler : provisionDevice
496
+ } ,
497
+ 'listProvisioned' : {
498
+ parameters : [ ] ,
499
+ description : '\tList all the provisioned devices in an IoT Agent.' ,
500
+ handler : listProvisioned
501
+ } ,
502
+ 'removeProvisioned' : {
503
+ parameters : [ 'deviceId' ] ,
504
+ description : '\tRemove the selected provisioned device from the IoT Agent, specified by its Device ID.' ,
505
+ handler : removeProvisioned
506
+ } ,
507
+ 'addGroup' : {
508
+ parameters : [ 'filename' ] ,
509
+ description : '\tAdd a new device group to the specified IoT Agent through the Configuration API. The \n' +
510
+ '\tbody is taken from the file specified in the "filename" parameter.' ,
511
+ handler : addGroup
512
+ } ,
513
+ 'listGroups' : {
514
+ parameters : [ ] ,
515
+ description : '\tList all the device groups created in the selected IoT Agent for the configured service' ,
516
+ handler : listGroups
517
+ } ,
518
+ 'removeGroup' : {
519
+ parameters : [ ] ,
520
+ description : '\tRemove the device group corresponding to the current configured subservice.' ,
521
+ handler : removeGroup
312
522
}
313
523
} ;
314
524
0 commit comments