Commit 805e0383 by Adi Amir

support getTrafficLanes from cisco

parent 911b4d87
...@@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.node.NullNode; ...@@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.node.NullNode;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.gdata.util.common.base.PercentEscaper; import com.google.gdata.util.common.base.PercentEscaper;
import com.google.gdata.util.common.util.Base64; import com.google.gdata.util.common.util.Base64;
import com.google.gson.JsonNull;
import http.simpleHttpClient.SimpleHttpClient; import http.simpleHttpClient.SimpleHttpClient;
import http.simpleHttpClient.SimpleHttpRequest; import http.simpleHttpClient.SimpleHttpRequest;
import http.simpleHttpClient.SimpleHttpResponse; import http.simpleHttpClient.SimpleHttpResponse;
...@@ -227,13 +228,35 @@ public class CiscoManager { ...@@ -227,13 +228,35 @@ public class CiscoManager {
private BaseRestResponse reLogin(LoginInfo loginInfo) { private BaseRestResponse reLogin(LoginInfo loginInfo) {
return null; return null;
} }
private BaseRestResponse getTrafficLanes() { private BaseRestResponse getTrafficLanes() {
return null; SimpleHttpRequest req = null;
SimpleHttpResponse httpResp = null;
BaseRestResponse brr = new BaseRestResponse(true, null);
logger.info("cisco.getTrafficLanes() activated");
try {
// get parking
req = buildTrafficLanesRequest();
httpResp = httpClient.processRequest(req);
if(httpResp.getStatusCode() == 200)
brr = parseTrafficLanesResp(httpResp);
else {
logHttpError("getTrafficLanes", httpResp);
return errorHttpResponse(httpResp);
}
} catch (Exception e) {
String err = CISCO + ".getTrafficLanes() failed with exception. e=" + e.toString();
logger.error(err);
return errorResponse(err);
}
return brr;
} }
...@@ -416,6 +439,103 @@ public class CiscoManager { ...@@ -416,6 +439,103 @@ public class CiscoManager {
} }
} }
private SimpleHttpRequest buildTrafficLanesRequest() {
SimpleHttpRequest request = new SimpleHttpRequest();
request.setProtocol("https");
request.setDomain(CISCO_DOMAIN);
request.setProtocol("http");
request.setPort(80);
request.setMethod(SimpleHttpRequest.Method.POST);
// path
String path = API_URI + "devices/traffic";
request.setPath(path);
// query string
StringBuilder sb = new StringBuilder();
sb.append("UserKey=").append(loginInfo.userId).append("&")
.append("SensorCustomerKey=").append(loginInfo.parentUserId).append("&")
.append("AppKey=CDP-App");
String queryString = sb.toString();
request.setQueryString(queryString);
// content
String content = "{\"Query\":{\"Find\": {\"RoadSegment\": {}}}}";
request.setContent(content);
// headers
// WSO2-Authorization
String WSO2_Authorization = "oAuth Bearer " + loginInfo.appAccessToken;
request.addHeader("WSO2-Authorization", WSO2_Authorization);
// Authorization
String Authorization = "Bearer " + loginInfo.apiAccessToken;
request.addHeader("Authorization", Authorization);
// accept
String Accept = "application/json";
request.addHeader("Accept", Accept);
// Content-Type
String Content_Type = "application/json";
request.addHeader("Content-Type", Content_Type);
return request;
}
private BaseRestResponse parseTrafficLanesResp(SimpleHttpResponse httpResp) {
try {
JsonNode contentObj = objMapper.readTree(httpResp.getContent());
ArrayNode csTrafficLanes = (ArrayNode)contentObj.at("/Find/Result");
ArrayNode trafficLanesResult = objMapper.createArrayNode();
for (JsonNode csTrafficLane: csTrafficLanes) {
ObjectNode objTrafficLane = objMapper.createObjectNode();
// location uid
objTrafficLane.put("location-uid", csTrafficLane.at("/RoadSegment/sid").asText());
// coordinates
JsonNode csCoordinates = csTrafficLane.at("/ParkingSpot/geocoordinates");
String p1Loc = "0" + "," + "0";
ObjectNode p1 = objMapper.createObjectNode();
p1.put("P1", p1Loc);
objTrafficLane.put("coordinates", p1);
// properties
ObjectNode properties = objMapper.createObjectNode();
// vehicleType
properties.putNull("vehicleType");
// measures
ArrayNode arrayMeasures = objMapper.createArrayNode();
// measure/speed
ObjectNode measureSpeed = objMapper.createObjectNode();
measureSpeed.put("tag", "speed");
measureSpeed.put("value", csTrafficLane.at("/RoadSegment/refSpeed").asDouble());
measureSpeed.put("unit", "MPS");
arrayMeasures.add(measureSpeed);
// measure/vehicleCount
ObjectNode measureVehicleCount = objMapper.createObjectNode();
measureVehicleCount.put("tag", "vehicleCount");
measureVehicleCount.put("value", 20);
measureVehicleCount.put("unit", "");
arrayMeasures.add(measureVehicleCount);
properties.set("measures", arrayMeasures);
// timestamp
properties.put("timestamp", 0);
// mde-key
properties.put("mde-key", csTrafficLane.at("/RoadSegment/sid").asText());
objTrafficLane.set("properties", properties);
trafficLanesResult.add(objTrafficLane);
}
logger.info("parseTrafficLanesResp() - found " + trafficLanesResult.size() + " traffic lanes. tenant=" + CISCO);
return successResponse(trafficLanesResult);
} catch (Exception e) {
String err = "parseUserInfoResp failed with exception. e: " + e.toString();
return errorResponse(err);
}
}
private Boolean parseLoginRequestParameters(RequestContext requestContext) { private Boolean parseLoginRequestParameters(RequestContext requestContext) {
...@@ -492,8 +612,9 @@ public class CiscoManager { ...@@ -492,8 +612,9 @@ public class CiscoManager {
private Boolean isAccessDenied(BaseRestResponse brr) { private Boolean isAccessDenied(BaseRestResponse brr) {
if (brr.success == false) { if (brr.success == false) {
int statusCode = brr.objectNode.get("statusCode").asInt(); //int statusCode = brr.objectNode.get("statusCode").asInt();
Boolean bAccessDenied = (statusCode == 403 ||statusCode == 401); //Boolean bAccessDenied = (statusCode == 403 ||statusCode == 401);
Boolean bAccessDenied = true;
return bAccessDenied; return bAccessDenied;
} }
else else
......
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