Difference between revisions of "Datonis:Using the Datonis Ruby SDK"

From Datonis
Jump to: navigation, search
(Created page with " You can download the [https://github.com/Altizon/Aliot-SDK-Examples Aliot-SDK-Examples] zip file here. Extract the zip file in your local directory. Folder ruby contains a...")
(No difference)

Revision as of 10:21, 1 July 2017

You can download the Aliot-SDK-Examples zip file here. Extract the zip file in your local directory. Folder ruby contains a aliot-3.0.2.gem and sample.rb file.

First install aliot gem in your system.

Install Gem

cd <path-to-folder>
gem install aliot-3.0.2.gem

Aliots (ALtizon Internet Of Things) agents are embedded agents sitting at an endpoint that stream data to Datonis. To understand how an Aliot Agent works, we will be walking through the sample agent file in detail. Firstly we require following files to be included in your sample code.

Require

require 'aliot/thing'
require 'aliot/aliot_configuration'
require 'aliot/aliot_gateway_http'

require 'aliot/thing'

require 'aliot/aliot_configuration'

require 'aliot/aliot_gateway_http'

require 'aliot/thing'

require 'aliot/aliot_configuration'

require 'aliot/aliot_gateway_http'

Configure Datonis Gateway

You will then need to configure the agent to send data securely to Datonis. The following lines of code achieve this. Since the Aliot agent can act as a gateway for multiple Things, the SDK provides a gateway object that can hold the information about multiple Things.

The GatewayConfig function takes access_key and secret_key as parameter. Add the access_key and secret_keyobtained by downloading the key-pair in the GatewayConfig function. Construct a gateway that helps us send various types of events to Datonis. The ruby gem aliot-3.0.0.gem does not support MQTT/MQTTS protocol.

Aliot Configuration

conf = Aliot::AliotConfiguration.new(<access_key>, <secret_key>)

gateway= Aliot::AliotGatewayHttp.new(conf)

Create Thing object and register your Thing


After the device is able to connect to Datonis, add thing details of the thing whose data you want to send. Things detail include thing_key, thing_name and thing_description.

Try to register your Thing, if registration is successfully continue further else verify your access_key, secret_key and thing_key from Datonis portal.

Add Thing Details and register

thing = Aliot::Thing.new(<thing_key>, <thing_name>, <thing_description>)

gateway.register(thing)

Sending Data


Create a hash of data as metric name and metric value. In our example, metric name is 'temperature' and 'humidity' and its corresponding value. If you want to send waypoint data (data related to location of Thing) create a array named waypoint and insert latitude and longitude information.

Then send data with t. send_data function basically takes 4 parameters, thing object, data hash, waypoint array and timestamp. The thing object created previously is a mandatory parameter while you wish to whose what data you want to send and set rest parameters to nil. Your can choose to send timestamp, default it will send current time.

Sending Data

data = {:temperature => <temperature_value>, :humidity => <humidity_value>}

waypoint = [<latitude>, <longitude>];

timestamp = Time.now.to_i * 1000;

gateway.send_data(thing, data, waypoint, timestamp)

Sending Heartbeat


You can also send heartbeat messages to Datonis. These messages does not send any data but assures Datonis that your Thing is still connected.

Sending heartbeat

gateway.send_heart_beat(thing)

Sending Alerts


Things can also send alerts to Datonis, which is not an event data but data send during abnormal situation such as a thermometer reporting reading that are out of permissible range. The alert function takes thing_key, alert message as string, alert level as integer and alert_data as hash. Alert message can be any user defined message which states the cause of alert.

Alert level can only be of four types:

  1. Info : value = 0
  2. Warning : value= 1
  3. Error : value = 2
  4. Critical : value = 3

Alert data has to be a hash representing a key and value pair.

Sending Alerts

gateway.send_alert(thing, <alert_message>, <alert_type>, {:temperature => "98"})