This documentation is for WSO2 IoT Server 3.2.0. View the documentation for the latest release.
Sending Operations to Devices Based on Time - IoT Server 3.2.0 - WSO2 Documentation
||
Skip to end of metadata
Go to start of metadata

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.

  1. Start WSO2 IoT Server:

    1. Start the WSO2 IoT Server broker profile.

      cd <IOTS_HOME>/bin
      sh broker.sh
    2. Start the WSO2 IoT Server core profile.

      cd <IOTS_HOME>/bin
      sh iot-server.sh
    3. Next, start the WSO2 IoT Server analytics profile.

      cd <IOTS_HOME>/bin
      sh analytics.sh
  2. Access the WSO2 IoT Server's analytics management console.
    1. For access via secured HTTP: https://<IOTS_HTTPS_HOST>:9445/carbon/ 
      For example: https://localhost:9445/carbon/

    2. For access via HTTP:  http://<IOTS_HTTP_HOST>:9765/carbon/ 

      For example: http://localhost:9765/carbon/
  3. On the Main tab click Streams that is under Events.

  4. Click + Add Events Streams.

  5. Enter the following values:

    Event Stream Name

    org.wso2.iot.triggerStream

    Event Stream Version

    1.0.0

    Payload Data AttributesAdd 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

  6. 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:

  1. On the Main tab, click Execution Plans.
  2. Click Add Execution Plan.
  3. 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;
  4. 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:

  1. On the Main tab, click Execution Plans.
  2. Click Add Execution Plan.
  3. 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 here for more information.
    • First, you need to define the import the stream where you get the data from and export the stream to which the data is inserted.

      @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);
    • Next, you need to filter out the devices that belong to the admin user. This is done using the device:hasDevicesOfUser extension.
    • Then you convert the data you want to send to the output stream to match the data types of the exported output stream.
      For example, the output stream does not recognize CAMERA. Therefore, it is equaled to code using the term as.

      "CAMERA" as code
    • After converting the data to the format that the output stream identifies, you need to insert the data to the output stream.
  4. 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.

  1. Sign in to the WOS2 IoT Server device management console using admin as the username and admin as the password.

  2. Enroll an Android or iOS device.

    • For more information on how to enroll an Android device, see Android.
    • For more information on how to enroll an iOS device, see iOS.
  3. 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 the cronLockTriggerStream 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.

  4. 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?

  • No labels