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 the API Core SDK (Poseidon)¶
Get the Maven dependency information of the API Core SDK for Java from the Maven repository and add it to your development project.
Open the Maven repository of the SDK at https://mvnrepository.com/artifact/com.envisioniot/apim-poseidon.
Open the development environment and include the maven dependency for the SDK in your Java project.
<dependency> <groupId>com.envisioniot</groupId> <artifactId>apim-poseidon</artifactId> <version>0.1.8</version> </dependency>
Getting the Stored Data¶
After the API Core SDK for Java is installed, invoke the TSDB Data Service APIs to get the stored data in TSDB with the following steps.
Use the Get Asset AI Raw Data API to get the AI type raw data of the electric meter within a time range.
Use the Get Asset AI Data with Aggregation Logic API to get the calculated data within a time range.
Getting the Raw Data of the Electric Meter¶
Compile the code for the Get Asset AI Raw Data API with the following steps.
Get the gateway URL of the EnOS API service. Contact your EnOS project manager for details if needed. See the following example:
https://{apigw-address}/tsdb-service/v2.0/ai
Get the
accessKeyandsecretKeyof your application, which can be generated by registering an application in the Application Registration section on the EnOS Management Console.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", "yourOrgId"); 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);
Call the API to get the stored raw data of the Reading measurement 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", "yourOrgId"); 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 will vary with the cloud region and instance. For private cloud instances, contact your Envision project manager or support representative to get the URL. The
accessKeyandsecretKeyare generated by registering your application on the EnOS Management Console.Invoke the API and check the result. The stored data of the Reading measurement point will be returned with the timestamp. 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 the Calculated Data¶
The data of the Reading measurement point processed by the streaming engine is assigned to the MaxReading10Min and MinReading10Min measurement points, which is stored as AI Normalized Data type. The difference between the MaxReading10Min and MinReading10Min measurement points is assigned to the ReadingDifference measurement point, which is also stored as AI Normalized Data type.
Compile the 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", "yourOrgId"); 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(); } }
Invoke the API and check the result. The stored data of the MaxReading10Min, MinReading10Min, and ReadingDifference measurement points will be returned with the timestamp. 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 Management Console > EnOS API. The APIs are categorized by the API services.