If value is a string, then type Text is used.
If value is a number, then type Number is used.
If value is a boolean, then type Boolean is used.
If value is Date, then DateTime is used.
If value is an object or array, then StructuredValue is used.
If value is null, then None is used.
How to Install
npm install ngsi-parser
or
yarn add ngsi-parser
Import
ES5
var ngsi =require('ngsi-parser')
ES6
importNGSIasngsifrom'ngsi-parser'
Entities Functions
Parse an Entity
var entity =ngsi.parseEntity({
id :'Room1',
type:'Room',
temperature :{
value :50,
metadata :{
frecuency:40,
scale:'Celsious'
}
},
dateStamp :newDate()
})
Output
{
"id":"Room1",
"type":"Room",
"temperature":{
"value":50,
"type":"Number",
"metadata":{
"frecuency":{
"value":40,
"type":"Number"
},
"scale":{
"value":"Celsious",
"type":"Text"
}
}
},
"dateStamp":{
"value":"2017-10-08T04:01:19.560Z",
"type":"DateTime",
"metadata":{}
}
}
Parse an Attribute
var attribute =ngsi.parseAttrs({
temperature :{
value :50,
metadata :{
frecuency :50,
scale:'Fahrenheit'
}
}
})
Output
{
"temperature":{
"value":50,
"type":"Number",
"metadata":{
"frecuency":{
"value":50,
"type":"Number"
},
"scale":{
"value":"Fahrenheit",
"type":"Text"
}
}
}
}
Parse a Value
var value =ngsi.parseValue(50)
Output
{
"value":50,
"type":"Number",
"metadata":{}
}
Usage with ocb-sender
For more information about ocb-sender see its documentation.
Create an Entity in the ContextBroker
//Convert a Json to Ngsi
var entity =ngsi.parseEntity({
id :'Room1',
type:'Room',
temperature :{
value :50,
metadata :{
frecuency:40,
scale:'Celsious'
}
},
dateStamp :newDate()
})
// Send to ContextBroker
cb.createEntity(entity)
.then((result)=>console.log(result))
.catch((err)=>console.log(err))
Update all the object attributes of an entity
//Convert a Json to Ngsi
var attribute =ngsi.parseAttrs({
temperature :{
value :50
}
})
// Send to ContextBroker
cb.updateEntityAttrs('Room1', attribute)
.then((result)=>console.log(result))
.catch((err)=>console.log(err))
Add a JSON Attribute to a NGSI entity.
//Convert a Json to Ngsi
var attribute =ngsi.parseAttrs({
temperature :{
value :50
}
})
// Send to ContextBroker
cb.addJSONAttributeToEntity('Room1', attribute)
.then((result)=>console.log(result))
.catch((err)=>console.log(err))
Update the JSON Object of an atttribute of the entity
ngsi-parser helps you to manage your Data Models like used in Fiware, ngsi-parser can analize if the entity complies with the specified model and identify its errors, to do it you need the JSON Schema of this Data Model provided or you can build some.
To use the schemas imported from a JSON file only you need to specify the name with which you entered it to ngsi-parser and it will return you one array with the errors found.
var ngsi =require('ngsi-parser');
var mySchema =require('mySchema.json');
ngsi.setModel({
mySchema : mySchema
});
var entity ={
id :'Room1',
type:'Room',
temperature :50,
dateStamp :newDate()
};
let errors =ngsi.verifyModel('mySchema', entity);
if(errors.length===0){
console.log("The entity it's OK")
}else{
errors.map(console.log)
}
To use Schemas from a remote repository is necesary download it, is because you need use ocb-sender, and in this case the method ngsi.verifyModel() becomes to a promise.