vicinityManager/server/services/items/registration.js
/*
Save in Mongo dB all objects contained in the req.
Message producing the req is sent by the agent with thingDescriptions
*/
Save in Mongo dB all objects contained in the req.
Message producing the req is sent by the agent with thingDescriptions
Save in Mongo dB all objects contained in the req . Message producing the req is sent by the agent with thingDescriptions
function create(data, callback){
  var objectsArray = data.thingDescriptions;
  var adid = data.adid !== undefined ? data.adid : data.agid;
  console.time("ALL REGISTRATION EXECUTION");
  console.time("REGISTRATION FIX PART");
  nodeOp.findOne({adid: adid, status: "active"}, {cid:1, hasItems: 1},
    function(err,data){
        if(err || !data){
          if(err){
            callback(true, "Error in Mongo: " + err);
          }else{
            callback(true, "Invalid adid identificator");
          }
        } else {
          var nodeId = data._id;
          var cid = data.cid;
          var semanticTypes = {};
          semanticRepo.getTypes("Device")
          .then(function(response){
            return parseGetTypes(JSON.parse(response).data.results.bindings);
          })
          .then(function(response){
            semanticTypes.devices = response;
            return semanticRepo.getTypes("Service");
          })
          .then(function(response){
            return parseGetTypes(JSON.parse(response).data.results.bindings);
          })
          .then(function(response){
            semanticTypes.services = response;
            console.timeEnd("REGISTRATION FIX PART");
            sync.forEachAll(objectsArray,
              function(value, allresult, next, otherParams) { 
                saveDocuments(value, otherParams, function(value, result) {
                    allresult.push({data: value, result: result});
                    next();
                });
              },
              function(allresult) {
                if(allresult.length === objectsArray.length){ 
                  logger.debug('Completed async handler: ' + JSON.stringify(allresult));
                  updateItemsList(data.hasItems, allresult)
                  .then(function(response){
                    data.hasItems = response;
                    return data.save();
                  })
                  .then(function(response){ return deviceActivityNotif(cid); })
                  .then(function(response){ return createAuditLogs(cid, allresult, adid); })
                  .then(function(response){
                    var finalResult = [];
                    for(var item in allresult){
                      if(allresult[item].result === "Success"){
                        finalResult.push({
                          oid: allresult[item].data.oid,
                          password: allresult[item].data.password,
                          "infrastructure-id": allresult[item].data["infrastructure-id"]
                        });
                      }
                    }
                    callback(false, finalResult);
                    console.timeEnd("ALL REGISTRATION EXECUTION");
                  })
                  .catch(function(err){ callback(true, "Error in final steps: " + err); });
                  }
                },
                false,
                {adid: adid, cid:cid, nodeId: nodeId, data:data, types:semanticTypes} 
              );
            }
          )
          .catch(function(err){callback(true, err); });
        }
      }
    );
  }
function create ( data , callback ) { var objectsArray = data . thingDescriptions ; var adid = data . adid !== undefined ? data . adid : data . agid ; console . time ( "ALL REGISTRATION EXECUTION" ) ; console . time ( "REGISTRATION FIX PART" ) ; nodeOp . findOne ( { adid : adid , status : "active" } , { cid : 1 , hasItems : 1 } , function ( err , data ) { if ( err || ! data ) { if ( err ) { callback ( true , "Error in Mongo: " + err ) ; } else { callback ( true , "Invalid adid identificator" ) ; } } else { var nodeId = data . _id ; var cid = data . cid ; var semanticTypes = { } ; semanticRepo . getTypes ( "Device" ) . then ( function ( response ) { return parseGetTypes ( JSON . parse ( response ) . data . results . bindings ) ; } ) . then ( function ( response ) { semanticTypes . devices = response ; return semanticRepo . getTypes ( "Service" ) ; } ) . then ( function ( response ) { return parseGetTypes ( JSON . parse ( response ) . data . results . bindings ) ; } ) . then ( function ( response ) { semanticTypes . services = response ; console . timeEnd ( "REGISTRATION FIX PART" ) ; sync . forEachAll ( objectsArray , function ( value , allresult , next , otherParams ) { saveDocuments ( value , otherParams , function ( value , result ) { allresult . push ( { data : value , result : result } ) ; next ( ) ; } ) ; } , function ( allresult ) { if ( allresult . length === objectsArray . length ) { logger . debug ( 'Completed async handler: ' + JSON . stringify ( allresult ) ) ; updateItemsList ( data . hasItems , allresult ) . then ( function ( response ) { data . hasItems = response ; return data . save ( ) ; } ) . then ( function ( response ) { return deviceActivityNotif ( cid ) ; } ) . then ( function ( response ) { return createAuditLogs ( cid , allresult , adid ) ; } ) . then ( function ( response ) { var finalResult = [ ] ; for ( var item in allresult ) { if ( allresult [ item ] . result === "Success" ) { finalResult . push ( { oid : allresult [ item ] . data . oid , password : allresult [ item ] . data . password , "infrastructure-id" : allresult [ item ] . data [ "infrastructure-id" ] } ) ; } } callback ( false , finalResult ) ; console . timeEnd ( "ALL REGISTRATION EXECUTION" ) ; } ) . catch ( function ( err ) { callback ( true , "Error in final steps: " + err ) ; } ) ; } } , false , { adid : adid , cid : cid , nodeId : nodeId , data : data , types : semanticTypes } ) ; } ) . catch ( function ( err ) { callback ( true , err ) ; } ) ; } } ) ; }
Save in Mongo dB all objects contained in the req.
Save in Mongo dB all objects contained in the req .
// Get available item types in the semantic repository // Process new items internally // Process all new items //logger.debug('END execution with value =', value, 'and result =', result); // Final part: Return results, update node and notify // Only process final step if all the stack of tasks completed // additional parameters