forked from kevin.shehu/GGD
ADD : NEW Format for all the code with prettier
This commit is contained in:
@@ -3,75 +3,76 @@ package eu.hsrw.ias.ggd;
|
||||
import Credentials.UserCredentials;
|
||||
import Files.WriteFile;
|
||||
import JSON_Unpack.ReadSensors4Partner;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class ApiCall {
|
||||
private static String token = "0";
|
||||
private static String oldId = "1";
|
||||
|
||||
private static String token = "0";
|
||||
private static String oldId = "1";
|
||||
|
||||
public static HashMap<String, SensorData> FetchDataFromApi() throws Exception {
|
||||
public static Greenhouse FetchDataFromApi() throws Exception {
|
||||
UserCredentials userCredentials = new UserCredentials();
|
||||
final String username = userCredentials.getUsername();
|
||||
final String password = userCredentials.getPassword();
|
||||
final String homeRoot = UserCredentials.getHomeRoot();
|
||||
String outputToken;
|
||||
WriteFile writingIntoFile = new WriteFile();
|
||||
Date now; // to display current time
|
||||
now = new Date();
|
||||
HashMap<String, Device> devices = new HashMap();
|
||||
|
||||
if (token.equals(oldId)) {
|
||||
//do nothing
|
||||
} else {
|
||||
try {
|
||||
outputToken = SecurityToken.OnCallMethod(username, password);
|
||||
JSONObject obj = new JSONObject(outputToken);
|
||||
token = obj.getString("id");
|
||||
|
||||
UserCredentials userCredentials = new UserCredentials();
|
||||
final String username = userCredentials.getUsername();
|
||||
final String password = userCredentials.getPassword();
|
||||
final String homeRoot = UserCredentials.getHomeRoot();
|
||||
String outputToken;
|
||||
WriteFile writingIntoFile = new WriteFile();
|
||||
Date now; // to display current time
|
||||
now = new Date();
|
||||
HashMap<String,
|
||||
SensorData> finalOutput = new HashMap();
|
||||
if (token.equals(oldId)) {
|
||||
//do nothing
|
||||
} else {
|
||||
try {
|
||||
outputToken = SecurityToken.OnCallMethod(username, password);
|
||||
JSONObject obj = new JSONObject(outputToken);
|
||||
token = obj.getString("id");
|
||||
|
||||
writingIntoFile.WriteFile(homeRoot + "securityToken.txt", token);
|
||||
oldId = token;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
ReadSensors4Partner readSensors4Partner = new ReadSensors4Partner();
|
||||
ArrayList<String> outputValue = (readSensors4Partner.FetchSensor4Box("Germes", token));
|
||||
for(int i=0;i<outputValue.size();i++){
|
||||
Device device;
|
||||
JSONArray sensorDatas = new JSONArray(outputValue.get(i));
|
||||
|
||||
for(int j=0; j<sensorDatas.length(); j++) { //15
|
||||
JSONObject sensorData = sensorDatas.getJSONObject(j);
|
||||
|
||||
String deviceId = sensorData.getString("deviceId");
|
||||
|
||||
double value = sensorData.getDouble("value");
|
||||
String sensorTag = sensorData.getString("sensorTag");
|
||||
String sensorId = sensorData.getString("sensorId");
|
||||
|
||||
SensorData sd = new SensorData(sensorId, value, sensorTag);
|
||||
|
||||
if(devices.containsKey(deviceId)) {
|
||||
device = devices.get(deviceId);
|
||||
device.addSensorData(sd);
|
||||
} else {
|
||||
device = new Device(deviceId);
|
||||
device.addSensorData(sd);
|
||||
devices.put(deviceId, device);
|
||||
}
|
||||
}
|
||||
}
|
||||
return finalOutput;
|
||||
|
||||
writingIntoFile.WriteFile(homeRoot + "securityToken.txt", token);
|
||||
oldId = token;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
ReadSensors4Partner readSensors4Partner = new ReadSensors4Partner();
|
||||
ArrayList<String> outputValue =
|
||||
(readSensors4Partner.FetchSensor4Box("Germes", token));
|
||||
|
||||
for (int i = 0; i < outputValue.size(); i++) {
|
||||
Device device;
|
||||
JSONArray sensorDatas = new JSONArray(outputValue.get(i));
|
||||
|
||||
for (int j = 0; j < sensorDatas.length(); j++) { //15
|
||||
JSONObject sensorData = sensorDatas.getJSONObject(j);
|
||||
|
||||
String deviceId = sensorData.getString("deviceId");
|
||||
|
||||
double value = sensorData.getDouble("value");
|
||||
String sensorTag = sensorData.getString("sensorTag");
|
||||
String sensorId = sensorData.getString("sensorId");
|
||||
|
||||
SensorData sd = new SensorData(sensorId, value, sensorTag);
|
||||
|
||||
if (devices.containsKey(deviceId)) {
|
||||
device = devices.get(deviceId);
|
||||
device.addSensorData(sd);
|
||||
} else {
|
||||
device = new Device(deviceId);
|
||||
device.addSensorData(sd);
|
||||
devices.put(deviceId, device);
|
||||
}
|
||||
}
|
||||
}
|
||||
Greenhouse greenhouse = new Greenhouse();
|
||||
for (Device device : devices.values()) {
|
||||
greenhouse.addDevice(device);
|
||||
}
|
||||
return greenhouse;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,80 +4,102 @@ import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class Device {
|
||||
private String id;
|
||||
private List<SensorData> data;
|
||||
|
||||
public Device(String identifier) {
|
||||
this.id = identifier;
|
||||
this.data = new LinkedList<SensorData>();
|
||||
private String id;
|
||||
private String externalID;
|
||||
private List<SensorData> data;
|
||||
private Severity state = Severity.Unknown;
|
||||
|
||||
public Device(String identifier) {
|
||||
this.id = identifier;
|
||||
this.data = new LinkedList<SensorData>();
|
||||
}
|
||||
|
||||
public void addSensorData(SensorData d) {
|
||||
this.data.add(d);
|
||||
}
|
||||
|
||||
public List<SensorData> getSensorData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String setId(String id) {
|
||||
this.id = id;
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setExternalID(String externalID) {
|
||||
this.externalID = externalID;
|
||||
}
|
||||
|
||||
public String getExternalID() {
|
||||
return externalID;
|
||||
}
|
||||
|
||||
public String IdMapper(String id) {
|
||||
switch (id) {
|
||||
case "fd694041-581e-4c2c-9810-505e62b762e6":
|
||||
return setId("70B3D570500042D7");
|
||||
case "e8c3fca3-c5cd-48e7-9224-0f8c294fa3c0":
|
||||
return setId("70B3D57050004EF4");
|
||||
case "f9bb4a5c-f783-4bcf-8c34-0a7c3dd5a632":
|
||||
return setId("70B3D57050006223");
|
||||
case "6c9efd4a-db04-453d-8ab8-d480ec97dd26":
|
||||
return setId("70B3D5705000623C");
|
||||
default:
|
||||
return "NO Working id was found";
|
||||
}
|
||||
}
|
||||
|
||||
public Boolean hasTag(String tag) {
|
||||
for (SensorData sensorData : data) {
|
||||
if (sensorData.getTag() == tag) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public SensorData getSensorDataByTag(String tag) {
|
||||
for (SensorData sd : data) {
|
||||
if (sd.getTag() == tag) return sd;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public SensorData getSensorDataById(String id) {
|
||||
for (SensorData sd : data) {
|
||||
if (sd.getSensorId() == id) return sd;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
String str = "Device: '" + id + "', data: [";
|
||||
|
||||
for (SensorData sd : data) {
|
||||
str += "SensorData(id: '" + sd.getSensorId() + "',";
|
||||
str += " tag: '" + sd.getTag() + "',";
|
||||
str += " value: " + sd.getValue() + ")";
|
||||
}
|
||||
|
||||
public void addSensorData(SensorData d) {
|
||||
this.data.add(d);
|
||||
}
|
||||
return str + "]";
|
||||
}
|
||||
|
||||
public List<SensorData> getSensorData() {
|
||||
return data;
|
||||
}
|
||||
public Device(String id, List<SensorData> data) {
|
||||
this.id = id;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
public void setState(Severity state) {
|
||||
if (this.state != Severity.Critical) {
|
||||
this.state = state;
|
||||
}
|
||||
}
|
||||
|
||||
public String setId(String id) {
|
||||
this.id = id;
|
||||
return id;
|
||||
}
|
||||
public String IdMapper(String id){
|
||||
switch (id) {
|
||||
case "fd694041-581e-4c2c-9810-505e62b762e6":
|
||||
return setId("70B3D570500042D7");
|
||||
case "e8c3fca3-c5cd-48e7-9224-0f8c294fa3c0":
|
||||
return setId("70B3D57050004EF4");
|
||||
case "f9bb4a5c-f783-4bcf-8c34-0a7c3dd5a632":
|
||||
return setId("70B3D57050006223");
|
||||
case "6c9efd4a-db04-453d-8ab8-d480ec97dd26":
|
||||
return setId("70B3D5705000623C");
|
||||
default:
|
||||
return "NO Working id was found";
|
||||
}
|
||||
}
|
||||
|
||||
public Boolean hasTag(String tag) {
|
||||
for (SensorData sensorData : data) {
|
||||
if (sensorData.getTag() == tag) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public SensorData getSensorDataByTag(String tag) {
|
||||
for (SensorData sd : data) {
|
||||
if (sd.getTag() == tag) return sd;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public SensorData getSensorDataById(String id) {
|
||||
for (SensorData sd : data) {
|
||||
if (sd.getSensorId() == id) return sd;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
String str = "Device: '"+ id +"', data: [";
|
||||
|
||||
for(SensorData sd: data) {
|
||||
str += "SensorData(id: '"+ sd.getSensorId() +"',";
|
||||
str += " tag: '"+ sd.getTag() +"',";
|
||||
str += " value: "+ sd.getValue() +")";
|
||||
}
|
||||
|
||||
return str + "]";
|
||||
}
|
||||
|
||||
public Device(String id, List<SensorData> data) {
|
||||
this.id = id;
|
||||
this.data = data;
|
||||
}
|
||||
public Severity getState() {
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
||||
50
src/main/java/eu/hsrw/ias/ggd/Greenhouse.java
Normal file
50
src/main/java/eu/hsrw/ias/ggd/Greenhouse.java
Normal file
@@ -0,0 +1,50 @@
|
||||
package eu.hsrw.ias.ggd;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Greenhouse {
|
||||
|
||||
private String id = "8bdbe6ae-eafb-4e99-bb01-db8784dd9633";
|
||||
private Severity state = Severity.Unknown;
|
||||
|
||||
public Greenhouse() {
|
||||
this.devices = new ArrayList<>();
|
||||
}
|
||||
|
||||
public Greenhouse(List<Device> devices) {
|
||||
this.devices = devices;
|
||||
}
|
||||
|
||||
public void addDevice(Device device) {
|
||||
this.devices.add(device);
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Severity getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public void setState(Severity state) {
|
||||
if (this.state != Severity.Critical) {
|
||||
this.state = state;
|
||||
}
|
||||
}
|
||||
|
||||
public List<Device> getDevices() {
|
||||
return devices;
|
||||
}
|
||||
|
||||
public void setDevices(List<Device> devices) {
|
||||
this.devices = devices;
|
||||
}
|
||||
|
||||
private List<Device> devices;
|
||||
}
|
||||
@@ -3,23 +3,30 @@ package eu.hsrw.ias.ggd;
|
||||
import HttpCall.HttpPost;
|
||||
|
||||
public class Isis {
|
||||
public static String OnCallMethod(String deviceName, String sensorName) throws Exception {
|
||||
//CONFIG PARAMETERS:
|
||||
//BEGIN------------CONFIG PARAMETERS BELOW TO YOUR ENVIRONMENT---------------------------------------
|
||||
String baseURL = "https://api.dev.whysor.com/devices/readValueFromSpeech?access_token=2MreqC9fqXxFHJBEcrCL38LKhEaVcEHmEi6o45CwTV9SSgTAuZ93oiMfY3HVc2fj";
|
||||
final String body = String.format("{\"deviceName\": \"%s\", \"sensorName\": \"%s\", \"domain\": \"my.yookr.org\"}", deviceName, sensorName);
|
||||
HttpPost httpMethodPost = new HttpPost();
|
||||
httpMethodPost.HttpCallPost(baseURL,body);
|
||||
|
||||
return httpMethodPost.HttpCallPost(baseURL, body);
|
||||
}
|
||||
public static void main(String[] args){
|
||||
try {
|
||||
String output = OnCallMethod("Greenhouse 1", "status");
|
||||
System.out.println(output);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
public static String OnCallMethod(String deviceName, String sensorName)
|
||||
throws Exception {
|
||||
//CONFIG PARAMETERS:
|
||||
//BEGIN------------CONFIG PARAMETERS BELOW TO YOUR ENVIRONMENT---------------------------------------
|
||||
String baseURL =
|
||||
"https://api.dev.whysor.com/devices/readValueFromSpeech?access_token=2MreqC9fqXxFHJBEcrCL38LKhEaVcEHmEi6o45CwTV9SSgTAuZ93oiMfY3HVc2fj";
|
||||
final String body = String.format(
|
||||
"{\"deviceName\": \"%s\", \"sensorName\": \"%s\", \"domain\": \"my.yookr.org\"}",
|
||||
deviceName,
|
||||
sensorName
|
||||
);
|
||||
HttpPost httpMethodPost = new HttpPost();
|
||||
httpMethodPost.HttpCallPost(baseURL, body);
|
||||
|
||||
return httpMethodPost.HttpCallPost(baseURL, body);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
String output = OnCallMethod("Greenhouse 1", "status");
|
||||
System.out.println(output);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
package eu.hsrw.ias.ggd;
|
||||
|
||||
import java.util.Timer;
|
||||
|
||||
//Before pushing files to the git run : npx prettier --write "**/*.java"
|
||||
public class MainExe {
|
||||
public static void main(String[] args){
|
||||
|
||||
Timer time = new Timer(); // Instantiate Timer Object
|
||||
ScheduledTask scheduledTask = new ScheduledTask(); // Instantiate SheduledTask class
|
||||
time.schedule(scheduledTask, 0, 1000 * 60 * 1 ); // Create Repetitively task for every 2 min
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Timer time = new Timer(); // Instantiate Timer Object
|
||||
ScheduledTask scheduledTask = new ScheduledTask(); // Instantiate SheduledTask class
|
||||
time.schedule(scheduledTask, 0, 1000 * 60 * 1); // Create Repetitively task for every 2 min
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,37 +1,38 @@
|
||||
package eu.hsrw.ias.ggd;
|
||||
|
||||
public class Notification {
|
||||
private Severity severity = Severity.Optimal;
|
||||
private String type;
|
||||
private SensorData cause;
|
||||
|
||||
public Notification(String t, SensorData d, Severity s) {
|
||||
this.type = t;
|
||||
this.cause = d;
|
||||
this.severity = s;
|
||||
}
|
||||
private Severity severity = Severity.Optimal;
|
||||
private String type;
|
||||
private SensorData cause;
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
public Notification(String t, SensorData d, Severity s) {
|
||||
this.type = t;
|
||||
this.cause = d;
|
||||
this.severity = s;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public SensorData getCause() {
|
||||
return cause;
|
||||
}
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public void setCause(SensorData cause) {
|
||||
this.cause = cause;
|
||||
}
|
||||
public SensorData getCause() {
|
||||
return cause;
|
||||
}
|
||||
|
||||
public Severity getSeverity() {
|
||||
return this.severity;
|
||||
}
|
||||
public void setCause(SensorData cause) {
|
||||
this.cause = cause;
|
||||
}
|
||||
|
||||
public void setSeverity(Severity severity) {
|
||||
this.severity = severity;
|
||||
}
|
||||
public Severity getSeverity() {
|
||||
return this.severity;
|
||||
}
|
||||
|
||||
public void setSeverity(Severity severity) {
|
||||
this.severity = severity;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,23 +1,23 @@
|
||||
package eu.hsrw.ias.ggd;
|
||||
|
||||
import HttpCall.HttpPost;
|
||||
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
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.whysor.com/users/login";
|
||||
String devURL = "https://api.dev.whysor.com/users/login";
|
||||
final String body = String.format("{\"email\": \"%s\", \"password\": \"%s\", \"domain\": \"my.dev.yookr.org\"}", username, password);
|
||||
HttpPost httpMethodPost = new HttpPost();
|
||||
return httpMethodPost.HttpCallPost(devURL, body);
|
||||
}
|
||||
|
||||
public static String OnCallMethod(String username, String password)
|
||||
throws Exception {
|
||||
//CONFIG PARAMETERS:
|
||||
//BEGIN------------CONFIG PARAMETERS BELOW TO YOUR ENVIRONMENT---------------------------------------
|
||||
String baseURL = "https://api.whysor.com/users/login";
|
||||
String devURL = "https://api.dev.whysor.com/users/login";
|
||||
final String body = String.format(
|
||||
"{\"email\": \"%s\", \"password\": \"%s\", \"domain\": \"my.dev.yookr.org\"}",
|
||||
username,
|
||||
password
|
||||
);
|
||||
HttpPost httpMethodPost = new HttpPost();
|
||||
return httpMethodPost.HttpCallPost(devURL, body);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -2,34 +2,48 @@ package eu.hsrw.ias.ggd;
|
||||
|
||||
public class SensorData {
|
||||
|
||||
private String sensorId;
|
||||
private double value;
|
||||
private String deviceId;
|
||||
private String sensorId;
|
||||
private String tag;
|
||||
private double value;
|
||||
private Severity state = Severity.Unknown;
|
||||
|
||||
public SensorData(String sensorId, double value,String deviceId) {
|
||||
this.sensorId = sensorId;
|
||||
this.value = value;
|
||||
this.deviceId = deviceId;
|
||||
}
|
||||
|
||||
public String getSensorTag() {
|
||||
return sensorId;
|
||||
}
|
||||
|
||||
public void setSensorTag(String sensorTag) {
|
||||
this.sensorId = sensorTag;
|
||||
}
|
||||
|
||||
public double getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(double value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getDeviceId() {return deviceId;}
|
||||
|
||||
public void setDeviceId(String deviceTag){ this.deviceId = deviceTag;
|
||||
public SensorData(String sensorId, double value, String tag) {
|
||||
this.sensorId = sensorId;
|
||||
this.value = value;
|
||||
this.tag = tag;
|
||||
}
|
||||
|
||||
public String getTag() {
|
||||
return tag;
|
||||
}
|
||||
|
||||
public void setTag(String tag) {
|
||||
this.tag = tag;
|
||||
}
|
||||
|
||||
public double getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(double value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getSensorId() {
|
||||
return sensorId;
|
||||
}
|
||||
|
||||
public void setSensorId(String id) {
|
||||
this.sensorId = id;
|
||||
}
|
||||
|
||||
public Severity getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public void setState(Severity state) {
|
||||
if (this.state != Severity.Critical) {
|
||||
this.state = state;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,15 +3,14 @@ package eu.hsrw.ias.ggd;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class SensorMap {
|
||||
private HashMap<String, Double> sensors = new HashMap<String, Double>();
|
||||
|
||||
public HashMap<String, Double> getSensors() {
|
||||
return sensors;
|
||||
}
|
||||
|
||||
public void setSensors(HashMap<String, Double> sensors) {
|
||||
this.sensors = sensors;
|
||||
}
|
||||
private HashMap<String, Double> sensors = new HashMap<String, Double>();
|
||||
|
||||
public HashMap<String, Double> getSensors() {
|
||||
return sensors;
|
||||
}
|
||||
|
||||
public void setSensors(HashMap<String, Double> sensors) {
|
||||
this.sensors = sensors;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
package eu.hsrw.ias.ggd;
|
||||
|
||||
public enum Severity {
|
||||
Optimal,
|
||||
Info,
|
||||
Warning,
|
||||
Error,
|
||||
Critical
|
||||
}
|
||||
Optimal,
|
||||
Info,
|
||||
Warning,
|
||||
Error,
|
||||
Critical,
|
||||
Unknown,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user