Skip to content

Commit

Permalink
Added type implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
camendoza94 authored and camendoza94 committed Oct 23, 2017
1 parent 1a99430 commit 46665dd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
8 changes: 7 additions & 1 deletion src/main/java/com/camendoza94/semanticinterface/Method.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
class Method {
private String URL;
private ArrayList<String> fields;
private ArrayList<String> types;

Method(String URL, ArrayList<String> fields) {
Method(String URL, ArrayList<String> fields, ArrayList<String> types) {
this.URL = URL;
this.fields = fields;
this.types = types;
}

ArrayList<String> getFields() {
Expand All @@ -18,4 +20,8 @@ ArrayList<String> getFields() {
String getURL() {
return URL;
}

public ArrayList<String> getTypes() {
return types;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ class SemanticController {
ResponseEntity<String> requestService(@RequestBody DeviceObservation observation) {
String deviceID = observation.getDeviceId();
String URL;
ArrayList<String> serviceFields;
ArrayList<String> serviceTypes;
JsonParser parser = new JsonParser();
JsonObject body = parser.parse(observation.getPayload()).getAsJsonObject();
ArrayList<String> serviceFields;
if (cache.get(deviceID) == null) {
List<AbstractMap.SimpleEntry<RDFNode, RDFNode>> matches = queryMatching();
System.out.println("Device ID: " + deviceID);
Expand All @@ -45,18 +46,23 @@ ResponseEntity<String> requestService(@RequestBody DeviceObservation observation
URL = "http://" + service[0] + "/" + service[1];
String[] fields = service[2].split(" ");
String[] fieldTypes = service[3].split(" ");
//TODO use fieldTypes
serviceFields = new ArrayList<>(Arrays.asList(fields));
cache.put(deviceID, new Method(URL, serviceFields));
serviceTypes = new ArrayList<>(Arrays.asList(fieldTypes));
cache.put(deviceID, new Method(URL, serviceFields, serviceTypes));
} else {
URL = cache.get(deviceID).getURL();
serviceFields = cache.get(deviceID).getFields();
serviceTypes = cache.get(deviceID).getTypes();
}
JsonObject requestJson = new JsonObject();
//TODO what if there is no field matches?
for (String serviceField : serviceFields) {
for (int i = 0; i < serviceFields.size(); i++) {
String serviceField = serviceFields.get(i);
try {
requestJson.addProperty(serviceField, body.get(serviceField).getAsNumber());
if (serviceTypes.get(i).equalsIgnoreCase("String"))
requestJson.addProperty(serviceField, body.get(serviceField).getAsString());
else
requestJson.addProperty(serviceField, body.get(serviceField).getAsNumber());
} catch (NullPointerException e) {
System.out.println("Field " + serviceField + " not found. Skipping.");
}
Expand Down

0 comments on commit 46665dd

Please sign in to comment.