REST Data Set

The REST dataset enables Knowage to retrieve data from external REST services. The developer of the dataset is free to define the body, method, headers and parameters of the request; then he has to specify how to read data from the service response using JSON Path expressions (at the moment no other ways to read data is available, therefore the REST service is presumed to return data in JSON format).

Let’s make as example in order to understand how it works. Suppose an external REST service providing data from sensors, we want to retrieve values from prosumers electricity meters, a prosumer being a producer/consumer of electricity, and that the request body should be something like:

Listing 10 Request body code
1{  "entities": [ {
2   "isPattern": "true",
3   "id": ".*",
4   "type":"Meter"
5   } ]
6}

while querying for Meter entities, and that the JSON response is something like:

Listing 11 RJSON response code
 1{
 2       "contextResponses": [
 3    {
 4       "contextElement": {
 5       "id": "pros6_Meter",
 6       "type": "Meter",
 7       "isPattern": "false",
 8       "attributes": [
 9           {
10             "name": "atTime",
11             "type": "timestamp",
12              "value": "2015-07-21T14:49:46.968+0200"
13            },
14            {
15             "name": "downstreamActivePower",
16             "type": "double",
17             "value": "3.8"
18            },
19           {
20             "name": "prosumerId",
21             "type": "string",
22             "value": "pros3"
23           },
24           {
25             "name": "unitOfMeasurement",
26             "type": "string",
27             "value": "kW"
28            },
29            {
30             "name": "upstreamActivePower",
31             "type": "double",
32             "value": "3.97"
33             }
34           ]
35           },
36      "statusCode": {
37              "reasonPhrase": "OK",
38              "code": "200"
39                    }
40        },
41           {
42       "contextElement": {
43              "id": "pros5_Meter",
44              "type": "Meter",
45              "isPattern": "false",
46              "attributes": [
47           {
48              "name": "atTime",
49              "type": "timestamp",
50              "value": "2015-08-09T20:29:45.698+0200"
51           },
52           {
53              "name": "downstreamActivePower",
54              "type": "double",
55              "value": "1.8"
56           },
57            {
58              "name": "prosumerId",
59              "type": "string",
60              "value": "pros5"
61          },
62           {
63             "name": "unitOfMeasurement",
64             "type": "string",
65             "value": "kW"
66          },
67        {
68             "name": "upstreamActivePower",
69             "type": "double",
70             "value": "0"
71         }
72                 ]
73        },
74             "statusCode": {
75             "reasonPhrase": "OK",
76             "code": "200"
77              }
78        }
79                ]
80        }

In this example we have two Context Elements with the following attributes:

  • atTime ;

  • downstreamActivePower;

  • prosumerId;

  • unitOfMeasurement;

  • upstreamActivePower.

Let’s see how to define a Knowage dataset:

../../_images/image341.png

Fig. 69 REST dataset interface.

We specified

  • the URL of the REST service;

  • the request body;

  • the request headers (in this example we ask the service for JSON data);

  • the HTTP method;

  • the JSONPath to retrieve the items (see below), i.e. the JSONPath where the items are stored;

  • the JSONPaths to retrieve the attributes (see below), i.e. the JSONPaths useful to retrieve the attributes of the items we are looking for; those paths are relative to the “JSON Path items”;

  • offset, fetch size and max results parameters, in case the REST service has pagination.

Once followed the steps above the user obtains upstream/downstream active power for each prosumer.

NGSI checkbox is specific for NGSI REST calls: it permits easy the job when querying the Orion Context Broker (https://github.com/telefonicaid/fiware-orion) and to omit some of the REST fields (since the JSON format from NGSI specifications is fixed): you don’t need to specify headers, JSONPath items, JSONPath attributes (all available attributes are fetched) and pagination parameters (offset and fetch size).

When checking the Use directly JSON attributes checkbox, yon can skip the definition of the JSONPath attributes, since the JSON structure is presumed to be fixed as in the following example:

Listing 12 Use directly JSON attributes
 1{
 2 "contextResponses": [
 3   {
 4     "prosumerId":"pros1",
 5     "downstreamActivePower":3.1,
 6     "upstreamActivePower":0.0
 7   },{
 8     "prosumerId":"pros2",
 9     "downstreamActivePower":0.5,
10     "upstreamActivePower":2.4
11      }
12                    ]
13}

Then it will be enough to define only the JSON Path Items and check Use directly JSON Attributes without defining the attributes; the attributes will be retrieved automatically from the JSON object.

In the above examples, the JSON Path Items will be $.contextResponses[:sub:`\*`] and the dataset result will look like:

Table 3 Dataset result

prosumerId

downstreamActivePower

upstreamActivePower

pros1

3.1

0.0

pros2

0.5

2.4

The REST dataset permits usage of profile attributes and parameters using the same syntax as for other dataset types: $<profile attribute> and $P<parameter>. You can use both of them as placeholders in every field: most likely you need to use them in REST service URL or on the request body. As an example, suppose you want to retrieve the value of just one prosumer that is specified by the prosumerId parameter, you have to set the request body as:

Listing 13 Request body for prosumerId parameter
1{
2 "entities":[
3   {
4    "isPattern":"true",
5    "type":"Meter",
6    "id":"$P{prosumerId}"
7   }
8            ]
9}