69 lines
2.6 KiB
Java
69 lines
2.6 KiB
Java
|
|
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("]]","]");// To convert in Jsonarray we need to remove the array list brackets
|
|
JSONArray jsonArray = new JSONArray(outputBoxFixed);
|
|
System.out.println("The working array:"+jsonArray);
|
|
ArrayList<String> list=new ArrayList<String>();
|
|
int loopTerminator = 0;
|
|
char par = '"';
|
|
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);
|
|
String sensorTag= par+"sensortag"+par+":"+jsonTag+"}]";
|
|
String makingList=sensorOutput.replace("}]","") ;
|
|
list.add(makingList+","+sensorTag); // This modification of the list is done for the purpse of converting this list to a jsonarray.
|
|
|
|
writingIntoFile.WriteFile(homeRoot + jsonID + ".json", sensorOutput + jsonTag);
|
|
loopTerminator++;
|
|
if(loopTerminator == jsonArray.length()){
|
|
return list;
|
|
}
|
|
}
|
|
|
|
|
|
return null;
|
|
}
|
|
|
|
|
|
}
|