{"info":{"_postman_id":"a25317b1-5952-4a35-9730-21c65a6dcc52","name":"FIWARE XML Agent","description":"<html><head></head><body><p><a href=\"https://github.com/FIWARE/catalogue/blob/master/iot-agents/README.md\"><img src=\"https://nexus.lab.fiware.org/repository/raw/public/badges/chapters/iot-agents.svg\" alt=\"FIWARE IoT Agents\"></a>\n<a href=\"https://fiware-ges.github.io/orion/api/v2/stable/\"><img src=\"https://img.shields.io/badge/NGSI-v2-5dc0cf.svg\" alt=\"NGSI v2\"></a> </p>\n<p>This tutorial a wires up the dummy IoT devices which are responding using a custom <a href=\"https://www.w3.org/TR/xml11/\">XML</a>\nmessage format. A <strong>custom IoT Agent</strong> is created based on the IoT Agent Node.js\n<a href=\"https://iotagent-node-lib.readthedocs.io/en/latest/\">library</a> and the framework found in the\n<a href=\"https://fiware-iotagent-ul.readthedocs.io/en/latest/usermanual/index.html#user-programmers-manual\">IoT Agent for Ultralight</a>\ndevices so that measurements can be read and commands can be sent using\n<a href=\"https://fiware.github.io/specifications/OpenAPI/ngsiv2\">NGSI-v2</a> requests sent to the\n<a href=\"https://fiware-orion.readthedocs.io/en/latest/\">Orion Context Broker</a>.</p>\n<p>The tutorial uses <a href=\"https://ec.haxx.se/\">cUrl</a> commands throughout, but is also available as\n<a href=\"https://fiware.github.io/tutorials.Custom-IoT-Agent/\">Postman documentation</a></p>\n<p>The <code>docker-compose</code> files for this tutorial can be found on GitHub: </p>\n<p><img src=\"https://fiware.github.io/tutorials.Custom-IoT-Agent/icon/GitHub-Mark-32px.png\" alt=\"GitHub\"> <a href=\"https://github.com/Fiware/tutorials.Custom-IoT-Agent\">FIWARE 205: Creating a Custom IoT Agent</a></p>\n<h1 id=\"passing-custom-message-formats\">Passing custom message formats</h1>\n<p>As defined previously, an IoT Agent is a component that lets a group of devices send their data to and be managed from a\nContext Broker using their own native protocols. Every IoT Agent is defined for a single payload format, although they\nmay be able to use multiple disparate transports for that payload.</p>\n<p>IoT Agents for many standard payloads exist, however it is possible to envisage that additional payloads may be needed\nas many potential sources of context data have already their own well-defined de-facto or de-jure standards for passing\ndata around systems. As an example the <strong>ISOXML</strong> standard\n<a href=\"https://www.iso.org/obp/ui/#iso:std:iso:11783:-10:ed-2:v1:en\">iso:11783</a> is frequently used with Agricultural\nMachinery.</p>\n<p>The process for creating your own IoT Agent is relatively simple. It is best achieved through selecting an IoT Agent\nwhich uses the required data transport and rewriting/amending the payload processing code to handle the payloads in\nquestion.</p>\n<p>For the purpose of this tutorial we will amend code from the existing Ultralight IoT Agent to process a similar custom\nXML format. A direct comparison of the two IoT Agents can be seen below:</p>\n<div class=\"click-to-expand-wrapper is-table-wrapper\"><table>\n<thead>\n<tr>\n<th>IoT Agent for Ultralight</th>\n<th>IoT Agent for JSON</th>\n<th>Protocol's Area of Concern</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>Sample Measure <code>c|1</code></td>\n<td>Sample Measure <code>&lt;measure device=\"lamp002\" key=\"xxx\"&gt;</code><br>&nbsp;<code>&lt;c value=\"1\"/&gt;</code><br></td>\n<td></td>\n</tr>\n<tr>\n<td><code>&lt;/measure&gt;</code></td>\n<td>Message Payload</td>\n<td></td>\n</tr>\n<tr>\n<td>Sample Command <code>Robot1@turn|left=30</code></td>\n<td>Sample Command <code>&lt;turn device=\"Robot1\"&gt;</code><br>&nbsp;<code>&lt;left&gt;30&lt;/left&gt;</code><br><code>&lt;/turn&gt;</code></td>\n<td>Message Payload</td>\n</tr>\n<tr>\n<td>Content Type is <code>text/plain</code></td>\n<td>Content Type is <code>application/xml</code></td>\n<td>Message Payload</td>\n</tr>\n<tr>\n<td>Offers 3 transports - HTTP, MQTT and AMPQ</td>\n<td>Offers 3 transports - HTTP, MQTT and AMPQ</td>\n<td>Transport Mechanism</td>\n</tr>\n<tr>\n<td>HTTP listens for measures on <code>iot/d</code> by default</td>\n<td>HTTP listens for measures on <code>iot/xml</code> by default</td>\n<td>Transport Mechanism</td>\n</tr>\n<tr>\n<td>HTTP devices are identified by parameters <code>?i=XXX&amp;k=YYY</code></td>\n<td>HTTP devices are identified by payload <code>&lt;measure device=\"XXX\" key=\"YYY\"&gt;</code></td>\n<td>Device Identification</td>\n</tr>\n<tr>\n<td>HTTP commands posted to a well-known URL - response is in the reply</td>\n<td>HTTP commands posted to a well-known URL - response is in the reply</td>\n<td>Communications Handshake</td>\n</tr>\n<tr>\n<td>MQTT devices are identified by the path of the topic <code>/XXX/YYY</code></td>\n<td>MQTT devices are identified by the path of the topic <code>/XXX/YYY</code></td>\n<td>Device Identification</td>\n</tr>\n<tr>\n<td>MQTT commands posted to the <code>cmd</code> topic</td>\n<td>MQTT commands posted to the <code>cmd</code> topic</td>\n<td>Communications Handshake</td>\n</tr>\n<tr>\n<td>MQTT command responses posted to the <code>cmdexe</code> topic</td>\n<td>MQTT commands posted to the <code>cmdexe</code> topic</td>\n<td>Communications Handshake</td>\n</tr>\n</tbody>\n</table>\n</div><p>As can be seen, the supported communications transports (HTTP, MQTT, AMPQ) remain the same, it is processing of the\ncustom payload which will need to be adapted to ensure that the XML devices can communicate with the IoT Agent.</p>\n<p>It should be noted that, depending on your use case, it also may be necessary to create an additional middleware for\ncommunications purposes. In this example the <em>devices</em> are capable of sending measures and listening and responding to\ncommands directly on two separate comms channels. A different paradigm is used within the\n<a href=\"https://fiware-lorawan.readthedocs.io\">LoRaWAN</a> and <a href=\"https://iotagent-opcua.readthedocs.io\">OPC-UA</a> IoT Agents where an\nHTTP middleware responds to the IoT Agent, and it is then responsible for converting the communications to the\nlower-level CoAP transport used by the devices.</p>\n<h2 id=\"the-teaching-goal-of-this-tutorial\">The teaching goal of this tutorial</h2>\n<p>The aim of this tutorial is to improve developer understanding of how to create their own custom IoT Agents, a series of\nsimple modificiations has been made to the code of the Ultralight IoT Agent demonstrating how to make changes. The\ntutorial consists of a walk-through of the relevant code and a series of HTTP requests to connect the new IoT Agent. The\ncode can be found within the current\n<a href=\"https://github.com/FIWARE/tutorials.Custom-IoT-Agent/tree/master/iot-agent\">GitHub Repository</a></p>\n<h2 id=\"reusing-common-functionality\">Reusing Common Functionality</h2>\n<p>The benefit of modifying an existing IoT Agent is that the developer will be able to reuse the common functionality\nfound across all IoT Agents. This includes functions such as:</p>\n<ul>\n<li>Offering a standard location to listen to device updates</li>\n<li>Offering a standard location to listen to context data updates</li>\n<li>Holding a list of devices and mapping context data attributes to device syntax</li>\n<li>Security Authorization</li>\n</ul>\n<p>This base functionality has been abstracted out into a common\n<a href=\"https://iotagent-node-lib.readthedocs.io/\">IoT Agent framework library</a></p>\n<h4 id=\"device-monitor\">Device Monitor</h4>\n<p>For the purpose of this tutorial, a series of dummy IoT devices have been created, which will be attached to the context\nbroker. Details of the architecture and protocol used can be found in the\n<a href=\"https://github.com/FIWARE/tutorials.IoT-Sensors\">IoT Sensors tutorial</a> The state of each device can be seen on the JSON\ndevice monitor web page found at: <code>http://localhost:3000/device/monitor</code></p>\n<p><img src=\"https://fiware.github.io/tutorials.IoT-Agent-JSON/img/device-monitor.png\" alt=\"FIWARE Monitor\"></p>\n<h1 id=\"architecture\">Architecture</h1>\n<p>This application builds on the components created in\n<a href=\"https://github.com/FIWARE/tutorials.Subscriptions/\">previous tutorials</a>. It will make use of one FIWARE component - the\n<a href=\"https://fiware-orion.readthedocs.io/en/latest/\">Orion Context Broker</a> plus a <strong>Custom IoT Agent for XML</strong>. Usage of the\nOrion Context Broker is sufficient for an application to qualify as <em>“Powered by FIWARE”</em>. Both the Orion Context Broker\nand the IoT Agent and rely on open source <a href=\"https://www.mongodb.com/\">MongoDB</a> technology to keep persistence of the\ninformation they hold. We will also be using the dummy IoT devices created in the\n<a href=\"https://github.com/FIWARE/tutorials.IoT-Sensors/\">previous tutorial</a>, however they have been already been adapted to\nrespond to the custom XML messaging format.</p>\n<p>Therefore the overall architecture will consist of the following elements:</p>\n<ul>\n<li>The FIWARE <a href=\"https://fiware-orion.readthedocs.io/en/latest/\">Orion Context Broker</a> which will receive requests using\n<a href=\"https://fiware.github.io/specifications/OpenAPI/ngsiv2\">NGSI-v2</a></li>\n<li>The <strong>Custom IoT Agent for XML</strong> which will receive southbound requests using\n<a href=\"https://fiware.github.io/specifications/OpenAPI/ngsiv2\">NGSI-v2</a> and convert them to XML commands for the devices</li>\n<li>The underlying <a href=\"https://www.mongodb.com/\">MongoDB</a> database :<ul>\n<li>Used by the <strong>Orion Context Broker</strong> to hold context data information such as data entities, subscriptions and\nregistrations</li>\n<li>Used by the <strong>IoT Agent</strong> to hold device information such as device URLs and Keys</li>\n</ul>\n</li>\n<li>A webserver acting as set of <a href=\"https://github.com/FIWARE/tutorials.IoT-Sensors\">dummy IoT devices</a> using the custom\nXML messaging protocol running over HTTP.</li>\n</ul>\n<p>Since all interactions between the elements are initiated by HTTP requests, the entities can be containerized and run\nfrom exposed ports.</p>\n<p><img src=\"https://fiware.github.io/tutorials.IoT-Agent-JSON/img/architecture.png\" alt=\"\"></p>\n<p>The necessary configuration information for wiring up the IoT devices and the IoT Agent can be seen in the services\nsection of the associated <code>docker-compose.yml</code> file:</p>\n<h2 id=\"dummy-iot-devices-configuration\">Dummy IoT Devices Configuration</h2>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-yaml\">tutorial:\n    image: fiware/tutorials.context-provider\n    hostname: iot-sensors\n    container_name: fiware-tutorial\n    networks:\n        - default\n    expose:\n        - \"3000\"\n        - \"3001\"\n    ports:\n        - \"3000:3000\"\n        - \"3001:3001\"\n    environment:\n        - \"DEBUG=tutorial:*\"\n        - \"PORT=3000\"\n        - \"IOTA_HTTP_HOST=iot-agent\"\n        - \"IOTA_HTTP_PORT=7896\"\n        - \"DUMMY_DEVICES_PORT=3001\"\n        - \"DUMMY_DEVICES_API_KEY=4jggokgpepnvsb2uv4s40d59ov\"\n        - \"DUMMY_DEVICES_TRANSPORT=HTTP\"\n        - \"DUMMY_DEVICES_PAYLOAD=XML\"\n</code></pre>\n<p>The <code>tutorial</code> container is listening on two ports:</p>\n<ul>\n<li>Port <code>3000</code> is exposed so we can see the web page displaying the Dummy IoT devices.</li>\n<li>Port <code>3001</code> is exposed purely for tutorial access - so that cUrl or Postman can make JSON commands without being\npart of the same network.</li>\n</ul>\n<p>The <code>tutorial</code> container is driven by environment variables as shown:</p>\n<div class=\"click-to-expand-wrapper is-table-wrapper\"><table>\n<thead>\n<tr>\n<th>Key</th>\n<th>Value</th>\n<th>Description</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>DEBUG</td>\n<td><code>tutorial:*</code></td>\n<td>Debug flag used for logging</td>\n</tr>\n<tr>\n<td>WEB_APP_PORT</td>\n<td><code>3000</code></td>\n<td>Port used by web-app which displays the dummy device data</td>\n</tr>\n<tr>\n<td>IOTA_HTTP_HOST</td>\n<td><code>iot-agent</code></td>\n<td>The hostname of the IoT Agent for JSON - see below</td>\n</tr>\n<tr>\n<td>IOTA_HTTP_PORT</td>\n<td><code>7896</code></td>\n<td>The port that the IoT Agent for JSON will be listening on. <code>7896</code> is a common default for JSON over HTTP</td>\n</tr>\n<tr>\n<td>DUMMY_DEVICES_PORT</td>\n<td><code>3001</code></td>\n<td>Port used by the dummy IoT devices to receive commands</td>\n</tr>\n<tr>\n<td>DUMMY_DEVICES_API_KEY</td>\n<td><code>4jggokgpepnvsb2uv4s40d59ov</code></td>\n<td>Random security key used for IoT interactions - used to ensure the integrity of interactions between the devices and the IoT Agent</td>\n</tr>\n<tr>\n<td>DUMMY_DEVICES_TRANSPORT</td>\n<td><code>HTTP</code></td>\n<td>The transport protocol used by the dummy IoT devices</td>\n</tr>\n<tr>\n<td>DUMMY_DEVICES_PAYLOAD</td>\n<td><code>XML</code></td>\n<td>The message payload protocol by the dummy IoT devices</td>\n</tr>\n</tbody>\n</table>\n</div><p>The other <code>tutorial</code> container configuration values described in the YAML file are not used in this tutorial.</p>\n<h2 id=\"custom-xml-iot-agent-configuration\">Custom XML IoT Agent configuration</h2>\n<p>The code for the custom XML IoT Agent can be found within the\n<a href=\"https://github.com/FIWARE/tutorials.Custom-IoT-Agent/tree/master/iot-agent\">GitHub Repository</a> associated to this\ntutorial. It is a copy of the 1.12.0 version of the IoT Agent for Ultralight, lightly modified as described below. The\nassociated <a href=\"https://github.com/FIWARE/tutorials.Custom-IoT-Agent/blob/master/iot-agent/Dockerfile\">Dockerfile</a> merely\ncopies the code into an appropiate location within a Docker container running Node.js. This allows the component to be\ninstansiated using a <code>docker-compose.yaml</code> file. The necessary configuration can be seen below:</p>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-yaml\">iot-agent:\n    image: fiware/iotagent-xml\n    build:\n        context: iot-agent\n        dockerfile: Dockerfile\n    hostname: iot-agent\n    container_name: fiware-iot-agent\n    depends_on:\n        - mongo-db\n    networks:\n        - default\n    expose:\n        - \"4041\"\n        - \"7896\"\n    ports:\n        - \"4041:4041\"\n        - \"7896:7896\"\n    environment:\n        - IOTA_CB_HOST=orion\n        - IOTA_CB_PORT=1026\n        - IOTA_NORTH_PORT=4041\n        - IOTA_REGISTRY_TYPE=mongodb\n        - IOTA_LOG_LEVEL=DEBUG\n        - IOTA_TIMESTAMP=true\n        - IOTA_CB_NGSI_VERSION=v2\n        - IOTA_AUTOCAST=true\n        - IOTA_MONGO_HOST=mongo-db\n        - IOTA_MONGO_PORT=27017\n        - IOTA_MONGO_DB=iotagentjson\n        - IOTA_HTTP_PORT=7896\n        - IOTA_PROVIDER_URL=http://iot-agent:4041\n        - IOTA_DEFAULT_RESOURCE=/iot/xml\n</code></pre>\n<p>The <code>iot-agent</code> container relies on the precence of the Orion Context Broker and uses a MongoDB database to hold device\ninformation such as device URLs and Keys. The container is listening on two ports:</p>\n<ul>\n<li>Port <code>7896</code> is exposed to receive JSON measurements over HTTP from the Dummy IoT devices</li>\n<li>Port <code>4041</code> is exposed purely for tutorial access - so that cUrl or Postman can make provisioning commands without\nbeing part of the same network.</li>\n</ul>\n<p>The <code>iot-agent</code> container is driven by environment variables as shown:</p>\n<div class=\"click-to-expand-wrapper is-table-wrapper\"><table>\n<thead>\n<tr>\n<th>Key</th>\n<th>Value</th>\n<th>Description</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>IOTA_CB_HOST</td>\n<td><code>orion</code></td>\n<td>Hostname of the context broker to update context</td>\n</tr>\n<tr>\n<td>IOTA_CB_PORT</td>\n<td><code>1026</code></td>\n<td>Port that context broker listens on to update context</td>\n</tr>\n<tr>\n<td>IOTA_NORTH_PORT</td>\n<td><code>4041</code></td>\n<td>Port used for Configuring the IoT Agent and receiving context updates from the context broker</td>\n</tr>\n<tr>\n<td>IOTA_REGISTRY_TYPE</td>\n<td><code>mongodb</code></td>\n<td>Whether to hold IoT device info in memory or in a database</td>\n</tr>\n<tr>\n<td>IOTA_LOG_LEVEL</td>\n<td><code>DEBUG</code></td>\n<td>The log level of the IoT Agent</td>\n</tr>\n<tr>\n<td>IOTA_TIMESTAMP</td>\n<td><code>true</code></td>\n<td>Whether to supply timestamp information with each measurement received from attached devices</td>\n</tr>\n<tr>\n<td>IOTA_CB_NGSI_VERSION</td>\n<td><code>v2</code></td>\n<td>Whether to supply use NGSI v2 when sending updates for active attributes</td>\n</tr>\n<tr>\n<td>IOTA_AUTOCAST</td>\n<td><code>true</code></td>\n<td>Ensure JSON number values are read as numbers not strings</td>\n</tr>\n<tr>\n<td>IOTA_MONGO_HOST</td>\n<td><code>context-db</code></td>\n<td>The hostname of mongoDB - used for holding device information</td>\n</tr>\n<tr>\n<td>IOTA_MONGO_PORT</td>\n<td><code>27017</code></td>\n<td>The port mongoDB is listening on</td>\n</tr>\n<tr>\n<td>IOTA_MONGO_DB</td>\n<td><code>iotagentjson</code></td>\n<td>The name of the database used in mongoDB</td>\n</tr>\n<tr>\n<td>IOTA_HTTP_PORT</td>\n<td><code>7896</code></td>\n<td>The port where the IoT Agent listens for IoT device traffic over HTTP</td>\n</tr>\n<tr>\n<td>IOTA_PROVIDER_URL</td>\n<td><code>http://iot-agent:4041</code></td>\n<td>URL passed to the Context Broker when commands are registered, used as a forwarding URL location when the Context Broker issues a command to a device</td>\n</tr>\n<tr>\n<td>IOTA_DEFAULT_RESOURCE</td>\n<td><code>/iot/xml</code></td>\n<td>The default path the IoT Agent uses listenening for custom XML measures.</td>\n</tr>\n</tbody>\n</table>\n</div></body></html>","schema":"https://schema.getpostman.com/json/collection/v2.0.0/collection.json","toc":[{"content":"Passing custom message formats","slug":"passing-custom-message-formats"},{"content":"Architecture","slug":"architecture"}],"owner":"513743","collectionId":"a25317b1-5952-4a35-9730-21c65a6dcc52","publishedId":"Szzn6c2L","public":true,"customColor":{"top-bar":"FFFFFF","right-sidebar":"303030","highlight":"45D3DD"},"publishDate":"2020-06-19T15:09:04.000Z"},"item":[{"name":"Creating a Custom IoT Agent","item":[{"name":"IoT Agent - Obtain Version Information","id":"712284a0-510a-4297-a6dd-674aee83869b","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"method":"GET","header":[],"url":"http://localhost:4041/iot/about","description":"<p>Once the IoT Agent is running, You can check the status by making an HTTP request to the exposed port. \nIf the response is blank, this is usually because the MongoDB database holding the context information is not running or not connected.</p>\n<p>This is standard functionality coming directly from the IoT Agent Node.js library and does not involve an code changes.</p>\n","urlObject":{"protocol":"http","path":["iot","about"],"host":["localhost:4041"],"query":[],"variable":[]}},"response":[],"_postman_id":"712284a0-510a-4297-a6dd-674aee83869b"},{"name":"IoT Agent - Create a Service Group","id":"0a79c36c-a6ad-46a0-92dc-29e15c1b31ce","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"method":"POST","header":[{"key":"fiware-service","value":"openiot"},{"key":"fiware-servicepath","value":"/"},{"key":"Content-Type","value":"application/json"}],"body":{"mode":"raw","raw":"{\n \"services\": [\n   {\n     \"apikey\":      \"4jggokgpepnvsb2uv4s40d59ov\",\n     \"cbroker\":     \"http://orion:1026\",\n     \"entity_type\": \"Thing\",\n     \"resource\":    \"/iot/xml\"\n   }\n ]\n}"},"url":"http://localhost:4041/iot/services","description":"<p>In the example the IoT Agent is informed that the <code>/iot/xml</code> endpoint will be used and that devices will authenticate\nthemselves by including the token <code>4jggokgpepnvsb2uv4s40d59ov</code>. For the custom XML IoT Agent this means devices will be\nsending POST requests to:</p>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code>http://iot-agent:7896/iot/xml\n</code></pre><p>Where the <code>&lt;measure&gt;</code> holds the relevant device ID and API key.</p>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code>&lt;measure device=\"motion001\" key=\"4jggokgpepnvsb2uv4s40d59ov\"&gt;\n    &lt;c value=\"3\"/&gt;\n&lt;/measure&gt;\n</code></pre><p>This syntax differs from the Ultralight IoT Agent where the device ID and API key are sent as URL parameters.</p>\n<p>The relevant changes can be found in the <code>HTTPBindings.js</code> file where an XML parser is instanciated.</p>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-javascript\">const xmlBodyParser = require(\"express-xml-bodyparser\");\n</code></pre>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-javascript\">httpBindingServer.router.post(\n    config.getConfig().iota.defaultResource || constants.HTTP_MEASURE_PATH,\n    ...xmlBodyParser({ trim: false, explicitArray: false }),\n    checkMandatoryParams(false),\n    ...etc\n);\n</code></pre>\n<p>This means that attributes from the XML request can be accessed using the square-bracket syntax. Since both <code>apiKey</code> and\n<code>deviceId</code> are required parameters, they can be found within the received <code>&lt;measure&gt;</code>.</p>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-javascript\">function checkMandatoryParams(queryPayload) {\n    return function(req, res, next) {\n        var notFoundParams = [],\n            error;\n\n        req.apiKey = req.body[\"measure\"][\"$\"][\"key\"];\n        req.deviceId = req.body[\"measure\"][\"$\"][\"device\"];\n\n        if (!req.apiKey) {\n            notFoundParams.push(\"API Key\");\n        }\n\n        if (!req.deviceId) {\n            notFoundParams.push(\"Device Id\");\n        }\n\n        // CHeck if retrievingParam\n        if (queryPayload &amp;&amp; !req.query.d &amp;&amp; req.query.getCmd !== \"1\") {\n            notFoundParams.push(\"Payload\");\n        }\n\n        if (req.method === \"POST\" &amp;&amp; !req.is(\"application/xml\")) {\n            error = new errors.UnsupportedType(\"application/xml\");\n        }\n\n        if (notFoundParams.length !== 0) {\n            next(new errors.MandatoryParamsNotFound(notFoundParams));\n        } else {\n            next(error);\n        }\n    };\n}\n</code></pre>\n<p>This function also checks that the appropriate MIME type has been received and fails fast if the incoming message does\nnot hold sufficient information.</p>\n","urlObject":{"protocol":"http","path":["iot","services"],"host":["localhost:4041"],"query":[],"variable":[]}},"response":[],"_postman_id":"0a79c36c-a6ad-46a0-92dc-29e15c1b31ce"},{"name":"IoT Agent - Provision a Sensor","id":"544ef54f-f23a-4121-bad3-7194517acee6","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"method":"POST","header":[{"key":"fiware-service","value":"openiot"},{"key":"fiware-servicepath","value":"/"},{"key":"Content-Type","value":"application/json"}],"body":{"mode":"raw","raw":"{\n \"devices\": [\n   {\n     \"device_id\":   \"motion001\",\n     \"entity_name\": \"urn:ngsi-ld:Motion:001\",\n     \"entity_type\": \"Motion\",\n     \"timezone\":    \"Europe/Berlin\",\n     \"attributes\": [\n       { \"object_id\": \"c\", \"name\":\"count\", \"type\":\"Integer\"}\n      ],\n      \"static_attributes\": [\n         {\"name\":\"refStore\", \"type\": \"Relationship\",\"value\": \"urn:ngsi-ld:Store:001\"}\n      ]\n   }\n ]\n}\n"},"url":"http://localhost:4041/iot/devices","description":"<p>As expected the HTTP command to <strong>provision a device</strong> does not change based on the underlying payload or transport\nprotocol since we are using the same HTTP transport as the original Ultralight IoT Agent. <code>internal_atttributes</code> can be\nused to supply additional information for the custom IoT Agent if necessary. In the request we are associating the\ndevice <code>motion001</code> with the URN <code>urn:ngsi-ld:Motion:001</code> and mapping the device reading <code>c</code> with the context attribute\n<code>count</code> (which is defined as an <code>Integer</code>) A <code>refStore</code> is defined as a <code>static_attribute</code>, placing the device within\n<strong>Store</strong> <code>urn:ngsi-ld:Store:001</code></p>\n","urlObject":{"protocol":"http","path":["iot","devices"],"host":["localhost:4041"],"query":[],"variable":[]}},"response":[],"_postman_id":"544ef54f-f23a-4121-bad3-7194517acee6"},{"name":"Dummy Device  - Measurement","id":"64970df3-d315-4b54-b845-63a70eec0acb","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"method":"POST","header":[{"key":"Content-Type","name":"Content-Type","value":"application/xml","type":"text"}],"body":{"mode":"raw","raw":"<measure device=\"motion001\" key=\"4jggokgpepnvsb2uv4s40d59ov\">\n\t<c value=\"3\"/>\n</measure>"},"url":"http://localhost:7896/iot/xml","description":"<p>You can simulate a dummy IoT device measurement coming from the <strong>Motion Sensor</strong> device <code>motion001</code>, by making the\nfollowing XML request</p>\n<p>Both the payload and the <code>Content-Type</code> have been updated. The dummy devices made a similar Ultralight request in the\nprevious tutorials when the door was unlocked, you will have seen the state of each motion sensor changing and a\nNorthbound request will be logged in the device monitor.</p>\n<p>Now the IoT Agent is connected, the service group has defined the resource upon which the IoT Agent is listening\n(<code>iot/xml</code>) and the API key used to authenticate the request (<code>4jggokgpepnvsb2uv4s40d59ov</code>) is found in the body. Since\nboth of these are recognized, the measurement is valid.</p>\n<p>The next step is to parse the payload extract the attributes. This can be found in the amended <code>parse</code> method of the\n<code>xmlparser.js</code> file</p>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-javascript\">function parse(payload) {\n    let result = [];\n    const keys = Object.keys(payload[\"measure\"]);\n    for (let i = 0; i &lt; keys.length; i++) {\n        if (keys[i] !== \"$\") {\n            let obj = {};\n            obj[keys[i]] = payload[\"measure\"][keys[i]][\"$\"].value;\n            result.push(obj);\n        }\n    }\n    return result;\n}\n</code></pre>\n<p><code>parse()</code> returns a JSON array of key-value pairs which can then be mapped from device attribute names (like <code>c</code>) to\nentity attribute names (like <code>count</code>) - obviously the mapping is based on the values sent in the original provisioning.</p>\n","urlObject":{"protocol":"http","port":"7896","path":["iot","xml"],"host":["localhost"],"query":[],"variable":[]}},"response":[],"_postman_id":"64970df3-d315-4b54-b845-63a70eec0acb"},{"name":"Context Broker - Read the Motion Sensor","id":"b3d15216-9ee7-41fc-b538-371336e800be","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"method":"GET","header":[{"key":"fiware-service","value":"openiot"},{"key":"fiware-servicepath","value":"/"}],"url":"http://localhost:1026/v2/entities/urn:ngsd-ld:Motion:001","description":"<p>You can see that a measurement has been recorded, by retrieving the entity data from the context broker. Don't forget to\nadd the <code>fiware-service</code> and <code>fiware-service-path</code> headers.</p>\n<p>The response shows that the device with <code>id=motion001</code> has been successfully identifed by the IoT Agent and mapped to the\nentity <code>id=urn:ngsd-ld:Motion:001</code>. This new entity has been created within the context data. The <code>c</code> \nattribute from the dummy device measurement request has been mapped to the more meaningful <code>count</code> attribute\nwithin the context. As you will notice, a <code>TimeInstant</code> attribute has been added to both the entity and the\nmeta data of the attribute - this represents the last time the entity and attribute have been updated, and is\nautomatically added to each new entity because the <code>IOTA_TIMESTAMP</code>  environment variable was set when the\nIoT Agent was started up. The <code>refStore</code> attribute comes from the <code>static_attributes</code> set when the device was provisioned.</p>\n","urlObject":{"protocol":"http","path":["v2","entities","urn:ngsd-ld:Motion:001"],"host":["localhost:1026"],"query":[],"variable":[]}},"response":[],"_postman_id":"b3d15216-9ee7-41fc-b538-371336e800be"},{"name":"IoT Agent - Provision an Actuator","id":"1e4c5df9-21fd-495a-a78a-04c9180be0f8","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"method":"POST","header":[{"key":"fiware-service","value":"openiot"},{"key":"fiware-servicepath","value":"/"},{"key":"Content-Type","value":"application/json"}],"body":{"mode":"raw","raw":"{\n  \"devices\": [\n    {\n      \"device_id\": \"bell001\",\n      \"entity_name\": \"urn:ngsi-ld:Bell:001\",\n      \"entity_type\": \"Bell\",\n      \"transport\": \"HTTP\",\n      \"endpoint\": \"http://context-provider:3001/iot/bell001\",\n      \"commands\": [ \n        {\n          \"name\": \"ring\",\n          \"type\": \"command\"\n        }\n       ],\n       \"static_attributes\": [\n         {\"name\":\"refStore\", \"type\": \"Relationship\",\"value\": \"urn:ngsi-ld:Store:001\"}\n    \t]\n    }\n  ]\n}\n"},"url":"http://localhost:4041/iot/devices","description":"<p>Provisioning an actuator is similar to provisioning a sensor. This time an <code>endpoint</code> attribute holds the location where\nthe IoT Agent needs to send the JSON command and the <code>commands</code> array includes a list of each command that can be\ninvoked. The example below provisions a bell with the <code>deviceId=bell001</code>. The endpoint is\n<code>http://iot-sensors:3001/iot/bell001</code> and it can accept the <code>ring</code> command. The <code>transport=HTTP</code> attribute defines the\ncommunications protocol to be used.</p>\n<p>As is the case with provisioning a measure, this request remains unchanged. Implicitly within the structure of the IoT\nAgent the provisioning of commands fulfills the following implied contract:</p>\n<ol>\n<li>The custom IoT Agent is making a registration for an attribute</li>\n<li>The custom IoT Agent each request for updating context (on the <code>/v2/op/update</code> endpoint)</li>\n<li>A decision is made how to handle the request - for both the Custom IoT Agent and the Ultralight Agent this follows\nthe paradigm of setting a <code>&lt;command&gt;State</code> attribute, amending and forwarding the request on a <code>/cmd</code> endpoint to\nthe device (or alternatively to a middleware responsible for the device).</li>\n</ol>\n<p>The first two items - listening to context changes from the context broker follow the well-defined NGSI syntax and\ntherefore are common to all IoT Agents. However the third item - what to do to prepare the message for ongoing\nconsumption will vary according to the protocol which is being abstracted out.</p>\n","urlObject":{"protocol":"http","path":["iot","devices"],"host":["localhost:4041"],"query":[],"variable":[]}},"response":[],"_postman_id":"1e4c5df9-21fd-495a-a78a-04c9180be0f8"},{"name":"IoT Agent - Invoke a Command","id":"652cfcde-ac5f-44b3-bc21-0e699c29cb35","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"method":"POST","header":[{"key":"fiware-service","value":"openiot"},{"key":"fiware-servicepath","value":"/"},{"key":"Content-Type","value":"application/json"}],"body":{"mode":"raw","raw":"{\n    \"actionType\": \"update\",\n    \"entities\": [\n        {\n            \"type\": \"Bell\",\n            \"id\": \"urn:ngsi-ld:Bell:001\",\n            \"ring\" : {\n                \"type\": \"command\",\n                \"value\": \"\"\n            }\n        }\n    ]\n}"},"url":"http://localhost:4041/v2/op/update","description":"<p>Before we wire-up the context broker, we can test that a command can be send to a device by making a REST request\ndirectly to the IoT Agent's North Port using the <code>/v2/op/update</code> endpoint. It is this endpoint that will eventually be\ninvoked by the context broker once we have connected it up. To test the configuration you can run the command directly\nas shown</p>\n<p>If you are viewing the device monitor page, you can also see the state of the bell change.</p>\n<p><img src=\"https://fiware.github.io/tutorials.IoT-Agent/img/bell-ring.gif\" alt /></p>\n<p>The result of the command to ring the bell can be read by querying the entity within the Orion Context Broker.</p>\n<p>Within the custom IoT Agent the <code>start()</code> function sets a series of handler functions to fire when a request arrives\nfrom the context broker.</p>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-javascript\">iotAgentLib.setProvisioningHandler(deviceProvisioningHandler);\niotAgentLib.setConfigurationHandler(configurationHandler);\niotAgentLib.setCommandHandler(commandHandler);\niotAgentLib.setDataUpdateHandler(updateHandler);\n</code></pre>\n<p>This is passed to the appropriate transport binding and in this case, the <code>commandHandler()</code> method within\n<code>HTTPBindings.js</code> is fired. It provides an HTTP error handler, but delegates the actual work of creating and sending the\ncommand to the device to <code>generateCommandExecution()</code>.</p>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-javascript\">function generateCommandExecution(apiKey, device, attribute) {\n...\n    const options = {\n        url: device.endpoint,\n        method: 'POST',\n        body: xmlParser.createCommandPayload(device, cmdName, cmdAttributes),\n        headers: {\n            'fiware-service': device.service,\n            'fiware-servicepath': device.subservice\n        }\n    };\n... etc\n</code></pre>\n<p>The payload itself, in other words how a command must be created such that it can be interpreted by the device, is\nunique to our custom XML messaging protocol and is generated in <code>createCommandPayload()</code> method in <code>xmlParser.js</code>.</p>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-javascript\">function createCommandPayload(device, command, attributes) {\n    if (typeof attributes === \"object\") {\n        let payload = \"&lt;\" + command + '  device=\"' + device.id + '\"&gt;';\n\n        Object.keys(attributes).forEach(function(key, value) {\n            payload = payload + \"&lt;\" + key + \"&gt;\" + value + \"&lt;/\" + key + \"&gt;\";\n        });\n        payload = payload + \"&lt;/\" + command + \"&gt;\";\n        return payload;\n    } else {\n        return \"&lt;\" + command + '  device=\"' + device.id + '\"/&gt;';\n    }\n}\n</code></pre>\n<p>This is an amendment from the Ultralight protocol where the <code>@</code> and <code>|</code> symbol is generated for Ultralight devices.</p>\n<p>Howver creating a payload is only half the job, it must be sent to the device and understood, so communications must be\ncompleted using a well-defined communications handshake. So after generating the payload the <code>sendXMLCommandHTTP()</code>\nmethod of <code>HTTPBindings.js</code> sends the message and passes the response to the <code>result()</code> method in <code>xmlParser.js</code> to\ninterprets the command response from the device.</p>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-javascript\">function result(payload) {\n    const xmlToJson = require(\"xml-parser\");\n    const data = xmlToJson(payload);\n    const result = {};\n    result.deviceId = data.root.attributes.device;\n    result.command = data.root.attributes.command;\n    result.result = data.root.name;\n\n    return result;\n}\n</code></pre>\n<p>Finally the success or failure of the command is updated into the context broker using common code from the IoT Agent\nnode library.</p>\n<p>As is typical for IoT Agents, creation of payloads and the handshake of communications has been split into two separate\nconcerns for ease of maintenance. Therefore since in our case only the payload has changed, it is only the XML payload\nside of the code that needs modification to fulfil our custom use case.</p>\n","urlObject":{"protocol":"http","path":["v2","op","update"],"host":["localhost:4041"],"query":[],"variable":[]}},"response":[],"_postman_id":"652cfcde-ac5f-44b3-bc21-0e699c29cb35"},{"name":"Context Broker - Read the Command Result","id":"22ec0981-8167-45a8-a72a-9cd5a9eb0c44","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"method":"GET","header":[{"key":"fiware-service","value":"openiot"},{"key":"fiware-servicepath","value":"/"}],"url":"http://localhost:1026/v2/entities/urn:ngsi-ld:Bell:001?options=keyValues","description":"<p>The result of the command to ring the bell can be read by querying the entity within the Orion Context Broker. </p>\n","urlObject":{"protocol":"http","path":["v2","entities","urn:ngsi-ld:Bell:001"],"host":["localhost:1026"],"query":[{"key":"options","value":"keyValues"}],"variable":[]}},"response":[],"_postman_id":"22ec0981-8167-45a8-a72a-9cd5a9eb0c44"},{"name":"IoT Agent - Provision a Smart Door","id":"664521e3-36d4-4473-b136-9bc85b6abb21","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"method":"POST","header":[{"key":"fiware-service","value":"openiot"},{"key":"fiware-servicepath","value":"/"},{"key":"Content-Type","value":"application/json"}],"body":{"mode":"raw","raw":"{\n  \"devices\": [\n    {\n      \"device_id\": \"door001\",\n      \"entity_name\": \"urn:ngsi-ld:Door:001\",\n      \"entity_type\": \"Door\",\n      \"protocol\": \"PDI-IoTA-UltraLight\",\n      \"transport\": \"HTTP\",\n      \"endpoint\": \"http://context-provider:3001/iot/door001\",\n      \"commands\": [ \n        {\"name\": \"unlock\",\"type\": \"command\"},\n        {\"name\": \"open\",\"type\": \"command\"},\n        {\"name\": \"close\",\"type\": \"command\"},\n        {\"name\": \"lock\",\"type\": \"command\"}\n       ],\n       \"attributes\": [\n       \t{\"object_id\": \"s\", \"name\": \"state\", \"type\":\"Text\"}\n       ],\n       \"static_attributes\": [\n         {\"name\":\"refStore\", \"type\": \"Relationship\",\"value\": \"urn:ngsi-ld:Store:001\"}\n       ]\n    }\n  ]\n}\n"},"url":"http://localhost:4041/iot/devices","description":"<p>Provisioning  a device which offers both commands and measurements is merely a matter of making an HTTP POST request\nwith both <code>attributes</code> and <code>command</code> attributes in the body of the request.</p>\n<p>This example provisions a smart door with four commands <code>lock</code>, <code>unlock</code>, <code>open</code> and <code>close</code> and maps a single attribute.</p>\n","urlObject":{"protocol":"http","path":["iot","devices"],"host":["localhost:4041"],"query":[],"variable":[]}},"response":[],"_postman_id":"664521e3-36d4-4473-b136-9bc85b6abb21"},{"name":"IoT Agent - Provision a Smart Lamp","id":"6add0b88-05ab-493e-9149-b88500b92c37","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"method":"POST","header":[{"key":"fiware-service","value":"openiot"},{"key":"fiware-servicepath","value":"/"},{"key":"Content-Type","value":"application/json"}],"body":{"mode":"raw","raw":"{\n  \"devices\": [\n    {\n      \"device_id\": \"lamp001\",\n      \"entity_name\": \"urn:ngsi-ld:Lamp:001\",\n      \"entity_type\": \"Lamp\",\n      \"protocol\": \"PDI-IoTA-UltraLight\",\n      \"transport\": \"HTTP\",\n      \"endpoint\": \"http://context-provider:3001/iot/lamp001\",\n      \"commands\": [ \n        {\"name\": \"on\",\"type\": \"command\"},\n        {\"name\": \"off\",\"type\": \"command\"}\n       ],\n       \"attributes\": [\n       \t{\"object_id\": \"s\", \"name\": \"state\", \"type\":\"Text\"},\n        {\"object_id\": \"l\", \"name\": \"luminosity\", \"type\":\"Integer\"}\n       ],\n       \"static_attributes\": [\n         {\"name\":\"refStore\", \"type\": \"Relationship\",\"value\": \"urn:ngsi-ld:Store:001\"}\n    \t]\n    }\n  ]\n}\n"},"url":"http://localhost:4041/iot/devices","description":"<p>Provisioning  a device which offers both commands and measurements is merely a matter of making an HTTP POST request\nwith both <code>attributes</code> and <code>command</code> attributes in the body of the request.</p>\n<p>This example provisions a smart door with two commands <code>on</code> and <code>off</code> and maps two attributes.</p>\n","urlObject":{"protocol":"http","path":["iot","devices"],"host":["localhost:4041"],"query":[],"variable":[]}},"response":[],"_postman_id":"6add0b88-05ab-493e-9149-b88500b92c37"},{"name":"IoT Agent - List all Provisioned Devices","id":"99bd9349-99fe-479d-b206-aee5d78f512b","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"method":"GET","header":[{"key":"fiware-service","value":"openiot"},{"key":"fiware-servicepath","value":"/"}],"url":"http://localhost:4041/iot/devices","description":"<p>This example lists all provisioned devices by making a GET request to the <code>/iot/devices</code> endpoint.</p>\n<p>The response includes all the commands and attributes mappings associated with all provisioned IoT devices.</p>\n","urlObject":{"protocol":"http","path":["iot","devices"],"host":["localhost:4041"],"query":[],"variable":[]}},"response":[],"_postman_id":"99bd9349-99fe-479d-b206-aee5d78f512b"}],"id":"e25235b5-dcce-46f6-8ecf-736cde03e560","description":"<p>The following section consists of a series of HTTP commands used to provision the IoT Agent and receive measures and\nsend commands. The relevant amended code within the Custom IoT Agent is discussed as each action is processed.</p>\n<p>To follow the tutorial correctly please ensure you have the device monitor page available in your browser and click on\nthe page to enable audio before you enter any cUrl commands. The device monitor displays the current state of an array\nof dummy devices using XML syntax</p>\n<h4 id=\"device-monitor\">Device Monitor</h4>\n<p>The device monitor can be found at: <code>http://localhost:3000/device/monitor</code></p>\n","event":[{"listen":"prerequest","script":{"id":"661d9977-6eda-41bc-ab3a-5ab127cf21a5","type":"text/javascript","exec":[""]}},{"listen":"test","script":{"id":"1c47421c-2f50-4cee-add5-99cb44744a53","type":"text/javascript","exec":[""]}}],"_postman_id":"e25235b5-dcce-46f6-8ecf-736cde03e560"}],"event":[{"listen":"prerequest","script":{"id":"e64b3c59-ab46-46cf-b504-30a027ca2986","type":"text/javascript","exec":[""]}},{"listen":"test","script":{"id":"04676146-332d-41b3-a65e-b9fd5a527aca","type":"text/javascript","exec":[""]}}],"variable":[{"key":"iot-agent","value":"localhost:4041"},{"key":"orion","value":"localhost:1026"},{"key":"ultralight","value":"localhost:3001"}]}