forked from kevin.shehu/GGD
ADD : NEW Format for all the code with prettier
parent
64e7af7f13
commit
504baa1962
|
@ -1,11 +1,13 @@
|
||||||
package Credentials;
|
package Credentials;
|
||||||
|
|
||||||
public class UserCredentials {
|
public class UserCredentials {
|
||||||
|
|
||||||
private final String username = "kevin.shehu@hochschule-rhein-waal.de";
|
private final String username = "kevin.shehu@hochschule-rhein-waal.de";
|
||||||
private final String password = "DK7SxFkGJgnLhnU3";
|
private final String password = "DK7SxFkGJgnLhnU3";
|
||||||
private final String baseURL = "https://api.whysor.com/";
|
private final String baseURL = "https://api.whysor.com/";
|
||||||
private final String devUrl = "https://api.dev.whysor.com/";
|
private final String devUrl = "https://api.dev.whysor.com/";
|
||||||
private static final String homeRoot = "D:\\WORK\\GGD\\src\\main\\java\\Data\\";
|
private static final String homeRoot =
|
||||||
|
"D:\\WORK\\GGD\\src\\main\\java\\Data\\";
|
||||||
|
|
||||||
public static String getHomeRoot() {
|
public static String getHomeRoot() {
|
||||||
return homeRoot;
|
return homeRoot;
|
||||||
|
@ -29,11 +31,21 @@ public class UserCredentials {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "UserCredentials{" +
|
return (
|
||||||
"username='" + username + '\'' +
|
"UserCredentials{" +
|
||||||
", password='" + password + '\'' +
|
"username='" +
|
||||||
", baseURL='" + baseURL + '\'' +
|
username +
|
||||||
", devUrl='" + devUrl + '\'' +
|
'\'' +
|
||||||
'}';
|
", password='" +
|
||||||
|
password +
|
||||||
|
'\'' +
|
||||||
|
", baseURL='" +
|
||||||
|
baseURL +
|
||||||
|
'\'' +
|
||||||
|
", devUrl='" +
|
||||||
|
devUrl +
|
||||||
|
'\'' +
|
||||||
|
'}'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,10 +5,10 @@ import java.io.File; // Import the File class
|
||||||
import java.util.Scanner; // Import the Scanner class to read text files
|
import java.util.Scanner; // Import the Scanner class to read text files
|
||||||
|
|
||||||
public class ReadFile {
|
public class ReadFile {
|
||||||
|
|
||||||
public String data;
|
public String data;
|
||||||
|
|
||||||
public String ReadFromFile(String path) {
|
public String ReadFromFile(String path) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
File myObj = new File(path);
|
File myObj = new File(path);
|
||||||
Scanner myReader = new Scanner(myObj);
|
Scanner myReader = new Scanner(myObj);
|
||||||
|
@ -23,6 +23,4 @@ public class ReadFile {
|
||||||
}
|
}
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,12 +1,11 @@
|
||||||
package Files;
|
package Files;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
|
||||||
|
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.TimeoutException;
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
public class WriteFile {
|
public class WriteFile {
|
||||||
|
|
||||||
BufferedWriter buffer = null;
|
BufferedWriter buffer = null;
|
||||||
|
|
||||||
public void WriteFile(String path, String input) throws Exception {
|
public void WriteFile(String path, String input) throws Exception {
|
||||||
|
@ -19,20 +18,17 @@ public class WriteFile {
|
||||||
FileWriter fileWriter = new FileWriter(path);
|
FileWriter fileWriter = new FileWriter(path);
|
||||||
buffer = new BufferedWriter(fileWriter);
|
buffer = new BufferedWriter(fileWriter);
|
||||||
buffer.write(input);
|
buffer.write(input);
|
||||||
|
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
if (buffer != null)
|
if (buffer != null) buffer.flush();
|
||||||
buffer.flush();
|
|
||||||
// buffer.close();
|
// buffer.close();
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
System.out.println("Error in closing the BufferedWriter" + ex);
|
System.out.println("Error in closing the BufferedWriter" + ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,30 +11,36 @@ import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.TimeoutException;
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
public class HttpGet {
|
public class HttpGet {
|
||||||
public String HttpGetCall (String baseURL, String token) throws InterruptedException, ExecutionException, TimeoutException {
|
|
||||||
var client = HttpClient.newBuilder()
|
public String HttpGetCall(String baseURL, String token)
|
||||||
|
throws InterruptedException, ExecutionException, TimeoutException {
|
||||||
|
var client = HttpClient
|
||||||
|
.newBuilder()
|
||||||
.version(HttpClient.Version.HTTP_1_1)
|
.version(HttpClient.Version.HTTP_1_1)
|
||||||
.followRedirects(HttpClient.Redirect.ALWAYS)
|
.followRedirects(HttpClient.Redirect.ALWAYS)
|
||||||
.build();
|
.build();
|
||||||
HttpRequest request = null;
|
HttpRequest request = null;
|
||||||
try {
|
try {
|
||||||
request = HttpRequest.newBuilder(new URI(baseURL))
|
request =
|
||||||
|
HttpRequest
|
||||||
|
.newBuilder(new URI(baseURL))
|
||||||
.setHeader("Authorization", token)
|
.setHeader("Authorization", token)
|
||||||
// .setHeader("access_token", token)
|
// .setHeader("access_token", token)
|
||||||
.setHeader("Content-Type", "application/json")
|
.setHeader("Content-Type", "application/json")
|
||||||
.setHeader("Accept", "application/json")
|
.setHeader("Accept", "application/json")
|
||||||
.header("Authority", "https://api.dev.whysor.com/")
|
.header("Authority", "https://api.dev.whysor.com/")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
} catch (URISyntaxException e) {
|
} catch (URISyntaxException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
CompletableFuture<HttpResponse<String>> response = client.sendAsync(request, HttpResponse.BodyHandlers.ofString());
|
CompletableFuture<HttpResponse<String>> response = client.sendAsync(
|
||||||
String result = response.thenApply(HttpResponse::body).get(5, TimeUnit.SECONDS);
|
request,
|
||||||
|
HttpResponse.BodyHandlers.ofString()
|
||||||
|
);
|
||||||
|
String result = response
|
||||||
|
.thenApply(HttpResponse::body)
|
||||||
|
.get(5, TimeUnit.SECONDS);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,26 +9,33 @@ import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
public class HttpPost {
|
public class HttpPost {
|
||||||
public String HttpCallPost(String baseUrl, String body) throws Exception {
|
|
||||||
|
|
||||||
var client = HttpClient.newBuilder()
|
public String HttpCallPost(String baseUrl, String body) throws Exception {
|
||||||
|
var client = HttpClient
|
||||||
|
.newBuilder()
|
||||||
.version(HttpClient.Version.HTTP_1_1)
|
.version(HttpClient.Version.HTTP_1_1)
|
||||||
.followRedirects(HttpClient.Redirect.ALWAYS)
|
.followRedirects(HttpClient.Redirect.ALWAYS)
|
||||||
.build();
|
.build();
|
||||||
HttpRequest request = null;
|
HttpRequest request = null;
|
||||||
try {
|
try {
|
||||||
request = HttpRequest.newBuilder(new URI(baseUrl))
|
request =
|
||||||
|
HttpRequest
|
||||||
|
.newBuilder(new URI(baseUrl))
|
||||||
.POST(HttpRequest.BodyPublishers.ofString(body))
|
.POST(HttpRequest.BodyPublishers.ofString(body))
|
||||||
.setHeader("Content-Type", "application/json")
|
.setHeader("Content-Type", "application/json")
|
||||||
.setHeader("Accept", "application/json")
|
.setHeader("Accept", "application/json")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
} catch (URISyntaxException e) {
|
} catch (URISyntaxException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
CompletableFuture<HttpResponse<String>> response = client.sendAsync(request, HttpResponse.BodyHandlers.ofString());
|
CompletableFuture<HttpResponse<String>> response = client.sendAsync(
|
||||||
String result = response.thenApply(HttpResponse::body).get(5, TimeUnit.SECONDS);
|
request,
|
||||||
|
HttpResponse.BodyHandlers.ofString()
|
||||||
|
);
|
||||||
|
String result = response
|
||||||
|
.thenApply(HttpResponse::body)
|
||||||
|
.get(5, TimeUnit.SECONDS);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ import org.json.JSONArray;
|
||||||
|
|
||||||
//Get a list of all devices
|
//Get a list of all devices
|
||||||
public class GeneralCall4AllDevices {
|
public class GeneralCall4AllDevices {
|
||||||
|
|
||||||
public static String FetchAll(String token) throws Exception {
|
public static String FetchAll(String token) throws Exception {
|
||||||
String extension = "devices";
|
String extension = "devices";
|
||||||
UserCredentials userCredentials = new UserCredentials();
|
UserCredentials userCredentials = new UserCredentials();
|
||||||
|
@ -17,7 +18,10 @@ public class GeneralCall4AllDevices {
|
||||||
String devUrl = userCredentials.getDevUrl();
|
String devUrl = userCredentials.getDevUrl();
|
||||||
HttpGet httpCall = new HttpGet();
|
HttpGet httpCall = new HttpGet();
|
||||||
|
|
||||||
String fetchAll = httpCall.HttpGetCall(devUrl + extension + "?access_token=" + token, token);
|
String fetchAll = httpCall.HttpGetCall(
|
||||||
|
devUrl + extension + "?access_token=" + token,
|
||||||
|
token
|
||||||
|
);
|
||||||
writingIntoFile.WriteFile(homeRoot + extension + "Output.json", fetchAll);
|
writingIntoFile.WriteFile(homeRoot + extension + "Output.json", fetchAll);
|
||||||
|
|
||||||
return fetchAll;
|
return fetchAll;
|
||||||
|
|
|
@ -1,19 +1,18 @@
|
||||||
|
|
||||||
package JSON_Unpack;
|
package JSON_Unpack;
|
||||||
|
|
||||||
import Credentials.UserCredentials;
|
import Credentials.UserCredentials;
|
||||||
import Files.WriteFile;
|
import Files.WriteFile;
|
||||||
import HttpCall.HttpGet;
|
import HttpCall.HttpGet;
|
||||||
|
import java.util.ArrayList;
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
///url/sensors/id/read
|
///url/sensors/id/read
|
||||||
|
|
||||||
public class ReadSensors4Partner {
|
public class ReadSensors4Partner {
|
||||||
public ArrayList<String> FetchSensor4Box(String companyName, String token) throws Exception {
|
|
||||||
|
|
||||||
|
public ArrayList<String> FetchSensor4Box(String companyName, String token)
|
||||||
|
throws Exception {
|
||||||
switch (companyName) {
|
switch (companyName) {
|
||||||
case "Germes":
|
case "Germes":
|
||||||
return SensorRead("Germes", token);
|
return SensorRead("Germes", token);
|
||||||
|
@ -27,7 +26,8 @@ public class ReadSensors4Partner {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected ArrayList<String> SensorRead(String cName, String token) throws Exception {
|
protected ArrayList<String> SensorRead(String cName, String token)
|
||||||
|
throws Exception {
|
||||||
UserCredentials userCredentials = new UserCredentials();
|
UserCredentials userCredentials = new UserCredentials();
|
||||||
HttpGet httpCall = new HttpGet();
|
HttpGet httpCall = new HttpGet();
|
||||||
WriteFile writingIntoFile = new WriteFile();
|
WriteFile writingIntoFile = new WriteFile();
|
||||||
|
@ -44,32 +44,44 @@ public class ReadSensors4Partner {
|
||||||
for (int i = 0; i < outputBox.size(); i++) {
|
for (int i = 0; i < outputBox.size(); i++) {
|
||||||
JSONArray jsonArray = new JSONArray(outputBox.get(i));
|
JSONArray jsonArray = new JSONArray(outputBox.get(i));
|
||||||
for (int j = 0; j < jsonArray.length(); j++) {
|
for (int j = 0; j < jsonArray.length(); j++) {
|
||||||
|
|
||||||
JSONObject jsonobject = jsonArray.getJSONObject(j);
|
JSONObject jsonobject = jsonArray.getJSONObject(j);
|
||||||
String jsonID = jsonobject.getString("id");
|
String jsonID = jsonobject.getString("id");
|
||||||
String jsonTag = jsonobject.getString("tag");
|
String jsonTag = jsonobject.getString("tag");
|
||||||
String deviceID = jsonobject.getString("deviceId");
|
String deviceID = jsonobject.getString("deviceId");
|
||||||
String finalTag = ","+par+"deviceId"+par+":"+deviceID+","+par+"sensorTag"+par+":"+jsonTag+"}]";
|
String finalTag =
|
||||||
|
"," +
|
||||||
|
par +
|
||||||
|
"deviceId" +
|
||||||
|
par +
|
||||||
|
":" +
|
||||||
|
deviceID +
|
||||||
|
"," +
|
||||||
|
par +
|
||||||
|
"sensorTag" +
|
||||||
|
par +
|
||||||
|
":" +
|
||||||
|
jsonTag +
|
||||||
|
"}]";
|
||||||
|
|
||||||
String sensorOutput = httpCall.HttpGetCall(devUrl + "sensors/" + jsonID + "/read" + "?access_token=" + token, token);
|
String sensorOutput = httpCall.HttpGetCall(
|
||||||
|
devUrl + "sensors/" + jsonID + "/read" + "?access_token=" + token,
|
||||||
|
token
|
||||||
|
);
|
||||||
if (sensorOutput.contains("error")) {
|
if (sensorOutput.contains("error")) {
|
||||||
System.out.println(sensorOutput);
|
System.out.println(sensorOutput);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
String modification = sensorOutput.replace("}]", "");
|
String modification = sensorOutput.replace("}]", "");
|
||||||
String finalSensorOutput = modification + finalTag;
|
String finalSensorOutput = modification + finalTag;
|
||||||
list.add(finalSensorOutput);
|
list.add(finalSensorOutput);
|
||||||
|
|
||||||
writingIntoFile.WriteFile(homeRoot + jsonID + ".json", sensorOutput + jsonTag+","+deviceID);
|
writingIntoFile.WriteFile(
|
||||||
}
|
homeRoot + jsonID + ".json",
|
||||||
|
sensorOutput + jsonTag + "," + deviceID
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,22 +3,18 @@ package eu.hsrw.ias.ggd;
|
||||||
import Credentials.UserCredentials;
|
import Credentials.UserCredentials;
|
||||||
import Files.WriteFile;
|
import Files.WriteFile;
|
||||||
import JSON_Unpack.ReadSensors4Partner;
|
import JSON_Unpack.ReadSensors4Partner;
|
||||||
import org.json.JSONArray;
|
|
||||||
import org.json.JSONObject;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import org.json.JSONArray;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
public class ApiCall {
|
public class ApiCall {
|
||||||
|
|
||||||
private static String token = "0";
|
private static String token = "0";
|
||||||
private static String oldId = "1";
|
private static String oldId = "1";
|
||||||
|
|
||||||
|
public static Greenhouse FetchDataFromApi() throws Exception {
|
||||||
public static HashMap<String, SensorData> FetchDataFromApi() throws Exception {
|
|
||||||
|
|
||||||
|
|
||||||
UserCredentials userCredentials = new UserCredentials();
|
UserCredentials userCredentials = new UserCredentials();
|
||||||
final String username = userCredentials.getUsername();
|
final String username = userCredentials.getUsername();
|
||||||
final String password = userCredentials.getPassword();
|
final String password = userCredentials.getPassword();
|
||||||
|
@ -27,8 +23,8 @@ public class ApiCall {
|
||||||
WriteFile writingIntoFile = new WriteFile();
|
WriteFile writingIntoFile = new WriteFile();
|
||||||
Date now; // to display current time
|
Date now; // to display current time
|
||||||
now = new Date();
|
now = new Date();
|
||||||
HashMap<String,
|
HashMap<String, Device> devices = new HashMap();
|
||||||
SensorData> finalOutput = new HashMap();
|
|
||||||
if (token.equals(oldId)) {
|
if (token.equals(oldId)) {
|
||||||
//do nothing
|
//do nothing
|
||||||
} else {
|
} else {
|
||||||
|
@ -43,8 +39,11 @@ public class ApiCall {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ReadSensors4Partner readSensors4Partner = new ReadSensors4Partner();
|
ReadSensors4Partner readSensors4Partner = new ReadSensors4Partner();
|
||||||
ArrayList<String> outputValue = (readSensors4Partner.FetchSensor4Box("Germes", token));
|
ArrayList<String> outputValue =
|
||||||
|
(readSensors4Partner.FetchSensor4Box("Germes", token));
|
||||||
|
|
||||||
for (int i = 0; i < outputValue.size(); i++) {
|
for (int i = 0; i < outputValue.size(); i++) {
|
||||||
Device device;
|
Device device;
|
||||||
JSONArray sensorDatas = new JSONArray(outputValue.get(i));
|
JSONArray sensorDatas = new JSONArray(outputValue.get(i));
|
||||||
|
@ -70,8 +69,10 @@ public class ApiCall {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return finalOutput;
|
Greenhouse greenhouse = new Greenhouse();
|
||||||
|
for (Device device : devices.values()) {
|
||||||
|
greenhouse.addDevice(device);
|
||||||
|
}
|
||||||
|
return greenhouse;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,8 +4,11 @@ import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class Device {
|
public class Device {
|
||||||
|
|
||||||
private String id;
|
private String id;
|
||||||
|
private String externalID;
|
||||||
private List<SensorData> data;
|
private List<SensorData> data;
|
||||||
|
private Severity state = Severity.Unknown;
|
||||||
|
|
||||||
public Device(String identifier) {
|
public Device(String identifier) {
|
||||||
this.id = identifier;
|
this.id = identifier;
|
||||||
|
@ -28,6 +31,15 @@ public class Device {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setExternalID(String externalID) {
|
||||||
|
this.externalID = externalID;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getExternalID() {
|
||||||
|
return externalID;
|
||||||
|
}
|
||||||
|
|
||||||
public String IdMapper(String id) {
|
public String IdMapper(String id) {
|
||||||
switch (id) {
|
switch (id) {
|
||||||
case "fd694041-581e-4c2c-9810-505e62b762e6":
|
case "fd694041-581e-4c2c-9810-505e62b762e6":
|
||||||
|
@ -80,4 +92,14 @@ public class Device {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.data = data;
|
this.data = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setState(Severity state) {
|
||||||
|
if (this.state != Severity.Critical) {
|
||||||
|
this.state = state;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Severity getState() {
|
||||||
|
return state;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,16 +3,24 @@ package eu.hsrw.ias.ggd;
|
||||||
import HttpCall.HttpPost;
|
import HttpCall.HttpPost;
|
||||||
|
|
||||||
public class Isis {
|
public class Isis {
|
||||||
public static String OnCallMethod(String deviceName, String sensorName) throws Exception {
|
|
||||||
|
public static String OnCallMethod(String deviceName, String sensorName)
|
||||||
|
throws Exception {
|
||||||
//CONFIG PARAMETERS:
|
//CONFIG PARAMETERS:
|
||||||
//BEGIN------------CONFIG PARAMETERS BELOW TO YOUR ENVIRONMENT---------------------------------------
|
//BEGIN------------CONFIG PARAMETERS BELOW TO YOUR ENVIRONMENT---------------------------------------
|
||||||
String baseURL = "https://api.dev.whysor.com/devices/readValueFromSpeech?access_token=2MreqC9fqXxFHJBEcrCL38LKhEaVcEHmEi6o45CwTV9SSgTAuZ93oiMfY3HVc2fj";
|
String baseURL =
|
||||||
final String body = String.format("{\"deviceName\": \"%s\", \"sensorName\": \"%s\", \"domain\": \"my.yookr.org\"}", deviceName, sensorName);
|
"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();
|
HttpPost httpMethodPost = new HttpPost();
|
||||||
httpMethodPost.HttpCallPost(baseURL, body);
|
httpMethodPost.HttpCallPost(baseURL, body);
|
||||||
|
|
||||||
return httpMethodPost.HttpCallPost(baseURL, body);
|
return httpMethodPost.HttpCallPost(baseURL, body);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
try {
|
try {
|
||||||
String output = OnCallMethod("Greenhouse 1", "status");
|
String output = OnCallMethod("Greenhouse 1", "status");
|
||||||
|
@ -20,6 +28,5 @@ public class Isis {
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,12 @@
|
||||||
package eu.hsrw.ias.ggd;
|
package eu.hsrw.ias.ggd;
|
||||||
|
|
||||||
import java.util.Timer;
|
import java.util.Timer;
|
||||||
|
//Before pushing files to the git run : npx prettier --write "**/*.java"
|
||||||
public class MainExe {
|
public class MainExe {
|
||||||
public static void main(String[] args){
|
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
Timer time = new Timer(); // Instantiate Timer Object
|
Timer time = new Timer(); // Instantiate Timer Object
|
||||||
ScheduledTask scheduledTask = new ScheduledTask(); // Instantiate SheduledTask class
|
ScheduledTask scheduledTask = new ScheduledTask(); // Instantiate SheduledTask class
|
||||||
time.schedule(scheduledTask, 0, 1000 * 60 * 1); // Create Repetitively task for every 2 min
|
time.schedule(scheduledTask, 0, 1000 * 60 * 1); // Create Repetitively task for every 2 min
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package eu.hsrw.ias.ggd;
|
package eu.hsrw.ias.ggd;
|
||||||
|
|
||||||
public class Notification {
|
public class Notification {
|
||||||
|
|
||||||
private Severity severity = Severity.Optimal;
|
private Severity severity = Severity.Optimal;
|
||||||
private String type;
|
private String type;
|
||||||
private SensorData cause;
|
private SensorData cause;
|
||||||
|
|
|
@ -1,23 +1,23 @@
|
||||||
package eu.hsrw.ias.ggd;
|
package eu.hsrw.ias.ggd;
|
||||||
|
|
||||||
import HttpCall.HttpPost;
|
import HttpCall.HttpPost;
|
||||||
|
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.TimeoutException;
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
public class SecurityToken {
|
public class SecurityToken {
|
||||||
public static String OnCallMethod(String username, String password) throws Exception {
|
|
||||||
|
public static String OnCallMethod(String username, String password)
|
||||||
|
throws Exception {
|
||||||
//CONFIG PARAMETERS:
|
//CONFIG PARAMETERS:
|
||||||
//BEGIN------------CONFIG PARAMETERS BELOW TO YOUR ENVIRONMENT---------------------------------------
|
//BEGIN------------CONFIG PARAMETERS BELOW TO YOUR ENVIRONMENT---------------------------------------
|
||||||
String baseURL = "https://api.whysor.com/users/login";
|
String baseURL = "https://api.whysor.com/users/login";
|
||||||
String devURL = "https://api.dev.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);
|
final String body = String.format(
|
||||||
|
"{\"email\": \"%s\", \"password\": \"%s\", \"domain\": \"my.dev.yookr.org\"}",
|
||||||
|
username,
|
||||||
|
password
|
||||||
|
);
|
||||||
HttpPost httpMethodPost = new HttpPost();
|
HttpPost httpMethodPost = new HttpPost();
|
||||||
return httpMethodPost.HttpCallPost(devURL, body);
|
return httpMethodPost.HttpCallPost(devURL, body);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -3,21 +3,22 @@ package eu.hsrw.ias.ggd;
|
||||||
public class SensorData {
|
public class SensorData {
|
||||||
|
|
||||||
private String sensorId;
|
private String sensorId;
|
||||||
|
private String tag;
|
||||||
private double value;
|
private double value;
|
||||||
private String deviceId;
|
private Severity state = Severity.Unknown;
|
||||||
|
|
||||||
public SensorData(String sensorId, double value,String deviceId) {
|
public SensorData(String sensorId, double value, String tag) {
|
||||||
this.sensorId = sensorId;
|
this.sensorId = sensorId;
|
||||||
this.value = value;
|
this.value = value;
|
||||||
this.deviceId = deviceId;
|
this.tag = tag;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getSensorTag() {
|
public String getTag() {
|
||||||
return sensorId;
|
return tag;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSensorTag(String sensorTag) {
|
public void setTag(String tag) {
|
||||||
this.sensorId = sensorTag;
|
this.tag = tag;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getValue() {
|
public double getValue() {
|
||||||
|
@ -28,8 +29,21 @@ public class SensorData {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDeviceId() {return deviceId;}
|
public String getSensorId() {
|
||||||
|
return sensorId;
|
||||||
|
}
|
||||||
|
|
||||||
public void setDeviceId(String deviceTag){ this.deviceId = deviceTag;
|
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,6 +3,7 @@ package eu.hsrw.ias.ggd;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
public class SensorMap {
|
public class SensorMap {
|
||||||
|
|
||||||
private HashMap<String, Double> sensors = new HashMap<String, Double>();
|
private HashMap<String, Double> sensors = new HashMap<String, Double>();
|
||||||
|
|
||||||
public HashMap<String, Double> getSensors() {
|
public HashMap<String, Double> getSensors() {
|
||||||
|
@ -12,6 +13,4 @@ public class SensorMap {
|
||||||
public void setSensors(HashMap<String, Double> sensors) {
|
public void setSensors(HashMap<String, Double> sensors) {
|
||||||
this.sensors = sensors;
|
this.sensors = sensors;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,5 +5,6 @@ public enum Severity {
|
||||||
Info,
|
Info,
|
||||||
Warning,
|
Warning,
|
||||||
Error,
|
Error,
|
||||||
Critical
|
Critical,
|
||||||
|
Unknown,
|
||||||
}
|
}
|
|
@ -0,0 +1,31 @@
|
||||||
|
import eu.hsrw.ias.ggd.Device;
|
||||||
|
import eu.hsrw.ias.ggd.Severity;
|
||||||
|
import eu.hsrw.ias.ggd.SensorData;
|
||||||
|
|
||||||
|
rule "Sensor of device gone critical"
|
||||||
|
when
|
||||||
|
sensorData: SensorData(state == Severity.Critical)
|
||||||
|
device: Device(sensorData contains(sensorData))
|
||||||
|
then
|
||||||
|
modify(device){
|
||||||
|
setState(Severity.Critical)
|
||||||
|
}
|
||||||
|
System.out.println("Device: \n" +
|
||||||
|
"\tID: '" + device.getId() + "'" +
|
||||||
|
"\tState: '" + device.getState().toString() + "'"
|
||||||
|
);
|
||||||
|
// Do something the info that a device is gone critical
|
||||||
|
end
|
||||||
|
|
||||||
|
rule "Sensor of device gone optimal"
|
||||||
|
when
|
||||||
|
sensorData: SensorData(state == Severity.Optimal)
|
||||||
|
device: Device(sensorData contains(sensorData))
|
||||||
|
then
|
||||||
|
modify(device){
|
||||||
|
setState(Severity.Optimal)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Device is gone optimal
|
||||||
|
end
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
import eu.hsrw.ias.ggd.Greenhouse;
|
||||||
|
import eu.hsrw.ias.ggd.Device;
|
||||||
|
import eu.hsrw.ias.ggd.Severity;
|
||||||
|
|
||||||
|
rule "Device of greenhouse gone critical"
|
||||||
|
when
|
||||||
|
device: Device(state == Severity.Critical)
|
||||||
|
greenhouse: Greenhouse(devices contains(device))
|
||||||
|
//greenhouse: Greenhouse(devices contains(Device(state == Severity.Critical)))
|
||||||
|
then
|
||||||
|
modify(greenhouse){
|
||||||
|
setState(Severity.Critical)
|
||||||
|
}
|
||||||
|
System.out.println("Greenhouse: '" + greenhouse.getId() + "' is '" + greenhouse.getState().toString() + "'");
|
||||||
|
end
|
||||||
|
|
||||||
|
rule "Device of greenhouse has gone optimal"
|
||||||
|
when
|
||||||
|
device: Device(state == Severity.Optimal)
|
||||||
|
greenhouse: Greenhouse(devices contains(device))
|
||||||
|
then
|
||||||
|
modify(greenhouse){
|
||||||
|
setState(Severity.Optimal)
|
||||||
|
}
|
||||||
|
end
|
|
@ -0,0 +1,24 @@
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import eu.hsrw.ias.ggd.SensorData
|
||||||
|
import javax.swing.JOptionPane
|
||||||
|
import HttpCall.HttpPost
|
||||||
|
import eu.hsrw.ias.ggd.Notification
|
||||||
|
import eu.hsrw.ias.ggd.Severity;
|
||||||
|
import eu.hsrw.ias.ggd.Device;
|
||||||
|
|
||||||
|
rule "CriticalPrecipitation"
|
||||||
|
when
|
||||||
|
device: Device(sensorDatas: sensorData)
|
||||||
|
sensorData: SensorData(tag == "precipitation", value > 15.5) from sensorDatas
|
||||||
|
then
|
||||||
|
String greenhouse1="8bdbe6ae-eafb-4e99-bb01-db8784dd9633";
|
||||||
|
HttpPost httpPost = new HttpPost();
|
||||||
|
final String criticalPrecipitationOutput = String.format("{\"id\":\"%s\",\"data\":{\"measured\":[{ \"status\": \"Critical Precipitation %s in the following sensor %s \", \"precipitation\": %s}]}}",greenhouse1, sensorData.getValue(),sensorData.getTag(),sensorData.getValue());
|
||||||
|
httpPost.HttpCallPost("http://connector.dev.whysor.com/default/insert?access_token=3hosOhAeh4k0XmcuAMQGfYldvTuQDvtAj2PJJ4irKPBefD5Ijij6gnUkLtVLd4fW",criticalPrecipitationOutput);
|
||||||
|
insert(new Notification("precipitation", sensorData, Severity.Warning));
|
||||||
|
|
||||||
|
// modify(sensorData){
|
||||||
|
// setState(Severity.Critical)
|
||||||
|
// }
|
||||||
|
end
|
|
@ -0,0 +1,7 @@
|
||||||
|
package rules;
|
||||||
|
dialect "mvel"
|
||||||
|
|
||||||
|
rule "CriticalSoilConductivity"
|
||||||
|
when
|
||||||
|
then
|
||||||
|
end
|
|
@ -0,0 +1,7 @@
|
||||||
|
package rules;
|
||||||
|
dialect "mvel"
|
||||||
|
|
||||||
|
rule "CriticalSoilTemperature"
|
||||||
|
when
|
||||||
|
then
|
||||||
|
end
|
|
@ -0,0 +1,31 @@
|
||||||
|
import eu.hsrw.ias.ggd.Device;
|
||||||
|
import eu.hsrw.ias.ggd.Severity;
|
||||||
|
import eu.hsrw.ias.ggd.SensorData;
|
||||||
|
|
||||||
|
rule "Sensor of device gone critical"
|
||||||
|
when
|
||||||
|
sensorData: SensorData(state == Severity.Critical)
|
||||||
|
device: Device(sensorData contains(sensorData))
|
||||||
|
then
|
||||||
|
modify(device){
|
||||||
|
setState(Severity.Critical)
|
||||||
|
}
|
||||||
|
System.out.println("Device: \n" +
|
||||||
|
"\tID: '" + device.getId() + "'" +
|
||||||
|
"\tState: '" + device.getState().toString() + "'"
|
||||||
|
);
|
||||||
|
// Do something the info that a device is gone critical
|
||||||
|
end
|
||||||
|
|
||||||
|
rule "Sensor of device gone optimal"
|
||||||
|
when
|
||||||
|
sensorData: SensorData(state == Severity.Optimal)
|
||||||
|
device: Device(sensorData contains(sensorData))
|
||||||
|
then
|
||||||
|
modify(device){
|
||||||
|
setState(Severity.Optimal)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Device is gone optimal
|
||||||
|
end
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
import eu.hsrw.ias.ggd.Greenhouse;
|
||||||
|
import eu.hsrw.ias.ggd.Device;
|
||||||
|
import eu.hsrw.ias.ggd.Severity;
|
||||||
|
|
||||||
|
rule "Device of greenhouse gone critical"
|
||||||
|
when
|
||||||
|
device: Device(state == Severity.Critical)
|
||||||
|
greenhouse: Greenhouse(devices contains(device))
|
||||||
|
//greenhouse: Greenhouse(devices contains(Device(state == Severity.Critical)))
|
||||||
|
then
|
||||||
|
modify(greenhouse){
|
||||||
|
setState(Severity.Critical)
|
||||||
|
}
|
||||||
|
System.out.println("Greenhouse: '" + greenhouse.getId() + "' is '" + greenhouse.getState().toString() + "'");
|
||||||
|
end
|
||||||
|
|
||||||
|
rule "Device of greenhouse has gone optimal"
|
||||||
|
when
|
||||||
|
device: Device(state == Severity.Optimal)
|
||||||
|
greenhouse: Greenhouse(devices contains(device))
|
||||||
|
then
|
||||||
|
modify(greenhouse){
|
||||||
|
setState(Severity.Optimal)
|
||||||
|
}
|
||||||
|
end
|
|
@ -0,0 +1,15 @@
|
||||||
|
import eu.hsrw.ias.ggd.Notification;
|
||||||
|
import eu.hsrw.ias.ggd.Severity
|
||||||
|
import HttpCall.HttpPost;
|
||||||
|
import eu.hsrw.ias.ggd.Device;
|
||||||
|
|
||||||
|
rule "Green House Optimal"
|
||||||
|
when
|
||||||
|
warning: Notification(severity == Severity.Optimal)
|
||||||
|
|
||||||
|
then
|
||||||
|
HttpPost httpPost = new HttpPost();
|
||||||
|
final String optimalGreenhouse = String.format("{\"id\":\"germes-greenhouse-status\",\"data\":{\"measured\":[{ \"status\": \"The status of the greenhouse is optimal\"}]}}");
|
||||||
|
// httpPost.HttpCallPost("http://connector.dev.whysor.com/default/insert?access_token=3hosOhAeh4k0XmcuAMQGfYldvTuQDvtAj2PJJ4irKPBefD5Ijij6gnUkLtVLd4fW",optimalGreenhouse);
|
||||||
|
System.out.println("Green House optimal, because of: '"+warning.getType()+"'");
|
||||||
|
end
|
Loading…
Reference in New Issue