Unit 4: Getting the Subscribed Data

You can now install the EnOS SDK for data subscription to consume the subscribed data. When the data subscriptions are enabled, the subscribed data will be pushed to your application.

Installing EnOS SDK for data subscription

In this tutorial, get the Maven dependency information of the EnOS SDK for data subscription from the Maven repository and add it to your development project. Detailed steps are as follows:

  1. Open the Maven repository of the SDK at https://mvnrepository.com/artifact/com.envisioniot/enos-subscribe.

  2. Open the development environment, add the maven dependency for the SDK in your Java project. See the following example.

    <dependency>
      <groupId>com.envisioniot</groupId>
      <artifactId>enos-subscribe</artifactId>
      <version>2.3.0</version>
    </dependency>
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.0</version>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.16</version>
    </dependency>
    

Programming to Get Subscribed Real-time Data

Use the getDataService() method to get the subscribed real-time data of the electric meter. See the following sample code:

import com.envisioniot.sub.client.EosClient;
import com.envisioniot.sub.client.data.IDataHandler;
import com.envisioniot.sub.client.data.IDataService;
import com.envisioniot.sub.common.exception.SubscribeException;
import com.envisioniot.sub.common.model.dto.StreamMessage;

public class demo {

    public static void main(String[] args) throws SubscribeException {
        String host = "subscription_server";
        int port = 9001;
        String accessKey = "access_key";
        String secretKey = "secret_key";
        String subId = "subscription_id";

        /* service */
        EosClient eosClient = new EosClient(host, port, accessKey, secretKey);
        IDataService dataService = eosClient.getDataService();

        /* handler */
        IDataHandler dataHandler = new IDataHandler(){

            public void dataRead(StreamMessage message) {
                System.out.println(message);
            }
        };

        /* subscribe */
        dataService.subscribe(dataHandler, subId);

    }
}

Note

  • The host and port of the subscription server vary with the cloud region and instance. To get the address and port of the subscription service for your cloud instance, log in EnOS Console and select Help > Environment List.
  • The accessKey and secretKey are generated by registering your application on EnOS Console. The subId is the ID of the data subscription topic.

When the program is running, the subscribed data will be pushed to the program. See the following sample:

2019-04-08 15:17:42,399 INFO  SubClient:119 - connect success
2019-04-08 15:17:42,404 INFO  KeeperHandler:27 - channel active
2019-04-08 15:17:42,464 INFO  KeeperHandler:46 - sent auth info
2019-04-08 15:17:42,730 INFO  AuthRspProcessor:22 - receive auth rsp from server: ack: 0
2019-04-08 15:17:42,851 INFO  SubRspProcessor:17 - receive sub rsp from server: ack: 0

{"orgId":"o15520323695671","modelId":"Grid.TopupAcMeter","payload":[{"assetId":"KuBqAoBz","time":1554707867019,"points":"{\"Temperature\":0.76931}"}]}
{"orgId":"o15520323695671","modelId":"Grid.TopupAcMeter","payload":[{"assetId":"KuBqAoBz","time":1554707877116,"points":"{\"Temperature\":0.43013537}"}]}
{"orgId":"o15520323695671","modelId":"Grid.TopupAcMeter","payload":[{"assetId":"KuBqAoBz","time":1554707887212,"points":"{\"Temperature\":0.28075862}"}]}
{"orgId":"o15520323695671","modelId":"Grid.TopupAcMeter","payload":[{"assetId":"KuBqAoBz","time":1554707897318,"points":"{\"Temperature\":0.84750724}"}]}
{"orgId":"o15520323695671","modelId":"Grid.TopupAcMeter","payload":[{"assetId":"KuBqAoBz","time":1554707907413,"points":"{\"Temperature\":0.1094988}"}]}

Programming to Get Subscribed Alert Records

Use the getAlertService() method to get the subscribed alert records of the electric meter. See the following sample code:

import com.envisioniot.sub.client.EosClient;
import com.envisioniot.sub.client.data.IDataHandler;
import com.envisioniot.sub.client.data.IDataService;
import com.envisioniot.sub.common.exception.SubscribeException;
import com.envisioniot.sub.common.model.dto.StreamMessage;

public class demo {

    public static void main(String[] args) throws SubscribeException {
        String host = "subscription_server";
        int port = 9001;
        String accessKey = "access_key";
        String secretKey = "secret_key";
        String subId = "subscription_id";


        /* service */
        EosClient eosClient = new EosClient(host, port, accessKey, secretKey);
        IAlertService alertService = eosClient.getAlertService();

        /* handler */
        IAlertHandler alertHandler = new IAlertHandler (){

            public void alertRead(Alert alert) {
                System.out.println(alert);
            }
        };

        /* subscribe */
        alertService.subscribe(alertHandler, subId);

    }
}

When the program is running, the subscribed alert records will be pushed to the program. See the following sample:

2019-04-09 11:50:29,818 INFO  SubClient:119 - connect success
2019-04-09 11:50:29,822 INFO  KeeperHandler:27 - channel active
2019-04-09 11:50:29,872 INFO  KeeperHandler:46 - sent auth info
2019-04-09 11:51:29,887 INFO  HeartbeatHandler:27 - write req idle
2019-04-09 11:51:30,009 INFO  AuthRspProcessor:22 - receive auth rsp from server: ack: 0
2019-04-09 11:51:30,118 INFO  SubRspProcessor:17 - receive sub rsp from server: ack: 0

BaseEvent{eventId='201904091731a3b0ff3a10003592ed7e6b31d34e', orgId='o15520323695671', deviceId='KuBqAoBz', modelId='Grid.TopupAcMeter', pointId='Temperature', hitRuleId=55, value='0.9728574', time=1554781909105, localTime=1554810709105, severityId=38, severityDesc={"en_US":"High level severity alert","zh_CN":"Warning"}, typeId=30, typeDesc={"en_US":"Device data exceeds limit.","zh_CN":"Device data exceeds limit."}, subTypeId=null, subTypeDesc=null, contentId=39, contentDesc={"en_US":"The device temperature exceeds limit.","zh_CN":"The device temperature exceeds limit."}, eventType=1, ruleTag={}, ruleDesc=null}
BaseEvent{eventId='2019040914bcf8e56eae1eaf3503cf9ca1e1d7dc', orgId='o15520323695671', deviceId='KuBqAoBz', modelId='Grid.TopupAcMeter', pointId='Temperature', hitRuleId=55, value='0.99026257', time=1554781969548, localTime=1554810769548, severityId=38, severityDesc={"en_US":"High level severity alert","zh_CN":"Warning"}, typeId=30, typeDesc={"en_US":"Device data exceeds limit.","zh_CN":"Device data exceeds limit."}, subTypeId=null, subTypeDesc=null, contentId=39, contentDesc={"en_US":"The device temperature exceeds limit.","zh_CN":"The device temperature exceeds limit."}, eventType=1, ruleTag={}, ruleDesc=null}

For detailed information about EnOS SDK for data subscription, see Data Subscription SDK Reference.