Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

When managing devices the administrator must be notified of events occurring on the user's devices that reduce the effectiveness and efficiency of the tasks performed by a device, so that he/she can take immediate action and correct it. For example, if a cooperate application is utilizing a high CPU or memory percentage, you as the admin can stop that application from running or uninstall and re-install the application again on the user's device.

In WSO2 IoT Server all such events are captured and published to the WSO2 Data Analytics Server (WSO2 DAS). In this section, let's take a look at how WSO2 IoT Server creates alerts to report critical issues.

Table of Contents
maxLevel4
minLevel4

Step 1: Enabling the existing event listeners.

  1. Open the Constants.java file that is in the org.wso2.emm.agent.utils package via a preferred IDE.

    Info

    For more information on the default configuration, see the Constants.java file.

  2. Assign true as the value for the ENABLE_EVENT_LISTENING property that is in the EventListners class.

    Info
    titleWhy is this step needed?

     The ENABLE_EVENT_LISTENING property is a global configuration and it needs to be enabled in order to enable event listening.

  3. Enable the preferred listeners.
    • APPLICATION_STATE_LISTENER
    • RUNTIME_STATE_LISTENER

Step 2: Writing a new event listener.

In this section, let's look at how a new event can be captured from the Android agent and published to WSO2 DAS.

Panel
titlePrerequisite

 Download and install Android Studio. For more information, see installing Android Studio.

  1. Open the Android agent source code in Android studio.
    The event related logic is included in the org.wso2.emm.agent.events package. It has the following folder structure.
    Image Added
  2. Implement the AlertEventListener interface in the org.wso2.emm.agent.events.listeners package.

    Info
    Expand
    titleClick here for more information on the methods used in the AlertEventListener interface.

    The following methods are used:

    • startListening() - This method is used to start listening to an event broadcasted by the OS. In the case of a polling based check, this method can be called by the onReceive method of the AlarmReceiver.
    • stopListening() - Unregister the event receiver or stop an alarm.
    • publishEvent(String payload) - Handle the data published to IoT Server.

    Example:

    Code Block
    languagejava
    titleAlertEventListener interface
    package org.wso2.emm.agent.events.listeners;
    
    /**
     * This is used to define any new events that needs to be captured and sent to server.
     */
    public interface AlertEventListener {
    
        /**
         * This can be used to start listening to a specific broadcast receiver.
         * Another usage would be, when there is an event that doesn't do a broadcast. For example
         * Application exceeding 75% of CPU is not broadcasted by default from the Android OS. Only way
         * to catch it is by constantly polling a specific API and check for the status. In such a
         * situation, AlarmManager can call startListening on it onReceiver method to do the polling on
         * an API.
         */
        void startListening();
    
        /**
         * If in case, listening to a specific receiver need to be done here. This can be a place to,
         * stop an AlarmManager.
         */
        void stopListening();
    
        /**
         * This is where publishing data to EMM/DAS would happen. This can ideally be called from
         * an onReceive method of a BroadcastReceiver, or from startListening method to inform the
         * results of a polling.
         *
         * @param payload JSON string payload to be published.
         * @param type type of the alert being published.
         */
        void publishEvent(String payload, String type);
    
    }
    
  3. Create a new class in the org.wso2.emm.agent.events.listeners package, extend it from BroadcastReceiver, and implement the AlertEventListener interface.

    Example: Create a class named ApplicationStateListener.

    Code Block
    public class ApplicationStateListener extends BroadcastReceiver implements AlertEventListener {}
  4. Capturing events. There are two ways to capture the events as listed below:
    1. Listen to events broadcasted by the Android OS.

      Expand
      titleClick here for more information.
      Panel
      borderColor#11375B
      bgColor#ffffff
      borderWidth1

      Include Page
      Listening to Events Broadcasted by the Android OS
      Listening to Events Broadcasted by the Android OS

    2. Poll an API to check for event changes that are not broadcasted continuously by the Android OS.

      Expand
      titleClick here for more information.
      Panel
      borderColor#11375B
      bgColor#ffffff
      borderWidth1

      Include Page
      Listening to Events Not Broadcasted by the Android OS
      Listening to Events Not Broadcasted by the Android OS