Page tree
Skip to end of metadata
Go to start of metadata

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 in one console or screen
  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. Local 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.

Deployment (console 1)
wget "https://linksmart.eu/repo/service/local/artifact/maven/content?r=releases&g=eu.linksmart.services.events.gpl.distributions&a=iot.learning.universal.agent&v=LATEST" -O agent.jar
Result Deployment (console 1)
--2017-10-26 13:24:26--  https://linksmart.eu/repo/service/local/artifact/maven/content?r=releases&g=eu.linksmart.services.events.gpl.distributions&a=iot.learning.universal.agent&v=LATEST
Resolving linksmart.eu (linksmart.eu)... 52.59.168.195, 52.59.86.181
Connecting to linksmart.eu (linksmart.eu)|52.59.168.195|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 177127313 (169M) [application/java-archive]
Saving to: ‘agent.jar’

100%[===========================================================================================================================================================================================================>] 177,127,313 3.44MB/s   in 2m 30s

2017-10-26 13:26:56 (1.13 MB/s) - ‘agent.jar’ saved [177127313/177127313]

Again in console 1, we started the agent. 

Run (console 1)
export env_var_enabled=true
export cep_init_engines=eu.linksmart.services.event.cep.engines.EsperEngine
export agent_init_extensions=eu.linksmart.services.event.ceml.core.CEML
java -jar agent.jar 
Result 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.

Deploy Statement (console 2)
curl -v -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/

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


