Giter Site home page Giter Site logo

cenidetiot / ngsi-parser Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 1.0 236 KB

ngsi-parser is an npm module for parsing and converting a simple unstructured JSON or value to an NSGI-compliant object

License: MIT License

JavaScript 100.00%
ngsi orion-context-broker fiware smartsdk

ngsi-parser's Introduction

NGSI - parser

https://nodei.co/npm/ngsi-parser.png?downloads=true&downloadRank=true&stars=true

What is ngsi-parser?

ngsi-parser is a npm module for parsing and converting a simple JSON or value to a NSGI-compliant object


Data types suported

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

	import NGSI as ngsi from 'ngsi-parser'

Entities Functions

Parse an Entity

	var entity = ngsi.parseEntity({
		id :'Room1',
		type:'Room',
		temperature : {
			value : 50 ,
			metadata : {
				frecuency: 40,
				scale: 'Celsious'
			}
		},
		dateStamp : new Date()
	})

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 : new Date()
	})
	// 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

	//Convert a Json to Ngsi
	var value = ngsi.parseValue(50)
	
	// Send to ContextBroker 
	cb.updateJSONAttrEntity(
		'idEntity', 'nameAttribute', value
	)
	.then((result) => console.log(result))
	.catch((err) => console.log(err))

Special Consults

Dinamic Query Consult

	//Convert a Json to Query
	let query = ngsi.createQuery({
	"id":"Device.*",
	"type":"Device",
	"options":"keyValues",
	"dateObserved" : ">=2018-02-20T16:54:03.931-06:00"
	})
	console.log(query)

Output

	?id=Device.*&Device&type=Device&options=keyValues&q=dateObserved>=2018-02-20T16:54:03.931-06:00

Usage with OCB-sender

	//Send requests to ContextBroker
	cb.getWithQuery(query)
    .then((result) => console.log(result))
	.catch((err) => console.log(err))

Data Model JSON Schema Analizer

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.

You can know about JSON Schemas in JSON Schema.

Importing your JSON Schema

You can import your JSON schema importing a JSON from a file or from a repository using the help of ocb-sender.

Importing From a JSON File

	var ngsi = require('ngsi-parser');
	var mySchema = require('mySchema.json');
	ngsi.setModel({
		mySchema : mySchema
	});

Importing From a remote repository

	var ngsi = require('ngsi-parser');
	ngsi.setModel({
		myRemoteSchema : 'https://yourdatamodels.com/myRemote'
	});

Importing from various sources

	var ngsi = require('ngsi-parser');
	var mySchema = require('mySchema.json');
	ngsi.setModel({
		mySchema : mySchema,
		myRemoteSchema : 'https://yourdatamodels.com/myRemote',
		AlertModel : 'https://raw.githubusercontent.com/smartsdk/dataModels/master/Alert/schema.json',
		anotherModel : require('anotherSchema.json')
	});

Using your Data Models Schemas

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 :  new  Date()  
	};
	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.

	var ngsi = require('ngsi-parser');
	var ocb = require('ocb-sender');
	ngsi.setModel({
		myRemoteSchema : 'https://yourdatamodels.com/myRemote',
	});
	var entity = { 
		id :'Room1',
		type:'Room',
		temperature : 50, 
		dateStamp :  new  Date()  
	};
	ngsi.verifyModel('myRemoteSchema', entity, ocb)
	.then((errors) => { 
		if (errors.length === 0 ){
			console.log("The entity it's OK")
		}else {
			errors.map(console.log)
		}
	})

Use without storing the JSON schema

	var ngsi = require('ngsi-parser');
	var ocb = require('ocb-sender');
	ngsi.verifyModel('https://yourdatamodels.com/myRemote', entity ,cb)
	.then((errors) => { 
		if (errors.length === 0 ){
			console.log("The entity it's OK")
		}else {
			errors.map(console.log)
		}
	})

Real Example Case

The Alert Schema imported from Fiware Data Models Repository in this Link.

{
	"$schema": "http://json-schema.org/schema#",
	"id": "https://fiware.github.io/dataModels/Alert/schema.json",
	"title": "Alert data model JSON Schema",
	"description": "An alert generated by a user or device in a givel location",
	"type": "object",
	"allOf": [
		{
			"$ref": "https://fiware.github.io/dataModels/common-schema.json#/definitions/GSMA-Commons"
		},
		{

			"$ref": "https://fiware.github.io/dataModels/common-schema.json#/definitions/Location-Commons"

		},
		{
			"properties": {
				"description": {
					"type": "string"
				},
				"dateObserved": {
					"type": "string",
					"format": "date-time"	
				},
				"validFrom": {
					"type": "string",
					"format": "date-time"
				},
				"validTo": {
					"type": "string",
					"format": "date-time"
				},
				"severity": {
					"type": "string",
						"enum": [
							"informational",
							"low",
							"medium",
							"high",
							"critical"
						]
				},
				"category": {
					"type": "string",
					"enum": [
						"traffic",
						"weather",
						"environment",
						"health",
						"security"
					]
				},
				"subCategory": {
					"type": "string",
					"enum": [
						"trafficJam",
						"carAccident",
						"carWrongDirection",
						"carStopped",
						"pothole",
						"roadClosed",
						"roadWorks",
						"hazardOnRoad",
						"injuredBiker",
						"rainfall",
						"highTemperature",
						"lowTemperature",
						"heatWave",
						"ice",
						"snow",
						"wind",
						"fog",
						"flood",
						"tsunami",
						"tornado",
						"tropicalCyclone",
						"hurricane",
						"asthmaAttack",
						"bumpedPatient",
						"fallenPatient",
						"heartAttack",
						"suspiciousAction",
						"robbery",
						"assault"
					]
			},
			"alertSource": {
				"oneOf": [
					{
						"type": "string",
						"format": "uri"
					},
					{
						"$ref": "https://fiware.github.io/dataModels/common-schema.json#/definitions/EntityIdentifierType"
					}
				]
			},
			"data": {
				"type": "object"
			},

				"type": {
					"type": "string",
					"enum": [
						"Alert"
					],
					"description": "NGSI Entity type"
				}
			}
		}
	],
	"oneOf": [
		{
			"required": [
				"id",
				"type",
				"location",
				"alertSource",
				"category",
				"dateObserved"
			]
		},
		{
			"required": [
				"id",
				"type",
				"address",
				"alertSource",
				"category",
				"dateObserved"
			]
		}
	]
}
var ngsi = require('ngsi-parser');
var ocb = require('ocb-sender');

