Commit 4cdc3108 by Adi Amir

support sensor list with details

parent 2e579c94
Showing with 80 additions and 0 deletions
...@@ -36,16 +36,22 @@ import java.util.Map; ...@@ -36,16 +36,22 @@ import java.util.Map;
* Created by gil on 01/03/17. * Created by gil on 01/03/17.
*/ */
public class GEManager { public class GEManager {
private static final Long ONE_DAY_IN_MILLI_SECS = 1L * 24 * 60 * 60 * 1000;
public static String AUTH = "Basic aXBnYWxsZXJ5OjFQR2FsbDNyeQ=="; public static String AUTH = "Basic aXBnYWxsZXJ5OjFQR2FsbDNyeQ==";
public static String AUTH_URL = "8553482c-1d32-4d38-8597-2e56ab642dd3.predix-uaa.run.asv-pr.ice.predix.io"; public static String AUTH_URL = "8553482c-1d32-4d38-8597-2e56ab642dd3.predix-uaa.run.asv-pr.ice.predix.io";
public static String AUTH_PATH = "oauth/token"; public static String AUTH_PATH = "oauth/token";
public static String AUTH_PARAMS = "grant_type=client_credentials&client_id=ipgallery"; public static String AUTH_PARAMS = "grant_type=client_credentials&client_id=ipgallery";
public static String METADATA_URL = "ie-cities-metadata.run.asv-pr-pub.ice.predix.io"; public static String METADATA_URL = "ie-cities-metadata.run.asv-pr-pub.ice.predix.io";
public static String MEDIA_URL = "ie-cities-media.run.asv-pr-pub.ice.predix.io"; public static String MEDIA_URL = "ie-cities-media.run.asv-pr-pub.ice.predix.io";
public static String EVENT_URL = "ie-cities-events.run.asv-pr-pub.ice.predix.io";
public static String ASSET_LIST_URL = "/v2/assets/search"; public static String ASSET_LIST_URL = "/v2/assets/search";
public static String ASSETS_URL = "/v2/assets"; public static String ASSETS_URL = "/v2/assets";
public static String PARKING_ZONE_ID = "ics-IE-PARKING"; public static String PARKING_ZONE_ID = "ics-IE-PARKING";
public static String PS_ZONE_ID = "ics-IE-PUBLIC-SAFETY"; public static String PS_ZONE_ID = "ics-IE-PUBLIC-SAFETY";
public static String ENV_ZONE_ID = "ics-IE-ENVIRONMENTAL";
public static String TOKEN = "NOT_VALID"; public static String TOKEN = "NOT_VALID";
private SimpleHttpClient httpClient = null; private SimpleHttpClient httpClient = null;
...@@ -89,6 +95,9 @@ public class GEManager { ...@@ -89,6 +95,9 @@ public class GEManager {
else if (apiIdAsParams.length == 1 && apiIdAsParams[0].equals("sensors")) { else if (apiIdAsParams.length == 1 && apiIdAsParams[0].equals("sensors")) {
brr = getSensorList(requestContext); brr = getSensorList(requestContext);
} }
else if (apiIdAsParams.length == 3 && apiIdAsParams[0].equals("sensor") && apiIdAsParams[2].equals("details")) {
brr = getSensorDetails(apiIdAsParams[1]/*sensorId*/, requestContext);
}
else { else {
String err = "executeRequest() failed. Unknown api: " + getApiIdString(apiIdAsParams); String err = "executeRequest() failed. Unknown api: " + getApiIdString(apiIdAsParams);
brr = errorResponse(err); brr = errorResponse(err);
...@@ -222,6 +231,77 @@ public class GEManager { ...@@ -222,6 +231,77 @@ public class GEManager {
logger.info("getCameraDetails() successfull. camId=" + cameraId); logger.info("getCameraDetails() successfull. camId=" + cameraId);
return brr; return brr;
} }
private BaseRestResponse getSensorDetails(String sensorId, RequestContext requestContext) {
SimpleHttpRequest req = null;
SimpleHttpResponse httpResp = null;
BaseRestResponse brr = new BaseRestResponse(false,null);
logger.info("getSensorDetails() activated. camId=" + sensorId);
// execute the request ...
String envType = requestContext.getParameter("envType");
Long tsEndTime = System.currentTimeMillis();
Long tsStartTime = tsEndTime - ONE_DAY_IN_MILLI_SECS;
req = buildSensorDetailsRequest(sensorId, envType, tsStartTime,tsEndTime);
try {
httpResp = httpClient.processRequest(req);
if(httpResp.getStatusCode() == 200)
brr = buildSensorDetailsResp(httpResp);
else {
logHttpError("getSensorDetails", httpResp);
return errorHttpResponse(httpResp);
}
} catch (Exception e) {
String err = "getSensorDetails() failed with exception. e=" + e.toString();
logger.error(err);
return errorResponse(err);
}
logger.info("getSensorDetails() successfull. camId=" + sensorId);
return brr;
}
private SimpleHttpRequest buildSensorDetailsRequest(String sensdorId, String envType, Long tsStartTime, Long tsEndTime) {
SimpleHttpRequest request = new SimpleHttpRequest();
request.setProtocol("https");
request.setDomain(EVENT_URL);
request.setPort(443);
request.setMethod(SimpleHttpRequest.Method.GET);
// path
String path = ASSETS_URL + "/" + sensdorId + "/" + "events";
request.setPath(path);
// query string
StringBuilder sb = new StringBuilder();
sb.append("eventType=").append(envType).append("&")
.append("startTime=").append(1486755073000L).append("&")
.append("endTime=").append(1486758673000L);
String queryStr = sb.toString();
request.setQueryString(queryStr);
request.addHeader("Authorization", "Bearer " + TOKEN);
request.addHeader("Predix-Zone-Id", ENV_ZONE_ID);
return request;
}
private BaseRestResponse buildSensorDetailsResp(SimpleHttpResponse resp) {
BaseRestResponse brr = new BaseRestResponse(true,null);
int size;
String content = resp.getContent();
ObjectMapper mapper = new ObjectMapper();
try {
JsonNode contentObj = mapper.readTree(content);
ArrayNode eventsArray = (ArrayNode)contentObj.get("content");
if (eventsArray.size() > 0) {
int lastIdx = eventsArray.size() - 1;
JsonNode latestEvent = eventsArray.get(lastIdx);
brr.objectNode = latestEvent;
}
} catch (IOException e) {
return new BaseRestResponse(false, e.toString());
}
return brr;
}
private boolean getNewAccessToken() { private boolean getNewAccessToken() {
SimpleHttpRequest req = null; SimpleHttpRequest req = null;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment