FIX : ApiCall.java
ADD : Json_Unpack TODO : KnowledgeBase
This commit is contained in:
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
|
@@ -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{" +
|
||||
|
1
src/main/java/Data/Germes.txt
Normal file
1
src/main/java/Data/Germes.txt
Normal file
@@ -0,0 +1 @@
|
||||
70B3D57050004223,70B3D570500042D7,70B3D57050004EEF,70B3D57050006223,70B3D5705000623C
|
1
src/main/java/Data/Heufs.txt
Normal file
1
src/main/java/Data/Heufs.txt
Normal file
@@ -0,0 +1 @@
|
||||
70B3D57050004682,70B3D570500046AB,70B3D57050004D14
|
1
src/main/java/Data/Jacobs.txt
Normal file
1
src/main/java/Data/Jacobs.txt
Normal file
@@ -0,0 +1 @@
|
||||
0004A30B00227D68,70B3D5705000E019
|
1
src/main/java/Data/Nica.txt
Normal file
1
src/main/java/Data/Nica.txt
Normal file
@@ -0,0 +1 @@
|
||||
70B3D5705000E030,70B3D5705000E05E,70B3D5705000E386
|
@@ -1 +0,0 @@
|
||||
e5728985-f4f5-4b9a-ae32-9d6ae40f9f53
|
@@ -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"}]
|
@@ -1 +1 @@
|
||||
ZcUXmzWkBB5eJSzcllxFlUpgWh7ujC2T6TdB9YdXr5ddERrjiHsp5rmPYrpG5sCU
|
||||
OnomznAUvxd8lmIUu7FRiIkfYdoUCGN1x84B4AjS9yE4TW3xLpmh2rOxlBtBD0IJ
|
@@ -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) {
|
||||
|
@@ -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) {
|
||||
|
71
src/main/java/JSON_Unpack/BoxCall4Partner.java
Normal file
71
src/main/java/JSON_Unpack/BoxCall4Partner.java
Normal file
@@ -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<String> 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<String> listOfCompanyOutputs=new ArrayList<String>();
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
23
src/main/java/JSON_Unpack/GeneralCall4All.java
Normal file
23
src/main/java/JSON_Unpack/GeneralCall4All.java
Normal file
@@ -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;
|
||||
}
|
||||
}
|
68
src/main/java/JSON_Unpack/ReadSensors4Partner.java
Normal file
68
src/main/java/JSON_Unpack/ReadSensors4Partner.java
Normal file
@@ -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<String> 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<String> list=new ArrayList<String>();
|
||||
int loopTerminator = 0;
|
||||
|
||||
for(int j=0; j<jsonArray.length(); j++){
|
||||
JSONObject jsonobject = jsonArray.getJSONObject(j);
|
||||
String jsonID = jsonobject.getString("id");
|
||||
String jsonTag = jsonobject.getString("tag");
|
||||
|
||||
String sensorOutput = httpCall.HttpGet(baseURL + "sensors/" + jsonID + "/read" + "?access_token=" + token, token);
|
||||
list.add(sensorOutput);
|
||||
writingIntoFile.WriteFile(homeRoot + jsonID + ".json", sensorOutput + jsonTag);
|
||||
loopTerminator++;
|
||||
if(loopTerminator == jsonArray.length()){
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
4
src/main/java/JSON_Unpack/Reading4AllSensors.java
Normal file
4
src/main/java/JSON_Unpack/Reading4AllSensors.java
Normal file
@@ -0,0 +1,4 @@
|
||||
package JSON_Unpack;
|
||||
|
||||
public class Reading4AllSensors {
|
||||
}
|
@@ -6,6 +6,7 @@ import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.TimerTask;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
@@ -15,14 +16,13 @@ import java.util.concurrent.TimeoutException;
|
||||
public class ScheduledTask extends TimerTask {
|
||||
|
||||
Date now; // to display current time
|
||||
BufferedWriter bw = null;
|
||||
|
||||
// Add your task here
|
||||
public void run() {
|
||||
// Display current time
|
||||
now = new Date(); // initialize date
|
||||
System.out.println("Time is :" + now);
|
||||
String outputDevices = "";
|
||||
String outputDevices = null;
|
||||
String devicesExtension = "devices";
|
||||
String sensorExtension = "sensors"; //read and readMultiple
|
||||
try {
|
||||
@@ -35,6 +35,8 @@ public class ScheduledTask extends TimerTask {
|
||||
e.printStackTrace();
|
||||
} catch (TimeoutException e) {
|
||||
e.printStackTrace();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("Output :" + outputDevices); // Display current output
|
||||
|
||||
|
@@ -7,8 +7,8 @@ public class SecurityToken {
|
||||
public static String OnCallMethod(String username, String password) throws Exception {
|
||||
//CONFIG PARAMETERS:
|
||||
//BEGIN------------CONFIG PARAMETERS BELOW TO YOUR ENVIRONMENT---------------------------------------
|
||||
String baseURL = "https://api.dev.whysor.com/users/login";
|
||||
final String body = String.format("{\"email\": \"%s\", \"password\": \"%s\", \"domain\": \"my.dev.yookr.org\"}", username, password);
|
||||
String baseURL = "https://api.whysor.com/users/login";
|
||||
final String body = String.format("{\"email\": \"%s\", \"password\": \"%s\", \"domain\": \"my.yookr.org\"}", username, password);
|
||||
HttpPost httpMethodPost = new HttpPost();
|
||||
|
||||
return httpMethodPost.HttpCallPost(baseURL, body);
|
||||
|
@@ -1,25 +0,0 @@
|
||||
import Credentials.UserCredentials;
|
||||
import Files.ReadFile;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
|
||||
public class SpecificApiCall {
|
||||
public static String FetchSpecificDataWithID(String extension) {
|
||||
|
||||
JSONParser parser = new JSONParser();
|
||||
UserCredentials userCredentials = new UserCredentials();
|
||||
ReadFile fileReader = new ReadFile();
|
||||
String token = fileReader.ReadFromFile("/Users/ksh/Documents/Work/GGD/src/MainExe/java/Data/securityToken.txt");
|
||||
|
||||
|
||||
return token;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user