FIX : ApiCall.java

ADD : Json_Unpack
TODO : KnowledgeBase
This commit is contained in:
2021-06-12 00:37:45 +02:00
parent e51ce37fe3
commit 39251400d7
21 changed files with 204 additions and 86 deletions

View 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;
}
}

View 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;
}
}

View 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;
}
}

View File

@@ -0,0 +1,4 @@
package JSON_Unpack;
public class Reading4AllSensors {
}