Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents

Summary

This example we show a simple use of the IoT Agent Statement API. As an example, we will count a number of events revived by the agent. For this we need:

  1. to deploy and run an agent
  2. to deploy a statement that does the counting
  3. subscribing to the results of this statement
  4. generate events so the agent can process them

Requirements 

  1. Java >= 1.8
  2. MQTT broker
  3. mosquito clients
  4. curl
  5. wget

Notes:

For running the IoT Data-Processing Agent is only needed the (1) and an MQTT broker no matter where. Anyhow, for realizing this tutorial the all above is needed.  Please, notice that for this demo we need three console sessions. 

1) Deploy and run the agent

In the console 1 download the agent.

Hey

Code Block
languagebash
themeRDark
titleDeployment (console 1)
user@hostname:~/dir$ wget "https://linksmart.eu/repo/service/local/artifact/maven/redirect?r=releases&g=eu.linksmart.services.events.gpl.distributions.rest&a=iot.learning.rest.agent&v=LATEST" -O DataProcessingAgent.jar
Code Block
languagebash
themeDJango
titleResult Deployment (console 1)
--2016-09-08 15:59:52--  https://linksmart.eu/repo/service/local/artifact/maven/redirect?r=releases&g=eu.linksmart.services.events.gpl.distributions.rest&a=iot.learning.rest.agent&v=LATEST
Resolving linksmart.eu (linksmart.eu)... 193.175.161.121
Connecting to linksmart.eu (linksmart.eu)|193.175.161.121|:443... connected.
HTTP request sent, awaiting response... 307 Temporary Redirect
Location: https://linksmart.eu/repo/service/local/repositories/releases/content/eu/linksmart/services/events/gpl/distributions/rest/iot.learning.rest.agent/1.3.1/iot.learning.rest.agent-1.3.1.jar [following]
--2016-09-08 15:59:52--  https://linksmart.eu/repo/service/local/repositories/releases/content/eu/linksmart/services/events/gpl/distributions/rest/iot.learning.rest.agent/1.3.1/iot.learning.rest.agent-1.3.1.jar
Reusing existing connection to linksmart.eu:443.
HTTP request sent, awaiting response... 200 OK
Length: 89665190 (86M) [application/java-archive]
Saving to: ‘DataProcessingAgent.jar’

100%[===================================================================================================================================================================================================================================================================================>] 89.665.190  11,2MB/s   in 7,7s

2016-09-08 15:59:59 (11,2 MB/s) - ‘DataProcessingAgent.jar’ saved [89665190/89665190]

Again in console 1, we started the agent.

Code Block
languagebash
themeRDark
titleRun (console 1)
user@hostname:~/dir$ java -jar DataProcessingAgent.jar 
Code Block
languagebash
themeDJango
titleResult Run (console 1)
....
Lot of the output had being remove. 
If no exception is given then this output is not relevant now. 
....
2016-09-08 16:00:16.202  INFO 16010 --- [           main] eu.linksmart.services.Application        : Started Application in 6.607 seconds (JVM running for 9.535)

2) Deploy a statement in the agent

 
In console three we deploy a statement.

Code Block
languagebash
themeRDark
titleDeploy Statement (console 2)
user@hostname:~/dir$ curl -H "Content-Type: application/json" -X POST -d '{"name":"countEvents","statement":"select count(*) as obs from Observation.win:time(1 sec)"}' http://localhost:8319/statement/add

The content of the result is not important what is important is that returns 201 created as the response. 

Code Block
languagepy
themeDJango
titlePrettify Result (console 2)
collapsetrue
{  
   "resources":{  
      "97939930775881634626547151622159126031434832852884240170848287519360840953073":{  
         "name":"countEvents",
         "statement":"select count(*) as obs from Observation.win:time(1 sec)",
         "source":"",
         "input":[  
            ""
         ],
         "output":null,
         "stateLifecycle":"RUN",
         "scope":[  
            "default"
         ],
         "targetAgents":[  

         ],
         "id":"97939930775881634626547151622159126031434832852884240170848287519360840953073",
         "cehandler":"eu.almanac.event.datafusion.handler.ComplexEventHandler",
         "synchronousResponse":null
      }
   },
   "overallStatus":201,
   "responsesTail":{  
      "headline":"Created",
      "agentID":"0ab8b321-b182-4e72-812c-bd7b9ea31d3c",
      "producerID":"EsperEngine",
      "producerName":"CEPEngine",
      "message":"Statement 97939930775881634626547151622159126031434832852884240170848287519360840953073 was successful",
      "status":201,
      "messageType":"SUCCESS",
      "topic":"0ab8b321-b182-4e72-812c-bd7b9ea31d3c/EsperEngine/"
   },
   "responses":[  
      {  
         "headline":"Created",
         "agentID":"0ab8b321-b182-4e72-812c-bd7b9ea31d3c",
         "producerID":"EsperEngine",
         "producerName":"CEPEngine",
         "message":"Statement 97939930775881634626547151622159126031434832852884240170848287519360840953073 was successful",
         "status":201,
         "messageType":"SUCCESS",
         "topic":"0ab8b321-b182-4e72-812c-bd7b9ea31d3c/EsperEngine/"
      }
   ],
   "headResourceKey":"97939930775881634626547151622159126031434832852884240170848287519360840953073",
   "headResource":{  
      "name":"countEvents",
      "statement":"select count(*) as obs from Observation.win:time(1 sec)",
      "source":"",
      "input":[  
         ""
      ],
      "output":null,
      "stateLifecycle":"RUN",
      "scope":[  
         "default"
      ],
      "targetAgents":[  

      ],
      "id":"97939930775881634626547151622159126031434832852884240170848287519360840953073"
   }
}

We subscribe to all statements.

Code Block
languagebash
themeRDark
titleSubscribe to an event (Console 2)
user@hostname:~/dir$ mosquitto_sub -t /out/#

3) Subscribe to the statement 


For see the result of the of the statement we need to generate data. For this, we publish an event payload located in the file event.json.


Code Block
languagebash
themeRDark
titleGenerate event (console 3)
user@hostname:~/dir$ mosquitto_pub -t /in/something -f event.json

4) Generate data 


Code Block
languagepy
themeFadeToGrey
titleevent.json
linenumberstrue
collapsetrue
{  
   "Datastream":{  
      "id":"1"
   },
   "ResultValue":1,
   "ResultType":"Int",
   "Time":"2015-09-08T17:16:12.00Z",
   "Sensor":{  
      "id":"1"
   },
   "id":"1"
}

Then in console 2, we can see the result. Quicker the data is being generated the "ResulValue" will increase. 

Code Block
languagebash
themeDJango
titleResult (Console 2)
{"Time":"2016-09-08T15:18:36.367+0000","ResultValue":1,"ResultType":"obs","Datastream":{"id":"98ce0a31-53c1-484a-a1a3-2c1d677060f1"},"Sensor":{"id":"98ce0a31-53c1-484a-a1a3-2c1d677060f1"}}