Unit 5: Getting Stored Data with EnOS APIs

When storage policies are configured for the data of the smart electric meter, and the stream processing jobs are running, both the raw data uploaded to the Cloud and the data processed by the stream processing engine will be stored in EnOS TSDB. You can now invoke the corresponding TSDB Data Service APIs to get the stored data of the electric meter.

Installing API Core SDK (Poseidon)

In this tutorial, get the Maven dependency information of the API Core SDK for Java 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/apim-poseidon.

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

    <dependency>
        <groupId>com.envisioniot</groupId>
        <artifactId>apim-poseidon</artifactId>
        <version>0.1.8</version>
    </dependency>
    

Programming for getting the stored data

After the API Core SDK for Java is installed, take the following steps to invoke TSDB Data Service APIs to get the stored data in TSDB:

  1. Use the Get Asset AI Raw Data API to get the AI type raw data of the electric meter in a time range.
  2. Use the Get Asset AI Data with Aggregation Logic API to get the calculated data in a time range.

Getting raw data of the electric meter

Take the following steps to compile the code for the Get Asset AI Raw Data API:

  1. Get the gateway URL of EnOS API service. Contact your EnOS project manager for details if needed. See the following example:

    https://{apigw-address}/tsdb-service/v2.0/ai
    
  2. Get the accessKey and secretKey of your application, which can be generated by registering an application in the Application Registration section on EnOS Console.

  3. Define the request parameters for the API, including the properties of the electric meter. See the following example:

    Request request = new Request();
    request.setQueryParam("orgId", "o1552032369xxxx");
    request.setQueryParam("modelId", "ElectricMeter");
    request.setQueryParam("assetIds","asset_id");
    request.setQueryParam("measurepoints", "Reading");
    request.setQueryParam("startTime", "2019-03-21T09:00:00-08:00");
    request.setQueryParam("endTime", "2019-03-26T09:12:00-08:00");
    request.setQueryParam("pageSize", 100);
    
  4. Call the API to get the stored raw data of the Reading measuring point. See the following example:

    private static class Request extends PoseidonRequest{
    
        public void setQueryParam(String key, Object value){
            queryEncodeParams().put(key, value);
        }
    
        public void setMethod(String method) {
            this.method = method;
        }
    
        private String method;
    
        public String baseUri() {
            return "";
        }
    
        public String method() {
            return method;
        }
    }
    
    public void getAssetsAIRawDataTest(){
    
        String appKey = "accessKey";
        String appSecret = "secretKey";
    
        Request request = new Request();
        request.setQueryParam("orgId", "o1552032369xxxx");
        request.setQueryParam("modelId", "ElectricMeter");
        request.setQueryParam("assetIds","asset_id");
        request.setQueryParam("measurepoints", "Reading");
        request.setQueryParam("startTime", "2019-03-21T09:00:00-08:00");
        request.setQueryParam("endTime", "2019-03-26T09:12:00-08:00");
        request.setQueryParam("pageSize", 100);
        request.setQueryParam("accessKey", appKey);
    
        request.setMethod("GET");
    
        try {
            JSONObject response =  Poseidon.config(PConfig.init().appKey(appKey).appSecret(appSecret).debug())
                    .url("http://apim-gateway/tsdb-service/v2.0/ai")
                    .getResponse(request, JSONObject.class);
            System.out.println(response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    Note

    The gateway URL of the API service vary with the cloud region and instance. For private cloud instances, contact your Envision project manager or support representative to get the URL. The accessKey and secretKey are generated by registering your application on EnOS Console.

  5. Invoke the API and check the result. The stored data of the Reading measuring point will be returned with time stamp. See the following example:

    DATA: [
      {assetId=asset_id, Reading=0.99552613, timestamp=1553234234380},
      {assetId=asset_id, Reading=0.68855274, timestamp=1553234234540},
      {assetId=asset_id, Reading=0.51011103, timestamp=1553234244631},
      {assetId=asset_id, Reading=0.18516362, timestamp=1553234254727},
      {assetId=asset_id, Reading=0.80360246, timestamp=1553234264825},
      {assetId=asset_id, Reading=0.12011969, timestamp=1553234274934},
      ]
    STATUS:0
    MSG:success
    SUBMSG:null
    

Getting calculated data

The data of the Reading measuring point processed by the streaming engine is assigned to the MaxReading10Min and MinReading10Min measuring points, which is stored as AI Normalized Data type. The difference between the MaxReading10Min and MinReading10Min measuring points is assigned to the ReadingDifference measuring point, which is also stored as AI Normalized Data type.

  1. Compile code to call the Get Asset AI Data with Aggregation Logic API to get the stored data. See the following example:

    private static class Request extends PoseidonRequest{
    
        public void setQueryParam(String key, Object value){
            queryEncodeParams().put(key, value);
        }
    
        public void setMethod(String method) {
            this.method = method;
        }
    
        private String method;
    
        public String baseUri() {
            return "";
        }
    
        public String method() {
            return method;
        }
    }
    
    public void getAssetsAINormalizedDataTest(){
    
        String appKey = "accessKey";
        String appSecret = "secretKey";
    
        Request request = new Request();
        request.setQueryParam("orgId", "o1552032369xxxx");
        request.setQueryParam("modelId", "ElectricMeter");
        request.setQueryParam("assetIds","asset_id");
        request.setQueryParam("MaxReading10Min, MinReading10Min, ReadingDifference");
        request.setQueryParam("interval", 0);
        request.setQueryParam("startTime", "2019-03-21T09:00:00-08:00");
        request.setQueryParam("endTime", "2019-03-26T09:12:00-08:00");
        request.setQueryParam("pageSize" , 100);
        request.setQueryParam("accessKey", appKey);
    
        request.setMethod("GET");
    
        try {
            JSONObject response =  Poseidon.config(PConfig.init().appKey(appKey).appSecret(appSecret).debug())
                    .url("http://apim-gateway/tsdb-service/v2.0/ai-normalized")
                    .getResponse(request, JSONObject.class);
            System.out.println(response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
  2. Invoke the API and check the result. The stored data of the MaxReading10Min, MinReading10Min, and ReadingDifference measuring points will be returned with time stamp. See the following example:

    DATA:[
      {assetId=asset_id, MinReading10Min=0.060317636, timestamp=1553234400000},
      {assetId=asset_id, MinReading10Min=0.00997293, timestamp=1553235000000},
      {assetId=asset_id, MinReading10Min=0.0032151341, timestamp=1553235600000},
      {assetId=asset_id, MinReading10Min=0.022154748, timestamp=1553236200000},
      {assetId=asset_id, MinReading10Min=0.0015020967, timestamp=1553236800000},
    
      {assetId=asset_id, MaxReading10Min=0.97941846, timestamp=1553234400000},
      {assetId=asset_id, MaxReading10Min=0.9878034, timestamp=1553235000000},
      {assetId=asset_id, MaxReading10Min=0.9939039, timestamp=1553235600000},
      {assetId=asset_id, MaxReading10Min=0.9860072, timestamp=1553236200000},
      {assetId=asset_id, MaxReading10Min=0.99802256, timestamp=1553236800000},
    
      {assetId=asset_id, ReadingDifference=0.919100824, timestamp=1553234400000},
      {assetId=asset_id, ReadingDifference=0.97783047, timestamp=1553235000000},
      {assetId=asset_id, ReadingDifference=0.9906887659, timestamp=1553235600000},
      {assetId=asset_id, ReadingDifference=0.963852452, timestamp=1553236200000},
      {assetId=asset_id, ReadingDifference=0.9965204633, timestamp=1553236800000},
      ]
    STATUS:0
    MSG:success
    SUBMSG:null
    

API reference

The reference documentation for each API can be found through EnOS Console > EnOS API. Summary of APIs is displayed by API service categories.