Warning |
---|
This document is work in progress and can only be viwed by those at WSO2! |
In this tutorial, you summarize the battery lelve data of the device and create the will get the average of a device's battery level and create the Battery_Level_Per_Hour
table table by annalzing the analyzing the data gathered from all the enrolled devices.
MobX created creates new mobile applications or send updated sends updates to corporate mobile applications. These applications need to be installed on the users user's devices. To ensure that the applciations applications are successfully installed or updated on all the enrolled devices successfully, all the devices need to have a battery level of 50% or above. Using the steps given below, MobX administartors administrators are able to identify the devices that had less battery power when an application installation request was pushed and confirm with those users if the application was installed successfully.
Let's get started!
Table of Contents |
---|
...
Configuring WSO2 IoT
...
Server to publish device information
You need to configure WSO2 IoT Server to publish device information as it is disabled by default. Follow the steps given below.
By default, WSO2 IoT Server does not publish device information, such as the device ID and type, which we need to collect to analyze the battery level of devices. To enable IoT Server to publish the device data, do the following:
- Open the
<IOTS_HOME>/conf/
file.cdm-config.xml
Configure the
PublishDeviceInfoResponse
parameter astrue
.Code Block <OperationAnalyticsConfiguration> <PublishLocationResponse>false</PublishLocationResponse> <PublishDeviceInfoResponse>true</PublishDeviceInfoResponse> <PublishOperationResponse> <Enabled>false</Enabled> <Operations> <Operation>*</Operation> </Operations> </PublishOperationResponse> </OperationAnalyticsConfiguration>
WSO2 IoT Server can now publish the device information to WSO2 Data Analytics Server (WSO2 DAS). The published data can then be stored and analyzed using the steps given below.
Creating a stream and persisting data
You need to create a stream to gather the device data. Follow the steps given below:
- Start WSO2 IoT Server:
Start the WSO2 IoT Server broker profile.
Code Block cd <IOTS_HOME>/bin sh broker.sh
Start the WSO2 IoT Server core profile.
Code Block cd <IOTS_HOME>/bin sh iot-server.sh
Next, start the WSO2 IoT Server analytics profile.
Code Block cd <IOTS_HOME>/bin sh analytics.sh
Creating a stream and persisting data
You need to create a stream to gather the device data. Follow the steps given below:
- Access the WSO2 IoT Server's analytics management console.
For access via secured HTTP:
https://<IOTS_HTTPS_HOST>:9445/carbon/
For example:https://localhost:9445/carbon/
For access via HTTP:
For example:http://<IOTS_HTTP_HOST>:9765/carbon/
http://localhost:9765/carbon/
On the Main tab, under Events, click Streams that is under Events.
Click + Add Events Streams.
Enter the following values:
Event Stream Name org.wso2.iot.BatteryStream
Event Stream Version 1.0.0
Meta Data Attributes Add a meta data each metadata attribute by entering the its name, selecting the type from the drop-down list, and by clicking clicking Add.
Attribute name Attribute type deviceId
String deviceType
String timestamp
Long Payload Data Attributes Add a each payload data attribute by entering the name, selecting the type from the drop-down list, and by clicking Add.
Attribute name Attribute type level
Double year
Int month
Int day
Int hour
Int minute
Int - Click Perisist Events. What happens when you do this?
- Click Add Event Stream.
Creating an analytics script to summarize data
Follow the steps given below to create a script that summarizes the data:
- On the Main tab click Scripts that is under Batch Analytics.
- Click Add New Analytics Scripts.
- Give the script a name, such as battery_data_analytics.
Copy the script given below to the Spark SQL Queries text box.
Code Block CREATE TEMPORARY TABLE BatteryData USING CarbonAnalytics OPTIONS(tableName "ORG_WSO2_IOT_BATTERYSTREAM", incrementalParams "ORG_WSO2_IOT_BATTERYSTREAM, HOUR"); CREATE TEMPORARY TABLE Battery_Level_Per_Hour USING CarbonAnalytics OPTIONS (tableName "Battery_Level_Per_Hour", schema "deviceId STRING, type STRING, level DOUBLE -i, year INT -i, month INT -i, day INT -i, hour INT -i, timestamp LONG", primaryKeys "year, month, day, hour, deviceId, type", mergeSchema "false"); INSERT INTO TABLE Battery_Level_Per_Hour SELECT meta_deviceId as deviceId, meta_deviceType as type, avg(level) as level, year, month, day, hour, getHourStartingTime(year, month, day, hour) as timestamp FROM BatteryData GROUP BY meta_deviceId, meta_deviceType, year, month, day, hour; INCREMENTAL_TABLE_COMMIT ORG_WSO2_IOT_BATTERYSTREAM;
- Enter
0 0 0/1 1/1 * ? *
as the value for the Cron Expression.
This field is used to determine how often the analytics script needs to be executed. In this sample we have configured the script to run every hour. - Click AddPersist Events to save the device data into the
EVENT_STORE
table so that it can be used later on. - Select the following attributes:
Select Persist Event Stream
- Under Meta Data Attributes, select Persist Attribute.
This selects all the sub-attributes too. - Under Meta Data Attributes, select Index Column for deviceId, deviceType, and timestamp.
- Under Payload Data Attributes select Persist Attribute.
This selects all the sub-attributes too.
- Click Save Event Stream.
Creating an execution plan to publish data
Create Now that you've created the event stream, you will create an execution plan to publish the data received to an output stream. publish data to that stream. For more information on creating execution plans, see the WSO2 Data Analytics Server (WSO2 DAS) documentation.
Follow the steps given below:
- On the Main tab, click Execution Plans.
- Click Add Execution Plan.
Copy the execution plan that is given below and replace the sample content that is in the text box.
Code Block @Plan:name('analzeBattery_Level_batteryAverage_levelSummarization') @Import('org.wso2.iot.DeviceInfoStream:1.0.0') define stream inputStreaminput (meta_deviceId string, meta_deviceType string, timeStamp long, imei string, imsi string, deviceModel string, vendor string, osVersion string, osBuildDate string, batteryLevel double, totalInternalMemory double, availableInternalMemory double, totalExternalMemory double, availableExternalMemory double, operator string, connectionType string, mobileSignalStrength double, ssid string, cpuUsage double, totalRAM double, availableRAM double, pluggedIn bool); @Export('org.wso2.iot.BatteryStream:1.0.0') define stream outputStreamoutput (meta_deviceId string, meta_deviceType string, meta_timestamp long, level double, year int, month int, day int, hour int, minute int); from inputStreaminput select meta_deviceId, meta_deviceType, timeStamp as meta_timestamp, batteryLevel as level, time:extract(time:timestampInMilliseconds(), 'year') as year, time:extract(time:timestampInMilliseconds(), 'month') as month, time:extract(time:timestampInMilliseconds(), 'day') as day, time:extract(time:timestampInMilliseconds(), 'hour') as hour, time:extract(time:timestampInMilliseconds(), 'minute') as minute insert into outputStreamoutput;
- Click Add Execution Plan.
Try it out
What's next
...
Try out the tutorials listed below and see how the extensions described above are put into use.
...
Sending Operations to Devices Based on Time
...
You can now use this execution plan to gather the device information that published by the device and pass them on to the BatteryStream
stream you created.
Creating an analytics script to summarize the battery data
Follow the steps given below to create a script that summarizes and presents the average battery level of the device every hour. For more information on creating scripts, see the WSO2 Data Analytics Server (WSO2 DAS) documentation.
- On the Main tab, under Batch Analytics, click Scripts.
- Click Add New Analytics Scripts.
- Give the script a name, such as
battery_data_analytics
. Copy the script given below to the Spark SQL Queries text box.
Code Block CREATE TEMPORARY TABLE BatteryData USING CarbonAnalytics OPTIONS(tableName "ORG_WSO2_IOT_BATTERYSTREAM", incrementalParams "ORG_WSO2_IOT_BATTERYSTREAM, HOUR"); CREATE TEMPORARY TABLE Battery_Level_Per_Hour USING CarbonAnalytics OPTIONS (tableName "Battery_Level_Per_Hour", schema "deviceId STRING, type STRING, level DOUBLE -i, year INT -i, month INT -i, day INT -i, hour INT -i, timestamp LONG", primaryKeys "year, month, day, hour, deviceId, type", mergeSchema "false"); INSERT INTO TABLE Battery_Level_Per_Hour SELECT meta_deviceId as deviceId, meta_deviceType as type, avg(level) as level, year, month, day, hour, getHourStartingTime(year, month, day, hour) as timestamp FROM BatteryData GROUP BY meta_deviceId, meta_deviceType, year, month, day, hour; INCREMENTAL_TABLE_COMMIT ORG_WSO2_IOT_BATTERYSTREAM;
Note Make sure to add a space when starting a new line in the script. If you copy the script given above, it has the spaces set by default.
- Enter
0 0 0/1 1/1 * ? *
as the value for the Cron Expression.
This field is used to determine how often the analytics script needs to be executed. In this sample, we have configured the script to run every minute. - Click Add.
Try it out
The script you wrote above summarizes the battery data of the devices and adds it to the Battery_Level_Per_Hour
database table. Now the administrator can check the data in the table and see the devices that did not have a battery level of 50% or above at the time of pushing the applications to the devices, and check with those device owners if the application was installed successfully.
To check out the data that was summarized and added to the table, follow the steps given below:
- Access the WSO2 IoT Server's analytics management console.
For access via secured HTTP:
https://<IOTS_HTTPS_HOST>:9445/carbon/
For example:https://localhost:9445/carbon/
For access via HTTP:
For example:http://<IOTS_HTTP_HOST>:9765/carbon/
http://localhost:9765/carbon/
- On the Main tab, click Data Explorer.
- From the Table Name* drop-down list, select BATTERY_LEVEL_PER_HOUR.
- Click Search.
The data is displayed in the table format. Now, MobX is able to find out the battery levels of each device on an hourly basis.