Result part 1
> POST /statement/ HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:8319
> Accept: */*
> Content-Type: application/json
> Content-Length: 92
>
* upload completely sent off: 92 out of 92 bytes
< HTTP/1.1 201 Created
< Date: Thu, 26 Oct 2017 11:30:56 GMT
< Location: /statement/d88809505168bb760859e4651c15008d9d0a4435c9b9419555716dab3a78ecf1
< Content-Type: application/json; charset=UTF-8
< Content-Length: 3049
* Server Jetty(9.2.15.v20160210) is not blacklisted
< Server: Jetty(9.2.15.v20160210)
Prettify Result (console 2)
{
	"resources": {
		"d88809505168bb760859e4651c15008d9d0a4435c9b9419555716dab3a78ecf1": {
			"cehandler": "eu.linksmart.services.event.handler.ComplexEventHandler",
			"registrable": true,
			"restoutput": false,
			"name": "countEvents",
			"statement": "select count(*) as obs from Observation.win:time(1 sec)",
			"source": "",
			"input": [""],
			"CEHandler": "eu.linksmart.services.event.handler.ComplexEventHandler",
			"StateLifecycle": "RUN",
			"scope": ["outgoing"],
			"TargetAgents": [],
			"ID": "d88809505168bb760859e4651c15008d9d0a4435c9b9419555716dab3a78ecf1",
			"isRestOutput": false,
			"lastOutput": {
				"@iot.id": "397fface-94c6-48b0-8dbb-198c042849d0",
				"featureOfInterest": {
					"@iot.id": "9e8f9be9b26237df0cbaf374a274e12233fbee6e7f690fe775503194dcc54181",
					"description": "BootstrapMessage",
					"observations": [{}, {}
					],
					"@iot.selfLink": "http://linksmart.eu/v1.0/FeatureOfInterest(9e8f9be9b26237df0cbaf374a274e12233fbee6e7f690fe775503194dcc54181)",
					"Observations@iot.navigationLink": "FeatureOfInterest(9e8f9be9b26237df0cbaf374a274e12233fbee6e7f690fe775503194dcc54181)/Observations"
				},
				"datastream": {
					"@iot.id": "e0e0c70e-2df4-4bbc-baee-df463bdb892d",
					"observations": [{}
					],
					"sensor": {
						"@iot.id": "e0e0c70e-2df4-4bbc-baee-df463bdb892d",
						"datastreams": [{}
						],
						"@iot.selfLink": "http://linksmart.eu/v1.0/Sensor(e0e0c70e-2df4-4bbc-baee-df463bdb892d)",
						"Datastreams@iot.navigationLink": "Sensor(e0e0c70e-2df4-4bbc-baee-df463bdb892d)/Datastreams"
					},
					"thing": {
						"@iot.id": "e0e0c70e-2df4-4bbc-baee-df463bdb892d",
						"datastreams": [{}
						],
						"@iot.selfLink": "http://linksmart.eu/v1.0/Thing(e0e0c70e-2df4-4bbc-baee-df463bdb892d)",
						"Datastreams@iot.navigationLink": "Thing(e0e0c70e-2df4-4bbc-baee-df463bdb892d)/Datastreams",
						"HistoricalLocations@iot.navigationLink": "Thing(e0e0c70e-2df4-4bbc-baee-df463bdb892d)/HistoricalLocations",
						"Locations@iot.navigationLink": "Thing(e0e0c70e-2df4-4bbc-baee-df463bdb892d)/Locations"
					},
					"@iot.selfLink": "http://linksmart.eu/v1.0/Datastream(e0e0c70e-2df4-4bbc-baee-df463bdb892d)",
					"Observations@iot.navigationLink": "Datastream(e0e0c70e-2df4-4bbc-baee-df463bdb892d)/Observations",
					"Sensor@iot.navigationLink": "Datastream(e0e0c70e-2df4-4bbc-baee-df463bdb892d)/Sensor",
					"ObservedProperty@iot.navigationLink": "Datastream(e0e0c70e-2df4-4bbc-baee-df463bdb892d)/ObservedProperty",
					"Thing@iot.navigationLink": "Datastream(e0e0c70e-2df4-4bbc-baee-df463bdb892d)/Thing"
				},
				"phenomenonTime": "2017-10-26T11:30:57.300+0000",
				"result": "NaN",
				"@iot.selfLink": "http://linksmart.eu/v1.0/Observation(397fface-94c6-48b0-8dbb-198c042849d0)",
				"FeatureOfInterest@iot.navigationLink": "Observation(397fface-94c6-48b0-8dbb-198c042849d0)/FeatureOfInterest",
				"Datastream@iot.navigationL* Connection #0 to host localhost left intactink": "Observation(397fface-94c6-48b0-8dbb-198c042849d0)/Datastream"
			}
		}
	},
	"responses": [{
			"headline": "Created",
			"agentID": "e0e0c70e-2df4-4bbc-baee-df463bdb892d",
			"producerID": "EsperEngine",
			"producerName": "CEPEngine",
			"message": "Statement d88809505168bb760859e4651c15008d9d0a4435c9b9419555716dab3a78ecf1 was successful",
			"status": 201,
			"messageType": "SUCCESS",
			"topic": "LS/LA/e0e0c70e-2df4-4bbc-baee-df463bdb892d/OGC/1.0/Datastreams/d88809505168bb760859e4651c15008d9d0a4435c9b9419555716dab3a78ecf1/"
		}
	]
}

3) Subscribe to the statement 

We subscribe to all statements.

Subscribe to an event (Console 2)
mosquitto_sub -t LS/LA/+/OGC/1.0/Datastreams/# &

Note:

The output topic can be configured see IoT agents configuration.

In the default case, the '+' can be replaced by the id of the agent which can be set, or it is autogenerated and display at the start of the agent with a message such as:

[main] INFO  e.l.s.event.core.DataProcessingCore - The Agent streaming core version 1.6.0 is starting with ID: e0e0c70e-2df4-4bbc-baee-df463bdb892d

or it is shown every minute in the console as: 

INFO 261 --- [      Thread-34] e.l.s.event.core.DataProcessingCore      : The Agent with ID e0e0c70e-2df4-4bbc-baee-df463bdb892d is alive

The '#' can be replaced with the id of the statement shown in location attributed located in the header response after the statement had been posted. In out case: 

d88809505168bb760859e4651c15008d9d0a4435c9b9419555716dab3a78ecf1

4) Generate data 


Generate event (console 2)
mosquitto_pub -t LS/test/1/OGC/1.0/Datastreams/1/ -f event.json

Note:

The input topic can be configured see IoT agents configuration.


event.json
{  
   "Datastream":{  
      "id":1
   },
   "Result":1,
   "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 "Result" will increase. 

Result (Console 2)
{
  "@iot.id": "40c86297-2421-4e54-9a2e-5fef2f300e6e",
  "featureOfInterest": {
    "@iot.id": "6c35493a2b937829c9815c39e23af964bc84e5430a7dc104c700bbc0de2b59e3",
    "description": "count(*)",
    "observations": [{}, {}],
    "@iot.selfLink": "http://linksmart.eu/v1.0/FeatureOfInterest(6c35493a2b937829c9815c39e23af964bc84e5430a7dc104c700bbc0de2b59e3)",
    "Observations@iot.navigationLink": "FeatureOfInterest(6c35493a2b937829c9815c39e23af964bc84e5430a7dc104c700bbc0de2b59e3)/Observations"
  },
  "datastream": {
    "@iot.id": "276f2948-c252-491b-9b93-8268383fdd92",
    "observations": [{}],
    "sensor": {
      "@iot.id": "276f2948-c252-491b-9b93-8268383fdd92",
      "datastreams": [{}],
      "@iot.selfLink": "http://linksmart.eu/v1.0/Sensor(276f2948-c252-491b-9b93-8268383fdd92)",
      "Datastreams@iot.navigationLink": "Sensor(276f2948-c252-491b-9b93-8268383fdd92)/Datastreams"
    },
    "thing": {
      "@iot.id": "276f2948-c252-491b-9b93-8268383fdd92",
      "datastreams": [{}],
      "@iot.selfLink": "http://linksmart.eu/v1.0/Thing(276f2948-c252-491b-9b93-8268383fdd92)",
      "Datastreams@iot.navigationLink": "Thing(276f2948-c252-491b-9b93-8268383fdd92)/Datastreams",
      "HistoricalLocations@iot.navigationLink": "Thing(276f2948-c252-491b-9b93-8268383fdd92)/HistoricalLocations",
      "Locations@iot.navigationLink": "Thing(276f2948-c252-491b-9b93-8268383fdd92)/Locations"
    },
    "@iot.selfLink": "http://linksmart.eu/v1.0/Datastream(276f2948-c252-491b-9b93-8268383fdd92)",
    "Observations@iot.navigationLink": "Datastream(276f2948-c252-491b-9b93-8268383fdd92)/Observations",
    "Sensor@iot.navigationLink": "Datastream(276f2948-c252-491b-9b93-8268383fdd92)/Sensor",
    "ObservedProperty@iot.navigationLink": "Datastream(276f2948-c252-491b-9b93-8268383fdd92)/ObservedProperty",
    "Thing@iot.navigationLink": "Datastream(276f2948-c252-491b-9b93-8268383fdd92)/Thing"
  },
  "phenomenonTime": "2017-08-17T16:17:35.919+0000",
  "result": 120,
  "@iot.selfLink": "http://linksmart.eu/v1.0/Observation(40c86297-2421-4e54-9a2e-5fef2f300e6e)",
  "FeatureOfInterest@iot.navigationLink": "Observation(40c86297-2421-4e54-9a2e-5fef2f300e6e)/FeatureOfInterest",
  "Datastream@iot.navigationLink": "Observation(40c86297-2421-4e54-9a2e-5fef2f300e6e)/Datastream"
}



  • No labels