Skip to content

guillermofr/ngsijs

 
 

Repository files navigation

new info of ngsijs

This library has been modified to work with iot-broker

More info about iotbroker Install IOTbroker info and historyqueries info

And only 2 querys of v1 are tested

connection.v1.query and connection.v1.updateAttributes

Examples


query

var connection = new NGSI.Connection("http://iotbroker.example.com:8060");
connection.v1.query([
    { type: 'Room', id: 'Room.*', isPattern: true }
],
    ['temperature'],
    {
        restriction: {
            "attributeExpression": "",
            "scopes": [{
                "scopeType": "ISO8601TimeInterval",
                "scopeValue": "2015-08-18T16:54:00+0000/2018-08-18T16:55:00+0000"
            }]
        },
        limit: 100,
        offset: 200,
        details: true,
        onSuccess: function (data) {
            console.log("OK")
            console.log(JSON.stringify(data))
        },
        onFailure: function (data) {
            console.log("ERROR")
            console.log(JSON.stringify(data))
        }
    }
);

update

var connection = new NGSI.Connection("http://iotbroker.example.com:8060");
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0! 
var yyyy = today.getFullYear();
if (dd < 10) { dd = '0' + dd }
if (mm < 10) { mm = '0' + mm }
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();

var today = yyyy + "-" + mm + "-" + dd + " " + h + ":" + m + ":" + s;

    connection.v1.updateAttributes([
        {
            'entity': { type: 'Room', id: 'Room1' },
            "attributes": [
                {
                    "name": "temperature",
                    "type": "float",
                    "value": "26.5",
                    "metadata": [{
                        "name": "date",
                        "type": "date",
                        "value": ""+today
                    }]
                }
            ]
        }
    ], {
            onSuccess: function (data) {
                console.log(data)
            },
            onFailure: function (data) {
                console.log(data)
            }
        }
    );

none of v2 functions will work because v2 is not implemented on iotbroker

old info ngsijs

License Documentation Status Build Status Coverage Status

Ngsijs is the JavaScript library used by WireCloud for adding FIWARE NGSI capabilities to widgets and operators. However, this library has also been designed to be used in other environments as normal web pages and clients/servers running on Node.js.

This library has been developed following the FIWARE NGSI v1 and NGSI v2 specifications and has been tested to work against version 0.26.0+ of the Orion Context Broker.

Reference documentation of the API is available at http://conwetlab.github.io/ngsijs/stable/NGSI.html.

Using ngsijs from normal web pages

Just include a <script> element linking to the NGSI.min.js file:

<script type="text/javascript" src="url_to_NGSI.js"></script>

Once added the <script> element, you will be able to use all the features provided by the ngsijs library (except receiving notifications):

var connection = new NGSI.Connection("http://orion.example.com:1026");
connection.v2.listEntities().then((result) => {
    response.results.forEach((entity) => {
        console.log(entity.id);
    });
});

This example will display the id of the first 20 entities. See the documentation of the listEntities method for more info.

To be able to receive notifications inside a web browser the library requires the use of a ngsi-proxy server. You can use your own instance or you the ngsi-proxy available at https://ngsiproxy.lab.fiware.org.

var connection = new NGSI.Connection("http://orion.example.com:1026", {
    ngsi_proxy_url: "https://ngsiproxy.lab.fiware.org"
});

Using ngsijs from Node.js

$ npm install ngsijs

After installing the ngsijs node module, you will be able to use the API as usual:

var NGSI = require('ngsijs');
var connection = new NGSI.Connection("http://orion.example.com:1026");

Note: Node.js doesn't require the usage of a ngsi-proxy as you can create an HTTP endpoint easily (e.g. using express). Anyway, you can use it if you want, you only have to take into account that is better to directly provide the HTTP endpoint to reduce the overhead.

Using ngsijs from WireCloud widgets/operators

Take a look to the "3.2.1. Using Orion Context Broker" tutorial available at FIWARE Academy.

About

JavaScript library for Orion Context Broker services fixed to work with iot-broker (web & nodejs)

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 77.0%
  • CSS 23.0%