forked from kevin.shehu/GGD
NEW : REBASE THE ENTIRE WORKING PROJECT
This commit is contained in:
@@ -1,78 +0,0 @@
|
||||
package eu.hsrw.ias.ggd;
|
||||
|
||||
import Credentials.UserCredentials;
|
||||
import Files.WriteFile;
|
||||
import JSON_Unpack.ReadSensors4Partner;
|
||||
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";
|
||||
|
||||
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");
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,105 +0,0 @@
|
||||
package eu.hsrw.ias.ggd;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class Device {
|
||||
|
||||
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() + ")";
|
||||
}
|
||||
|
||||
return str + "]";
|
||||
}
|
||||
|
||||
public Device(String id, List<SensorData> data) {
|
||||
this.id = id;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public void setState(Severity state) {
|
||||
if (this.state != Severity.Critical) {
|
||||
this.state = state;
|
||||
}
|
||||
}
|
||||
|
||||
public Severity getState() {
|
||||
return state;
|
||||
}
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
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;
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
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;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public SensorData getCause() {
|
||||
return cause;
|
||||
}
|
||||
|
||||
public void setCause(SensorData cause) {
|
||||
this.cause = cause;
|
||||
}
|
||||
|
||||
public Severity getSeverity() {
|
||||
return this.severity;
|
||||
}
|
||||
|
||||
public void setSeverity(Severity severity) {
|
||||
this.severity = severity;
|
||||
}
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
package eu.hsrw.ias.ggd;
|
||||
|
||||
import org.kie.api.KieServices;
|
||||
import org.kie.api.runtime.KieContainer;
|
||||
import org.kie.api.runtime.KieSession;
|
||||
import org.kie.api.runtime.rule.FactHandle;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.TimerTask;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
// Create a class extends with TimerTask
|
||||
public class ScheduledTask extends TimerTask {
|
||||
|
||||
Date now; // to display current time
|
||||
HashMap<String, SensorData> outputDevices ;
|
||||
|
||||
// Add your task here
|
||||
public void run() {
|
||||
// Display current time
|
||||
now = new Date(); // initialize date
|
||||
System.out.println("Time is :" + now);
|
||||
|
||||
try {
|
||||
outputDevices = ApiCall.FetchDataFromApi();
|
||||
System.out.println("Final Size"+outputDevices.size()+outputDevices);
|
||||
KieServices ks = KieServices.Factory.get();
|
||||
KieContainer kContainer = ks.getKieClasspathContainer();
|
||||
//Get the session named kseesion-rule that we defined in kmodule.xml above.
|
||||
//Also by default the session returned is always stateful.
|
||||
KieSession kSession = kContainer.newKieSession("ksession-rule");
|
||||
FactHandle fact1;
|
||||
fact1 = kSession.insert(outputDevices);
|
||||
// System.out.println(fact1);
|
||||
// kSession.insert(fact1);
|
||||
kSession.fireAllRules();
|
||||
|
||||
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
} catch (TimeoutException e) {
|
||||
e.printStackTrace();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package eu.hsrw.ias.ggd;
|
||||
|
||||
public enum Severity {
|
||||
Optimal,
|
||||
Info,
|
||||
Warning,
|
||||
Error,
|
||||
Critical,
|
||||
Unknown,
|
||||
}
|
||||
Reference in New Issue
Block a user