diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index 854a262e1..989d7880e 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -1,3 +1,4 @@ - Fix: service header to use uppercase in case of update and delete (#1528) - Fix: Allow to send to CB batch update for multimeasures for NGSI-LD (#1623) - Add: new JEXL transformations for including into an array keys that have a certain value: valuePicker and valuePickerMulti +- Add: attribute metadata and static attributes metadata added to jexl context (#1630) diff --git a/doc/api.md b/doc/api.md index 6b3c4e19a..d3aa87aa7 100644 --- a/doc/api.md +++ b/doc/api.md @@ -260,10 +260,13 @@ measure name, unless `explicitAttrs` is defined. Measures `id` or `type` names a ## Device autoprovision and entity creation For those agents that uses IoTA Node LIB version 3.4.0 or higher, you should consider that the entity is not created -automatically when a device is created. This means that all entities into the Context Broker are created when data -arrives from a device, no matter if the device is explicitly provisioned (via [device provisioning API](#create-device-post-iotdevices)) or autoprovisioned. +automatically when a device is created. This means that all entities into the Context Broker are created when data +arrives from a device, no matter if the device is explicitly provisioned (via +[device provisioning API](#create-device-post-iotdevices)) or autoprovisioned. -If for any reason you need the entity at CB before the first measure of the corresponding device arrives to the IOTAgent, you can create it in advance using the Context Broker [NGSI v2 API](https://github.com/telefonicaid/fiware-orion/blob/master/doc/manuals/orion-api.md). +If for any reason you need the entity at CB before the first measure of the corresponding device arrives to the +IOTAgent, you can create it in advance using the Context Broker +[NGSI v2 API](https://github.com/telefonicaid/fiware-orion/blob/master/doc/manuals/orion-api.md). ## Multientity support @@ -494,7 +497,8 @@ mappings of the provision. If `explicitAttrs` is provided both at device and con precedence. Additionally `explicitAttrs` can be used to define which measures (identified by their attribute names, not by their object_id) defined in JSON/JEXL array will be propagated to NGSI interface. -Note that when `explicitAttrs` is an array or a JEXL expression resulting in to Array, if this array is empty then `TimeInstant` is not propaged to CB. +Note that when `explicitAttrs` is an array or a JEXL expression resulting in to Array, if this array is empty then +`TimeInstant` is not propaged to CB. The different possibilities are summarized below: @@ -603,6 +607,14 @@ expression. In all cases the following data is available to all expressions: Additionally, for attribute expressions (`expression`, `entity_name`), `entityNameExp` and metadata expressions (`expression`) measures are available in the **context** used to evaluate them. +Attribute metadata and Static Attribute metadata are available in the **context** under the following convention: +`metadata..` or `metadata..` in a similar way of defined +for [Context Broker](https://github.com/telefonicaid/fiware-orion/blob/master/doc/manuals/orion-api.md#metadata-support) + +Moreover, if attribute metadata has an expression metadata attribute value in jexl context it is updated after that +expression is evaluated. Note that there is no order into metadata structure and there is no warranty about which +metadata attribute expression will be evaluated first. + ### Examples of JEXL expressions The following table shows expressions and their expected outcomes taking into account the following measures at @@ -647,53 +659,52 @@ to incorporate new transformations from the IoT Agent configuration file in a fa Current common transformation set: -| JEXL Transformation | Equivalent JavaScript Function | -| ------------------------------------ | ----------------------------------------------------------------------------------------------------------------------- | -| jsonparse: (str) | `JSON.parse(str);` | -| jsonstringify: (obj) | `JSON.stringify(obj);` | -| indexOf: (val, char) | `String(val).indexOf(char);` | -| length: (val) | `String(val).length;` | -| trim: (val) | `String(val).trim();` | -| substr: (val, int1, int2) | `String(val).substr(int1, int2);` | -| addreduce: (arr) | arr.reduce((i, v) | i + v)); | -| lengtharray: (arr) | `arr.length;` | -| typeof: (val) | `typeof val;` | -| isarray: (arr) | `Array.isArray(arr);` | -| isnan: (val) | `isNaN(val);` | -| parseint: (val) | `parseInt(val);` | -| parsefloat: (val) | `parseFloat(val);` | -| toisodate: (val) | `new Date(val).toISOString();` | -| timeoffset:(isostr) | `new Date(isostr).getTimezoneOffset();` | -| tostring: (val) | `val.toString();` | -| urlencode: (val) | `encodeURI(val);` | -| urldecode: (val) | `decodeURI(val);` | -| replacestr: (str, from, to) | `str.replace(from, to);` | -| replaceregexp: (str, reg, to) | `str.replace(new RegExp(reg), to);` | -| replaceallstr: (str, from, to) | `str.replaceAll(from, to);` | -| replaceallregexp: (str, reg, to) | `str.replaceAll(new RegExp(reg,"g"), to);` | -| split: (str, ch) | `str.split(ch);` | -| joinarrtostr: (arr, ch) | `arr.join(ch);` | -| concatarr: (arr, arr2) | `arr.concat(arr2);` | -| mapper: (val, values, choices) | choices[values.findIndex((target) | target == val)]); | -| thmapper: (val, values, choices) | choices[values.reduce((acc,curr,i,arr) | (acc==0)||acc?acc:val<=curr?acc=i:acc=null,null)]; | -| bitwisemask: (i,mask,op,shf) | (op==="&"?parseInt(i)&mask: op==="|"?parseInt(i)|mask: op==="^"?parseInt(i)^mask:i)>>shf; | -| slice: (arr, init, end) | `arr.slice(init,end);` | -| addset: (arr, x) | { return Array.from((new Set(arr)).add(x)) } | -| removeset: (arr, x) | { let s = new Set(arr); s.delete(x); return Array.from(s) } | -| touppercase: (val) | `String(val).toUpperCase()` | -| tolowercase: (val) | `String(val).toLowerCase()` | -| round: (val) | `Math.round(val)` | -| floor: (val) | `Math.floor(val)` | -| ceil: (val) | `Math.ceil(val)` | -| tofixed: (val, decimals) | `Number.parseFloat(val).toFixed(decimals)` | -| gettime: (d) | `new Date(d).getTime()` | -| toisostring: (d) | `new Date(d).toISOString()` | -| localestring: (d, timezone, options) | `new Date(d).toLocaleString(timezone, options)` | -| now: () | `Date.now()` | -| hextostring: (val) | `new TextDecoder().decode(new Uint8Array(val.match(/.{1,2}/g).map(byte => parseInt(byte, 16))))` | -| valuePicker: (val,pick) | valuePicker: (val,pick) => Object.entries(val).filter(([_, v]) => v === pick).map(([k, _]) => k) | -| valuePickerMulti: (val,pick) | valuePickerMulti: (val,pick) => Object.entries(val).filter(([_, v]) => pick.includes(v)).map(([k, _]) => k) | - +| JEXL Transformation | Equivalent JavaScript Function | +| ------------------------------------ | ------------------------------------------------------------------------------------------------------------------------ | +| jsonparse: (str) | `JSON.parse(str);` | +| jsonstringify: (obj) | `JSON.stringify(obj);` | +| indexOf: (val, char) | `String(val).indexOf(char);` | +| length: (val) | `String(val).length;` | +| trim: (val) | `String(val).trim();` | +| substr: (val, int1, int2) | `String(val).substr(int1, int2);` | +| addreduce: (arr) | arr.reduce((i, v) | i + v)); | +| lengtharray: (arr) | `arr.length;` | +| typeof: (val) | `typeof val;` | +| isarray: (arr) | `Array.isArray(arr);` | +| isnan: (val) | `isNaN(val);` | +| parseint: (val) | `parseInt(val);` | +| parsefloat: (val) | `parseFloat(val);` | +| toisodate: (val) | `new Date(val).toISOString();` | +| timeoffset:(isostr) | `new Date(isostr).getTimezoneOffset();` | +| tostring: (val) | `val.toString();` | +| urlencode: (val) | `encodeURI(val);` | +| urldecode: (val) | `decodeURI(val);` | +| replacestr: (str, from, to) | `str.replace(from, to);` | +| replaceregexp: (str, reg, to) | `str.replace(new RegExp(reg), to);` | +| replaceallstr: (str, from, to) | `str.replaceAll(from, to);` | +| replaceallregexp: (str, reg, to) | `str.replaceAll(new RegExp(reg,"g"), to);` | +| split: (str, ch) | `str.split(ch);` | +| joinarrtostr: (arr, ch) | `arr.join(ch);` | +| concatarr: (arr, arr2) | `arr.concat(arr2);` | +| mapper: (val, values, choices) | choices[values.findIndex((target) | target == val)]); | +| thmapper: (val, values, choices) | choices[values.reduce((acc,curr,i,arr) | (acc==0)||acc?acc:val<=curr?acc=i:acc=null,null)]; | +| bitwisemask: (i,mask,op,shf) | (op==="&"?parseInt(i)&mask: op==="|"?parseInt(i)|mask: op==="^"?parseInt(i)^mask:i)>>shf; | +| slice: (arr, init, end) | `arr.slice(init,end);` | +| addset: (arr, x) | { return Array.from((new Set(arr)).add(x)) } | +| removeset: (arr, x) | { let s = new Set(arr); s.delete(x); return Array.from(s) } | +| touppercase: (val) | `String(val).toUpperCase()` | +| tolowercase: (val) | `String(val).toLowerCase()` | +| round: (val) | `Math.round(val)` | +| floor: (val) | `Math.floor(val)` | +| ceil: (val) | `Math.ceil(val)` | +| tofixed: (val, decimals) | `Number.parseFloat(val).toFixed(decimals)` | +| gettime: (d) | `new Date(d).getTime()` | +| toisostring: (d) | `new Date(d).toISOString()` | +| localestring: (d, timezone, options) | `new Date(d).toLocaleString(timezone, options)` | +| now: () | `Date.now()` | +| hextostring: (val) | `new TextDecoder().decode(new Uint8Array(val.match(/.{1,2}/g).map(byte => parseInt(byte, 16))))` | +| valuePicker: (val,pick) | valuePicker: (val,pick) => Object.entries(val).filter(([_, v]) => v === pick).map(([k, _]) => k) | +| valuePickerMulti: (val,pick) | valuePickerMulti: (val,pick) => Object.entries(val).filter(([_, v]) => pick.includes(v)).map(([k, _]) => k) | You have available this [JEXL interactive playground][99] with all the transformations already loaded, in which you can test all the functions described above. @@ -1233,13 +1244,20 @@ In this case a batch update (`POST /v2/op/update`) to CB will be generated with ## Command execution -This section reviews the end-to-end process to trigger and receive commands into devices. The URL paths and messages format is based on the [IoT Agent JSON](https://github.com/telefonicaid/iotagent-json). It may differ in the case of using any other IoT Agent. In that case, please refer to the specific IoTA documentation. +This section reviews the end-to-end process to trigger and receive commands into devices. The URL paths and messages +format is based on the [IoT Agent JSON](https://github.com/telefonicaid/iotagent-json). It may differ in the case of +using any other IoT Agent. In that case, please refer to the specific IoTA documentation. ### Triggering commands -This starts the process of sending data to devices. It starts by updating an attribute into the Context Broker defined as `command` in the [config group](#config-group-datamodel) or in the [device provision](#device-datamodel). Commands attributes are created using `command` as attribute type. Also, you can define the protocol you want the commands to be sent (HTTP/MQTT) with the `transport` parameter at the provisioning process. +This starts the process of sending data to devices. It starts by updating an attribute into the Context Broker defined +as `command` in the [config group](#config-group-datamodel) or in the [device provision](#device-datamodel). Commands +attributes are created using `command` as attribute type. Also, you can define the protocol you want the commands to be +sent (HTTP/MQTT) with the `transport` parameter at the provisioning process. -For a given device provisioned with a `ping` command defined, any update on this attribute "ping" at the NGSI entity in the Context Broker will send a command to your device. For instance, to send the `ping` command with value `Ping request` you could use the following operation in the Context Broker API: +For a given device provisioned with a `ping` command defined, any update on this attribute "ping" at the NGSI entity in +the Context Broker will send a command to your device. For instance, to send the `ping` command with value +`Ping request` you could use the following operation in the Context Broker API: ``` PUT /v2/entities//attrs/ping?type= @@ -1253,30 +1271,36 @@ PUT /v2/entities//attrs/ping?type= It is important to note that parameter `type`, with the entity type must be included. -Context Broker API is quite flexible and allows to update an attribute in several ways. Please have a look to the [Orion API]([http://telefonicaid.github.io/fiware-orion/api/v2/stable](https://github.com/telefonicaid/fiware-orion/blob/master/doc/manuals/orion-api.md)) for details. +Context Broker API is quite flexible and allows to update an attribute in several ways. Please have a look to the +[Orion API](<[http://telefonicaid.github.io/fiware-orion/api/v2/stable](https://github.com/telefonicaid/fiware-orion/blob/master/doc/manuals/orion-api.md)>) +for details. -**Important note**: don't use operations in the NGSI API with creation semantics. Otherwise, the entity/attribute will be created locally to Context Broker and the command will not progress to the device (and you will need to delete the created entity/attribute if you want to make it to work again). Thus, the following operations *must not* be used: +**Important note**: don't use operations in the NGSI API with creation semantics. Otherwise, the entity/attribute will +be created locally to Context Broker and the command will not progress to the device (and you will need to delete the +created entity/attribute if you want to make it to work again). Thus, the following operations _must not_ be used: -* `POST /v2/entities` -* `POST /v2/entities//attrs` -* `PUT /v2/entities//attrs` -* `POST /v2/op/entites` with `actionType` `append`, `appendStrict` or `replace` +- `POST /v2/entities` +- `POST /v2/entities//attrs` +- `PUT /v2/entities//attrs` +- `POST /v2/op/entites` with `actionType` `append`, `appendStrict` or `replace` ### Command reception -Once the command is triggered, it is send to the device. Depending on transport protocol, it is going to be sent to the device in a different way. After sending the command, the IoT Agent will have updated the value of `ping_status` to `PENDING` for entity into the Context Broker. Neither -`ping_info` nor `ping` will be updated. +Once the command is triggered, it is send to the device. Depending on transport protocol, it is going to be sent to the +device in a different way. After sending the command, the IoT Agent will have updated the value of `ping_status` to +`PENDING` for entity into the Context Broker. Neither `ping_info` nor `ping` will be updated. #### HTTP devices **Push commands** -Push commands are those that are sent to the device once the IoT Agent receives the request from the Context Broker. In order to -send push commands it is needed to set the `"endpoint": "http://[DEVICE_IP]:[PORT]/"` in the device or group provision. The device -is supposed to be listening for commands at that URL in a synchronous way. Make sure the device endpoint is reachable by the IoT -Agent. Push commands are only valid for HTTP devices. For MQTT devices it is not needed to set the `endpoint` parameter. +Push commands are those that are sent to the device once the IoT Agent receives the request from the Context Broker. In +order to send push commands it is needed to set the `"endpoint": "http://[DEVICE_IP]:[PORT]/"` in the device or group +provision. The device is supposed to be listening for commands at that URL in a synchronous way. Make sure the device +endpoint is reachable by the IoT Agent. Push commands are only valid for HTTP devices. For MQTT devices it is not needed +to set the `endpoint` parameter. -Considering using the IoTA-JSON Agent, and given the previous example, the device should receive a POST request to +Considering using the IoTA-JSON Agent, and given the previous example, the device should receive a POST request to `http://[DEVICE_IP]:[PORT]` with the following payload: ``` @@ -1288,22 +1312,29 @@ Content-Type: application/json **Poll commands** -Poll commands are those that are stored in the IoT Agent waiting to be retrieved by the devices. This kind of -commands are typically used for devices that doesn't have a public IP or the IP cannot be reached because of -power or netkork constrictions. The device connects to the IoT Agent periodically to retrieve commands. In order -to configure the device as poll commands you just need to avoid the usage of `endpoint` parameter in the device provision. +Poll commands are those that are stored in the IoT Agent waiting to be retrieved by the devices. This kind of commands +are typically used for devices that doesn't have a public IP or the IP cannot be reached because of power or netkork +constrictions. The device connects to the IoT Agent periodically to retrieve commands. In order to configure the device +as poll commands you just need to avoid the usage of `endpoint` parameter in the device provision. -Once the command request is issued to the IoT agent, the command is stored waiting to be retrieved by the device. In that moment, the status of the command is `"_status": "PENDING"`. +Once the command request is issued to the IoT agent, the command is stored waiting to be retrieved by the device. In +that moment, the status of the command is `"_status": "PENDING"`. -For HTTP devices, in order to retrieve a poll command, the need to make a GET request to the IoT Agent to the path used as `resource` in the provisioned group (`/iot/json` by default in IoTA-JSON if no `resource` is used) with the following parameters: +For HTTP devices, in order to retrieve a poll command, the need to make a GET request to the IoT Agent to the path used +as `resource` in the provisioned group (`/iot/json` by default in IoTA-JSON if no `resource` is used) with the following +parameters: -**FIXME [#1524](https://github.com/telefonicaid/iotagent-node-lib/issues/1524)**: `resource` different to the default one (`/iot/json` in the case of the [IoTA-JSON](https://github.com/telefonicaid/iotagent-json)) is not working at the present moment, but it will when this issue gets solved. +**FIXME [#1524](https://github.com/telefonicaid/iotagent-node-lib/issues/1524)**: `resource` different to the default +one (`/iot/json` in the case of the [IoTA-JSON](https://github.com/telefonicaid/iotagent-json)) is not working at the +present moment, but it will when this issue gets solved. -* `k`: API key of the device. -* `i`: Device ID. -* `getCmd`: This parameter is used to indicate the IoT Agent that the device is requesting a command. It is needed to set it to `1` +- `k`: API key of the device. +- `i`: Device ID. +- `getCmd`: This parameter is used to indicate the IoT Agent that the device is requesting a command. It is needed to + set it to `1` -Taking the previous example, and considering the usage of the IoTA-JSON Agent, the device should make the following request, being the response to this request a JSON object with the command name as key and the command value as value: +Taking the previous example, and considering the usage of the IoTA-JSON Agent, the device should make the following +request, being the response to this request a JSON object with the command name as key and the command value as value: **Request:** @@ -1316,15 +1347,20 @@ Accept: application/json **Response:**: ``` -200 OK -Content-type: application/json +200 OK +Content-type: application/json -{"ping":"Ping request"} +{"ping":"Ping request"} ``` -For IoT Agents different from IoTA-JSON it is exactly the same just changing in the request the resource by the corresponding resource employed by the agent (i.e., IoTA-UL uses `/iot/d` as default resource instead of `/iot/json`) and setting the correct `` and ``. The response will be also different depending on the IoT Agent employed. +For IoT Agents different from IoTA-JSON it is exactly the same just changing in the request the resource by the +corresponding resource employed by the agent (i.e., IoTA-UL uses `/iot/d` as default resource instead of `/iot/json`) +and setting the correct `` and ``. The response will be also different depending on the IoT Agent +employed. -**FIXME [#1524](https://github.com/telefonicaid/iotagent-node-lib/issues/1524)**: `resource` different to the default one (`/iot/json` in the case of the [IoTA-JSON](https://github.com/telefonicaid/iotagent-json)) is not working at the present moment, but it will when this issue gets solved. +**FIXME [#1524](https://github.com/telefonicaid/iotagent-node-lib/issues/1524)**: `resource` different to the default +one (`/iot/json` in the case of the [IoTA-JSON](https://github.com/telefonicaid/iotagent-json)) is not working at the +present moment, but it will when this issue gets solved. **Request** @@ -1338,61 +1374,61 @@ Content-Type: application/json **Response** ``` -200 OK -Content-type: application/json +200 OK +Content-type: application/json {"ping":"Ping request"} ``` +This is also possible for IoTA-UL Agent changing in the request the resource, setting the correct ``, +``, payload and headers. -This is also possible for IoTA-UL Agent changing in the request the resource, setting the correct ``, ``, payload and headers. - -Once the command is retrieved by the device the status is updated to `"_status": "DELIVERED"`. Note that status `DELIVERED` only make sense in the case of poll commands. In the case of push command it cannot happen. - +Once the command is retrieved by the device the status is updated to `"_status": "DELIVERED"`. Note that status +`DELIVERED` only make sense in the case of poll commands. In the case of push command it cannot happen. #### MQTT devices -For MQTT devices, it is not needed to declare an endpoint (i.e. if included in the provisioning request, it is not used). The device -is supposed to be subscribed to the following MQTT topic where the IoT Agent will publish the command: +For MQTT devices, it is not needed to declare an endpoint (i.e. if included in the provisioning request, it is not +used). The device is supposed to be subscribed to the following MQTT topic where the IoT Agent will publish the command: ``` ///cmd ``` -In the case of using the IoTA-JSON Agent, the device should subscribe to the previous topic where it is going to receive a message like -the following one when a command is triggered in the Context Broker like the previous step: +In the case of using the IoTA-JSON Agent, the device should subscribe to the previous topic where it is going to receive +a message like the following one when a command is triggered in the Context Broker like the previous step: ```json -{"ping":"Ping request"} +{ "ping": "Ping request" } ``` -Please note that the device should subscribe to the broker using the disabled clean session mode (enabled using -`--disable-clean-session` option CLI parameter in `mosquitto_sub`). This option means that all of the subscriptions for the device will -be maintained after it disconnects, along with subsequent QoS 1 and QoS 2 commands that arrive. When the device reconnects, it will -receive all of the queued commands. +Please note that the device should subscribe to the broker using the disabled clean session mode (enabled using +`--disable-clean-session` option CLI parameter in `mosquitto_sub`). This option means that all of the subscriptions for +the device will be maintained after it disconnects, along with subsequent QoS 1 and QoS 2 commands that arrive. When the +device reconnects, it will receive all of the queued commands. ### Command confirmation -Once the command is completely processed by the device, it should return the result of the command to the IoT -Agent. This result will be progressed to the Context Broker where it will be stored in the `_info` -attribute. The status of the command will be stored in the `_status` attribute (`OK` if everything -goes right). +Once the command is completely processed by the device, it should return the result of the command to the IoT Agent. +This result will be progressed to the Context Broker where it will be stored in the `_info` attribute. The +status of the command will be stored in the `_status` attribute (`OK` if everything goes right). -For the IoTA-JSON, the payload of the confirmation message must be a JSON object with name of the command as key -and the result of the command as value. For other IoT Agents, the payload must follow the corresponding protocol. -For a given `ping` command, with a command result `status_ok`, the response payload should be: +For the IoTA-JSON, the payload of the confirmation message must be a JSON object with name of the command as key and the +result of the command as value. For other IoT Agents, the payload must follow the corresponding protocol. For a given +`ping` command, with a command result `status_ok`, the response payload should be: ```JSON {"ping":"status_ok"} ``` -Eventually, once the device makes the response request the IoTA would update the attributes `ping_status` to -`OK` and `ping_info` to `status_ok` for the previous example. +Eventually, once the device makes the response request the IoTA would update the attributes `ping_status` to `OK` and +`ping_info` to `status_ok` for the previous example. #### HTTP -In order confirm the command execution, the device must make a POST request to the IoT Agent with the result -of the command as payload, no matter if it is a push or a poll command. Following with the IoTAgent JSON case, the request must be made to the `/iot/json/commands`, this way: +In order confirm the command execution, the device must make a POST request to the IoT Agent with the result of the +command as payload, no matter if it is a push or a poll command. Following with the IoTAgent JSON case, the request must +be made to the `/iot/json/commands`, this way: ``` POST /iot/json/commands?k=&i= @@ -1404,14 +1440,16 @@ Accept: application/json #### MQTT -The device should publish the result of the command (`{"ping":"status_ok"}` in the previous example) to a -topic following the next pattern: +The device should publish the result of the command (`{"ping":"status_ok"}` in the previous example) to a topic +following the next pattern: ``` ////cmdexe ``` -The IoTA is subscribed to that topic, so it gets the result of the command. When this happens, the status is updated to`"_status": "OK"`. Also the result of the command delivered by the device is stored in the `_info` attribute. +The IoTA is subscribed to that topic, so it gets the result of the command. When this happens, the status is updated +to`"_status": "OK"`. Also the result of the command delivered by the device is stored in the `_info` +attribute. ## Overriding global Context Broker host diff --git a/lib/services/ngsi/entities-NGSI-v2.js b/lib/services/ngsi/entities-NGSI-v2.js index d8c64a8f9..4a0d89460 100644 --- a/lib/services/ngsi/entities-NGSI-v2.js +++ b/lib/services/ngsi/entities-NGSI-v2.js @@ -261,7 +261,7 @@ function sendQueryValueNgsi2(entityName, attributes, typeInformation, token, cal * @param {String} token User token to identify against the PEP Proxies (optional). */ function sendUpdateValueNgsi2(entityName, originMeasures, originTypeInformation, token, callback) { - //aux function used to builf JEXL context. + //aux functions used to builf JEXL context. //it returns a flat object from an Attr array function reduceAttrToPlainObject(attrs, initObj = {}) { if (attrs !== undefined && Array.isArray(attrs)) { @@ -273,6 +273,27 @@ function sendUpdateValueNgsi2(entityName, originMeasures, originTypeInformation, return initObj; } } + //it returns a metadata object using the same structure described + // at https://github.com/telefonicaid/fiware-orion/blob/master/doc/manuals/orion-api.md#metadata-support + function reduceMetadataAttrToPlainObject(attrs, initObj = {}) { + if (attrs !== undefined && Array.isArray(attrs)) { + return attrs.reduce((result, item) => { + if (result['metadata'] === undefined) { + result['metadata'] = {}; + } + if (item.metadata !== undefined) { + result['metadata'][item.name] = {}; + for (var meta in item.metadata) { + result['metadata'][item.name][meta] = item.metadata[meta].value; + } + } + return result; + }, initObj); + } else { + return initObj; + } + } + //Make a clone and overwrite let idTypeSSSList = pluginUtils.getIdTypeServSubServiceFromDevice(originTypeInformation); @@ -321,6 +342,10 @@ function sendUpdateValueNgsi2(entityName, originMeasures, originTypeInformation, jexlctxt = reduceAttrToPlainObject(typeInformation.staticAttributes, jexlctxt); //id type Service and Subservice jexlctxt = reduceAttrToPlainObject(idTypeSSSList, jexlctxt); + //metadata attributes + jexlctxt = reduceMetadataAttrToPlainObject(typeInformation.active, jexlctxt); + //metadata static attributes + jexlctxt = reduceMetadataAttrToPlainObject(typeInformation.staticAttributes, jexlctxt); logger.debug( context, @@ -511,6 +536,16 @@ function sendUpdateValueNgsi2(entityName, originMeasures, originTypeInformation, } newAttrMeta['value'] = metaValueExpression; currentAttr.metadata[metaKey] = newAttrMeta; + + //RE-Populate de JEXLcontext + // It is possible metadata is still not in ctxt + if (!jexlctxt.metadata) { + jexlctxt.metadata = {}; + } + if (!jexlctxt.metadata[currentAttr.name]) { + jexlctxt.metadata[currentAttr.name] = {}; + } + jexlctxt.metadata[currentAttr.name][currentAttr.metadata[metaKey]] = metaValueExpression; } } } diff --git a/test/functional/testCases.js b/test/functional/testCases.js index bb3f51bec..3c360197a 100644 --- a/test/functional/testCases.js +++ b/test/functional/testCases.js @@ -1828,6 +1828,99 @@ const testCases = [ } ] }, + { + describeName: + '0175 - Simple group with active attribute + JEXL expression referencing metadata context attributes', + provision: { + url: 'http://localhost:' + config.iota.server.port + '/iot/services', + method: 'POST', + json: { + services: [ + { + resource: '/iot/json', + apikey: globalEnv.apikey, + entity_type: globalEnv.entity_type, + commands: [], + lazy: [], + static_attributes: [ + { + name: 'st_attr1', + type: 'Number', + value: 1.5, + metadata: { + coef1: { + value: 0.8, + type: 'Float' + } + } + }, + { + name: 'st_attr', + type: 'Number', + value: 1.5, + metadata: { + coef: { + value: 0.8, + type: 'Float' + } + } + }, + { + name: 'st_attr2', + type: 'Number', + value: 1.5, + metadata: { + coef2: { + value: 0.8, + type: 'Float' + } + } + } + ], + attributes: [ + { + object_id: 'a', + name: 'attr_a', + type: 'Number', + expression: 'a*st_attr*metadata.st_attr.coef' + } + ], + explicitAttrs: "['attr_a']" + } + ] + }, + headers: { + 'fiware-service': globalEnv.service, + 'fiware-servicepath': globalEnv.servicePath + } + }, + should: [ + { + shouldName: + 'A - WHEN sending a value (number) through http IT should apply the expression using the context attributes value and send to Context Broker the value "39.60" ', + type: 'single', + measure: { + url: 'http://localhost:' + config.http.port + '/iot/json', + method: 'POST', + qs: { + i: globalEnv.deviceId, + k: globalEnv.apikey + }, + json: { + a: 33 + } + }, + expectation: { + id: globalEnv.entity_name, + type: globalEnv.entity_type, + attr_a: { + value: 39.6, + type: 'Number' + } + } + } + ] + }, { describeName: '0180 - Simple group with active attributes + JEXL multiples expressions at same time', skip: 'lib', // Explanation in #1523