@@ -216,51 +216,50 @@ function createInitialEntityHandlerNgsi2(deviceData, newDevice, callback) {
216216 */
217217function createInitialEntityNgsi2 ( newDevice , deviceObj , callback ) {
218218 logger . debug ( context , 'createInitialEntityNgsiv2 called with newDevice: %j' , newDevice ) ;
219- if (
220- ( ! newDevice . cmdMode && ( ! config . getConfig ( ) . cmdMode || config . getConfig ( ) . cmdMode === 'legacy' ) ) ||
221- ( newDevice && newDevice . cmdMode === 'legacy' )
222- ) {
219+
220+ const cmdModeConfig = config . getConfig ( ) . cmdMode ;
221+
222+ const isLegacy =
223+ ( ! newDevice . cmdMode && ( ! cmdModeConfig || cmdModeConfig === 'legacy' ) ) || newDevice . cmdMode === 'legacy' ;
224+ if ( isLegacy ) {
223225 logger . debug ( context , 'Old cmdMode with newDevice: %j' , deviceObj ) ;
224- callback ( null , deviceObj ) ;
225- } else {
226- // Creates initial entity when commands by sucriptions, to ensure commands appears in entity with null value
227- // (including a check so if device has no commands, no entity is created)
228- if ( newDevice . commands . length > 0 ) {
229- const options = {
230- url : config . getConfig ( ) . contextBroker . url + '/v2/entities?options=upsert' ,
231- method : 'POST' ,
232- json : {
233- id : String ( newDevice . name ) ,
234- type : newDevice . type
235- } ,
236- headers : {
237- 'fiware-service' : newDevice . service ,
238- 'fiware-servicepath' : newDevice . subservice ,
239- 'fiware-correlator' : ( domain . active && domain . active . corr ) || uuid . v4 ( )
240- }
241- } ;
242- if ( newDevice . cbHost && newDevice . cbHost . indexOf ( '://' ) !== - 1 ) {
243- options . url = newDevice . cbHost + '/v2/entities?options=upsert' ;
244- } else if ( newDevice . cbHost && newDevice . cbHost . indexOf ( '://' ) === - 1 ) {
245- options . url = 'http://' + newDevice . cbHost + '/v2/entities?options=upsert' ;
246- }
247- jsonConcat ( options . json , formatCommandsBySubsNgsi2 ( newDevice . commands ) ) ;
248- logger . debug ( context , 'Creating initial entity due to commands by subs with newDevice: %j' , newDevice ) ;
249- logger . debug (
250- context ,
251- 'Creating initial entity in the Context Broker:\n %s' ,
252- JSON . stringify ( options , null , 4 )
253- ) ;
254- utils . executeWithSecurity (
255- options ,
256- newDevice ,
257- createInitialEntityHandlerNgsi2 ( newDevice , deviceObj , callback )
258- ) ;
259- } else {
260- logger . debug ( context , 'No initial entity due to no commands by subs in newDevice: %j' , newDevice ) ;
261- callback ( null , deviceObj ) ;
262- }
226+ return callback ( null , deviceObj ) ;
263227 }
228+
229+ if ( ! Array . isArray ( newDevice . commands ) || newDevice . commands . length === 0 ) {
230+ logger . debug ( context , 'No initial entity due to no commands by subs in newDevice: %j' , newDevice ) ;
231+ return callback ( null , deviceObj ) ;
232+ }
233+
234+ const entityPayload = {
235+ id : String ( newDevice . name ) ,
236+ type : newDevice . type
237+ } ;
238+ jsonConcat ( entityPayload , formatCommandsBySubsNgsi2 ( newDevice . commands ) ) ;
239+
240+ let url = config . getConfig ( ) . contextBroker . url + '/v2/entities?options=upsert' ;
241+ if ( newDevice . cbHost ) {
242+ url = newDevice . cbHost . includes ( '://' )
243+ ? newDevice . cbHost + '/v2/entities?options=upsert'
244+ : 'http://' + newDevice . cbHost + '/v2/entities?options=upsert' ;
245+ }
246+
247+ const headers = {
248+ 'fiware-correlator' : ( domain . active && domain . active . corr ) || uuid . v4 ( )
249+ } ;
250+ if ( newDevice . service ) headers [ 'fiware-service' ] = newDevice . service ;
251+ if ( newDevice . subservice ) headers [ 'fiware-servicepath' ] = newDevice . subservice ;
252+
253+ const options = { url, method : 'POST' , json : entityPayload , headers } ;
254+
255+ logger . debug ( context , 'Creating initial entity due to commands by subs with newDevice: %j' , newDevice ) ;
256+ logger . debug ( context , 'Creating initial entity in the Context Broker:\n %s' , JSON . stringify ( options , null , 4 ) ) ;
257+
258+ return utils . executeWithSecurity (
259+ options ,
260+ newDevice ,
261+ createInitialEntityHandlerNgsi2 ( newDevice , deviceObj , callback )
262+ ) ;
264263}
265264
266265/**
0 commit comments