From f66fb9dd95184bfe526ade4b1169880500a4dc94 Mon Sep 17 00:00:00 2001 From: "kevin.shehu" Date: Wed, 2 Mar 2022 17:15:00 +0100 Subject: [PATCH] NEW : Introducing Plonk System --- .gitignore | 2 - src/main/java/Credentials/PlonkTest.java | 40 ++++++++++ src/main/java/Data/securityToken.txt | 2 +- src/main/java/HttpCall/HttpPost.java | 4 + src/main/java/HttpCall/PlonkHttpGet.java | 42 ++++++++++ src/main/java/HttpCall/PlonkHttpPost.java | 46 +++++++++++ src/main/java/eu/hsrw/ias/ggd/Isis.java | 1 + src/main/java/eu/hsrw/ias/ggd/MainExe.java | 3 +- .../eu/hsrw/ias/ggd/PlonkDataRequest.java | 33 ++++++++ .../eu/hsrw/ias/ggd/PlonkHttpPostRequest.java | 30 +++++++ src/main/java/eu/hsrw/ias/ggd/SensorData.java | 4 + src/main/resources/rules/CriticalBattery.drl | 79 ------------------- .../resources/rules/CriticalTemperature.drl | 4 - src/main/resources/rules/FinalChecker.drl | 7 ++ src/main/resources/rules/optimal.drl | 2 +- src/main/resources/rules/warnings.drl | 22 +++--- target/classes/rules/CriticalBattery.drl | 79 ------------------- target/classes/rules/CriticalTemperature.drl | 4 - target/classes/rules/FinalChecker.drl | 7 ++ target/classes/rules/optimal.drl | 2 +- target/classes/rules/warnings.drl | 22 +++--- 21 files changed, 241 insertions(+), 194 deletions(-) create mode 100644 src/main/java/Credentials/PlonkTest.java create mode 100644 src/main/java/HttpCall/PlonkHttpGet.java create mode 100644 src/main/java/HttpCall/PlonkHttpPost.java create mode 100644 src/main/java/eu/hsrw/ias/ggd/PlonkDataRequest.java create mode 100644 src/main/java/eu/hsrw/ias/ggd/PlonkHttpPostRequest.java create mode 100644 src/main/resources/rules/FinalChecker.drl create mode 100644 target/classes/rules/FinalChecker.drl diff --git a/.gitignore b/.gitignore index 89c4398..2380ebb 100644 --- a/.gitignore +++ b/.gitignore @@ -2,9 +2,7 @@ ### Java template # Compiled class file *.class - src/main/java/Data/*.json -node_modules # Log file *.log diff --git a/src/main/java/Credentials/PlonkTest.java b/src/main/java/Credentials/PlonkTest.java new file mode 100644 index 0000000..bb3febf --- /dev/null +++ b/src/main/java/Credentials/PlonkTest.java @@ -0,0 +1,40 @@ +package Credentials; + +public class PlonkTest { + + private final String server = "https://demo.m3lab.de"; // add your Keycloak-URL here (without /auth) + private final String realm = "Dokutool"; // the name of the realm + private final String grantType = "password"; // the granttype, with password you can login as a normal user + private final String clientId = "dokutool-postman"; // the name of the client you created in Keycloak + private final String clientSecret = "d102ee78-697d-4d1c-903b-f5883af2cf17"; // the secret you copied earlier + private final String username = "demo"; // the username of the user you want to test with + private final String password = "demo"; // the password of the user you want to test with + + public String getServer() { + return server; + } + + public String getRealm() { + return realm; + } + + public String getGrantType() { + return grantType; + } + + public String getClientId() { + return clientId; + } + + public String getClientSecret() { + return clientSecret; + } + + public String getUsername() { + return username; + } + + public String getPassword() { + return password; + } +} diff --git a/src/main/java/Data/securityToken.txt b/src/main/java/Data/securityToken.txt index fa9604e..4116f70 100644 --- a/src/main/java/Data/securityToken.txt +++ b/src/main/java/Data/securityToken.txt @@ -1 +1 @@ -0Q6cHVQo9umiVSiZVf4DnsFMO4CgSv7RvpFiNMFTvZF1qiKDEVzSNpUOHegBv8wP \ No newline at end of file +MmfFR7KvbrelQJAsfKnmLKfdIATdIuHOTZc0H4B9gJweQ3Ifx6SJjVoBs59hW1iY \ No newline at end of file diff --git a/src/main/java/HttpCall/HttpPost.java b/src/main/java/HttpCall/HttpPost.java index 3b71411..2976204 100644 --- a/src/main/java/HttpCall/HttpPost.java +++ b/src/main/java/HttpCall/HttpPost.java @@ -1,5 +1,8 @@ package HttpCall; +import Credentials.PlonkTest; +import eu.hsrw.ias.ggd.PlonkHttpPostRequest; + import java.net.URI; import java.net.URISyntaxException; import java.net.http.HttpClient; @@ -23,6 +26,7 @@ public class HttpPost { .newBuilder(new URI(baseUrl)) .POST(HttpRequest.BodyPublishers.ofString(body)) .setHeader("Content-Type", "application/json") +// .setHeader("Content-Type","application/x-www-form-urlencoded") .setHeader("Accept", "application/json") .build(); } catch (URISyntaxException e) { diff --git a/src/main/java/HttpCall/PlonkHttpGet.java b/src/main/java/HttpCall/PlonkHttpGet.java new file mode 100644 index 0000000..6a3f26b --- /dev/null +++ b/src/main/java/HttpCall/PlonkHttpGet.java @@ -0,0 +1,42 @@ +package HttpCall; + +import java.net.URI; +import java.net.URISyntaxException; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +public class PlonkHttpGet { + public String PlonkHttpGetCall(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","Bearer "+ token) + .setHeader("Content-Type", "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/HttpCall/PlonkHttpPost.java b/src/main/java/HttpCall/PlonkHttpPost.java new file mode 100644 index 0000000..42a4bf7 --- /dev/null +++ b/src/main/java/HttpCall/PlonkHttpPost.java @@ -0,0 +1,46 @@ +package HttpCall; + + +import Credentials.PlonkTest; +import eu.hsrw.ias.ggd.PlonkHttpPostRequest; + +import java.net.URI; +import java.net.URISyntaxException; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; + +public class PlonkHttpPost { + + public String PlonkHttpCallPost(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/x-www-form-urlencoded") + .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/eu/hsrw/ias/ggd/Isis.java b/src/main/java/eu/hsrw/ias/ggd/Isis.java index ce8a1bf..34e44f8 100644 --- a/src/main/java/eu/hsrw/ias/ggd/Isis.java +++ b/src/main/java/eu/hsrw/ias/ggd/Isis.java @@ -28,5 +28,6 @@ public class Isis { } 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 dddcded..00ccfc1 100644 --- a/src/main/java/eu/hsrw/ias/ggd/MainExe.java +++ b/src/main/java/eu/hsrw/ias/ggd/MainExe.java @@ -1,12 +1,13 @@ 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 + time.schedule(scheduledTask, 0, 1000 * 60 * 2); // Create Repetitively task for every 2 min } } diff --git a/src/main/java/eu/hsrw/ias/ggd/PlonkDataRequest.java b/src/main/java/eu/hsrw/ias/ggd/PlonkDataRequest.java new file mode 100644 index 0000000..bf60f0e --- /dev/null +++ b/src/main/java/eu/hsrw/ias/ggd/PlonkDataRequest.java @@ -0,0 +1,33 @@ +package eu.hsrw.ias.ggd; + +import Credentials.PlonkTest; +import HttpCall.PlonkHttpGet; +import org.json.JSONObject; + +public class PlonkDataRequest { + public static String x() throws Exception { + String token; + String result; + { + PlonkTest plonkTest = new PlonkTest(); + PlonkHttpGet plonkHttpGet = new PlonkHttpGet(); + String outputToken = PlonkHttpPostRequest.OnCallMethod(); + JSONObject obj = new JSONObject(outputToken); + token = obj.getString("access_token"); + String urlExtension= plonkTest.getServer()+"/api/pre/objects"; + result = plonkHttpGet.PlonkHttpGetCall(urlExtension,token); + + } + + return result; + } + + public static void main(String[] args) { + try { + String output = x(); + System.out.println(output); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/eu/hsrw/ias/ggd/PlonkHttpPostRequest.java b/src/main/java/eu/hsrw/ias/ggd/PlonkHttpPostRequest.java new file mode 100644 index 0000000..abbbe30 --- /dev/null +++ b/src/main/java/eu/hsrw/ias/ggd/PlonkHttpPostRequest.java @@ -0,0 +1,30 @@ +package eu.hsrw.ias.ggd; + +import Credentials.PlonkTest; +import HttpCall.HttpPost; +import HttpCall.PlonkHttpPost; + +public class PlonkHttpPostRequest { + public static String OnCallMethod() + throws Exception { + PlonkTest plonkTest = new PlonkTest(); + PlonkHttpPost plonkHttpPost = new PlonkHttpPost(); + + //creating the request URL + String url = plonkTest.getServer() + "/auth/realms/" + plonkTest.getRealm() + "/protocol/openid-connect/token"; + //creating the body of the request + String data = String.format("grant_type=%s&client_id=%s&username=%s&password=%s&client_secret=%s", plonkTest.getGrantType(), plonkTest.getClientId(), plonkTest.getUsername(), plonkTest.getPassword(), plonkTest.getClientSecret()); + + return plonkHttpPost.PlonkHttpCallPost(url, data); + } + + +// public static void main(String[] args) { +// try { +// String output = OnCallMethod(); +// System.out.println(output); +// } catch (Exception e) { +// e.printStackTrace(); +// } +// } +} \ No newline at end of file diff --git a/src/main/java/eu/hsrw/ias/ggd/SensorData.java b/src/main/java/eu/hsrw/ias/ggd/SensorData.java index f2a4ac5..765c073 100644 --- a/src/main/java/eu/hsrw/ias/ggd/SensorData.java +++ b/src/main/java/eu/hsrw/ias/ggd/SensorData.java @@ -46,4 +46,8 @@ public class SensorData { this.state = state; } } + + public void resetState() { + this.state = Severity.Unknown; + } } diff --git a/src/main/resources/rules/CriticalBattery.drl b/src/main/resources/rules/CriticalBattery.drl index 0310a6e..d36a328 100644 --- a/src/main/resources/rules/CriticalBattery.drl +++ b/src/main/resources/rules/CriticalBattery.drl @@ -45,82 +45,3 @@ rule "OptimalBattery" // } end - - - - - - -// System.out.println(sensorData.getTag()+" "+sensorData.getValue()); -// System.out.println(device); - - /*String fixedIdG1="8bdbe6ae-eafb-4e99-bb01-db8784dd9633"; - HttpPost httpPost = new HttpPost(); -// System.out.println("Tag: '"+tag+"' has low battery with value: '"+level+"'"); - final String battery = String.format("{\"id\":\"%s\",\"data\":{\"measured\":[{ \"status\": \"Critical! Low Battery %s \", \"battery\": %s}]}}", fixedIdG1,level,level); - httpPost.HttpCallPost("http://connector.dev.whysor.com/default/insert?access_token=3hosOhAeh4k0XmcuAMQGfYldvTuQDvtAj2PJJ4irKPBefD5Ijij6gnUkLtVLd4fW",battery); - System.out.println("Low Battery"+level+"in the following sensor"+tag); - */ - /*HttpPost httpPost = new HttpPost(); - - final String finalOutput; - boolean critical = false; - HashMap test = new HashMap<>(); - if (tag.equals("fd694041-581e-4c2c-9810-505e62b762e6")){ // This matches 70B3D570500042D7 - if (level < 7.2 && k.equals("battery") ) { - JOptionPane.showMessageDialog(null,"Low Battery : "+level+"! Please swap the battery soon of the following sensor:"+tag,"Low Battery Level",JOptionPane.INFORMATION_MESSAGE); - final String battery = String.format("{\"id\":\"%s\",\"data\":{\"measured\":[{ \"status\": \"Critical! Low Battery %s \", \"battery\": %s}]}}", fixedIdG1,level,level); - httpPost.HttpCallPost("http://connector.dev.whysor.com/default/insert?access_token=3hosOhAeh4k0XmcuAMQGfYldvTuQDvtAj2PJJ4irKPBefD5Ijij6gnUkLtVLd4fW",battery); - System.out.println("Low Battery"+level+"in the following sensor"+tag); - critical = true; - test.put("battery",level); - } - if (level > 80 && k.equals("temperature") ) { - JOptionPane.showMessageDialog(null,"High Temperature : "+level+" in the following sensor: "+tag+"in the following device id: "+id,"Temperature",JOptionPane.INFORMATION_MESSAGE); - final String temperature = String.format("{\"id\":\"%s\",\"data\":{\"measured\":[{ \"status\": \"Critical!High Temperature %s \", \"temperature\": %s}]}}", fixedIdG1,level,level); - httpPost.HttpCallPost("http://connector.dev.whysor.com/default/insert?access_token=3hosOhAeh4k0XmcuAMQGfYldvTuQDvtAj2PJJ4irKPBefD5Ijij6gnUkLtVLd4fW",temperature); - System.out.println("High Temp"); - critical = true; - } - if (level > 14.5 && k.equals("precipitation") ) { - JOptionPane.showMessageDialog(null,"High water level : "+level+" in the following sensor"+tag,"Precipitation",JOptionPane.INFORMATION_MESSAGE); - final String precipitation = String.format("{\"id\":\"%s\",\"data\":{\"measured\":[{ \"status\": \"High water level %s\", \"precipitation\": %s}]}}", fixedIdG1, level,level); - httpPost.HttpCallPost("http://connector.dev.whysor.com/default/insert?access_token=3hosOhAeh4k0XmcuAMQGfYldvTuQDvtAj2PJJ4irKPBefD5Ijij6gnUkLtVLd4fW",precipitation); - System.out.println("High water level"+level +" in the following sensor:"+tag); - critical = true; - } - if (level < 22 && k.equals("soilTemperature") ) { - JOptionPane.showMessageDialog(null,"Low soil temperature : "+level+" in the following sensor: "+tag+"in the following device id: "+id,"soil Temperature",JOptionPane.INFORMATION_MESSAGE); - final String soilTemperature = String.format("{\"id\":\"%s\",\"data\":{\"measured\":[{ \"status\": \"Low soil temperature %s in the following sensor: %s \", \"soilTemperature\": %s}]}}", fixedIdG1,level, tag,level); - httpPost.HttpCallPost("http://connector.dev.whysor.com/default/insert?access_token=3hosOhAeh4k0XmcuAMQGfYldvTuQDvtAj2PJJ4irKPBefD5Ijij6gnUkLtVLd4fW",soilTemperature); - System.out.println("Low soil Temperature"+level+" in the following sensor "+tag); - critical = true; - test.put("soilTemperature",level); - } - if (level <= 30 && k.equals("soilMoisture") ) { - JOptionPane.showMessageDialog(null,"Be Careful the soil is dry : "+level+" in the following sensor :"+tag +"in the device:"+id,"Soil Moisture",JOptionPane.INFORMATION_MESSAGE); - final String soilMoisture = String.format("{\"id\":\"%s\",\"data\":{\"measured\":[{ \"status\": \"Be Careful the soil sensor %s is dry: %s \", \"soilMoisture\": %s}]}}", fixedIdG1,tag,level,level); - httpPost.HttpCallPost("http://connector.dev.whysor.com/default/insert?access_token=3hosOhAeh4k0XmcuAMQGfYldvTuQDvtAj2PJJ4irKPBefD5Ijij6gnUkLtVLd4fW",soilMoisture); - System.out.println("Soil is dry"+level+" in the following sensor"+tag); - critical = true; - } - if (level <= 0.57 && level >= 0.11 && k.equals("soilConductivity") ) { - JOptionPane.showMessageDialog(null,"Optimal EC levels in the soil : "+level+" in the following sensor"+tag,"Soil Conductivity",JOptionPane.INFORMATION_MESSAGE); - final String soilConductivity = String.format("{\"id\":\"%s\",\"data\":{\"measured\":[{ \"status\": \"Optimal EC levels in the soil %s\", \"soilConductivity\": %s}]}}", fixedIdG1, level,level); - httpPost.HttpCallPost("http://connector.dev.whysor.com/default/insert?access_token=3hosOhAeh4k0XmcuAMQGfYldvTuQDvtAj2PJJ4irKPBefD5Ijij6gnUkLtVLd4fW",soilConductivity); - System.out.println("Optimal EC levels in the soil"+level +" in the following sensor:"+tag); - critical = true; - } - if (level > 20 && k.equals("dielectricPermittivity") ) { - JOptionPane.showMessageDialog(null,"High dielectricPermittivity : "+level+" in the following sensor"+tag,"dielectricPermittivity",JOptionPane.INFORMATION_MESSAGE); - final String dielectricPermittivity = String.format("{\"id\":\"%s\",\"data\":{\"measured\":[{ \"status\": \"High dielectricPermittivity %s\", \"dielectricPermittivity\": %s}]}}", fixedIdG1, level,level); - httpPost.HttpCallPost("http://connector.dev.whysor.com/default/insert?access_token=3hosOhAeh4k0XmcuAMQGfYldvTuQDvtAj2PJJ4irKPBefD5Ijij6gnUkLtVLd4fW",dielectricPermittivity); - System.out.println("High dielectricPermittivity"+level +" in the following sensor:"+tag); - critical = true; - } -// if(critical == true){ -// finalOutput -// } - finalOutput = String.format("{\"id\":\"70B3D570500042D7-status\",\"data\":{\"measured\":[{ \"status\": \"The status of the device 1 is critical\",}]}}"); - System.out.println(finalOutput); - }*/ \ No newline at end of file diff --git a/src/main/resources/rules/CriticalTemperature.drl b/src/main/resources/rules/CriticalTemperature.drl index 5472b34..cde7f84 100644 --- a/src/main/resources/rules/CriticalTemperature.drl +++ b/src/main/resources/rules/CriticalTemperature.drl @@ -39,9 +39,5 @@ rule "OptimalTemperature" insert(new Notification("temperature", sensorData, Severity.Optimal)); System.out.println("Optimal"); -// modify(sensorData){ -// setState(Severity.Optimal) -// } - end diff --git a/src/main/resources/rules/FinalChecker.drl b/src/main/resources/rules/FinalChecker.drl new file mode 100644 index 0000000..c86495c --- /dev/null +++ b/src/main/resources/rules/FinalChecker.drl @@ -0,0 +1,7 @@ +package rules; +dialect "mvel" + +rule "FinalChecker" + when + then +end diff --git a/src/main/resources/rules/optimal.drl b/src/main/resources/rules/optimal.drl index 950bda3..9d0657f 100644 --- a/src/main/resources/rules/optimal.drl +++ b/src/main/resources/rules/optimal.drl @@ -10,6 +10,6 @@ when 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); + httpPost.HttpCallPost("http://connector.dev.whysor.com/default/insert?access_token=3hosOhAeh4k0XmcuAMQGfYldvTuQDvtAj2PJJ4irKPBefD5Ijij6gnUkLtVLd4fW",optimalGreenhouse); System.out.println("Green House optimal, because of: '"+warning.getType()+"'"); end diff --git a/src/main/resources/rules/warnings.drl b/src/main/resources/rules/warnings.drl index bb4b855..b6bc444 100644 --- a/src/main/resources/rules/warnings.drl +++ b/src/main/resources/rules/warnings.drl @@ -2,14 +2,14 @@ import eu.hsrw.ias.ggd.Notification; import eu.hsrw.ias.ggd.Severity import HttpCall.HttpPost; -//rule "Green House Critical" -//when -// warning: Notification(severity > Severity.Warning) -//then -// HttpPost httpPost = new HttpPost(); -// final String criticalGreenhouse = String.format("{\"id\":\"germes-greenhouse-status\",\"data\":{\"measured\":[{ \"status\": \"The status of the greenhouse is critical\"}]}}"); -// httpPost.HttpCallPost("http://connector.dev.whysor.com/default/insert?access_token=3hosOhAeh4k0XmcuAMQGfYldvTuQDvtAj2PJJ4irKPBefD5Ijij6gnUkLtVLd4fW",criticalGreenhouse); -// System.out.println(criticalGreenhouse); -// System.out.println("Green House critical, because of: '"+warning.getType()+"'"); -// -//end \ No newline at end of file +rule "Green House Critical" +when + warning: Notification(severity > Severity.Warning) +then + HttpPost httpPost = new HttpPost(); + final String criticalGreenhouse = String.format("{\"id\":\"germes-greenhouse-status\",\"data\":{\"measured\":[{ \"status\": \"The status of the greenhouse is critical\"}]}}"); + httpPost.HttpCallPost("http://connector.dev.whysor.com/default/insert?access_token=3hosOhAeh4k0XmcuAMQGfYldvTuQDvtAj2PJJ4irKPBefD5Ijij6gnUkLtVLd4fW",criticalGreenhouse); + System.out.println(criticalGreenhouse); + System.out.println("Green House critical, because of: '"+warning.getType()+"'"); + +end \ No newline at end of file diff --git a/target/classes/rules/CriticalBattery.drl b/target/classes/rules/CriticalBattery.drl index 0310a6e..d36a328 100644 --- a/target/classes/rules/CriticalBattery.drl +++ b/target/classes/rules/CriticalBattery.drl @@ -45,82 +45,3 @@ rule "OptimalBattery" // } end - - - - - - -// System.out.println(sensorData.getTag()+" "+sensorData.getValue()); -// System.out.println(device); - - /*String fixedIdG1="8bdbe6ae-eafb-4e99-bb01-db8784dd9633"; - HttpPost httpPost = new HttpPost(); -// System.out.println("Tag: '"+tag+"' has low battery with value: '"+level+"'"); - final String battery = String.format("{\"id\":\"%s\",\"data\":{\"measured\":[{ \"status\": \"Critical! Low Battery %s \", \"battery\": %s}]}}", fixedIdG1,level,level); - httpPost.HttpCallPost("http://connector.dev.whysor.com/default/insert?access_token=3hosOhAeh4k0XmcuAMQGfYldvTuQDvtAj2PJJ4irKPBefD5Ijij6gnUkLtVLd4fW",battery); - System.out.println("Low Battery"+level+"in the following sensor"+tag); - */ - /*HttpPost httpPost = new HttpPost(); - - final String finalOutput; - boolean critical = false; - HashMap test = new HashMap<>(); - if (tag.equals("fd694041-581e-4c2c-9810-505e62b762e6")){ // This matches 70B3D570500042D7 - if (level < 7.2 && k.equals("battery") ) { - JOptionPane.showMessageDialog(null,"Low Battery : "+level+"! Please swap the battery soon of the following sensor:"+tag,"Low Battery Level",JOptionPane.INFORMATION_MESSAGE); - final String battery = String.format("{\"id\":\"%s\",\"data\":{\"measured\":[{ \"status\": \"Critical! Low Battery %s \", \"battery\": %s}]}}", fixedIdG1,level,level); - httpPost.HttpCallPost("http://connector.dev.whysor.com/default/insert?access_token=3hosOhAeh4k0XmcuAMQGfYldvTuQDvtAj2PJJ4irKPBefD5Ijij6gnUkLtVLd4fW",battery); - System.out.println("Low Battery"+level+"in the following sensor"+tag); - critical = true; - test.put("battery",level); - } - if (level > 80 && k.equals("temperature") ) { - JOptionPane.showMessageDialog(null,"High Temperature : "+level+" in the following sensor: "+tag+"in the following device id: "+id,"Temperature",JOptionPane.INFORMATION_MESSAGE); - final String temperature = String.format("{\"id\":\"%s\",\"data\":{\"measured\":[{ \"status\": \"Critical!High Temperature %s \", \"temperature\": %s}]}}", fixedIdG1,level,level); - httpPost.HttpCallPost("http://connector.dev.whysor.com/default/insert?access_token=3hosOhAeh4k0XmcuAMQGfYldvTuQDvtAj2PJJ4irKPBefD5Ijij6gnUkLtVLd4fW",temperature); - System.out.println("High Temp"); - critical = true; - } - if (level > 14.5 && k.equals("precipitation") ) { - JOptionPane.showMessageDialog(null,"High water level : "+level+" in the following sensor"+tag,"Precipitation",JOptionPane.INFORMATION_MESSAGE); - final String precipitation = String.format("{\"id\":\"%s\",\"data\":{\"measured\":[{ \"status\": \"High water level %s\", \"precipitation\": %s}]}}", fixedIdG1, level,level); - httpPost.HttpCallPost("http://connector.dev.whysor.com/default/insert?access_token=3hosOhAeh4k0XmcuAMQGfYldvTuQDvtAj2PJJ4irKPBefD5Ijij6gnUkLtVLd4fW",precipitation); - System.out.println("High water level"+level +" in the following sensor:"+tag); - critical = true; - } - if (level < 22 && k.equals("soilTemperature") ) { - JOptionPane.showMessageDialog(null,"Low soil temperature : "+level+" in the following sensor: "+tag+"in the following device id: "+id,"soil Temperature",JOptionPane.INFORMATION_MESSAGE); - final String soilTemperature = String.format("{\"id\":\"%s\",\"data\":{\"measured\":[{ \"status\": \"Low soil temperature %s in the following sensor: %s \", \"soilTemperature\": %s}]}}", fixedIdG1,level, tag,level); - httpPost.HttpCallPost("http://connector.dev.whysor.com/default/insert?access_token=3hosOhAeh4k0XmcuAMQGfYldvTuQDvtAj2PJJ4irKPBefD5Ijij6gnUkLtVLd4fW",soilTemperature); - System.out.println("Low soil Temperature"+level+" in the following sensor "+tag); - critical = true; - test.put("soilTemperature",level); - } - if (level <= 30 && k.equals("soilMoisture") ) { - JOptionPane.showMessageDialog(null,"Be Careful the soil is dry : "+level+" in the following sensor :"+tag +"in the device:"+id,"Soil Moisture",JOptionPane.INFORMATION_MESSAGE); - final String soilMoisture = String.format("{\"id\":\"%s\",\"data\":{\"measured\":[{ \"status\": \"Be Careful the soil sensor %s is dry: %s \", \"soilMoisture\": %s}]}}", fixedIdG1,tag,level,level); - httpPost.HttpCallPost("http://connector.dev.whysor.com/default/insert?access_token=3hosOhAeh4k0XmcuAMQGfYldvTuQDvtAj2PJJ4irKPBefD5Ijij6gnUkLtVLd4fW",soilMoisture); - System.out.println("Soil is dry"+level+" in the following sensor"+tag); - critical = true; - } - if (level <= 0.57 && level >= 0.11 && k.equals("soilConductivity") ) { - JOptionPane.showMessageDialog(null,"Optimal EC levels in the soil : "+level+" in the following sensor"+tag,"Soil Conductivity",JOptionPane.INFORMATION_MESSAGE); - final String soilConductivity = String.format("{\"id\":\"%s\",\"data\":{\"measured\":[{ \"status\": \"Optimal EC levels in the soil %s\", \"soilConductivity\": %s}]}}", fixedIdG1, level,level); - httpPost.HttpCallPost("http://connector.dev.whysor.com/default/insert?access_token=3hosOhAeh4k0XmcuAMQGfYldvTuQDvtAj2PJJ4irKPBefD5Ijij6gnUkLtVLd4fW",soilConductivity); - System.out.println("Optimal EC levels in the soil"+level +" in the following sensor:"+tag); - critical = true; - } - if (level > 20 && k.equals("dielectricPermittivity") ) { - JOptionPane.showMessageDialog(null,"High dielectricPermittivity : "+level+" in the following sensor"+tag,"dielectricPermittivity",JOptionPane.INFORMATION_MESSAGE); - final String dielectricPermittivity = String.format("{\"id\":\"%s\",\"data\":{\"measured\":[{ \"status\": \"High dielectricPermittivity %s\", \"dielectricPermittivity\": %s}]}}", fixedIdG1, level,level); - httpPost.HttpCallPost("http://connector.dev.whysor.com/default/insert?access_token=3hosOhAeh4k0XmcuAMQGfYldvTuQDvtAj2PJJ4irKPBefD5Ijij6gnUkLtVLd4fW",dielectricPermittivity); - System.out.println("High dielectricPermittivity"+level +" in the following sensor:"+tag); - critical = true; - } -// if(critical == true){ -// finalOutput -// } - finalOutput = String.format("{\"id\":\"70B3D570500042D7-status\",\"data\":{\"measured\":[{ \"status\": \"The status of the device 1 is critical\",}]}}"); - System.out.println(finalOutput); - }*/ \ No newline at end of file diff --git a/target/classes/rules/CriticalTemperature.drl b/target/classes/rules/CriticalTemperature.drl index 5472b34..cde7f84 100644 --- a/target/classes/rules/CriticalTemperature.drl +++ b/target/classes/rules/CriticalTemperature.drl @@ -39,9 +39,5 @@ rule "OptimalTemperature" insert(new Notification("temperature", sensorData, Severity.Optimal)); System.out.println("Optimal"); -// modify(sensorData){ -// setState(Severity.Optimal) -// } - end diff --git a/target/classes/rules/FinalChecker.drl b/target/classes/rules/FinalChecker.drl new file mode 100644 index 0000000..c86495c --- /dev/null +++ b/target/classes/rules/FinalChecker.drl @@ -0,0 +1,7 @@ +package rules; +dialect "mvel" + +rule "FinalChecker" + when + then +end diff --git a/target/classes/rules/optimal.drl b/target/classes/rules/optimal.drl index 950bda3..9d0657f 100644 --- a/target/classes/rules/optimal.drl +++ b/target/classes/rules/optimal.drl @@ -10,6 +10,6 @@ when 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); + httpPost.HttpCallPost("http://connector.dev.whysor.com/default/insert?access_token=3hosOhAeh4k0XmcuAMQGfYldvTuQDvtAj2PJJ4irKPBefD5Ijij6gnUkLtVLd4fW",optimalGreenhouse); System.out.println("Green House optimal, because of: '"+warning.getType()+"'"); end diff --git a/target/classes/rules/warnings.drl b/target/classes/rules/warnings.drl index bb4b855..b6bc444 100644 --- a/target/classes/rules/warnings.drl +++ b/target/classes/rules/warnings.drl @@ -2,14 +2,14 @@ import eu.hsrw.ias.ggd.Notification; import eu.hsrw.ias.ggd.Severity import HttpCall.HttpPost; -//rule "Green House Critical" -//when -// warning: Notification(severity > Severity.Warning) -//then -// HttpPost httpPost = new HttpPost(); -// final String criticalGreenhouse = String.format("{\"id\":\"germes-greenhouse-status\",\"data\":{\"measured\":[{ \"status\": \"The status of the greenhouse is critical\"}]}}"); -// httpPost.HttpCallPost("http://connector.dev.whysor.com/default/insert?access_token=3hosOhAeh4k0XmcuAMQGfYldvTuQDvtAj2PJJ4irKPBefD5Ijij6gnUkLtVLd4fW",criticalGreenhouse); -// System.out.println(criticalGreenhouse); -// System.out.println("Green House critical, because of: '"+warning.getType()+"'"); -// -//end \ No newline at end of file +rule "Green House Critical" +when + warning: Notification(severity > Severity.Warning) +then + HttpPost httpPost = new HttpPost(); + final String criticalGreenhouse = String.format("{\"id\":\"germes-greenhouse-status\",\"data\":{\"measured\":[{ \"status\": \"The status of the greenhouse is critical\"}]}}"); + httpPost.HttpCallPost("http://connector.dev.whysor.com/default/insert?access_token=3hosOhAeh4k0XmcuAMQGfYldvTuQDvtAj2PJJ4irKPBefD5Ijij6gnUkLtVLd4fW",criticalGreenhouse); + System.out.println(criticalGreenhouse); + System.out.println("Green House critical, because of: '"+warning.getType()+"'"); + +end \ No newline at end of file