diff --git a/src/main/java/Credentials/UserCredentials.java b/src/main/java/Credentials/UserCredentials.java index a1d1343..0a9f441 100644 --- a/src/main/java/Credentials/UserCredentials.java +++ b/src/main/java/Credentials/UserCredentials.java @@ -1,39 +1,51 @@ package Credentials; public class UserCredentials { - private final String username = "kevin.shehu@hochschule-rhein-waal.de"; - private final String password = "DK7SxFkGJgnLhnU3"; - private final String baseURL = "https://api.whysor.com/"; - private final String devUrl = "https://api.dev.whysor.com/"; - private static final String homeRoot = "D:\\WORK\\GGD\\src\\main\\java\\Data\\"; - public static String getHomeRoot() { - return homeRoot; - } + private final String username = "kevin.shehu@hochschule-rhein-waal.de"; + private final String password = "DK7SxFkGJgnLhnU3"; + private final String baseURL = "https://api.whysor.com/"; + private final String devUrl = "https://api.dev.whysor.com/"; + private static final String homeRoot = + "D:\\WORK\\GGD\\src\\main\\java\\Data\\"; - public String getUsername() { - return username; - } + public static String getHomeRoot() { + return homeRoot; + } - public String getPassword() { - return password; - } + public String getUsername() { + return username; + } - public String getBaseURL() { - return baseURL; - } + public String getPassword() { + return password; + } - public String getDevUrl() { - return devUrl; - } + public String getBaseURL() { + return baseURL; + } - @Override - public String toString() { - return "UserCredentials{" + - "username='" + username + '\'' + - ", password='" + password + '\'' + - ", baseURL='" + baseURL + '\'' + - ", devUrl='" + devUrl + '\'' + - '}'; - } + public String getDevUrl() { + return devUrl; + } + + @Override + public String toString() { + return ( + "UserCredentials{" + + "username='" + + username + + '\'' + + ", password='" + + password + + '\'' + + ", baseURL='" + + baseURL + + '\'' + + ", devUrl='" + + devUrl + + '\'' + + '}' + ); + } } diff --git a/src/main/java/Files/ReadFile.java b/src/main/java/Files/ReadFile.java index cd4b7bd..a9730bf 100644 --- a/src/main/java/Files/ReadFile.java +++ b/src/main/java/Files/ReadFile.java @@ -1,28 +1,26 @@ package Files; -import java.io.File; // Import the File class - import java.io.FileNotFoundException; // Import this class to handle errors - import java.util.Scanner; // Import the Scanner class to read text files +import java.io.File; // Import the File class +import java.io.FileNotFoundException; // Import this class to handle errors +import java.util.Scanner; // Import the Scanner class to read text files public class ReadFile { - public String data; - public String ReadFromFile(String path){ + public String data; - try { - File myObj = new File(path); - Scanner myReader = new Scanner(myObj); - while (myReader.hasNextLine()) { - data = myReader.nextLine(); - System.out.println(data); - } - myReader.close(); - } catch (FileNotFoundException e) { - System.out.println("An error occurred."); - e.printStackTrace(); - } - return data; + public String ReadFromFile(String path) { + try { + File myObj = new File(path); + Scanner myReader = new Scanner(myObj); + while (myReader.hasNextLine()) { + data = myReader.nextLine(); + System.out.println(data); + } + myReader.close(); + } catch (FileNotFoundException e) { + System.out.println("An error occurred."); + e.printStackTrace(); } - - -} \ No newline at end of file + return data; + } +} diff --git a/src/main/java/Files/WriteFile.java b/src/main/java/Files/WriteFile.java index 76304c7..4f3b4d8 100644 --- a/src/main/java/Files/WriteFile.java +++ b/src/main/java/Files/WriteFile.java @@ -1,38 +1,34 @@ package Files; import java.io.*; - - import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeoutException; public class WriteFile { - BufferedWriter buffer = null; - public void WriteFile(String path, String input) throws Exception{ - try { - File file = new File(path); - if (!file.exists()) { - file.createNewFile(); - } + BufferedWriter buffer = null; - FileWriter fileWriter = new FileWriter(path); - buffer = new BufferedWriter(fileWriter); - buffer.write(input); - - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } finally { - try { - if (buffer != null) - buffer.flush(); -// buffer.close(); - } catch (Exception ex) { - System.out.println("Error in closing the BufferedWriter" + ex); - } - } + public void WriteFile(String path, String input) throws Exception { + try { + File file = new File(path); + if (!file.exists()) { + file.createNewFile(); + } + FileWriter fileWriter = new FileWriter(path); + buffer = new BufferedWriter(fileWriter); + buffer.write(input); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + if (buffer != null) buffer.flush(); + // buffer.close(); + } catch (Exception ex) { + System.out.println("Error in closing the BufferedWriter" + ex); + } } + } } diff --git a/src/main/java/HttpCall/HttpGet.java b/src/main/java/HttpCall/HttpGet.java index d0bf488..fd75262 100644 --- a/src/main/java/HttpCall/HttpGet.java +++ b/src/main/java/HttpCall/HttpGet.java @@ -11,30 +11,36 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; public class HttpGet { - public String HttpGetCall (String baseURL, String token) throws InterruptedException, ExecutionException, TimeoutException { - var client = HttpClient.newBuilder() - .version(HttpClient.Version.HTTP_1_1) - .followRedirects(HttpClient.Redirect.ALWAYS) - .build(); - HttpRequest request = null; - try { - request = HttpRequest.newBuilder(new URI(baseURL)) - .setHeader("Authorization", token) -// .setHeader("access_token", token) - .setHeader("Content-Type", "application/json") - .setHeader("Accept", "application/json") - .header("Authority", "https://api.dev.whysor.com/") - .build(); - - } catch (URISyntaxException e) { - e.printStackTrace(); - } - CompletableFuture> response = client.sendAsync(request, HttpResponse.BodyHandlers.ofString()); - String result = response.thenApply(HttpResponse::body).get(5, TimeUnit.SECONDS); - - - - return result; + public String HttpGetCall(String baseURL, String token) + throws InterruptedException, ExecutionException, TimeoutException { + var client = HttpClient + .newBuilder() + .version(HttpClient.Version.HTTP_1_1) + .followRedirects(HttpClient.Redirect.ALWAYS) + .build(); + HttpRequest request = null; + try { + request = + HttpRequest + .newBuilder(new URI(baseURL)) + .setHeader("Authorization", token) + // .setHeader("access_token", token) + .setHeader("Content-Type", "application/json") + .setHeader("Accept", "application/json") + .header("Authority", "https://api.dev.whysor.com/") + .build(); + } catch (URISyntaxException e) { + e.printStackTrace(); } + CompletableFuture> response = client.sendAsync( + request, + HttpResponse.BodyHandlers.ofString() + ); + String result = response + .thenApply(HttpResponse::body) + .get(5, TimeUnit.SECONDS); + + return result; + } } diff --git a/src/main/java/HttpCall/HttpPost.java b/src/main/java/HttpCall/HttpPost.java index cc0cf4d..3b71411 100644 --- a/src/main/java/HttpCall/HttpPost.java +++ b/src/main/java/HttpCall/HttpPost.java @@ -9,26 +9,33 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; public class HttpPost { - public String HttpCallPost(String baseUrl, String body) throws Exception { - var client = HttpClient.newBuilder() - .version(HttpClient.Version.HTTP_1_1) - .followRedirects(HttpClient.Redirect.ALWAYS) - .build(); - HttpRequest request = null; - try { - request = HttpRequest.newBuilder(new URI(baseUrl)) - .POST(HttpRequest.BodyPublishers.ofString(body)) - .setHeader("Content-Type", "application/json") - .setHeader("Accept", "application/json") - .build(); - - } catch (URISyntaxException e) { - e.printStackTrace(); - } - - CompletableFuture> response = client.sendAsync(request, HttpResponse.BodyHandlers.ofString()); - String result = response.thenApply(HttpResponse::body).get(5, TimeUnit.SECONDS); - return result; + public String HttpCallPost(String baseUrl, String body) throws Exception { + var client = HttpClient + .newBuilder() + .version(HttpClient.Version.HTTP_1_1) + .followRedirects(HttpClient.Redirect.ALWAYS) + .build(); + HttpRequest request = null; + try { + request = + HttpRequest + .newBuilder(new URI(baseUrl)) + .POST(HttpRequest.BodyPublishers.ofString(body)) + .setHeader("Content-Type", "application/json") + .setHeader("Accept", "application/json") + .build(); + } catch (URISyntaxException e) { + e.printStackTrace(); } + + CompletableFuture> response = client.sendAsync( + request, + HttpResponse.BodyHandlers.ofString() + ); + String result = response + .thenApply(HttpResponse::body) + .get(5, TimeUnit.SECONDS); + return result; + } } diff --git a/src/main/java/JSON2RDF/JsonStreamRDFWriter.java b/src/main/java/JSON2RDF/JsonStreamRDFWriter.java index b92ba43..a87ed8a 100644 --- a/src/main/java/JSON2RDF/JsonStreamRDFWriter.java +++ b/src/main/java/JSON2RDF/JsonStreamRDFWriter.java @@ -121,4 +121,4 @@ // return iriResolver; // } // -//} \ No newline at end of file +//} diff --git a/src/main/java/JSON_Unpack/GeneralCall4AllDevices.java b/src/main/java/JSON_Unpack/GeneralCall4AllDevices.java index 7e9f234..b498bf3 100644 --- a/src/main/java/JSON_Unpack/GeneralCall4AllDevices.java +++ b/src/main/java/JSON_Unpack/GeneralCall4AllDevices.java @@ -7,19 +7,23 @@ import org.json.JSONArray; //Get a list of all devices public class GeneralCall4AllDevices { - 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(); - String devUrl = userCredentials.getDevUrl(); - HttpGet httpCall = new HttpGet(); + public static String FetchAll(String token) throws Exception { + String extension = "devices"; + UserCredentials userCredentials = new UserCredentials(); + WriteFile writingIntoFile = new WriteFile(); - String fetchAll = httpCall.HttpGetCall(devUrl + extension + "?access_token=" + token, token); - writingIntoFile.WriteFile(homeRoot + extension + "Output.json", fetchAll); + String baseURL = userCredentials.getBaseURL(); + String homeRoot = UserCredentials.getHomeRoot(); + String devUrl = userCredentials.getDevUrl(); + HttpGet httpCall = new HttpGet(); - return fetchAll; - } + String fetchAll = httpCall.HttpGetCall( + devUrl + extension + "?access_token=" + token, + token + ); + writingIntoFile.WriteFile(homeRoot + extension + "Output.json", fetchAll); + + return fetchAll; + } } diff --git a/src/main/java/JSON_Unpack/ReadSensors4Partner.java b/src/main/java/JSON_Unpack/ReadSensors4Partner.java index 11f7ccd..aa4302a 100644 --- a/src/main/java/JSON_Unpack/ReadSensors4Partner.java +++ b/src/main/java/JSON_Unpack/ReadSensors4Partner.java @@ -1,75 +1,87 @@ - package JSON_Unpack; import Credentials.UserCredentials; import Files.WriteFile; import HttpCall.HttpGet; +import java.util.ArrayList; import org.json.JSONArray; import org.json.JSONObject; -import java.util.ArrayList; - ///url/sensors/id/read public class ReadSensors4Partner { - public ArrayList FetchSensor4Box(String companyName, String token) throws Exception { - switch (companyName) { - case "Germes": - return SensorRead("Germes", token); - case "Heufs": - return SensorRead("Heufs", token); - case "Jacobs": - return SensorRead("Jacobs", token); - case "Nica": - return SensorRead("Nica", token); + public ArrayList FetchSensor4Box(String companyName, String token) + throws Exception { + switch (companyName) { + case "Germes": + return SensorRead("Germes", token); + case "Heufs": + return SensorRead("Heufs", token); + case "Jacobs": + return SensorRead("Jacobs", token); + case "Nica": + return SensorRead("Nica", token); + } + return null; + } + + protected ArrayList SensorRead(String cName, String token) + throws Exception { + UserCredentials userCredentials = new UserCredentials(); + HttpGet httpCall = new HttpGet(); + WriteFile writingIntoFile = new WriteFile(); + + String baseURL = userCredentials.getBaseURL(); + String devUrl = userCredentials.getDevUrl(); + String homeRoot = UserCredentials.getHomeRoot(); + + ArrayList list = new ArrayList(); + char par = '"'; + + BoxCall4Partner boxCall4Partner = new BoxCall4Partner(); + ArrayList outputBox = boxCall4Partner.FetchBox(cName, token); + for (int i = 0; i < outputBox.size(); i++) { + JSONArray jsonArray = new JSONArray(outputBox.get(i)); + for (int j = 0; j < jsonArray.length(); j++) { + JSONObject jsonobject = jsonArray.getJSONObject(j); + String jsonID = jsonobject.getString("id"); + String jsonTag = jsonobject.getString("tag"); + String deviceID = jsonobject.getString("deviceId"); + String finalTag = + "," + + par + + "deviceId" + + par + + ":" + + deviceID + + "," + + par + + "sensorTag" + + par + + ":" + + jsonTag + + "}]"; + + String sensorOutput = httpCall.HttpGetCall( + devUrl + "sensors/" + jsonID + "/read" + "?access_token=" + token, + token + ); + if (sensorOutput.contains("error")) { + System.out.println(sensorOutput); + } else { + String modification = sensorOutput.replace("}]", ""); + String finalSensorOutput = modification + finalTag; + list.add(finalSensorOutput); + + writingIntoFile.WriteFile( + homeRoot + jsonID + ".json", + sensorOutput + jsonTag + "," + deviceID + ); } - return null; + } } - protected ArrayList SensorRead(String cName, String token) throws Exception { - UserCredentials userCredentials = new UserCredentials(); - HttpGet httpCall = new HttpGet(); - WriteFile writingIntoFile = new WriteFile(); - - String baseURL = userCredentials.getBaseURL(); - String devUrl = userCredentials.getDevUrl(); - String homeRoot = UserCredentials.getHomeRoot(); - - ArrayList list=new ArrayList(); - char par = '"'; - - BoxCall4Partner boxCall4Partner = new BoxCall4Partner(); - ArrayList outputBox = boxCall4Partner.FetchBox(cName,token); - for (int i=0;i 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 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 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 outputValue = (readSensors4Partner.FetchSensor4Box("Germes", token)); - for(int i=0;i 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; + } } diff --git a/src/main/java/eu/hsrw/ias/ggd/Device.java b/src/main/java/eu/hsrw/ias/ggd/Device.java index a34dd75..5eba3a6 100644 --- a/src/main/java/eu/hsrw/ias/ggd/Device.java +++ b/src/main/java/eu/hsrw/ias/ggd/Device.java @@ -4,80 +4,102 @@ import java.util.LinkedList; import java.util.List; public class Device { - private String id; - private List data; - public Device(String identifier) { - this.id = identifier; - this.data = new LinkedList(); + private String id; + private String externalID; + private List data; + private Severity state = Severity.Unknown; + + public Device(String identifier) { + this.id = identifier; + this.data = new LinkedList(); + } + + public void addSensorData(SensorData d) { + this.data.add(d); + } + + public List 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 getSensorData() { - return data; - } + public Device(String id, List 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 data) { - this.id = id; - this.data = data; - } + public Severity getState() { + return state; + } } diff --git a/src/main/java/eu/hsrw/ias/ggd/Greenhouse.java b/src/main/java/eu/hsrw/ias/ggd/Greenhouse.java new file mode 100644 index 0000000..107144b --- /dev/null +++ b/src/main/java/eu/hsrw/ias/ggd/Greenhouse.java @@ -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 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 getDevices() { + return devices; + } + + public void setDevices(List devices) { + this.devices = devices; + } + + private List devices; +} diff --git a/src/main/java/eu/hsrw/ias/ggd/Isis.java b/src/main/java/eu/hsrw/ias/ggd/Isis.java index c305a80..ce8a1bf 100644 --- a/src/main/java/eu/hsrw/ias/ggd/Isis.java +++ b/src/main/java/eu/hsrw/ias/ggd/Isis.java @@ -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(); } + } } diff --git a/src/main/java/eu/hsrw/ias/ggd/MainExe.java b/src/main/java/eu/hsrw/ias/ggd/MainExe.java index fb04ac2..dddcded 100644 --- a/src/main/java/eu/hsrw/ias/ggd/MainExe.java +++ b/src/main/java/eu/hsrw/ias/ggd/MainExe.java @@ -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 + } } - diff --git a/src/main/java/eu/hsrw/ias/ggd/Notification.java b/src/main/java/eu/hsrw/ias/ggd/Notification.java index ba5df74..e0a364a 100644 --- a/src/main/java/eu/hsrw/ias/ggd/Notification.java +++ b/src/main/java/eu/hsrw/ias/ggd/Notification.java @@ -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; + } } diff --git a/src/main/java/eu/hsrw/ias/ggd/SecurityToken.java b/src/main/java/eu/hsrw/ias/ggd/SecurityToken.java index 913bbc0..ba4ce1f 100644 --- a/src/main/java/eu/hsrw/ias/ggd/SecurityToken.java +++ b/src/main/java/eu/hsrw/ias/ggd/SecurityToken.java @@ -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); + } } - - - - - diff --git a/src/main/java/eu/hsrw/ias/ggd/SensorData.java b/src/main/java/eu/hsrw/ias/ggd/SensorData.java index 36018a0..f2a4ac5 100644 --- a/src/main/java/eu/hsrw/ias/ggd/SensorData.java +++ b/src/main/java/eu/hsrw/ias/ggd/SensorData.java @@ -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; } + } } diff --git a/src/main/java/eu/hsrw/ias/ggd/SensorMap.java b/src/main/java/eu/hsrw/ias/ggd/SensorMap.java index 758adb7..ca6d516 100644 --- a/src/main/java/eu/hsrw/ias/ggd/SensorMap.java +++ b/src/main/java/eu/hsrw/ias/ggd/SensorMap.java @@ -3,15 +3,14 @@ package eu.hsrw.ias.ggd; import java.util.HashMap; public class SensorMap { - private HashMap sensors = new HashMap(); - public HashMap getSensors() { - return sensors; - } - - public void setSensors(HashMap sensors) { - this.sensors = sensors; - } + private HashMap sensors = new HashMap(); + public HashMap getSensors() { + return sensors; + } + public void setSensors(HashMap sensors) { + this.sensors = sensors; + } } diff --git a/src/main/java/eu/hsrw/ias/ggd/Severity.java b/src/main/java/eu/hsrw/ias/ggd/Severity.java index 7ac5dd0..db5b982 100644 --- a/src/main/java/eu/hsrw/ias/ggd/Severity.java +++ b/src/main/java/eu/hsrw/ias/ggd/Severity.java @@ -1,9 +1,10 @@ package eu.hsrw.ias.ggd; public enum Severity { - Optimal, - Info, - Warning, - Error, - Critical -} \ No newline at end of file + Optimal, + Info, + Warning, + Error, + Critical, + Unknown, +} diff --git a/src/main/resources/rules/device-sensor-critical.drl b/src/main/resources/rules/device-sensor-critical.drl new file mode 100644 index 0000000..66cbf0d --- /dev/null +++ b/src/main/resources/rules/device-sensor-critical.drl @@ -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 + diff --git a/src/main/resources/rules/greenhouse_critical.drl b/src/main/resources/rules/greenhouse_critical.drl new file mode 100644 index 0000000..45ac9c2 --- /dev/null +++ b/src/main/resources/rules/greenhouse_critical.drl @@ -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 diff --git a/target/classes/rules/CriticalPrecipitation.drl b/target/classes/rules/CriticalPrecipitation.drl new file mode 100644 index 0000000..1d32305 --- /dev/null +++ b/target/classes/rules/CriticalPrecipitation.drl @@ -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 diff --git a/target/classes/rules/CriticalSoilConductivity.drl b/target/classes/rules/CriticalSoilConductivity.drl new file mode 100644 index 0000000..c42a0f9 --- /dev/null +++ b/target/classes/rules/CriticalSoilConductivity.drl @@ -0,0 +1,7 @@ +package rules; +dialect "mvel" + +rule "CriticalSoilConductivity" + when + then +end diff --git a/target/classes/rules/CriticalSoilTemperature.drl b/target/classes/rules/CriticalSoilTemperature.drl new file mode 100644 index 0000000..d143850 --- /dev/null +++ b/target/classes/rules/CriticalSoilTemperature.drl @@ -0,0 +1,7 @@ +package rules; +dialect "mvel" + +rule "CriticalSoilTemperature" + when + then +end diff --git a/target/classes/rules/device-sensor-critical.drl b/target/classes/rules/device-sensor-critical.drl new file mode 100644 index 0000000..66cbf0d --- /dev/null +++ b/target/classes/rules/device-sensor-critical.drl @@ -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 + diff --git a/target/classes/rules/greenhouse_critical.drl b/target/classes/rules/greenhouse_critical.drl new file mode 100644 index 0000000..45ac9c2 --- /dev/null +++ b/target/classes/rules/greenhouse_critical.drl @@ -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 diff --git a/target/classes/rules/optimal.drl b/target/classes/rules/optimal.drl new file mode 100644 index 0000000..950bda3 --- /dev/null +++ b/target/classes/rules/optimal.drl @@ -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