{"info":{"_postman_id":"33990d83-5855-48cb-b4ed-48fb14fb0a6b","name":"FIWARE Persisting Context Data using NIFI","description":"<html><head></head><body><p>This tutorial is an introduction to <a href=\"http://fiware-draco.readthedocs.io/en/latest/\">FIWARE Draco</a> - a generic enabler which is used to persist context data into third-party databases creating a historical view of the context. The tutorial activates the IoT sensors connected in the <a href=\"https://github.com/Fiware/tutorials.IoT-Agent\">previous tutorial</a> and persists measurements\nfrom those sensors into a database for further analysis.</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.Historic-Context-NIFI/icon/GitHub-Mark-32px.png\" alt=\"GitHub\"> <a href=\"https://github.com/Fiware/tutorials.Historic-Context-NIFI\">FIWARE 302: Persisting Context Data NIFI</a></p>\n<h1 id=\"data-persistence\">Data Persistence</h1>\n<blockquote>\n<p>\"Plots within plots, but all roads lead down the dragon’s gullet.\"</p>\n<p>— George R.R. Martin (A Dance With Dragons)</p>\n</blockquote>\n<p>Previous tutorials have introduced a set of IoT Sensors (providing measurements of the\nstate of the real world), and two FIWARE Components - the <strong>Orion Context Broker</strong> and an <strong>IoT Agent</strong>. \nThis tutorial will introduct a new data persistance component - FIWARE <strong>Draco</strong>.</p>\n<p>The system so far has been built up to handle the current context, in other words it holds the data entities\ndefining the state of the real-world objects at a given moment in time.</p>\n<p>From this definition you can see - context is only interested in the <strong>current</strong> state of the system\nIt is not the responsibility of any of the existing components to report on the historical state of the system,\nthe context is based on the last measurement each sensor has sent data to the context broker.</p>\n<p>In order to do this, we will need to extend the existing architecture to persist changes of state into a database whenever \nthe context is updated.</p>\n<p>Persisting historical context data is useful for big data analysis - it can be used to discover trends, or data \ncan be sampled and aggregated to remove the influence of outlying data measurements. However within each Smart Solution,\nthe significance of each entity type will differ and entities and attributes may need to be sampled at different rates.</p>\n<p>Since the business requirements for using context data differ from application to appliation, there is no one standard use \ncase  for historical data persistence - each situation is unique - it is not the case that one size fits all.\nTherefore rather than overloading the context broker with the job of historical context data persistence, this role has been\nseparated out into a separate, highly configurable component - <strong>Draco</strong>.</p>\n<p>As you would expect, <strong>Draco</strong>, as part of an Open Source platform, is technology agnostic regarding the database \nto be used for data persistance. The database you choose to use will depend upon your own business needs. </p>\n<p>However there is a cost to offering this flexibility - each part of the system must be separately configured and\nnotifications must be set up to only pass the minimal data required as necessary.</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 broker.\nThe state of each device can be seen on the UltraLight device monitor web-page found at: <code>http://localhost:3000/device/monitor</code></p>\n<p><img src=\"https://fiware.github.io/tutorials.Historic-Context-NIFI/img/device-monitor.png\" alt=\"FIWARE Monitor\"></p>\n<h1 id=\"architecture\">Architecture</h1>\n<p>This application builds on the components and dummy IoT devices created in \n<a href=\"https://github.com/Fiware/tutorials.IoT-Agent/\">previous tutorials</a>. It will make use of three FIWARE components - \nthe <a href=\"https://fiware-orion.readthedocs.io/en/latest/\">Orion Context Broker</a>, the\n<a href=\"http://fiware-iotagent-ul.readthedocs.io/en/latest/\">IoT Agent for UltraLight 2.0</a> and introduce the\n<a href=\"http://fiware-draco.readthedocs.io/en/latest/\">Draco Generic Enabler</a> for persisting context data to a database.\nAdditional databases are now involved - both the Orion Context Broker and the IoT Agent rely on <a href=\"https://www.mongodb.com/\">MongoDB</a> technology to keep persistence of the information they hold, and we will be persisting our historical context data another database - either <strong>MySQL</strong> , <strong>PostgreSQL</strong>  or <strong>Mongo-DB</strong> database.</p>\n<p>Therefore the overall architecture will consist of the following elements:</p>\n<ul>\n<li>Three <strong>FIWARE Generic Enablers</strong>:<ul>\n<li>The FIWARE <a href=\"https://fiware-orion.readthedocs.io/en/latest/\">Orion Context Broker</a> which will receive requests using <a href=\"https://fiware.github.io/specifications/OpenAPI/ngsiv2\">NGSI</a></li>\n<li>The FIWARE <a href=\"http://fiware-iotagent-ul.readthedocs.io/en/latest/\">IoT Agent for UltraLight 2.0</a> which will receive southbound requests\nusing <a href=\"https://fiware.github.io/specifications/OpenAPI/ngsiv2\">NGSI</a> and convert them to \n<a href=\"http://fiware-iotagent-ul.readthedocs.io/en/latest/usermanual/index.html#user-programmers-manual\">UltraLight 2.0</a> commands for \nthe devices</li>\n<li>FIWARE <a href=\"http://fiware-draco.readthedocs.io/en/latest/\">Draco</a> which will subscribe to context changes and persist them into a database (<strong>MySQL</strong> , <strong>PostgreSQL</strong>  or <strong>Mongo-DB</strong>)</li>\n</ul>\n</li>\n<li>One, two or three of the following <strong>Databases</strong>:<ul>\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 registrations</li>\n<li>Used by the <strong>IoT Agent</strong> to hold device information such as device URLs and Keys</li>\n<li>Potentially used as a data sink to hold historical context data.</li>\n</ul>\n</li>\n<li>An additional <a href=\"https://www.postgresql.org/\">PostgreSQL</a> database :<ul>\n<li>Potentially used as a data sink to hold historical context data.</li>\n</ul>\n</li>\n<li>An additional <a href=\"https://www.mysql.com/\">MySQL</a> database :<ul>\n<li>Potentially used as a data sink to hold historical context data.</li>\n</ul>\n</li>\n</ul>\n</li>\n<li>Three <strong>Context Providers</strong>:<ul>\n<li>The <strong>Stock Management Frontend</strong> is not used in this tutorial. It does the following:<ul>\n<li>Display store information and allow users to interact with the dummy IoT devices</li>\n<li>Show which products can be bought at each store</li>\n<li>Allow users to \"buy\" products and reduce the stock count.</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 <a href=\"http://fiware-iotagent-ul.readthedocs.io/en/latest/usermanual/index.html#user-programmers-manual\">UltraLight 2.0</a> protocol running over HTTP.</li>\n<li>The <strong>Context Provider NGSI</strong> proxy is not used in this tutorial. It does the following:<ul>\n<li>receive requests using <a href=\"https://fiware.github.io/specifications/OpenAPI/ngsiv2\">NGSI</a></li>\n<li>makes requests to publicly available data sources using their own APIs in a proprietory format </li>\n<li>returns context data back to the Orion Context Broker in <a href=\"https://fiware.github.io/specifications/OpenAPI/ngsiv2\">NGSI</a> format.</li>\n</ul>\n</li>\n</ul>\n</li>\n</ul>\n<p>Since all interactions between the elements are initiated by HTTP requests, the entities can be containerized and run from exposed ports. </p>\n<p>The specific architecture of each section of the tutorial is discussed below.</p>\n<h1 id=\"start-up\">Start Up</h1>\n<p>Before you start you should ensure that you have obtained or built the necessary Docker images locally. Please run</p>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-console\">./services create\n</code></pre>\n<blockquote>\n<p><strong>Note</strong> The <code>context-provider</code> image has not yet been pushed to Docker hub.\nFailing to build the Docker sources before proceeding will result in the following error:</p>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code>Pulling context-provider (fiware/cp-web-app:latest)...\nERROR: The image for the service you're trying to recreate has been removed.\n</code></pre></blockquote>\n<p>Thereafter, all services can be initialised from the command line by running the <a href=\"https://github.com/Fiware/tutorials.Historic-Context/blob/master/services\">services</a> Bash script provided within the repository:</p>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-console\">./services &lt;command&gt;\n</code></pre>\n<p>Where <code>&lt;command&gt;</code> will vary depending upon the databases we wish to activate.\nThis command will also import seed data from the previous tutorials and provision the dummy IoT sensors on startup.</p>\n<blockquote>\n<p>:information_source: <strong>Note:</strong> If you want to clean up and start over again you can do so with the following command:</p>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-console\">./services stop\n</code></pre>\n</blockquote>\n</body></html>","schema":"https://schema.getpostman.com/json/collection/v2.0.0/collection.json","toc":[{"content":"Data Persistence","slug":"data-persistence"},{"content":"Architecture","slug":"architecture"},{"content":"Start Up","slug":"start-up"}],"owner":"513743","collectionId":"33990d83-5855-48cb-b4ed-48fb14fb0a6b","publishedId":"S17qTpnL","public":true,"customColor":{"top-bar":"FFFFFF","right-sidebar":"303030","highlight":"233C68"},"publishDate":"2020-01-02T11:06:41.000Z"},"item":[{"name":"Mongo DB - Persisting Context","item":[{"name":"Draco - Check System Health","id":"30081785-d630-4b5e-86eb-14fec18334e9","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"method":"GET","header":[],"url":"http://localhost:9090/nifi-api/system-diagnostics","description":"<p>Once Draco is running, you can check the status by making an HTTP request to the exposed draco port to <code>/nifi-api/system-diagnostics</code>. If the response is blank, this is usually because Draco is not running or is listening on another port.</p>\n","urlObject":{"protocol":"http","path":["nifi-api","system-diagnostics"],"host":["localhost:9090"],"query":[],"variable":[]}},"response":[],"_postman_id":"30081785-d630-4b5e-86eb-14fec18334e9"},{"name":"Orion - Subscribe to Context Changes","id":"9758eb6b-ce4d-4373-b779-a32e4ecbf1b0","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"method":"POST","header":[{"key":"Content-Type","value":"application/json"},{"key":"fiware-service","value":"openiot"},{"key":"fiware-servicepath","value":"/"}],"body":{"mode":"raw","raw":"{\n  \"description\": \"Notify Draco of all context changes\",\n  \"subject\": {\n    \"entities\": [\n      {\n        \"idPattern\": \".*\"\n      }\n    ]\n  },\n  \"notification\": {\n    \"http\": {\n      \"url\": \"http://draco:5050/v2/notify\"\n    }\n  },\n  \"throttling\": 5\n}"},"url":"http://localhost:1026/v2/subscriptions/","description":"<p>Once a dynamic context system is up and running, we need to inform <strong>Draco</strong> of changes in context.</p>\n<p>This is done by making a POST request to the <code>/v2/subscription</code> endpoint of the Orion Context Broker.</p>\n<ul>\n<li>The <code>fiware-service</code> and <code>fiware-servicepath</code> headers are used to filter the subscription to only listen to measurements from the attached IoT Sensors</li>\n<li>The <code>idPattern</code> in the request body ensures that Draco will be informed of all context data changes.</li>\n<li>The notification <code>url</code> must match the configured <code>NIFI_WEB_HTTP_PORT</code> and the URL the sync is listening on (usually  <code>/v2/notify</code>)</li>\n<li>The <code>throttling</code> value defines the rate that changes are sampled.</li>\n</ul>\n<p>As you can see, the database used to persist context data has no impact on the details of the subscription. It is the same for each database.</p>\n","urlObject":{"protocol":"http","path":["v2","subscriptions",""],"host":["localhost:1026"],"query":[],"variable":[]}},"response":[],"_postman_id":"9758eb6b-ce4d-4373-b779-a32e4ecbf1b0"},{"name":"Orion - Check Subscription is working","id":"fc0179d3-2235-4135-9045-896f6daa0d77","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"method":"GET","header":[{"key":"fiware-service","value":"openiot"},{"key":"fiware-servicepath","value":"/"}],"url":"http://localhost:1026/v2/subscriptions/","description":"<p>If a subscription has been created, you can check to see if it is firing by making a GET \nrequest to the <code>/v2/subscriptions</code> endpoint.</p>\n<p>Within the <code>notification</code> section of the response, you can see several additional <code>attributes</code> which describe the health of the subscription</p>\n<p>If the criteria of the subscription have been met, <code>timesSent</code> should be greater than <code>0</code>.\nA zero value would indicate that the <code>subject</code> of the subscription is incorrect or the subscription \nhas created with the wrong <code>fiware-service-path</code> or <code>fiware-service</code> header</p>\n<p>The <code>lastNotification</code> should be a recent timestamp - if this is not the case, then the devices\nare not regularly sending data. Remember to unlock the <strong>Smart Door</strong> and switch on the <strong>Smart Lamp</strong></p>\n<p>The <code>lastSuccess</code> should match the <code>lastNotification</code> date - if this is not the case \nthen <strong>Draco</strong> is not receiving the subscription properly. Check that the host name\nand port are correct. </p>\n<p>Finally, check that the <code>status</code> of the subscription is <code>active</code> - an expired subscription\nwill not fire.</p>\n","urlObject":{"protocol":"http","path":["v2","subscriptions",""],"host":["localhost:1026"],"query":[],"variable":[]}},"response":[],"_postman_id":"fc0179d3-2235-4135-9045-896f6daa0d77"}],"id":"98593e29-719e-4b24-b8c1-3d00f5321a10","description":"<p>Persisting historic context data using MongoDB technology is relatively simple to configure since we are already using a\nMongoDB instance to hold data related to the Orion Context Broker and the IoT Agent. The MongoDB instance is listening\non the standard <code>27017</code> port and the overall architecture can be seen below:</p>\n<p><img src=\"https://fiware.github.io/tutorials.Historic-Context-NIFI/img/mongo-draco-tutorial.png\" alt /></p>\n<h2 id=\"mongodb---database-server-configuration\">MongoDB - Database Server Configuration</h2>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-yaml\">mongo-db:\n    image: mongo:3.6\n    hostname: mongo-db\n    container_name: db-mongo\n    ports:\n        - \"27017:27017\"\n    networks:\n        - default\n    command: --bind_ip_all --smallfiles\n</code></pre>\n<h2 id=\"mongodb---draco-configuration\">MongoDB - Draco Configuration</h2>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-yaml\">draco:\n    image: ging/fiware-draco:1.1.0\n    container_name: draco\n    depends_on:\n        - mongo-db\n    environment:\n        - NIFI_WEB_HTTP_PORT=9090\n    ports:\n        - \"9090:9090\"\n        - \"5050:5050\"\n    healthcheck:\n        test: curl --fail -s http://localhost:9090/nifi-api/system-diagnostics || exit 1\n</code></pre>\n<p>The <code>draco</code> container is listening on two ports:</p>\n<ul>\n<li>The Subscription Port for Draco - <code>5050</code> is where the service will be listening for notifications from the Orion\ncontext broker</li>\n<li>The Web interface for Draco - <code>9090</code> is exposed purely for configuring the processors</li>\n</ul>\n<h2 id=\"mongodb---start-up\">MongoDB - Start up</h2>\n<p>To start the system with a <strong>MongoDB</strong> database only, run the following command:</p>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-console\">./services mongodb\n</code></pre>\n<p>Then go to your browser and open Draco using this URL <code>http://localhost:9090/nifi</code></p>\n<p>Now go to the Components toolbar which is placed in the upper section of the NiFi GUI, find the template icon and drag\nand drop it inside the Draco user space. At this point, a popup should be displayed with a list of all the templates\navailable. Please select the template MONGO-TUTORIAL.</p>\n<p><img src=\"https://fiware.github.io/tutorials.Historic-Context-NIFI/img/mongo-tutorial-template.png\" alt /></p>\n<p>Select all the processors (press shift and click on every processor) and start them by clicking on the start button.\nNow, you can see that the status icon of each processor turned from red to green.</p>\n<h3 id=\"generating-context-data\">Generating Context Data</h3>\n<p>For the purpose of this tutorial, we must be monitoring a system where the context is periodically being updated.\nThe dummy IoT Sensors can be used to do this. Open the device monitor page at <code>http://localhost:3000/device/monitor</code>\nand unlock a <strong>Smart Door</strong> and switch on a <strong>Smart Lamp</strong>. This can be done by selecting an appropriate the command \nfrom the drop down list and pressing the <code>send</code> button. The stream of measurements coming from the devices can then\nbe seen on the same page:</p>\n<p><img src=\"https://fiware.github.io/tutorials.Historic-Context/img/door-open.gif\" alt /></p>\n","event":[{"listen":"prerequest","script":{"id":"a63e990e-c30a-419c-a54c-5d24baa792f2","type":"text/javascript","exec":[""]}},{"listen":"test","script":{"id":"d2710f35-1e52-4ac5-87ad-d7a93c640386","type":"text/javascript","exec":[""]}}],"_postman_id":"98593e29-719e-4b24-b8c1-3d00f5321a10"},{"name":"Mongo DB - Reading Data","item":[],"id":"00912e55-cddb-4c17-a838-c98db269a4d2","description":"<p>To read mongo-db data from the command line, we will need access to the <code>mongo</code> tool run an interactive instance\nof the <code>mongo</code> image as shown to obtain a command line prompt:</p>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-console\">docker run -it --network fiware_default  --entrypoint /bin/bash mongo\n</code></pre>\n<p>You can then log into to the running <code>mongo-db</code> database by using the command line as shown:</p>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-bash\">mongo --host mongo-db\n</code></pre>\n<h3 id=\"show-available-databases-on-the-mongodb-server\">Show Available Databases on the MongoDB server</h3>\n<p>To show the list of available databases, run the statement as shown:</p>\n<h4 id=\"query\">Query:</h4>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code>show dbs\n</code></pre><h4 id=\"result\">Result:</h4>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code>admin          0.000GB\niotagentul     0.000GB\nlocal          0.000GB\norion          0.000GB\norion-openiot  0.000GB\nsth_openiot    0.000GB\n</code></pre><p>The result include two databases <code>admin</code> and <code>local</code> which are set up by default by <strong>MongoDB</strong>, along with four databases\ncreated by the FIWARE platform. The Orion Context Broker has created two separate database instance for each <code>fiware-service</code></p>\n<ul>\n<li>the Store enitities were created without defining a <code>fiware-service</code> and therefore are held within the <code>orion</code> database,\nwhereas the IoT device entities were created using the <code>openiot</code> <code>fiware-service</code> header and are held separately. The IoT Agent was intialized to hold the IoT sensor data in a separate  <strong>MongoDB</strong> database called <code>iotagentul</code>.</li>\n</ul>\n<p>As a result of the subscription of Draco to Orion Context Broker, a new database has been created called <code>sth_openiot</code>. The default value for a <strong>Mongo DB</strong> database holding historic context consists of the <code>sth_</code> prefix followed by the <code>fiware-service</code> header - therefore <code>sth_openiot</code> holds the historic context of the IoT devices.</p>\n<h3 id=\"read-historical-context-from-the-mongodb-server\">Read Historical Context from the MongoDB server</h3>\n<h4 id=\"query-1\">Query:</h4>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code>use sth_openiot\nshow collections\n</code></pre><h4 id=\"result-1\">Result:</h4>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code>switched to db sth_openiot\n\nsth_/_Door:001_Door\nsth_/_Door:001_Door.aggr\nsth_/_Lamp:001_Lamp\nsth_/_Lamp:001_Lamp.aggr\nsth_/_Motion:001_Motion\nsth_/_Motion:001_Motion.aggr\n</code></pre><p>Looking within the <code>sth_openiot</code> you will see that a series of tables have been created. The names of each table consist\nof the <code>sth_</code> prefix followed by the <code>fiware-servicepath</code> header followed by the entity id. Two table are created for\neach entity - the <code>.aggr</code> table holds some aggregated data which will be accessed in a later tutorial. The raw data\ncan be seen in the tables without the <code>.aggr</code> suffix. </p>\n<p>The historical data can be seen by looking at the data within each table, by default each row will contain the sampled value of a single attribute. </p>\n<h4 id=\"query-2\">Query:</h4>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code>db[\"sth_/_Door:001_Door\"].find().limit(10)\n</code></pre><h4 id=\"result-2\">Result:</h4>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code>{ \"_id\" : ObjectId(\"5b1fa48630c49e0012f7635d\"), \"recvTime\" : ISODate(\"2018-06-12T10:46:30.897Z\"), \"attrName\" : \"TimeInstant\", \"attrType\" : \"ISO8601\", \"attrValue\" : \"2018-06-12T10:46:30.836Z\" }\n{ \"_id\" : ObjectId(\"5b1fa48630c49e0012f7635e\"), \"recvTime\" : ISODate(\"2018-06-12T10:46:30.897Z\"), \"attrName\" : \"close_status\", \"attrType\" : \"commandStatus\", \"attrValue\" : \"UNKNOWN\" }\n{ \"_id\" : ObjectId(\"5b1fa48630c49e0012f7635f\"), \"recvTime\" : ISODate(\"2018-06-12T10:46:30.897Z\"), \"attrName\" : \"lock_status\", \"attrType\" : \"commandStatus\", \"attrValue\" : \"UNKNOWN\" }\n{ \"_id\" : ObjectId(\"5b1fa48630c49e0012f76360\"), \"recvTime\" : ISODate(\"2018-06-12T10:46:30.897Z\"), \"attrName\" : \"open_status\", \"attrType\" : \"commandStatus\", \"attrValue\" : \"UNKNOWN\" }\n{ \"_id\" : ObjectId(\"5b1fa48630c49e0012f76361\"), \"recvTime\" : ISODate(\"2018-06-12T10:46:30.836Z\"), \"attrName\" : \"refStore\", \"attrType\" : \"Relationship\", \"attrValue\" : \"Store:001\" }\n{ \"_id\" : ObjectId(\"5b1fa48630c49e0012f76362\"), \"recvTime\" : ISODate(\"2018-06-12T10:46:30.836Z\"), \"attrName\" : \"state\", \"attrType\" : \"Text\", \"attrValue\" : \"CLOSED\" }\n{ \"_id\" : ObjectId(\"5b1fa48630c49e0012f76363\"), \"recvTime\" : ISODate(\"2018-06-12T10:45:26.368Z\"), \"attrName\" : \"unlock_info\", \"attrType\" : \"commandResult\", \"attrValue\" : \" unlock OK\" }\n{ \"_id\" : ObjectId(\"5b1fa48630c49e0012f76364\"), \"recvTime\" : ISODate(\"2018-06-12T10:45:26.368Z\"), \"attrName\" : \"unlock_status\", \"attrType\" : \"commandStatus\", \"attrValue\" : \"OK\" }\n{ \"_id\" : ObjectId(\"5b1fa4c030c49e0012f76385\"), \"recvTime\" : ISODate(\"2018-06-12T10:47:28.081Z\"), \"attrName\" : \"TimeInstant\", \"attrType\" : \"ISO8601\", \"attrValue\" : \"2018-06-12T10:47:28.038Z\" }\n{ \"_id\" : ObjectId(\"5b1fa4c030c49e0012f76386\"), \"recvTime\" : ISODate(\"2018-06-12T10:47:28.081Z\"), \"attrName\" : \"close_status\", \"attrType\" : \"commandStatus\", \"attrValue\" : \"UNKNOWN\" }\n</code></pre><p>The usual <strong>Mongo-DB</strong> query syntax can be used to filter appropriate fields and values. For example to read the rate at which the <strong>Motion Sensor</strong> with the <code>id=Motion:001_Motion</code> is accumulating, you would make a query as follows:</p>\n<h4 id=\"query-3\">Query:</h4>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code>db[\"sth_/_Motion:001_Motion\"].find({attrName: \"count\"},{_id: 0, attrType: 0, attrName: 0 } ).limit(10)\n</code></pre><h4 id=\"result-3\">Result:</h4>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code>{ \"recvTime\" : ISODate(\"2018-06-12T10:46:18.756Z\"), \"attrValue\" : \"8\" }\n{ \"recvTime\" : ISODate(\"2018-06-12T10:46:36.881Z\"), \"attrValue\" : \"10\" }\n{ \"recvTime\" : ISODate(\"2018-06-12T10:46:42.947Z\"), \"attrValue\" : \"11\" }\n{ \"recvTime\" : ISODate(\"2018-06-12T10:46:54.893Z\"), \"attrValue\" : \"13\" }\n{ \"recvTime\" : ISODate(\"2018-06-12T10:47:00.929Z\"), \"attrValue\" : \"15\" }\n{ \"recvTime\" : ISODate(\"2018-06-12T10:47:06.954Z\"), \"attrValue\" : \"17\" }\n{ \"recvTime\" : ISODate(\"2018-06-12T10:47:15.983Z\"), \"attrValue\" : \"19\" }\n{ \"recvTime\" : ISODate(\"2018-06-12T10:47:49.090Z\"), \"attrValue\" : \"23\" }\n{ \"recvTime\" : ISODate(\"2018-06-12T10:47:58.112Z\"), \"attrValue\" : \"25\" }\n{ \"recvTime\" : ISODate(\"2018-06-12T10:48:28.218Z\"), \"attrValue\" : \"29\" }\n</code></pre><p>To leave the MongoDB client and leave interactive mode, run the following:</p>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-console\">exit\n</code></pre>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-console\">exit\n</code></pre>\n","event":[{"listen":"prerequest","script":{"id":"9e70a754-3893-4617-adc7-c0471f7372f7","type":"text/javascript","exec":[""]}},{"listen":"test","script":{"id":"c43d1162-ac29-4852-9094-b1b86604352b","type":"text/javascript","exec":[""]}}],"_postman_id":"00912e55-cddb-4c17-a838-c98db269a4d2"},{"name":"PostgreSQL - Persisting Context","item":[{"name":"Draco - Check System Health","id":"1b3be990-90d2-4aa6-9574-825c0d1d1a4e","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"method":"GET","header":[],"url":"http://localhost:9090/nifi-api/system-diagnostics","description":"<p>Once Draco is running, you can check the status by making an HTTP request to the exposed draco port to <code>/nifi-api/system-diagnostics</code>. If the response is blank, this is usually because Draco is not running or is listening on another port.</p>\n","urlObject":{"protocol":"http","path":["nifi-api","system-diagnostics"],"host":["localhost:9090"],"query":[],"variable":[]}},"response":[],"_postman_id":"1b3be990-90d2-4aa6-9574-825c0d1d1a4e"},{"name":"Orion - Subscribe to Context Changes","id":"2dc3dc3c-a4c5-4984-a6ac-eaf3f0983f6d","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"method":"POST","header":[{"key":"Content-Type","value":"application/json"},{"key":"fiware-service","value":"openiot"},{"key":"fiware-servicepath","value":"/"}],"body":{"mode":"raw","raw":"{\n  \"description\": \"Notify Draco of all context changes\",\n  \"subject\": {\n    \"entities\": [\n      {\n        \"idPattern\": \".*\"\n      }\n    ]\n  },\n  \"notification\": {\n    \"http\": {\n      \"url\": \"http://draco:5050/v2/notify\"\n    }\n  },\n  \"throttling\": 5\n}"},"url":"http://localhost:1026/v2/subscriptions/","description":"<p>Once a dynamic context system is up and running, we need to inform <strong>Draco</strong> of changes in context.</p>\n<p>This is done by making a POST request to the <code>/v2/subscription</code> endpoint of the Orion Context Broker.</p>\n<ul>\n<li>The <code>fiware-service</code> and <code>fiware-servicepath</code> headers are used to filter the subscription to only listen to measurements from the attached IoT Sensors</li>\n<li>The <code>idPattern</code> in the request body ensures that Draco will be informed of all context data changes.</li>\n<li>The notification <code>url</code> must match the configured <code>NIFI_WEB_HTTP_PORT</code> and the URL the sync is listening on (usually  <code>/v2/notify</code>)</li>\n<li>The <code>throttling</code> value defines the rate that changes are sampled.</li>\n</ul>\n<p>As you can see, the database used to persist context data has no impact on the details of the subscription. It is the same for each database.</p>\n","urlObject":{"protocol":"http","path":["v2","subscriptions",""],"host":["localhost:1026"],"query":[],"variable":[]}},"response":[],"_postman_id":"2dc3dc3c-a4c5-4984-a6ac-eaf3f0983f6d"},{"name":"Orion - Check Subscription is working","id":"9b69e477-aa81-46b4-9d74-a15ecadebada","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"method":"GET","header":[{"key":"fiware-service","value":"openiot"},{"key":"fiware-servicepath","value":"/"}],"url":"http://localhost:1026/v2/subscriptions/","description":"<p>If a subscription has been created, you can check to see if it is firing by making a GET \nrequest to the <code>/v2/subscriptions</code> endpoint.</p>\n<p>Within the <code>notification</code> section of the response, you can see several additional <code>attributes</code> which describe the health of the subscription</p>\n<p>If the criteria of the subscription have been met, <code>timesSent</code> should be greater than <code>0</code>.\nA zero value would indicate that the <code>subject</code> of the subscription is incorrect or the subscription \nhas created with the wrong <code>fiware-service-path</code> or <code>fiware-service</code> header</p>\n<p>The <code>lastNotification</code> should be a recent timestamp - if this is not the case, then the devices\nare not regularly sending data. Remember to unlock the <strong>Smart Door</strong> and switch on the <strong>Smart Lamp</strong></p>\n<p>The <code>lastSuccess</code> should match the <code>lastNotification</code> date - if this is not the case \nthen <strong>Draco</strong> is not receiving the subscription properly. Check that the host name\nand port are correct. </p>\n<p>Finally, check that the <code>status</code> of the subscription is <code>active</code> - an expired subscription\nwill not fire.</p>\n","urlObject":{"protocol":"http","path":["v2","subscriptions",""],"host":["localhost:1026"],"query":[],"variable":[]}},"response":[],"_postman_id":"9b69e477-aa81-46b4-9d74-a15ecadebada"}],"id":"593a71a2-d6c7-4d2c-a726-af03220ce410","description":"<p>To persist historic context data into an alternative database such as <strong>PostgreSQL</strong>, we will need an additional\ncontainer which hosts the PostgreSQL server - the default Docker image for this data can be used. The PostgreSQL\ninstance is listening on the standard <code>5432</code> port and the overall architecture can be seen below:</p>\n<p><img src=\"https://fiware.github.io/tutorials.Historic-Context-NIFI/img/postgres-draco-tutorial.png\" alt /></p>\n<p>We now have a system with two databases, since the MongoDB container is still required to hold data related to the Orion\nContext Broker and the IoT Agent.</p>\n<h2 id=\"postgresql---database-server-configuration\">PostgreSQL - Database Server Configuration</h2>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-yaml\">postgres-db:\n    image: postgres:latest\n    hostname: postgres-db\n    container_name: db-postgres\n    expose:\n        - \"5432\"\n    ports:\n        - \"5432:5432\"\n    networks:\n        - default\n    environment:\n        - \"POSTGRES_PASSWORD=password\"\n        - \"POSTGRES_USER=postgres\"\n        - \"POSTGRES_DB=postgres\"\n</code></pre>\n<p>The <code>postgres-db</code> container is listening on a single port:</p>\n<ul>\n<li>Port <code>5432</code> is the default port for a PostgreSQL server. It has been exposed so you can also run the <code>pgAdmin4</code> tool\nto display database data if you wish</li>\n</ul>\n<p>The <code>postgres-db</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>POSTGRES_PASSWORD</td>\n<td><code>password</code></td>\n<td>Password for the PostgreSQL database user</td>\n</tr>\n<tr>\n<td>POSTGRES_USER</td>\n<td><code>postgres</code></td>\n<td>Username for the PostgreSQL database user</td>\n</tr>\n<tr>\n<td>POSTGRES_DB</td>\n<td><code>postgres</code></td>\n<td>The name of the PostgreSQL database</td>\n</tr>\n</tbody>\n</table>\n</div><blockquote>\n<p>:information_source: <strong>Note:</strong> Passing the Username and Password in plain text environment variables like this is a\nsecurity risk. Whereas this is acceptable practice in a tutorial, for a production environment, you can avoid this\nrisk by applying <a href=\"https://blog.docker.com/2017/02/docker-secrets-management/\">Docker Secrets</a></p>\n</blockquote>\n<h2 id=\"postgresql---draco-configuration\">PostgreSQL - Draco Configuration</h2>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-yaml\">draco:\n    image: ging/fiware-draco:1.1.0\n    container_name: draco\n    depends_on:\n        - postgres-db\n    environment:\n        - NIFI_WEB_HTTP_PORT=9090\n    ports:\n        - \"9090:9090\"\n        - \"5050:5050\"\n    healthcheck:\n        test: curl --fail -s http://localhost:9090/nifi-api/system-diagnostics || exit 1\n</code></pre>\n<p>The <code>draco</code> container is listening on two ports:</p>\n<ul>\n<li>The Subscription Port for Draco - <code>5050</code> is where the service will be listening for notifications from the Orion\ncontext broker</li>\n<li>The Web interface for Draco - <code>9090</code> is exposed purely for configuring the processors.</li>\n</ul>\n<h2 id=\"postgresql---start-up\">PostgreSQL - Start up</h2>\n<p>To start the system with a <strong>PostgreSQL</strong> database run the following command:</p>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-console\">./services postgres\n</code></pre>\n<p>Then go to your browser and open Draco using this URL <code>http://localhost:9090/nifi</code></p>\n<p>Now go to the Components toolbar which is placed in the upper section of the NiFi GUI, find the template icon and drag\nand drop it inside the Draco user space. At this point, a popup should be displayed with a list of all the templates\navailable. Please select the template POSTGRESQL-TUTORIAL.</p>\n<p><img src=\"https://fiware.github.io/tutorials.Historic-Context-NIFI/img/postgres-tutorial-template.png\" alt /></p>\n<p>Before starting the processors, you need to set your PostgreSQL password and enable the DBCConnectionPool controller.\nFor doing that please follow the instructions:</p>\n<ol>\n<li><p>Do right click on any part of the Draco GUI user space, and then click on configure.\n<img src=\"https://fiware.github.io/tutorials.Historic-Context-NIFI/img/step1.png\" alt /></p>\n</li>\n<li><p>Go to the Controller Services Tab, at this point a list of controllers should be displayed, locate the\nDBCConnectionPool controller.</p>\n</li>\n<li><p>Click on the configuration button of the \"DBCPConnectionPool\"\n<img src=\"https://fiware.github.io/tutorials.Historic-Context-NIFI/img/step2.png\" alt /></p>\n</li>\n<li><p>Go to the controller Properties tab and put \"password\" in the password field, then apply the changes.\n<img src=\"https://fiware.github.io/tutorials.Historic-Context-NIFI/img/controller-postgresql.png\" alt /></p>\n</li>\n<li><p>Enable the processor by clicking on the thunder icon and then click on enable, then close the controller\nconfiguration page.</p>\n</li>\n</ol>\n<p><img src=\"https://fiware.github.io/tutorials.Historic-Context-NIFI/img/step4.png\" alt /></p>\n<p><img src=\"https://fiware.github.io/tutorials.Historic-Context-NIFI/img/step5.png\" alt /></p>\n<ol>\n<li>Select all the processors (press shift and click on every processor) and start them by clicking on the start button.\nNow, you can see that the status icon of each processor turned from red to green.</li>\n</ol>\n<h2 id=\"postgresql---start-up-1\">PostgreSQL - Start up</h2>\n<p>To start the system with a <strong>PostgreSQL</strong> database run the following command:</p>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-console\">./services postgres\n</code></pre>\n<h2 id=\"postgresql--database-server-configuration\">PostgreSQL- Database Server Configuration</h2>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-yaml\">postgres-db:\n      image: postgres:latest\n      hostname: postgres-db\n      container_name: db-postgres\n      expose:\n        - \"5432\"\n      ports:\n        - \"5432:5432\"\n      networks:\n        - default\n      environment:\n        - \"POSTGRES_PASSWORD=password\"\n        - \"POSTGRES_USER=postgres\"\n        - \"POSTGRES_DB=postgres\"\n\n</code></pre>\n<p>The <code>postgres-db</code> container is listening on a single port: </p>\n<ul>\n<li>Port <code>5432</code> is the default port for a PostgreSQL server. It has been exposed so you can also run the <code>pgAdmin4</code> tool to display database data if you wish</li>\n</ul>\n<p>The <code>postgres-db</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>POSTGRES_PASSWORD</td>\n<td><code>password</code></td>\n<td>Password for the PostgreSQL database user</td>\n</tr>\n<tr>\n<td>POSTGRES_USER</td>\n<td><code>postgres</code></td>\n<td>Username for the PostgreSQL database user</td>\n</tr>\n<tr>\n<td>POSTGRES_DB</td>\n<td><code>postgres</code></td>\n<td>The name of the PostgreSQL database</td>\n</tr>\n</tbody>\n</table>\n</div><h2 id=\"postgresql---draco-configuration-1\">PostgreSQL - Draco Configuration</h2>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-yaml\">draco:\n    image: fiware/draco-ngsi:latest\n    hostname: draco\n    container_name: fiware-draco\n    networks:\n        - default\n    depends_on:\n        - postgres-db\n    expose:\n        - \"5080\"\n    ports:\n        - \"5050:5050\"\n        - \"5080:5080\"\n    environment:\n        - \"CYGNUS_POSTGRESQL_HOST=postgres-db\"\n        - \"CYGNUS_POSTGRESQL_PORT=5432\"\n        - \"CYGNUS_POSTGRESQL_USER=postgres\" \n        - \"CYGNUS_POSTGRESQL_PASS=password\" \n        - \"CYGNUS_LOG_LEVEL=DEBUG\"\n        - \"CYGNUS_SERVICE_PORT=5050\"\n        - \"CYGNUS_API_PORT=5080\"\n        - \"CYGNUS_POSTGRESQL_ENABLE_CACHE=true\"\n</code></pre>\n<p>The <code>draco</code> container is listening on two ports: </p>\n<ul>\n<li>The service will be listening on port <code>5050</code> for notifications from the Orion context broker</li>\n<li>Port <code>5080</code> is exposed purely for tutorial access - so that cUrl or Postman can make provisioning commands\nwithout being part of the same network.</li>\n</ul>\n<h3 id=\"generating-context-data\">Generating Context Data</h3>\n<p>For the purpose of this tutorial, we must be monitoring a system where the context is periodically being updated.\nThe dummy IoT Sensors can be used to do this. Open the device monitor page at <code>http://localhost:3000/device/monitor</code>\nand unlock a <strong>Smart Door</strong> and switch on a <strong>Smart Lamp</strong>. This can be done by selecting an appropriate the command \nfrom the drop down list and pressing the <code>send</code> button. The stream of measurements coming from the devices can then\nbe seen on the same page:</p>\n<p><img src=\"https://fiware.github.io/tutorials.Historic-Context/img/door-open.gif\" alt /></p>\n","event":[{"listen":"prerequest","script":{"id":"f8ca5d71-4eb4-42b9-95e0-c3ef11567514","type":"text/javascript","exec":[""]}},{"listen":"test","script":{"id":"43d701ab-eb41-472e-875e-4ac51e27579a","type":"text/javascript","exec":[""]}}],"_postman_id":"593a71a2-d6c7-4d2c-a726-af03220ce410"},{"name":"PostgreSQL - Reading Data","item":[],"id":"19c465ee-0a57-461b-8e27-8994a18df89a","description":"<p>To read PostgreSQL data from the command line, we will need access to the <code>postgres</code> client, to do this, run an\ninteractive instance of the <code>postgresql-client</code> image supplying the connection string as shown to obtain a command \nline prompt:</p>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-console\">docker run -it --rm  --network fiware_default jbergknoff/postgresql-client \\\n   postgresql://postgres:password@postgres-db:5432/postgres\n</code></pre>\n<h3 id=\"show-available-databases-on-the-postgresql-server\">Show Available Databases on the PostgreSQL server</h3>\n<p>To show the list of available databases, run the statement as shown:</p>\n<h4 id=\"query\">Query:</h4>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code>\\list\n</code></pre><h4 id=\"result\">Result:</h4>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code>   Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges   \n-----------+----------+----------+------------+------------+-----------------------\n postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 | \n template0 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +\n           |          |          |            |            | postgres=CTc/postgres\n template1 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +\n           |          |          |            |            | postgres=CTc/postgres\n(3 rows)\n</code></pre><p>The result include two template databases <code>template0</code> and <code>template1</code> as well as the <code>postgres</code> database setup when the docker container was started.</p>\n<p>To show the list of available schemas, run the statement as shown:</p>\n<h4 id=\"query-1\">Query:</h4>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code>\\dn\n</code></pre><h4 id=\"result-1\">Result:</h4>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code>  List of schemas\n  Name   |  Owner   \n---------+----------\n openiot | postgres\n public  | postgres\n(2 rows)\n</code></pre><p>As a result of the subscription of Draco to Orion Context Broker, a new schema has been created called <code>openiot</code>. \nThe name of the schema matches the <code>fiware-service</code> header - therefore <code>openiot</code> holds the historic context of the\nIoT devices.</p>\n<h3 id=\"read-historical-context-from-the-postgresql-server\">Read Historical Context from the PostgreSQL server</h3>\n<p>Once running a docker container within the network, it is possible to obtain information about the running\ndatabase.</p>\n<h4 id=\"query-2\">Query:</h4>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-sql\">SELECT table_schema,table_name\nFROM information_schema.tables\nWHERE table_schema ='openiot'\nORDER BY table_schema,table_name;\n</code></pre>\n<h4 id=\"result-2\">Result:</h4>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code> table_schema |    table_name     \n--------------+-------------------\n openiot      | door_001_door\n openiot      | lamp_001_lamp\n openiot      | motion_001_motion\n(3 rows)\n</code></pre><p>The <code>table_schema</code> matches the <code>fiware-service</code> header supplied with the context data:</p>\n<p>To read the data within a table, run a select statement as shown:</p>\n<h4 id=\"query-3\">Query:</h4>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-sql\">SELECT * FROM openiot.motion_001_motion limit 10;\n</code></pre>\n<h4 id=\"result-3\">Result:</h4>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code>  recvtimets   |         recvtime         | fiwareservicepath |  entityid  | entitytype |  attrname   |   attrtype   |        attrvalue         |                                    attrmd                                    \n---------------+--------------------------+-------------------+------------+------------+-------------+--------------+--------------------------+------------------------------------------------------------------------------\n 1528803005491 | 2018-06-12T11:30:05.491Z | /                 | Motion:001 | Motion     | TimeInstant | ISO8601      | 2018-06-12T11:30:05.423Z | []\n 1528803005491 | 2018-06-12T11:30:05.491Z | /                 | Motion:001 | Motion     | count       | Integer      | 7                        | [{\"name\":\"TimeInstant\",\"type\":\"ISO8601\",\"value\":\"2018-06-12T11:30:05.423Z\"}]\n 1528803005491 | 2018-06-12T11:30:05.491Z | /                 | Motion:001 | Motion     | refStore    | Relationship | Store:001                | [{\"name\":\"TimeInstant\",\"type\":\"ISO8601\",\"value\":\"2018-06-12T11:30:05.423Z\"}]\n 1528803035501 | 2018-06-12T11:30:35.501Z | /                 | Motion:001 | Motion     | TimeInstant | ISO8601      | 2018-06-12T11:30:35.480Z | []\n 1528803035501 | 2018-06-12T11:30:35.501Z | /                 | Motion:001 | Motion     | count       | Integer      | 10                       | [{\"name\":\"TimeInstant\",\"type\":\"ISO8601\",\"value\":\"2018-06-12T11:30:35.480Z\"}]\n 1528803035501 | 2018-06-12T11:30:35.501Z | /                 | Motion:001 | Motion     | refStore    | Relationship | Store:001                | [{\"name\":\"TimeInstant\",\"type\":\"ISO8601\",\"value\":\"2018-06-12T11:30:35.480Z\"}]\n 1528803041563 | 2018-06-12T11:30:41.563Z | /                 | Motion:001 | Motion     | TimeInstant | ISO8601      | 2018-06-12T11:30:41.520Z | []\n 1528803041563 | 2018-06-12T11:30:41.563Z | /                 | Motion:001 | Motion     | count       | Integer      | 12                       | [{\"name\":\"TimeInstant\",\"type\":\"ISO8601\",\"value\":\"2018-06-12T11:30:41.520Z\"}]\n 1528803041563 | 2018-06-12T11:30:41.563Z | /                 | Motion:001 | Motion     | refStore    | Relationship | Store:001                | [{\"name\":\"TimeInstant\",\"type\":\"ISO8601\",\"value\":\"2018-06-12T11:30:41.520Z\"}]\n 1528803047545 | 2018-06-12T11:30:47.545Z | /     \n</code></pre><p>The usual <strong>PostgreSQL</strong> query syntax can be used to filter appropriate fields and values. For example to read the rate at which the <strong>Motion Sensor</strong> with the <code>id=Motion:001_Motion</code> is accumulating, you would make a query as follows:</p>\n<h4 id=\"query-4\">Query:</h4>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-sql\">SELECT recvtime, attrvalue FROM openiot.motion_001_motion WHERE attrname ='count'  limit 10;\n</code></pre>\n<h4 id=\"result-4\">Result:</h4>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code>         recvtime         | attrvalue \n--------------------------+-----------\n 2018-06-12T11:30:05.491Z | 7\n 2018-06-12T11:30:35.501Z | 10\n 2018-06-12T11:30:41.563Z | 12\n 2018-06-12T11:30:47.545Z | 13\n 2018-06-12T11:31:02.617Z | 15\n 2018-06-12T11:31:32.718Z | 20\n 2018-06-12T11:31:38.733Z | 22\n 2018-06-12T11:31:50.780Z | 24\n 2018-06-12T11:31:56.825Z | 25\n 2018-06-12T11:31:59.790Z | 26\n(10 rows)\n</code></pre><p>To leave the Postgres client and leave interactive mode, run the following:</p>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-console\">\\q\n</code></pre>\n<p> You will then return to the commmand line.</p>\n","event":[{"listen":"prerequest","script":{"id":"9547319e-f5df-4a3d-9a69-86221f7aeb2f","type":"text/javascript","exec":[""]}},{"listen":"test","script":{"id":"eaf3c847-e1eb-48e2-aadd-35caf0463942","type":"text/javascript","exec":[""]}}],"_postman_id":"19c465ee-0a57-461b-8e27-8994a18df89a"},{"name":"MySQL - Persisting Context","item":[{"name":"Draco - Check System Health","id":"468b00a7-1a20-4e8f-b97e-b4e334fc649a","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"method":"GET","header":[],"url":"http://localhost:9090/nifi-api/system-diagnostics","description":"<p>Once Draco is running, you can check the status by making an HTTP request to the exposed draco port to <code>/nifi-api/system-diagnostics</code>. If the response is blank, this is usually because Draco is not running or is listening on another port.</p>\n","urlObject":{"protocol":"http","path":["nifi-api","system-diagnostics"],"host":["localhost:9090"],"query":[],"variable":[]}},"response":[],"_postman_id":"468b00a7-1a20-4e8f-b97e-b4e334fc649a"},{"name":"Orion - Subscribe to Context Changes","id":"930c4b82-aec7-4aa4-b74c-b7d407b688bb","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"method":"POST","header":[{"key":"Content-Type","value":"application/json"},{"key":"fiware-service","value":"openiot"},{"key":"fiware-servicepath","value":"/"}],"body":{"mode":"raw","raw":"{\n  \"description\": \"Notify Draco of all context changes\",\n  \"subject\": {\n    \"entities\": [\n      {\n        \"idPattern\": \".*\"\n      }\n    ]\n  },\n  \"notification\": {\n    \"http\": {\n      \"url\": \"http://draco:5050/v2/notify\"\n    }\n  },\n  \"throttling\": 5\n}"},"url":"http://localhost:1026/v2/subscriptions/","description":"<p>Once a dynamic context system is up and running, we need to inform <strong>Draco</strong> of changes in context.</p>\n<p>This is done by making a POST request to the <code>/v2/subscription</code> endpoint of the Orion Context Broker.</p>\n<ul>\n<li>The <code>fiware-service</code> and <code>fiware-servicepath</code> headers are used to filter the subscription to only listen to measurements from the attached IoT Sensors</li>\n<li>The <code>idPattern</code> in the request body ensures that Draco will be informed of all context data changes.</li>\n<li>The notification <code>url</code> must match the configured <code>NIFI_WEB_HTTP_PORT</code> and the URL the sync is listening on (usually  <code>/v2/notify</code>)</li>\n<li>The <code>throttling</code> value defines the rate that changes are sampled.</li>\n</ul>\n<p>As you can see, the database used to persist context data has no impact on the details of the subscription. It is the same for each database.</p>\n","urlObject":{"protocol":"http","path":["v2","subscriptions",""],"host":["localhost:1026"],"query":[],"variable":[]}},"response":[],"_postman_id":"930c4b82-aec7-4aa4-b74c-b7d407b688bb"},{"name":"Orion - Check Subscription is working","id":"f44efe83-c13c-4608-ad5c-9d650245e02a","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"method":"GET","header":[{"key":"fiware-service","value":"openiot"},{"key":"fiware-servicepath","value":"/"}],"url":"http://localhost:1026/v2/subscriptions/","description":"<p>If a subscription has been created, you can check to see if it is firing by making a GET \nrequest to the <code>/v2/subscriptions</code> endpoint.</p>\n<p>Within the <code>notification</code> section of the response, you can see several additional <code>attributes</code> which describe the health of the subscription</p>\n<p>If the criteria of the subscription have been met, <code>timesSent</code> should be greater than <code>0</code>.\nA zero value would indicate that the <code>subject</code> of the subscription is incorrect or the subscription \nhas created with the wrong <code>fiware-service-path</code> or <code>fiware-service</code> header</p>\n<p>The <code>lastNotification</code> should be a recent timestamp - if this is not the case, then the devices\nare not regularly sending data. Remember to unlock the <strong>Smart Door</strong> and switch on the <strong>Smart Lamp</strong></p>\n<p>The <code>lastSuccess</code> should match the <code>lastNotification</code> date - if this is not the case \nthen <strong>Draco</strong> is not receiving the subscription properly. Check that the host name\nand port are correct. </p>\n<p>Finally, check that the <code>status</code> of the subscription is <code>active</code> - an expired subscription\nwill not fire.</p>\n","urlObject":{"protocol":"http","path":["v2","subscriptions",""],"host":["localhost:1026"],"query":[],"variable":[]}},"response":[],"_postman_id":"f44efe83-c13c-4608-ad5c-9d650245e02a"}],"id":"3fb4fac0-3643-473a-bc1b-e6455cd10d90","description":"<p>Similarly, to persisting historic context data into <strong>MySQL</strong>, we will again need an additional container which hosts\nthe MySQL server, once again the default Docker image for this data can be used. The MySQL instance is listening on the\nstandard <code>3306</code> port and the overall architecture can be seen below:</p>\n<p><img src=\"https://fiware.github.io/tutorials.Historic-Context-NIFI/img/mysql-draco-tutorial.png\" alt /></p>\n<p>Once again we have a system with two databases, since the MongoDB container is still required to hold data related to\nthe Orion Context Broker and the IoT Agent.</p>\n<h2 id=\"mysql---database-server-configuration\">MySQL - Database Server Configuration</h2>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-yaml\">mysql-db:\n    restart: always\n    image: mysql:5.7\n    hostname: mysql-db\n    container_name: db-mysql\n    expose:\n        - \"3306\"\n    ports:\n        - \"3306:3306\"\n    networks:\n        - default\n    environment:\n        - \"MYSQL_ROOT_PASSWORD=123\"\n        - \"MYSQL_ROOT_HOST=%\"\n</code></pre>\n<blockquote>\n<p>:information_source: <strong>Note:</strong> Using the default <code>root</code> user and displaying the password in an environment variables\nlike this is a security risk. Whereas this is acceptable practice in a tutorial, for a production environment, you can\navoid this risk by setting up another user and applying\n<a href=\"https://blog.docker.com/2017/02/docker-secrets-management/\">Docker Secrets</a></p>\n</blockquote>\n<p>The <code>mysql-db</code> container is listening on a single port:</p>\n<ul>\n<li>Port <code>3306</code> is the default port for a MySQL server. It has been exposed so you can also run other database tools to\ndisplay data if you wish</li>\n</ul>\n<p>The <code>mysql-db</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>MYSQL_ROOT_PASSWORD</td>\n<td><code>123</code>.</td>\n<td>specifies a password that is set for the MySQL <code>root</code> account.</td>\n</tr>\n<tr>\n<td>MYSQL_ROOT_HOST</td>\n<td><code>postgres</code></td>\n<td>By default, MySQL creates the <code>root'@'localhost</code> account. This account can only be connected to from inside the container. Setting this environment variable allows root connections from other hosts</td>\n</tr>\n</tbody>\n</table>\n</div><h2 id=\"mysql---draco-configuration\">MySQL - Draco Configuration</h2>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-yaml\">draco:\n    image: ging/fiware-draco:1.1.0\n    container_name: draco\n    depends_on:\n        - mysql-db\n    environment:\n        - NIFI_WEB_HTTP_PORT=9090\n    ports:\n        - \"9090:9090\"\n        - \"5050:5050\"\n    healthcheck:\n        test: curl --fail -s http://localhost:9090/nifi-api/system-diagnostics || exit 1\n</code></pre>\n<p>The <code>draco</code> container is listening on two ports:</p>\n<ul>\n<li>The Subscription Port for Draco - <code>5050</code> is where the service will be listening for notifications from the Orion\ncontext broker</li>\n<li>The Web interface for Draco - <code>9090</code> is exposed purely for configuring the processors</li>\n</ul>\n<h2 id=\"mysql---start-up\">MySQL - Start up</h2>\n<p>To start the system with a <strong>MySQL</strong> database run the following command:</p>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-console\">./services mysql\n</code></pre>\n<p>Then go to your browser and open Draco using this URL <code>http://localhost:9090/nifi</code></p>\n<p>Now go to the Components toolbar which is placed in the upper section of the NiFi GUI, find the template icon and drag\nand drop it inside the Draco user space. At this point, a popup should be displayed with a list of all the templates\navailable. Please select the template MYSQL-TUTORIAL.</p>\n<p><img src=\"https://fiware.github.io/tutorials.Historic-Context-NIFI/img/draco-template1.png\" alt /></p>\n<p>Before starting the processors, you need to set your MySQL password and enable the DBCConnectionPool controller. For\ndoing that please follow the instructions:</p>\n<ol>\n<li><p>Do right click on any part of the Draco GUI user space, and then click on configure.\n<img src=\"https://fiware.github.io/tutorials.Historic-Context-NIFI/img/step1.png\" alt /></p>\n</li>\n<li><p>Go to the Controller Services Tab, at this point a list of controllers should be displayed, locate the\nDBCConnectionPool controller.</p>\n</li>\n<li><p>Click on the configuration button of the \"DBCPConnectionPool\"\n<img src=\"https://fiware.github.io/tutorials.Historic-Context-NIFI/img/step2.png\" alt /></p>\n</li>\n<li><p>Go to the controller Properties tab and put \"123\" in the password field, then apply the changes.\n<img src=\"https://fiware.github.io/tutorials.Historic-Context-NIFI/img/step3.png\" alt /></p>\n</li>\n<li><p>Enable the processor by clicking on the thunder icon and then click on enable, then close the controller\nconfiguration page. <img src=\"https://fiware.github.io/tutorials.Historic-Context-NIFI/img/step4.png\" alt /></p>\n</li>\n</ol>\n<p><img src=\"https://fiware.github.io/tutorials.Historic-Context-NIFI/img/step5.png\" alt /></p>\n<ol>\n<li>Select all the processors (press shift and click on every processor) and start them by clicking on the start button.\nNow, you can see that the status icon of each processor turned from red to green.</li>\n</ol>\n<h3 id=\"generating-context-data\">Generating Context Data</h3>\n<p>For the purpose of this tutorial, we must be monitoring a system where the context is periodically being updated.\nThe dummy IoT Sensors can be used to do this. Open the device monitor page at <code>http://localhost:3000/device/monitor</code>\nand unlock a <strong>Smart Door</strong> and switch on a <strong>Smart Lamp</strong>. This can be done by selecting an appropriate the command \nfrom the drop down list and pressing the <code>send</code> button. The stream of measurements coming from the devices can then\nbe seen on the same page:</p>\n<p><img src=\"https://fiware.github.io/tutorials.Historic-Context/img/door-open.gif\" alt /></p>\n","event":[{"listen":"prerequest","script":{"id":"1c3c984c-886c-464c-b936-a5b355263c89","type":"text/javascript","exec":[""]}},{"listen":"test","script":{"id":"cafefa9a-6026-45d2-8877-2298a955ba2d","type":"text/javascript","exec":[""]}}],"_postman_id":"3fb4fac0-3643-473a-bc1b-e6455cd10d90"},{"name":"MySQL - Reading Data","item":[],"id":"023cc10d-0fa3-46c1-b2bc-bad848e141fe","description":"<p>To read MySQL data from the command line, we will need access to the <code>mysql</code> client, to do this, run an\ninteractive instance of the <code>mysql</code> image supplying the connection string as shown to obtain a command \nline prompt:</p>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-console\">docker exec -it  db-mysql mysql -h mysql-db -P 3306  -u root -p123\n</code></pre>\n<h3 id=\"show-available-databases-on-the-mysql-server\">Show Available Databases on the MySQL server</h3>\n<p>To show the list of available databases, run the statement as shown:</p>\n<h4 id=\"query\">Query:</h4>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-sql\">SHOW DATABASES;\n</code></pre>\n<h4 id=\"result\">Result:</h4>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code>+--------------------+\n| Database           |\n+--------------------+\n| information_schema |\n| mysql              |\n| openiot            |\n| performance_schema |\n| sys                |\n+--------------------+\n5 rows in set (0.00 sec)\n</code></pre><p>To show the list of available schemas, run the statement as shown:</p>\n<h4 id=\"query-1\">Query:</h4>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-sql\">SHOW SCHEMAS;\n</code></pre>\n<h4 id=\"result-1\">Result:</h4>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code>+--------------------+\n| Database           |\n+--------------------+\n| information_schema |\n| mysql              |\n| openiot            |\n| performance_schema |\n| sys                |\n+--------------------+\n5 rows in set (0.00 sec)\n</code></pre><p>As a result of the subscription of Draco to Orion Context Broker, a new schema has been created called <code>openiot</code>. \nThe name of the schema matches the <code>fiware-service</code> header - therefore <code>openiot</code> holds the historic context of the\nIoT devices.</p>\n<h3 id=\"read-historical-context-from-the-postgresql-server\">Read Historical Context from the PostgreSQL server</h3>\n<p>Once running a docker container within the network, it is possible to obtain information about the running\ndatabase.</p>\n<h4 id=\"query-2\">Query:</h4>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-sql\">SHOW tables FROM openiot;\n</code></pre>\n<h4 id=\"result-2\">Result:</h4>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code> table_schema |    table_name     \n--------------+-------------------\n openiot      | door_001_door\n openiot      | lamp_001_lamp\n openiot      | motion_001_motion\n(3 rows)\n</code></pre><p>The <code>table_schema</code> matches the <code>fiware-service</code> header supplied with the context data:</p>\n<p>To read the data within a table, run a select statement as shown:</p>\n<h4 id=\"query-3\">Query:</h4>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-sql\">SELECT * FROM openiot.Motion_001_Motion limit 10;\n</code></pre>\n<h4 id=\"result-3\">Result:</h4>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code>+---------------+-------------------------+-------------------+------------+------------+-------------+--------------+--------------------------+------------------------------------------------------------------------------+\n| recvTimeTs    | recvTime                | fiwareServicePath | entityId   | entityType | attrName    | attrType     | attrValue                | attrMd                                                                       |\n+---------------+-------------------------+-------------------+------------+------------+-------------+--------------+--------------------------+------------------------------------------------------------------------------+\n| 1528804397955 | 2018-06-12T11:53:17.955 | /                 | Motion:001 | Motion     | TimeInstant | ISO8601      | 2018-06-12T11:53:17.923Z | []                                                                           |\n| 1528804397955 | 2018-06-12T11:53:17.955 | /                 | Motion:001 | Motion     | count       | Integer      | 3                        | [{\"name\":\"TimeInstant\",\"type\":\"ISO8601\",\"value\":\"2018-06-12T11:53:17.923Z\"}] |\n| 1528804397955 | 2018-06-12T11:53:17.955 | /                 | Motion:001 | Motion     | refStore    | Relationship | Store:001                | [{\"name\":\"TimeInstant\",\"type\":\"ISO8601\",\"value\":\"2018-06-12T11:53:17.923Z\"}] |\n| 1528804403954 | 2018-06-12T11:53:23.954 | /                 | Motion:001 | Motion     | TimeInstant | ISO8601      | 2018-06-12T11:53:23.928Z | []                                                                           |\n| 1528804403954 | 2018-06-12T11:53:23.954 | /                 | Motion:001 | Motion     | count       | Integer      | 5                        | [{\"name\":\"TimeInstant\",\"type\":\"ISO8601\",\"value\":\"2018-06-12T11:53:23.928Z\"}] |\n| 1528804403954 | 2018-06-12T11:53:23.954 | /                 | Motion:001 | Motion     | refStore    | Relationship | Store:001                | [{\"name\":\"TimeInstant\",\"type\":\"ISO8601\",\"value\":\"2018-06-12T11:53:23.928Z\"}] |\n| 1528804409970 | 2018-06-12T11:53:29.970 | /                 | Motion:001 | Motion     | TimeInstant | ISO8601      | 2018-06-12T11:53:29.948Z | []                                                                           |\n| 1528804409970 | 2018-06-12T11:53:29.970 | /                 | Motion:001 | Motion     | count       | Integer      | 7                        | [{\"name\":\"TimeInstant\",\"type\":\"ISO8601\",\"value\":\"2018-06-12T11:53:29.948Z\"}] |\n| 1528804409970 | 2018-06-12T11:53:29.970 | /                 | Motion:001 | Motion     | refStore    | Relationship | Store:001                | [{\"name\":\"TimeInstant\",\"type\":\"ISO8601\",\"value\":\"2018-06-12T11:53:29.948Z\"}] |\n| 1528804446083 | 2018-06-12T11:54:06.83  | /                 | Motion:001 | Motion     | TimeInstant | ISO8601      | 2018-06-12T11:54:06.062Z | []                                                                           |\n+---------------+-------------------------+-------------------+------------+------------+-------------+--------------+--------------------------+------------------------------------------------------------------------------+    \n</code></pre><p>The usual <strong>MySQL</strong> query syntax can be used to filter appropriate fields and values. For example to read the rate at which the <strong>Motion Sensor</strong> with the <code>id=Motion:001_Motion</code> is accumulating, you would make a query as follows:</p>\n<h4 id=\"query-4\">Query:</h4>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-sql\">SELECT recvtime, attrvalue FROM openiot.Motion_001_Motion WHERE attrname ='count' LIMIT 10;\n</code></pre>\n<h4 id=\"result-4\">Result:</h4>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code>+-------------------------+-----------+\n| recvtime                | attrvalue |\n+-------------------------+-----------+\n| 2018-06-12T11:53:17.955 | 3         |\n| 2018-06-12T11:53:23.954 | 5         |\n| 2018-06-12T11:53:29.970 | 7         |\n| 2018-06-12T11:54:06.83  | 12        |\n| 2018-06-12T11:54:12.132 | 13        |\n| 2018-06-12T11:54:24.177 | 14        |\n| 2018-06-12T11:54:36.196 | 16        |\n| 2018-06-12T11:54:42.195 | 18        |\n| 2018-06-12T11:55:24.300 | 23        |\n| 2018-06-12T11:55:30.350 | 25        |\n+-------------------------+-----------+\n10 rows in set (0.00 sec)\n</code></pre><p>To leave the MySQL client and leave interactive mode, run the following:</p>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-console\">\\q\n</code></pre>\n<p> You will then return to the commmand line.</p>\n","event":[{"listen":"prerequest","script":{"id":"0a433918-9a68-4cbe-9bdc-031270322304","type":"text/javascript","exec":[""]}},{"listen":"test","script":{"id":"db11ec7d-1bde-4317-8318-db7b0ebf5c61","type":"text/javascript","exec":[""]}}],"_postman_id":"023cc10d-0fa3-46c1-b2bc-bad848e141fe"},{"name":"Multi-Agent Persisting to Multiple Databases","item":[{"name":"Draco - Obtain version information","id":"975983b4-c63c-4c0e-8d67-2e42dbd0acd8","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"method":"GET","header":[],"url":"http://localhost:9090/v1/version","description":"<p>Once Draco is running, You can check the status by making an HTTP request to the exposed <code>CYGNUS_API_PORT</code> port. \nIf the response is blank, this is usually because Draco is not running or is listening on another port.</p>\n<blockquote>\n<p><strong>Troubleshooting:</strong> What if the response is blank ?</p>\n<ul>\n<li>To check that a docker container is running try</li>\n</ul>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-bash\">docker ps\n</code></pre>\n<p>You should see several containers running. If <code>draco</code> is not running, you can restart the containers as necessary.</p>\n</blockquote>\n","urlObject":{"protocol":"http","path":["v1","version"],"host":["localhost:9090"],"query":[],"variable":[]}},"response":[],"_postman_id":"975983b4-c63c-4c0e-8d67-2e42dbd0acd8"},{"name":"Orion - Subscribe to Context Changes","id":"949903c8-7764-485c-a825-af1cd48cc605","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"method":"POST","header":[{"key":"Content-Type","value":"application/json"},{"key":"fiware-service","value":"openiot"},{"key":"fiware-servicepath","value":"/"}],"body":{"mode":"raw","raw":"{\n  \"description\": \"Notify Draco of all context changes for MySQL on port 5050\",\n  \"subject\": {\n    \"entities\": [\n      {\n        \"idPattern\": \".*\"\n      }\n    ]\n  },\n  \"notification\": {\n    \"http\": {\n      \"url\": \"http://draco:5050/notify\"\n    },\n    \"attrsFormat\": \"legacy\"\n  },\n  \"throttling\": 5\n}"},"url":"http://localhost:1026/v2/subscriptions/","description":"<p>Once a dynamic context system is up and running, we need to inform <strong>Draco</strong> of changes in context.</p>\n<p>This is done by making a POST request to the <code>/v2/subscription</code> endpoint of the Orion Context Broker.</p>\n<ul>\n<li>The <code>fiware-service</code> and <code>fiware-servicepath</code> headers are used to filter the subscription to only listen to measurements from the attached IoT Sensors</li>\n<li>The <code>idPattern</code> in the request body ensures that Draco will be informed of all context data changes.</li>\n<li>The <code>attrsFormat=legacy</code> is required since Draco currently only accepts notifications in the older NGSI v1 format.</li>\n<li>The <code>throttling</code> value defines the rate that changes are sampled.</li>\n</ul>\n<p>When running in <strong>multi-agent</strong> mode, the  notification <code>url</code> for each subscription must match the defaults for the given database.</p>\n<p>The default port mapping can be seen below:</p>\n<div class=\"click-to-expand-wrapper is-table-wrapper\"><table>\n<thead>\n<tr>\n<th>sink</th>\n<th>port</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>mysql</td>\n<td>5050</td>\n</tr>\n<tr>\n<td>mongo</td>\n<td>5051</td>\n</tr>\n<tr>\n<td>ckan</td>\n<td>5052</td>\n</tr>\n<tr>\n<td>hdfs</td>\n<td>5053</td>\n</tr>\n<tr>\n<td>postgresql</td>\n<td>5054</td>\n</tr>\n<tr>\n<td>cartodb</td>\n<td>5055</td>\n</tr>\n</tbody>\n</table>\n</div><p>Since this subscription is using port <code>5050</code> the context data will eventually be persisted to the <em>MySQL</em> database.</p>\n","urlObject":{"protocol":"http","path":["v2","subscriptions",""],"host":["localhost:1026"],"query":[],"variable":[]}},"response":[],"_postman_id":"949903c8-7764-485c-a825-af1cd48cc605"},{"name":"Orion - Check Subscription is working","id":"6e7c5d26-84ff-47f1-9175-3b9b913b294b","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"method":"GET","header":[{"key":"fiware-service","value":"openiot"},{"key":"fiware-servicepath","value":"/"}],"url":"http://localhost:1026/v2/subscriptions/","description":"<p>If a subscription has been created, you can check to see if it is firing by making a GET \nrequest to the <code>/v2/subscriptions</code> endpoint.</p>\n<p>Within the <code>notification</code> section of the response, you can see several additional <code>attributes</code> which describe the health of the subscription</p>\n<p>If the criteria of the subscription have been met, <code>timesSent</code> should be greater than <code>0</code>.\nA zero value would indicate that the <code>subject</code> of the subscription is incorrect or the subscription \nhas created with the wrong <code>fiware-service-path</code> or <code>fiware-service</code> header</p>\n<p>The <code>lastNotification</code> should be a recent timestamp - if this is not the case, then the devices\nare not regularly sending data. Remember to unlock the <strong>Smart Door</strong> and switch on the <strong>Smart Lamp</strong></p>\n<p>The <code>lastSuccess</code> should match the <code>lastNotification</code> date - if this is not the case \nthen <strong>Draco</strong> is not receiving the subscription properly. Check that the host name\nand port are correct. </p>\n<p>Finally, check that the <code>status</code> of the subscription is <code>active</code> - an expired subscription\nwill not fire.</p>\n","urlObject":{"protocol":"http","path":["v2","subscriptions",""],"host":["localhost:1026"],"query":[],"variable":[]}},"response":[],"_postman_id":"6e7c5d26-84ff-47f1-9175-3b9b913b294b"}],"id":"6fa0b191-0828-424f-9b38-c69b6c1ae030","description":"<p>It is also possible to configure Draco to populate multiple databases simultaneously. We can combine the architecture\nfrom the three previous examples and configure Draco to store data in multiple sinks.</p>\n<p><img src=\"https://fiware.github.io/tutorials.Historic-Context-NIFI/img/multiple-draco-tutorial.png\" alt /></p>\n<p>We now have a system with three databases, PostgreSQL and MySQL for data persistence and MongoDB for both data\npersistence and holding data related to the Orion Context Broker and the IoT Agent.</p>\n<h2 id=\"multi-agent---draco-configuration-for-multiple-databases\">Multi-Agent - Draco Configuration for Multiple Databases</h2>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-yaml\">draco:\n    image: ging/fiware-draco:1.1.0\n    container_name: draco\n    depends_on:\n        - mysql-db\n        - mongo-db\n        - postgres-db\n    environment:\n        - NIFI_WEB_HTTP_PORT=9090\n    ports:\n        - \"9090:9090\"\n        - \"5050:5050\"\n    healthcheck:\n        test: curl --fail -s http://localhost:9090/nifi-api/system-diagnostics || exit 1\n</code></pre>\n<p>The <code>draco</code> container is listening on two ports:</p>\n<ul>\n<li>The Subscription Port for Draco - <code>5050</code> is where the service will be listening for notifications from the Orion\ncontext broker</li>\n<li>The Web interface for Draco - <code>9090</code> is exposed purely for configuring the processors</li>\n</ul>\n<h2 id=\"multi-agent---start-up\">Multi-Agent - Start up</h2>\n<p>To start the system with <strong>multiple</strong> databases run the following command:</p>\n<pre class=\"click-to-expand-wrapper is-snippet-wrapper\"><code class=\"language-console\">./services multiple\n</code></pre>\n<p>Then go to your browser and open Draco using this URL <code>http://localhost:9090/nifi</code></p>\n<p>Now go to the Components toolbar which is placed in the upper section of the NiFi GUI, find the template icon and drag\nand drop it inside the Draco user space. At this point, a popup should be displayed with a list of all the templates\navailable. Please select the template MULTIPLE-SINKS-TUTORIAL.</p>\n<p><img src=\"https://fiware.github.io/tutorials.Historic-Context-NIFI/img/multiple-tutorial-template.png\" alt /></p>\n<p>Now repeat the process for setting the password in the controller \"DBCPConnectionPool\" each connection MySQL and\nPostgreSQL</p>\n<p>Select all the processors (press shift and click on every processor) and start them by clicking on the start button.\nNow, you can see that the status icon of each processor turned from red to green.</p>\n<h3 id=\"generating-context-data\">Generating Context Data</h3>\n<p>For the purpose of this tutorial, we must be monitoring a system where the context is periodically being updated.\nThe dummy IoT Sensors can be used to do this. Open the device monitor page at <code>http://localhost:3000/device/monitor</code>\nand unlock a <strong>Smart Door</strong> and switch on a <strong>Smart Lamp</strong>. This can be done by selecting an appropriate the command \nfrom the drop down list and pressing the <code>send</code> button. The stream of measurements coming from the devices can then\nbe seen on the same page:</p>\n<p><img src=\"https://fiware.github.io/tutorials.Historic-Context/img/door-open.gif\" alt /></p>\n","event":[{"listen":"prerequest","script":{"id":"1c3c984c-886c-464c-b936-a5b355263c89","type":"text/javascript","exec":[""]}},{"listen":"test","script":{"id":"cafefa9a-6026-45d2-8877-2298a955ba2d","type":"text/javascript","exec":[""]}}],"_postman_id":"6fa0b191-0828-424f-9b38-c69b6c1ae030"}],"event":[{"listen":"prerequest","script":{"id":"84ff3562-63ba-4285-b17d-ffb08b6ef75c","type":"text/javascript","exec":[""]}},{"listen":"test","script":{"id":"a6d99f86-6dcc-4e77-b2a7-e0fffe4af5de","type":"text/javascript","exec":[""]}}],"variable":[{"key":"orion","value":"localhost:1026"},{"key":"draco","value":"localhost:9090"}]}