Sending Operations to Devices Based on Time
In this tutorial, you are configuring WSO2 IoT Server to disable the camera on the Android devices that belong to the admin user after 8 AM and enable the camera on the device after 6 PM. The admin user is available by default in WSO2 IoT Server.Â
MobX wants to disable the camera on the admin user's devices during working hours and enable the camera on the device after working hours. This is required because the admin has access to confidential data.
Let's get started!
Create the event stream
Follow the steps given below to create the org.wso2.iot.triggerStream
stream that is required for the execution plan that generates two events.
Start WSO2 IoT Server:
Start the WSO2 IoT Server broker profile.
cd <IOTS_HOME>/bin sh broker.sh
Start the WSO2 IoT Server core profile.
cd <IOTS_HOME>/bin sh iot-server.sh
Next, start the WSO2 IoT Server analytics profile.
cd <IOTS_HOME>/bin sh analytics.sh
- 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 Streams that is under Events.
Click + Add Events Streams.
Enter the following values:
Event Stream Name org.wso2.iot.triggerStream
Event Stream Version 1.0.0
Payload Data Attributes Add a payload data attribute by entering the details given below and clicking Add. - Attribute Name :
isLocked
Attribute Type: Select bool from the drop-down list
- Attribute Name :
- Click Add Event Stream.
Create the execution plan to generate two events for 8 AM and 6 PM
You need to create an execution plan to disable and enable the camera on the devices during the specified time. 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.
@Plan:name('TimedTriggers') /* define streams/tables and write queries here ... */ define trigger cronLockTriggerStream at '0 0 18 * * ?' ; /* Disable the camera on the device at 18:00 '0 0 18 * * ?' */ define trigger cronUnlockTriggerStream at '0 0 8 * * ?' ; /* Enable the camera on the device at 08:00 '0 0 8 * * ?' */ @Export('org.wso2.iot.triggerStream:1.0.0') define stream dataOut (isLocked bool); /* Send the camera disable operation at 18:00 */ from cronLockTriggerStream select true as isLocked insert into dataOut; /* Send the camera enable operation at 8:00 */ from cronUnlockTriggerStream select false as isLocked insert into dataOut;
- Click Add Execution Plan.
Create the execution plan to retrieve the active devices that belong to the admin user
The above execution plan is not aware of the devices that belong to the admin user. Therefore, you need to write another execution plan to get the admin users devices so that the execution plan you created above can be executed on the filtered devices. This can be done using the WSO2 IoT Server Siddhi Extensions.
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.
@Plan:name('TimedTasks_operations') @Import('org.wso2.iot.triggerStream:1.0.0') define stream dataIn (isLocked bool); @Export('org.wso2.iot.operation:1.0.0') define stream dataOut (meta_deviceIdentifiers string, meta_deviceType string, code string, type string, isEnabled bool, payLoad string); /* Filter the amin users devices to enable and disable the camera */ from dataIn[device:hasDevicesOfUser("shavi", "android", "ACTIVE")] select device:getDevicesOfUser("shavi", "android", "ACTIVE") as meta_deviceIdentifiers, "android" as meta_deviceType, "CAMERA" as code, "COMMAND" as type, not isLocked as isEnabled, "{}" as payLoad insert into dataOut;
Want to know more about what happens in the execution plan?
- Click Add Execution Plan.
Congratulations! You have successfully configured WSO2 IoT Server to enable the camera on the admin user's device after 6 PM and disable it after 8 AM.
Try it out
Follow the steps given below to try out the scenario given above.
Sign in to the WOS2 IoT Server device management console using
admin
as the username andadmin
as the password.Enroll an Android or iOS device.
Change the time on the
TimedTriggers
 execution plan to match your current time. If you want the camera to be disabled at 12.50 PM, defineÂ0 50 12 * * ?
for thecronLockTriggerStream
property.
Example:define trigger cronLockTriggerStream at '0 50 12 * * ?' ; /* Disable the camera on the device at 12:50 '0 50 12 * * ?' */
Now, you see that the camera can't be accessed at 12.50 PM.
Similarly, change the time on theÂ
TimedTriggers
 execution plan to match your current time. If you want to enable the camera on the device at 1 PM, defineÂ0 0 13 * * ?
 for theÂcronUnlockTriggerStream
 property.
Example:define trigger cronUnlockTriggerStream at '0 0 13 * * ?' ; /* Enable the camera on the device at 13:00 '0 0 13 * * ?' */
Now, you see that you can access the camera after 1 PM.
What's next?
- Want to know more about the WSO2 IoT Server Siddhi extensions? See WSO2 IoT Server Siddhi Extensions.
- Want to try out other tutorials that allow you to monitor devices?