From 141d474ef3da0665f764533bab71c7ee85df881a Mon Sep 17 00:00:00 2001 From: "kevin.shehu" Date: Thu, 18 Nov 2021 13:22:12 +0100 Subject: [PATCH] ADD: New rules for the rule Engine FIX: ApiCall.java REWORK : JSON_Unpack --- .gitignore | 3 +- .idea/jpa-buddy.xml | 6 +++ .idea/runConfigurations.xml | 10 +++++ pom.xml | 6 +++ src/main/java/Data/securityToken.txt | 2 +- src/main/java/HttpCall/HttpGet.java | 2 +- .../java/JSON_Unpack/BoxCall4Partner.java | 27 +++++------- .../JSON_Unpack/GeneralCall4AllDevices.java | 3 +- .../java/JSON_Unpack/ReadSensors4Partner.java | 37 +++++++++------- src/main/java/eu/hsrw/ias/ggd/ApiCall.java | 28 ++++++------ src/main/java/eu/hsrw/ias/ggd/MainExe.java | 2 +- .../java/eu/hsrw/ias/ggd/SecurityToken.java | 5 ++- src/main/java/eu/hsrw/ias/ggd/SensorData.java | 9 +++- src/main/resources/rules/Battery.drl | 24 +++++++---- .../rules/DielectricPermittivity.drl | 7 +++ .../resources/rules/ExternalTemperature.drl | 7 +++ src/main/resources/rules/Precipitation.drl | 7 +++ src/main/resources/rules/SoilConductivity.drl | 31 ++++++++++--- src/main/resources/rules/SoilMoisture.drl | 43 ++++++++++++------- src/main/resources/rules/SoilTemperature.drl | 29 +++++++++++++ src/main/resources/rules/Temperature.drl | 40 +++++++++++++++++ 21 files changed, 241 insertions(+), 87 deletions(-) create mode 100644 .idea/jpa-buddy.xml create mode 100644 .idea/runConfigurations.xml create mode 100644 src/main/resources/rules/DielectricPermittivity.drl create mode 100644 src/main/resources/rules/ExternalTemperature.drl create mode 100644 src/main/resources/rules/Precipitation.drl create mode 100644 src/main/resources/rules/SoilTemperature.drl create mode 100644 src/main/resources/rules/Temperature.drl diff --git a/.gitignore b/.gitignore index bda420a..2fa7799 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,7 @@ ### Java template # Compiled class file *.class - +UserCredentials.java *.iml # Log file @@ -26,3 +26,4 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* + diff --git a/.idea/jpa-buddy.xml b/.idea/jpa-buddy.xml new file mode 100644 index 0000000..d08f400 --- /dev/null +++ b/.idea/jpa-buddy.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..797acea --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 910a735..a1c26ad 100644 --- a/pom.xml +++ b/pom.xml @@ -42,6 +42,12 @@ json-simple 1.1.1 + + + com.google.code.gson + gson + 2.8.6 + org.json diff --git a/src/main/java/Data/securityToken.txt b/src/main/java/Data/securityToken.txt index 9f9a149..390589c 100644 --- a/src/main/java/Data/securityToken.txt +++ b/src/main/java/Data/securityToken.txt @@ -1 +1 @@ -T2JK7TpbwuBYDkJx41m4tdL6AUH6sLrTzVolA9mSLsi7A6FtHWFJv2sXVl8IYGQo \ No newline at end of file +3jINGNigY2BcAHzSTU3K5734i1LvZzZq7ogMAVp4DYy4JkZtjzh0gHIdoqLeIV3n \ No newline at end of file diff --git a/src/main/java/HttpCall/HttpGet.java b/src/main/java/HttpCall/HttpGet.java index e617a1a..d0bf488 100644 --- a/src/main/java/HttpCall/HttpGet.java +++ b/src/main/java/HttpCall/HttpGet.java @@ -23,7 +23,7 @@ public class HttpGet { // .setHeader("access_token", token) .setHeader("Content-Type", "application/json") .setHeader("Accept", "application/json") - .header("Authority", "https://api.whysor.com/") + .header("Authority", "https://api.dev.whysor.com/") .build(); } catch (URISyntaxException e) { diff --git a/src/main/java/JSON_Unpack/BoxCall4Partner.java b/src/main/java/JSON_Unpack/BoxCall4Partner.java index 3c1f500..9b09126 100644 --- a/src/main/java/JSON_Unpack/BoxCall4Partner.java +++ b/src/main/java/JSON_Unpack/BoxCall4Partner.java @@ -12,17 +12,17 @@ import java.util.ArrayList; // Get sensor from device for each partner public class BoxCall4Partner { - public String FetchBox(String companyName, String token) throws Exception { + public ArrayList FetchBox(String companyName, String token) throws Exception { switch (companyName) { case "Germes": - return String.valueOf(onCallMethod("Germes", token)); + return onCallMethod("Germes", token); case "Heufs": - return String.valueOf(onCallMethod("Heufs", token)); + return onCallMethod("Heufs", token); case "Jacobs": - return String.valueOf(onCallMethod("Jacobs", token)); + return onCallMethod("Jacobs", token); case "Nica": - return String.valueOf(onCallMethod("Nica", token)); + return onCallMethod("Nica", token); } return null; } @@ -34,6 +34,7 @@ public class BoxCall4Partner { WriteFile writingIntoFile = new WriteFile(); String baseURL = userCredentials.getBaseURL(); + String devUrl = userCredentials.getDevUrl(); String homeRoot = UserCredentials.getHomeRoot(); String outputGeneral = GeneralCall4AllDevices.FetchAll(token); @@ -43,27 +44,21 @@ public class BoxCall4Partner { ArrayList listOfCompanyOutputs = new ArrayList(); String[] arrayOfCompany = cName.split(","); - int loopTerminator = 0; for (String iterator : arrayOfCompany) { for (int i = 0; i < jsonarray.length(); i++) { JSONObject jsonobject = jsonarray.getJSONObject(i); String jsonExternalID = jsonobject.getString("externalId"); - if (iterator.equals(jsonExternalID)) { String jsonID = jsonobject.getString("id"); - String companyOutput = httpCall.HttpGetCall(baseURL + "devices/" + jsonID + "/sensors" + "?access_token=" + token, token); +// String deviceID = jsonobject.getString("deviceId"); + String companyOutput = httpCall.HttpGetCall(devUrl + "devices/" + jsonID + "/sensors" + "?access_token=" + token, token); listOfCompanyOutputs.add(companyOutput); - writingIntoFile.WriteFile(homeRoot + i + " " + cName + ".json", companyOutput); - System.out.println(listOfCompanyOutputs); - loopTerminator++; - System.out.println(loopTerminator + " " + arrayOfCompany.length + " " + jsonarray.length() + " " + iterator); - } - if (loopTerminator == arrayOfCompany.length) { - return listOfCompanyOutputs; + writingIntoFile.WriteFile(homeRoot + iterator + ".json", companyOutput); + } } } - return null; + return listOfCompanyOutputs; } diff --git a/src/main/java/JSON_Unpack/GeneralCall4AllDevices.java b/src/main/java/JSON_Unpack/GeneralCall4AllDevices.java index d5060e7..7e9f234 100644 --- a/src/main/java/JSON_Unpack/GeneralCall4AllDevices.java +++ b/src/main/java/JSON_Unpack/GeneralCall4AllDevices.java @@ -14,9 +14,10 @@ public class GeneralCall4AllDevices { String baseURL = userCredentials.getBaseURL(); String homeRoot = UserCredentials.getHomeRoot(); + String devUrl = userCredentials.getDevUrl(); HttpGet httpCall = new HttpGet(); - String fetchAll = httpCall.HttpGetCall(baseURL + extension + "?access_token=" + token, token); + 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 2dcd829..11f7ccd 100644 --- a/src/main/java/JSON_Unpack/ReadSensors4Partner.java +++ b/src/main/java/JSON_Unpack/ReadSensors4Partner.java @@ -33,37 +33,42 @@ public class ReadSensors4Partner { WriteFile writingIntoFile = new WriteFile(); String baseURL = userCredentials.getBaseURL(); + String devUrl = userCredentials.getDevUrl(); String homeRoot = UserCredentials.getHomeRoot(); - BoxCall4Partner boxCall4Partner = new BoxCall4Partner(); - String outputBox = boxCall4Partner.FetchBox(cName,token); - String outputBoxFixed=outputBox.replace("[[","[") + outputBox.replace("]]","]");// To convert in Jsonarray we need to remove the array list brackets - JSONArray jsonArray = new JSONArray(outputBoxFixed); - System.out.println("FINAL WORKING ARRAY:"+jsonArray); ArrayList list=new ArrayList(); - int loopTerminator = 0; char par = '"'; + BoxCall4Partner boxCall4Partner = new BoxCall4Partner(); + ArrayList outputBox = boxCall4Partner.FetchBox(cName,token); + for (int i=0;i finalOutput = new HashMap(); if (token.equals(oldId)) { //do nothing } else { @@ -43,24 +45,18 @@ public class ApiCall { } ReadSensors4Partner readSensors4Partner = new ReadSensors4Partner(); ArrayList outputValue = (readSensors4Partner.FetchSensor4Box("Germes", token)); - String workingArray = String.valueOf(outputValue).replace("[{", "{").replace("}]", "}"); - - JSONArray jsonArray = new JSONArray(workingArray); - - HashMap finalOutput = new HashMap(); - - for (int j = 0; j < jsonArray.length(); j++) { - - JSONObject jsonobject = jsonArray.getJSONObject(j); - double value = jsonobject.getDouble("value"); - String sensorTag = jsonobject.getString("sensorTag"); - String sensorId = jsonobject.getString("sensorId"); - - finalOutput.put(sensorTag, new SensorData(sensorId, value)); + for(int i=0;i0.11) - + $tMap: HashMap( containsKey("soilConductivity") == true ) + //In this part we are iterating through the entire map + e: Map.Entry(k:key, v:value) from $tMap.entrySet() + // for(e <- $tMap) { + SensorData(level: value ) from v + SensorData(tag: sensorTag) from v + SensorData(id: deviceId) from v then - System.out.println("Optimal EC levels in the soil : "+$map.get("soilConductivity")); - JOptionPane.showMessageDialog(null,"Optimal EC levels in the soil : "+$map.get("soilConductivity"),"Soil Conductivity",JOptionPane.INFORMATION_MESSAGE); - + String fixedIdG1="8bdbe6ae-eafb-4e99-bb01-db8784dd9633"; + String fixedTag="soil Conductivity"; + HttpPost httpPost = new HttpPost(); + 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 data = String.format("{\"id\":\"%s\",\"data\":{\"measured\":[{ \"status\": \"Optimal EC levels in the soil %s\", \"soil conductivity\": %s}]}}", fixedIdG1, level,level); + httpPost.HttpCallPost("http://connector.dev.whysor.com/default/insert?access_token=3hosOhAeh4k0XmcuAMQGfYldvTuQDvtAj2PJJ4irKPBefD5Ijij6gnUkLtVLd4fW",data); + System.out.println("Optimal EC levels in the soil"+level +" in the following sensor:"+tag); + } +// else { +// JOptionPane.showMessageDialog(null,"Non Optimal EC levels in the soil : "+level+" in the following sensor:"+tag,"Soil Conductivity",JOptionPane.INFORMATION_MESSAGE); +// final String data1 = String.format("{\"id\":\"%s\",\"data\":{\"measured\":[{ \"status\": \"Non Optimal EC levels in the soil %s\", \"soil Conductivity\": %s}]}}", fixedIdG1, level,level); +//// httpPost.HttpCallPost("http://connector.dev.whysor.com/default/insert?access_token=3hosOhAeh4k0XmcuAMQGfYldvTuQDvtAj2PJJ4irKPBefD5Ijij6gnUkLtVLd4fW",data1); +// System.out.println(data1); +// } end diff --git a/src/main/resources/rules/SoilMoisture.drl b/src/main/resources/rules/SoilMoisture.drl index a3828c0..d04d54e 100644 --- a/src/main/resources/rules/SoilMoisture.drl +++ b/src/main/resources/rules/SoilMoisture.drl @@ -4,26 +4,37 @@ import java.util.Map; import java.util.Iterator; import eu.hsrw.ias.ggd.ScheduledTask; import javax.swing.JOptionPane; -rule "Dry Soil Moisture" +import HttpCall.HttpPost; +import eu.hsrw.ias.ggd.SensorData + + +rule "Soil Moisture" when - $map : Map (this["soilMoisture"]<40) + $tMap: HashMap( containsKey("soilMoisture") == true ) + //In this part we are iterating through the entire map + e: Map.Entry(k:key, v:value) from $tMap.entrySet() + // for(e <- $tMap) { + SensorData(level: value ) from v + SensorData(tag: sensorTag) from v + SensorData(id : deviceId) from v then - System.out.println("It is recommended to have a Soil Moisture higher than : "+$map.get("soilMoisture")); - JOptionPane.showMessageDialog(null,"It is recommended to have a Soil Moisture higher than : "+$map.get("soilMoisture"),"Dry Soil Moisture",JOptionPane.INFORMATION_MESSAGE); - -end - -rule "Wet Soil Mositure" -when - $map : Map (this["soilMoisture"]>70) - - then - System.out.println("Be careful the soil is extremly wet :"+$map.get("soilMoisture")+ "please dont apply water"); - JOptionPane.showMessageDialog(null,"Be careful the soil is extremly wet :"+$map.get("soilMoisture")+ "please dont apply water","Wet Soil Moisture",JOptionPane.INFORMATION_MESSAGE); - - + HttpPost httpPost = new HttpPost(); + String fixedIdG1="8bdbe6ae-eafb-4e99-bb01-db8784dd9633"; + String fixedTag="soil Moisture"; + 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 dataInfo = String.format("{\"id\":\"%s\",\"data\":{\"measured\":[{ \"status\": \"Be Careful the soil sensor %s is dry: %s \", \"soil moisture\": %s}]}}", fixedIdG1,fixedTag,level,level); + httpPost.HttpCallPost("http://connector.dev.whysor.com/default/insert?access_token=3hosOhAeh4k0XmcuAMQGfYldvTuQDvtAj2PJJ4irKPBefD5Ijij6gnUkLtVLd4fW",dataInfo); + System.out.println("Soil is dry"+level+" in the following sensor"+tag); + } + else if (level <= 80 && k.equals("soilMoisture") ) { + JOptionPane.showMessageDialog(null,"Optimal moisture in the soil : "+level+" in the following sensor"+tag,"Soil Moisture",JOptionPane.INFORMATION_MESSAGE); + final String data = String.format("{\"id\":\"%s\",\"data\":{\"measured\":[{ \"status\": \"Be Careful the soil sensor %s is wet: %s\", \"soil moisture\": %s}]}}", fixedIdG1,fixedTag,level,level); + httpPost.HttpCallPost("http://connector.dev.whysor.com/default/insert?access_token=3hosOhAeh4k0XmcuAMQGfYldvTuQDvtAj2PJJ4irKPBefD5Ijij6gnUkLtVLd4fW",data); + System.out.println("Optimal moisture in the soil"+level+" in the following sensor"+tag); + } end diff --git a/src/main/resources/rules/SoilTemperature.drl b/src/main/resources/rules/SoilTemperature.drl new file mode 100644 index 0000000..66c373a --- /dev/null +++ b/src/main/resources/rules/SoilTemperature.drl @@ -0,0 +1,29 @@ +import java.util.List; +import java.util.HashMap; +import java.util.Map; +import java.util.Iterator; +import eu.hsrw.ias.ggd.ScheduledTask; +import javax.swing.JOptionPane; +import HttpCall.HttpPost; +import eu.hsrw.ias.ggd.SensorData + + +rule "SoilTemperature" + + when + $tMap: HashMap( containsKey("soilTemperature") == true ) + e: Map.Entry(k:key, v:value) from $tMap.entrySet() + SensorData(level: value ) from v + SensorData(tag: sensorTag) from v + SensorData(id : deviceId) from v + then + + String fixedIdG1="8bdbe6ae-eafb-4e99-bb01-db8784dd9633"; + HttpPost httpPost = new HttpPost(); + 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 data = String.format("{\"id\":\"%s\",\"data\":{\"measured\":[{ \"status\": \"Low soil temperature %s in the following sensor: %s \", \"soil temperature\": %s}]}}", fixedIdG1,level, tag,level); + httpPost.HttpCallPost("http://connector.dev.whysor.com/default/insert?access_token=3hosOhAeh4k0XmcuAMQGfYldvTuQDvtAj2PJJ4irKPBefD5Ijij6gnUkLtVLd4fW",data); + System.out.println("Low soil Temperature"+level+" in the following sensor "+tag); + } +end diff --git a/src/main/resources/rules/Temperature.drl b/src/main/resources/rules/Temperature.drl new file mode 100644 index 0000000..375e1b3 --- /dev/null +++ b/src/main/resources/rules/Temperature.drl @@ -0,0 +1,40 @@ +import java.util.List; +import java.util.HashMap; +import java.util.Map; +import java.util.Iterator; +import eu.hsrw.ias.ggd.ScheduledTask +import eu.hsrw.ias.ggd.SensorData +import javax.swing.JOptionPane +import HttpCall.HttpPost +import Files.ReadFile +import Credentials.UserCredentials; + +rule "Temperature Check" + +when +// This is like declaring a variable tMap where is Map and contains a key with name battery + $tMap: HashMap( containsKey("temperature") == true ) +//In this part we are iterating through the entire map + e: Map.Entry(k:key, v:value) from $tMap.entrySet() + // for(e <- $tMap) { + SensorData(level: value ) from v + SensorData(tag: sensorTag) from v + SensorData(id: deviceId) from v + then + String fixedIdG1="8bdbe6ae-eafb-4e99-bb01-db8784dd9633"; + HttpPost httpPost = new HttpPost(); + 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 data = String.format("{\"id\":\"%s\",\"data\":{\"measured\":[{ \"status\": \"High Temperature %s in the following sensor: %s \", \"temperature\": %s}]}}", fixedIdG1,level, tag,level); + httpPost.HttpCallPost("http://connector.dev.whysor.com/default/insert?access_token=3hosOhAeh4k0XmcuAMQGfYldvTuQDvtAj2PJJ4irKPBefD5Ijij6gnUkLtVLd4fW",data); + System.out.println("High Temp"); + } + else if(level < 20 && k.equals("temperature")){ + JOptionPane.showMessageDialog(null,"Low Temperature : "+level+" in the following sensor: "+tag+"in the following device id: "+id,"Temperature",JOptionPane.INFORMATION_MESSAGE); + final String data1 = String.format("{\"id\":\"%s\",\"data\":{\"measured\":[{ \"status\": \"Low Temperature %s in the following sensor: %s \", \"temperature\": %s}]}}",fixedIdG1,level, tag,level); + httpPost.HttpCallPost("http://connector.dev.whysor.com/default/insert?access_token=3hosOhAeh4k0XmcuAMQGfYldvTuQDvtAj2PJJ4irKPBefD5Ijij6gnUkLtVLd4fW",data1); + System.out.println("Low Temp "+tag); + } + + +end \ No newline at end of file