ngsi.setModel({
	Alert : 'https://raw.githubusercontent.com/smartsdk/dataModels/master/Alert/schema.json'
})

var  alertEntity  = {
	id: "Alert:Device_Smartphone_7a85d9df7209b8bc:1519086635021",
	type: "Alert",
	alertSource: "Device_Smartphone_7a85d9df7209b8bc",
	category: "traffic",
	dateObserved: new  Date(),
	description: "Car Accident on Cenidet",
	location: {
	type : "geo:point",
		value : "18.81186166666667 ,-98.96342000000001"
	},
	severity: "medium",
	subCategory: "carAccident",
	validFrom: new  Date(),
	validTo: new  Date(),
	dateCreated : new  Date()
}

ngsi.verifyModel('Alert', alertEntity, ocb)
.then((errors) => {
	if (errors.length === 0 ){
		console.log("The entity it's OK");
		var ngsiEntity = ngsi.parseEntity(alertEntity);
		ocb.createEntity(ngsiEntity)
		.then((result) => console.log(result))
		.catch((err) => console.log(err))
	}else {
		errors.map(console.log)
		//another action if it does not comply with the model
	}
})

License

MIT

ngsi-parser's People

Contributors

cenidetiot avatar danieltorr avatar haidee09 avatar toodaniels avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

carlosbm

ngsi-parser's Issues

$ref error 'https://smart-data-models.github.io/data-models/common-schema.json'

Hello.

When I import a copying Fiware's JSON schema from a JSON file, an error will occur.
Are you planning to update the Github URL in ModelParser.js?

Error

Error: Actualy the only the refs supported are GSMA, Location and PhysicalObject commons
    at Schema.allOf.map (C:\Develop\nodejs\fiware\node_modules\ngsi-parser\lib\ModelParser.js:257:28)
    at Array.map (<anonymous>)
    at NGSI.prepareModel (C:\Develop\nodejs\fiware\node_modules\ngsi-parser\lib\ModelParser.js:248:25)
    at NGSI.verifyModel (C:\Develop\nodejs\fiware\node_modules\ngsi-parser\lib\ModelParser.js:294:36)
    at NGSI.verifyModel (C:\Develop\nodejs\fiware\node_modules\ngsi-parser\lib\ModelParser.js:380:31)
    at Object.<anonymous> (C:\Develop\nodejs\fiware\ngsiDataModelTest.js:21:19)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)

Use JSON Data
https://github.com/smart-data-models/dataModel.Weather/blob/master/WeatherObserved/schema.json

verifyTest.js


// Data Model
const mySchemaImported = require('./dataModels/AirQualityObserved.json');
ngsi.setModel({
    testSchema : mySchemaImported
    // testSchema : 'https://github.com/smart-data-models/dataModel.Environment/blob/master/AirQualityObserved/schema.json'
});

let test = { 
    // id :'Alert1',
    type:'AirQualityObserved',
    // location : "45.001, 45.001" 
    dateObserved : new Date()  
};

let errors = ngsi.verifyModel('testSchema', test);
// console.log(errors);
if (errors.length === 0 ){
    console.log("The entity it's OK")
}else {
    errors.map(console.log)
}```


AirQualityObserved.json
```{
    "$schema": "http://json-schema.org/schema#",
    "id": "https://smart-data-models.github.io/dataModel.Environment/AirQualityObserved/schema.json",
    "title": "GSMA / FIWARE - Air quality observed schema",
    "description": "An observation of air quality conditions at a certain place and time.",
    "type": "object",
    "allOf": [
      {
        "$ref": "https://smart-data-models.github.io/data-models/common-schema.json#/definitions/GSMA-Commons"
      },
      {
        "$ref": "https://smart-data-models.github.io/data-models/common-schema.json#/definitions/Location-Commons"
      },
      {
        "properties": {
          "type": {
            "type": "string",
            "enum": [
              "AirQualityObserved"
            ],
            "description": "NGSI Entity type"
          },
          "dateObserved": {
            "type": "string"
          },
          "airQualityIndex": {
            "type": "number",
            "minimum": 0
          },
          "airQualityLevel": {
            "type": "string",
            "minLength": 2
          },
          "reliability": {
            "type": "number",
            "minimum": 0,
            "maximum": 1.0
          }
        }
      }
    ],
    "required": [
      "id",
      "type",
      "dateObserved",
      "location"
    ]
  }```

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.