From 39251400d732bb0e98cc31a754262db6d22a81e2 Mon Sep 17 00:00:00 2001 From: "kevin.shehu" Date: Sat, 12 Jun 2021 00:37:45 +0200 Subject: [PATCH] FIX : ApiCall.java ADD : Json_Unpack TODO : KnowledgeBase --- .idea/copyright/Kevin_Shehu.xml | 6 ++ src/main/java/ApiCall.java | 60 +++------------- .../java/Credentials/UserCredentials.java | 11 ++- src/main/java/Data/Germes.txt | 1 + src/main/java/Data/Heufs.txt | 1 + src/main/java/Data/Jacobs.txt | 1 + src/main/java/Data/Nica.txt | 1 + src/main/java/Data/devicesID.txt | 1 - src/main/java/Data/devicesOutput.json | 1 - src/main/java/Data/readAll.json | 0 src/main/java/Data/securityToken.txt | 2 +- src/main/java/Data/sensorOutput.json | 0 src/main/java/HttpCall/HttpCall.java | 2 +- src/main/java/HttpCall/HttpPost.java | 2 +- .../java/JSON_Unpack/BoxCall4Partner.java | 71 +++++++++++++++++++ .../java/JSON_Unpack/GeneralCall4All.java | 23 ++++++ .../java/JSON_Unpack/ReadSensors4Partner.java | 68 ++++++++++++++++++ .../java/JSON_Unpack/Reading4AllSensors.java | 4 ++ src/main/java/ScheduledTask.java | 6 +- src/main/java/SecurityToken.java | 4 +- src/main/java/SpecificApiCall.java | 25 ------- 21 files changed, 204 insertions(+), 86 deletions(-) create mode 100644 .idea/copyright/Kevin_Shehu.xml create mode 100644 src/main/java/Data/Germes.txt create mode 100644 src/main/java/Data/Heufs.txt create mode 100644 src/main/java/Data/Jacobs.txt create mode 100644 src/main/java/Data/Nica.txt delete mode 100644 src/main/java/Data/devicesID.txt delete mode 100644 src/main/java/Data/devicesOutput.json delete mode 100644 src/main/java/Data/readAll.json delete mode 100644 src/main/java/Data/sensorOutput.json create mode 100644 src/main/java/JSON_Unpack/BoxCall4Partner.java create mode 100644 src/main/java/JSON_Unpack/GeneralCall4All.java create mode 100644 src/main/java/JSON_Unpack/ReadSensors4Partner.java create mode 100644 src/main/java/JSON_Unpack/Reading4AllSensors.java delete mode 100644 src/main/java/SpecificApiCall.java diff --git a/.idea/copyright/Kevin_Shehu.xml b/.idea/copyright/Kevin_Shehu.xml new file mode 100644 index 0000000..0e5e24b --- /dev/null +++ b/.idea/copyright/Kevin_Shehu.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/src/main/java/ApiCall.java b/src/main/java/ApiCall.java index 196dd8e..bb11460 100644 --- a/src/main/java/ApiCall.java +++ b/src/main/java/ApiCall.java @@ -1,38 +1,22 @@ import Credentials.UserCredentials; -import Files.ReadFile; import Files.WriteFile; -import HttpCall.HttpCall; -import HttpCall.HttpPost; -import org.apache.jena.base.Sys; -import org.json.JSONArray; +import JSON_Unpack.ReadSensors4Partner; import org.json.JSONObject; -import org.json.simple.parser.JSONParser; -import org.json.simple.parser.ParseException; -import java.io.IOException; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.http.HttpClient; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; +import java.util.ArrayList; public class ApiCall { private static String token = "0"; private static String oldId = "1"; - public static String FetchDataFromApi(String extension) throws InterruptedException, ExecutionException, TimeoutException { + + public static String FetchDataFromApi(String extension) throws Exception { UserCredentials userCredentials = new UserCredentials(); final String username = userCredentials.getUsername(); final String password = userCredentials.getPassword(); - String baseURL = userCredentials.getBaseURL(); + final String homeRoot = userCredentials.getHomeRoot(); String outputToken; WriteFile writingIntoFile = new WriteFile(); - HttpCall httpCall = new HttpCall(); - String devicesResult = ""; if (token.equals(oldId)) { //do nothing @@ -42,42 +26,16 @@ public class ApiCall { JSONObject obj = new JSONObject(outputToken); token = obj.getString("id"); - writingIntoFile.WriteFile("/Users/ksh/Documents/Work/GGD/src/main/java/Data/securityToken.txt", token); + writingIntoFile.WriteFile(homeRoot+"securityToken.txt", token); oldId = token; } catch (Exception e) { e.printStackTrace(); } } + ReadSensors4Partner readSensors4Partner = new ReadSensors4Partner(); + String y = (readSensors4Partner.FetchSensor4Box("Germes",token)); - try { - devicesResult = httpCall.HttpGet(baseURL + extension + "?access_token=" + token,token); - JSONArray jsonarray = new JSONArray(devicesResult); - for (int i = 0; i < jsonarray.length(); i++) { - JSONObject jsonobject = jsonarray.getJSONObject(i); - String id = jsonobject.getString("id"); - System.out.println(id); - writingIntoFile.WriteFile("/Users/ksh/Documents/Work/GGD/src/main/java/Data/devicesID.txt", id); - } - - - - } catch (Exception e) { - e.printStackTrace(); - } - - - try { - writingIntoFile.WriteFile("/Users/ksh/Documents/Work/GGD/src/main/java/Data/" + extension + "Output.json", devicesResult); - } catch (IOException e) { - e.printStackTrace(); - } catch (Exception e) { - e.printStackTrace(); - } - return devicesResult; - - - - + return y; } diff --git a/src/main/java/Credentials/UserCredentials.java b/src/main/java/Credentials/UserCredentials.java index 2c2861f..3281330 100644 --- a/src/main/java/Credentials/UserCredentials.java +++ b/src/main/java/Credentials/UserCredentials.java @@ -3,7 +3,8 @@ package Credentials; public class UserCredentials { private String username = "kevin.shehu@hochschule-rhein-waal.de"; private String password = "DK7SxFkGJgnLhnU3"; - private String baseURL = "https://api.dev.whysor.com/"; + private String baseURL = "https://api.whysor.com/"; + private static String homeRoot = "D:\\WORK\\GGD\\src\\main\\java\\Data\\"; @@ -31,6 +32,14 @@ public class UserCredentials { this.baseURL = baseURL; } + public static String getHomeRoot() { + return homeRoot; + } + + public static void setHomeRoot(String homeRoot) { + UserCredentials.homeRoot = homeRoot; + } + @Override public String toString() { return "UserCredentials{" + diff --git a/src/main/java/Data/Germes.txt b/src/main/java/Data/Germes.txt new file mode 100644 index 0000000..3b263b6 --- /dev/null +++ b/src/main/java/Data/Germes.txt @@ -0,0 +1 @@ +70B3D57050004223,70B3D570500042D7,70B3D57050004EEF,70B3D57050006223,70B3D5705000623C diff --git a/src/main/java/Data/Heufs.txt b/src/main/java/Data/Heufs.txt new file mode 100644 index 0000000..5805f0a --- /dev/null +++ b/src/main/java/Data/Heufs.txt @@ -0,0 +1 @@ +70B3D57050004682,70B3D570500046AB,70B3D57050004D14 \ No newline at end of file diff --git a/src/main/java/Data/Jacobs.txt b/src/main/java/Data/Jacobs.txt new file mode 100644 index 0000000..c92747c --- /dev/null +++ b/src/main/java/Data/Jacobs.txt @@ -0,0 +1 @@ +0004A30B00227D68,70B3D5705000E019 \ No newline at end of file diff --git a/src/main/java/Data/Nica.txt b/src/main/java/Data/Nica.txt new file mode 100644 index 0000000..2e0441e --- /dev/null +++ b/src/main/java/Data/Nica.txt @@ -0,0 +1 @@ +70B3D5705000E030,70B3D5705000E05E,70B3D5705000E386 \ No newline at end of file diff --git a/src/main/java/Data/devicesID.txt b/src/main/java/Data/devicesID.txt deleted file mode 100644 index 41a1c45..0000000 --- a/src/main/java/Data/devicesID.txt +++ /dev/null @@ -1 +0,0 @@ -e5728985-f4f5-4b9a-ae32-9d6ae40f9f53 \ No newline at end of file diff --git a/src/main/java/Data/devicesOutput.json b/src/main/java/Data/devicesOutput.json deleted file mode 100644 index bab946c..0000000 --- a/src/main/java/Data/devicesOutput.json +++ /dev/null @@ -1 +0,0 @@ -[{"id":"087c830b-1173-4b5b-bdbc-c139856cf6aa","externalDomain":["kpn-lora","modelapi"],"externalId":"70B3D57050004C5A","name":"70B3D57050004C5A","description":"6kg sensor","additionalInfo":{"location":{"latitude":51.289406,"longitude":6.042}},"deviceTemplateId":"7be8a213-65e9-4084-b73b-9d5cfcc46cc0","organizationId":"46224b7f-bf6e-4a14-8844-c8d9258a58bf"},{"id":"09d69136-7bdb-420e-83eb-8185d39b06c3","externalDomain":["kpn-lora","modelapi"],"externalId":"70B3D570500046AA","name":"70B3D570500046AA","description":"6kg sensor","additionalInfo":{},"deviceTemplateId":"7be8a213-65e9-4084-b73b-9d5cfcc46cc0","organizationId":"46224b7f-bf6e-4a14-8844-c8d9258a58bf"},{"id":"142ad259-c570-49cc-bf58-99105231c051","externalDomain":["kpn-lora","modelapi"],"externalId":"70B3D57050004657","name":"70B3D57050004657","description":"6kg sensor","additionalInfo":{},"deviceTemplateId":"7be8a213-65e9-4084-b73b-9d5cfcc46cc0","organizationId":"46224b7f-bf6e-4a14-8844-c8d9258a58bf"},{"id":"1f6184e2-3cfb-43e7-83a4-ea175c8590f4","externalDomain":["kpn-lora","modelapi"],"externalId":"70B3D5705000473D","name":"70B3D5705000473D","description":"","additionalInfo":{},"deviceTemplateId":"7be8a213-65e9-4084-b73b-9d5cfcc46cc0","organizationId":"46224b7f-bf6e-4a14-8844-c8d9258a58bf"},{"id":"21c94566-202d-4c12-af0d-035b8db19d32","externalDomain":["kpn-lora","modelapi"],"externalId":"70B3D57050004A7B","name":"70B3D57050004A7B","description":"","additionalInfo":{},"deviceTemplateId":"7be8a213-65e9-4084-b73b-9d5cfcc46cc0","organizationId":"46224b7f-bf6e-4a14-8844-c8d9258a58bf"},{"id":"98e242b8-e49d-4eb5-a56e-a5665c05b3a3","externalDomain":["kpn-lora","modelapi"],"externalId":"70B3D57050004619","name":"70B3D57050004619","description":"","additionalInfo":{},"deviceTemplateId":"7be8a213-65e9-4084-b73b-9d5cfcc46cc0","organizationId":"46224b7f-bf6e-4a14-8844-c8d9258a58bf"},{"id":"b4f3ca19-1648-4df1-a83d-089c8064a6ff","externalDomain":["kpn-lora","modelapi"],"externalId":"70B3D57050004636","name":"70B3D57050004636","description":"6kg sensor","additionalInfo":{},"deviceTemplateId":"7be8a213-65e9-4084-b73b-9d5cfcc46cc0","organizationId":"46224b7f-bf6e-4a14-8844-c8d9258a58bf"},{"id":"b61c30d1-9281-4fc7-ab70-a43861fe0264","externalDomain":["kpn-lora","modelapi"],"externalId":"70B3D5705000471F","name":"70B3D5705000471F","description":"","additionalInfo":{},"deviceTemplateId":"7be8a213-65e9-4084-b73b-9d5cfcc46cc0","organizationId":"46224b7f-bf6e-4a14-8844-c8d9258a58bf"},{"id":"c97e36da-8274-43aa-a226-7e1bf8b943b0","externalDomain":["kpn-lora","modelapi"],"externalId":"70B3D5705000473C","name":"70B3D5705000473C","description":"","additionalInfo":{},"deviceTemplateId":"7be8a213-65e9-4084-b73b-9d5cfcc46cc0","organizationId":"46224b7f-bf6e-4a14-8844-c8d9258a58bf"},{"id":"e5728985-f4f5-4b9a-ae32-9d6ae40f9f53","externalDomain":["kpn-lora","modelapi"],"externalId":"70B3D5705000461C","name":"70B3D5705000461C","description":"6kg sensor","additionalInfo":{"location":{"latitude":51.28905,"longitude":6.042781}},"deviceTemplateId":"7be8a213-65e9-4084-b73b-9d5cfcc46cc0","organizationId":"46224b7f-bf6e-4a14-8844-c8d9258a58bf"}] \ No newline at end of file diff --git a/src/main/java/Data/readAll.json b/src/main/java/Data/readAll.json deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/java/Data/securityToken.txt b/src/main/java/Data/securityToken.txt index e979737..c3a8a1e 100644 --- a/src/main/java/Data/securityToken.txt +++ b/src/main/java/Data/securityToken.txt @@ -1 +1 @@ -ZcUXmzWkBB5eJSzcllxFlUpgWh7ujC2T6TdB9YdXr5ddERrjiHsp5rmPYrpG5sCU \ No newline at end of file +OnomznAUvxd8lmIUu7FRiIkfYdoUCGN1x84B4AjS9yE4TW3xLpmh2rOxlBtBD0IJ \ No newline at end of file diff --git a/src/main/java/Data/sensorOutput.json b/src/main/java/Data/sensorOutput.json deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/java/HttpCall/HttpCall.java b/src/main/java/HttpCall/HttpCall.java index 7ba3605..bbbb548 100644 --- a/src/main/java/HttpCall/HttpCall.java +++ b/src/main/java/HttpCall/HttpCall.java @@ -23,7 +23,7 @@ public class HttpCall { // .setHeader("access_token", token) .setHeader("Content-Type", "application/json") .setHeader("Accept", "application/json") - .header("Authority", "https://api.dev.whysor.com/") + .header("Authority", "https://api.whysor.com/") .build(); } catch (URISyntaxException e) { diff --git a/src/main/java/HttpCall/HttpPost.java b/src/main/java/HttpCall/HttpPost.java index 6cdce9b..25e370e 100644 --- a/src/main/java/HttpCall/HttpPost.java +++ b/src/main/java/HttpCall/HttpPost.java @@ -21,7 +21,7 @@ public class HttpPost { .POST(HttpRequest.BodyPublishers.ofString(body)) .setHeader("Content-Type", "application/json") .setHeader("Accept", "application/json") - .header("Authority", "https://api.dev.whysor.com/") + .header("Authority", "https://api.whysor.com/") .build(); } catch (URISyntaxException e) { diff --git a/src/main/java/JSON_Unpack/BoxCall4Partner.java b/src/main/java/JSON_Unpack/BoxCall4Partner.java new file mode 100644 index 0000000..94d6561 --- /dev/null +++ b/src/main/java/JSON_Unpack/BoxCall4Partner.java @@ -0,0 +1,71 @@ +package JSON_Unpack; + +import Credentials.UserCredentials; +import Files.ReadFile; +import Files.WriteFile; +import HttpCall.HttpCall; +import org.json.JSONArray; +import org.json.JSONObject; + +import java.util.ArrayList; + +// Get sensor from device for each partner +public class BoxCall4Partner { + + public String FetchBox(String companyName, String token) throws Exception { + + switch (companyName) { + case "Germes": + return String.valueOf(onCallMethod("Germes", token)); + case "Heufs": + return String.valueOf(onCallMethod("Heufs", token)); + case "Jacobs": + return String.valueOf(onCallMethod("Jacobs", token)); + case "Nica": + return String.valueOf(onCallMethod("Nica", token)); + } + return null; + } + + protected ArrayList onCallMethod(String cName, String token) throws Exception { + ReadFile readFile = new ReadFile(); + UserCredentials userCredentials = new UserCredentials(); + HttpCall httpCall = new HttpCall(); + WriteFile writingIntoFile = new WriteFile(); + + String baseURL = userCredentials.getBaseURL(); + String homeRoot = userCredentials.getHomeRoot(); + + String outputGeneral = null; + outputGeneral = GeneralCall4All.FetchAll(token); + JSONArray jsonarray = new JSONArray(outputGeneral); + + cName = readFile.ReadFromFile(homeRoot +cName +".txt"); + ArrayList listOfCompanyOutputs=new ArrayList(); + + String[] arrayOfCompany = cName.split(","); + int loopTerminator = 0; + for (String iterator : arrayOfCompany) { + for (int i = 0; i < jsonarray.length(); i++) { + JSONObject jsonobject = jsonarray.getJSONObject(i); + String jsonExternalID = jsonobject.getString("externalId"); + + if (iterator.equals(jsonExternalID)) { + String jsonID = jsonobject.getString("id"); + String companyOutput = httpCall.HttpGet(baseURL + "devices/" + jsonID + "/sensors" + "?access_token=" + token, token); + listOfCompanyOutputs.add(companyOutput); + writingIntoFile.WriteFile(homeRoot +i+" "+ cName + ".json", companyOutput); + loopTerminator++; + } + + if(loopTerminator == arrayOfCompany.length ){ + return listOfCompanyOutputs; + } + + } + } + return null; + } + + +} diff --git a/src/main/java/JSON_Unpack/GeneralCall4All.java b/src/main/java/JSON_Unpack/GeneralCall4All.java new file mode 100644 index 0000000..e7cb6f0 --- /dev/null +++ b/src/main/java/JSON_Unpack/GeneralCall4All.java @@ -0,0 +1,23 @@ +package JSON_Unpack; + +import Credentials.UserCredentials; +import Files.WriteFile; +import HttpCall.HttpCall; +import org.json.JSONArray; +//Get a list of all devices +public class GeneralCall4All { + public static String FetchAll(String token) throws Exception { + String extension = "devices"; + UserCredentials userCredentials = new UserCredentials(); + WriteFile writingIntoFile = new WriteFile(); + + String baseURL = userCredentials.getBaseURL(); + String homeRoot = UserCredentials.getHomeRoot(); + HttpCall httpCall = new HttpCall(); + + String fetchAll = httpCall.HttpGet(baseURL + extension + "?access_token=" + token,token); + writingIntoFile.WriteFile(homeRoot + extension + "Output.json", fetchAll); + + return fetchAll; + } +} diff --git a/src/main/java/JSON_Unpack/ReadSensors4Partner.java b/src/main/java/JSON_Unpack/ReadSensors4Partner.java new file mode 100644 index 0000000..3c1772f --- /dev/null +++ b/src/main/java/JSON_Unpack/ReadSensors4Partner.java @@ -0,0 +1,68 @@ + +package JSON_Unpack; + +import Credentials.UserCredentials; +import Files.WriteFile; +import HttpCall.HttpCall; +import org.json.JSONArray; +import org.json.JSONObject; + +import java.util.ArrayList; + +///url/sensors/id/read + +public class ReadSensors4Partner { + + + + public String FetchSensor4Box(String companyName, String token) throws Exception { + + switch (companyName) { + case "Germes": + return String.valueOf(SensorRead("Germes", token)); + case "Heufs": + return String.valueOf(SensorRead("Heufs", token)); + case "Jacobs": + return String.valueOf(SensorRead("Jacobs", token)); + case "Nica": + return String.valueOf(SensorRead("Nica", token)); + } + return null; + } + + protected ArrayList SensorRead(String cName, String token) throws Exception { + UserCredentials userCredentials = new UserCredentials(); + HttpCall httpCall = new HttpCall(); + WriteFile writingIntoFile = new WriteFile(); + + String baseURL = userCredentials.getBaseURL(); + String homeRoot = userCredentials.getHomeRoot(); + + BoxCall4Partner boxCall4Partner = new BoxCall4Partner(); + String outputBox = boxCall4Partner.FetchBox(cName,token); + String outputBoxFixed=outputBox.replace("[[","[") + outputBox.replace("]]","]"); // + JSONArray jsonArray = new JSONArray(outputBoxFixed); + + ArrayList list=new ArrayList(); + int loopTerminator = 0; + + for(int j=0; j