ADD: New rules for the rule Engine

FIX: ApiCall.java
REWORK : JSON_Unpack
master
Kevin Shehu 2021-11-18 13:22:12 +01:00
parent 1e0ec23d08
commit 141d474ef3
21 changed files with 241 additions and 87 deletions

3
.gitignore vendored
View File

@ -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*

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JpaBuddyIdeaProjectConfig">
<option name="defaultUnitInitialized" value="true" />
</component>
</project>

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RunConfigurationProducerService">
<option name="ignoredProducers">
<set>
<option value="com.android.tools.idea.compose.preview.runconfiguration.ComposePreviewRunConfigurationProducer" />
</set>
</option>
</component>
</project>

View File

@ -42,6 +42,12 @@
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>org.json</groupId>

View File

@ -1 +1 @@
T2JK7TpbwuBYDkJx41m4tdL6AUH6sLrTzVolA9mSLsi7A6FtHWFJv2sXVl8IYGQo
3jINGNigY2BcAHzSTU3K5734i1LvZzZq7ogMAVp4DYy4JkZtjzh0gHIdoqLeIV3n

View File

@ -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) {

View File

@ -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<String> 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,28 +44,22 @@ public class BoxCall4Partner {
ArrayList<String> listOfCompanyOutputs = new ArrayList<String>();
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);
writingIntoFile.WriteFile(homeRoot + iterator + ".json", companyOutput);
}
}
}
if (loopTerminator == arrayOfCompany.length) {
return listOfCompanyOutputs;
}
}
}
return null;
}
}

View File

@ -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;

View File

@ -33,38 +33,43 @@ 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<String> list=new ArrayList<String>();
int loopTerminator = 0;
char par = '"';
BoxCall4Partner boxCall4Partner = new BoxCall4Partner();
ArrayList<String> 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 finalTag = ","+par+"sensorTag"+par+":"+jsonTag+"}]";
String deviceID = jsonobject.getString("deviceId");
String finalTag = ","+par+"deviceId"+par+":"+deviceID+","+par+"sensorTag"+par+":"+jsonTag+"}]";
String sensorOutput = httpCall.HttpGetCall(baseURL + "sensors/" + jsonID + "/read" + "?access_token=" + token, token);
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);
loopTerminator++;
if(loopTerminator == jsonArray.length()){
writingIntoFile.WriteFile(homeRoot + jsonID + ".json", sensorOutput + jsonTag+","+deviceID);
}
}
}
return list;
}
}
return null;
}
}

View File

@ -27,6 +27,8 @@ public class ApiCall {
WriteFile writingIntoFile = new WriteFile();
Date now; // to display current time
now = new Date();
HashMap<String,
SensorData> finalOutput = new HashMap();
if (token.equals(oldId)) {
//do nothing
} else {
@ -43,24 +45,18 @@ public class ApiCall {
}
ReadSensors4Partner readSensors4Partner = new ReadSensors4Partner();
ArrayList<String> outputValue = (readSensors4Partner.FetchSensor4Box("Germes", token));
String workingArray = String.valueOf(outputValue).replace("[{", "{").replace("}]", "}");
JSONArray jsonArray = new JSONArray(workingArray);
HashMap<String,
SensorData> finalOutput = new HashMap();
for(int i=0;i<outputValue.size();i++){
JSONArray jsonArray = new JSONArray(outputValue.get(i));
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");
String deviceId = jsonobject.getString("deviceId");
finalOutput.put(sensorTag, new SensorData(sensorId, value));
finalOutput.put(sensorTag, new SensorData(deviceId, value,sensorId));
}
}
return finalOutput;
}

View File

@ -7,7 +7,7 @@ public class MainExe {
Timer time = new Timer(); // Instantiate Timer Object
ScheduledTask scheduledTask = new ScheduledTask(); // Instantiate SheduledTask class
time.schedule(scheduledTask, 0, 1000 * 60 * 5 ); // Create Repetitively task for every 2 min
time.schedule(scheduledTask, 0, 1000 * 60 * 1 ); // Create Repetitively task for every 2 min
}
}

View File

@ -10,9 +10,10 @@ public class SecurityToken {
//CONFIG PARAMETERS:
//BEGIN------------CONFIG PARAMETERS BELOW TO YOUR ENVIRONMENT---------------------------------------
String baseURL = "https://api.whysor.com/users/login";
final String body = String.format("{\"email\": \"%s\", \"password\": \"%s\", \"domain\": \"my.yookr.org\"}", username, password);
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(baseURL, body);
return httpMethodPost.HttpCallPost(devURL, body);
}
}

View File

@ -4,10 +4,12 @@ public class SensorData {
private String sensorId;
private double value;
private String deviceId;
public SensorData(String sensorId, double value) {
public SensorData(String sensorId, double value,String deviceId) {
this.sensorId = sensorId;
this.value = value;
this.deviceId = deviceId;
}
public String getSensorTag() {
@ -25,4 +27,9 @@ public class SensorData {
public void setValue(double value) {
this.value = value;
}
public String getDeviceId() {return deviceId;}
public void setDeviceId(String deviceTag){ this.deviceId = deviceTag;
}
}

View File

@ -1,3 +1,4 @@
package rules;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
@ -19,18 +20,23 @@ when
// for(e <- $tMap) {
SensorData(level: value ) from v
SensorData(tag: sensorTag) from v
// level = v.getValue()
SensorData(id: deviceId) from v
then
if (level < 2.8 && 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 data = String.format("{\"id\":\"%s\",\"data\":{\"measured\":[{ \"status\": \"Low Battery %s\", \"battery\": \"%s\"}]}}", tag, level,level);
HttpPost httpPost = new HttpPost();
String fixedIdG1="8bdbe6ae-eafb-4e99-bb01-db8784dd9633";
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 data = String.format("{\"id\":\"%s\",\"data\":{\"measured\":[{ \"status\": \"Low Battery %s in the following sensor: %s \", \"battery\": %s}]}}", fixedIdG1,level, tag,level);
httpPost.HttpCallPost("http://connector.dev.whysor.com/default/insert?access_token=3hosOhAeh4k0XmcuAMQGfYldvTuQDvtAj2PJJ4irKPBefD5Ijij6gnUkLtVLd4fW",data);
System.out.println(data);
System.out.println("Low Battery"+level+"in the following sensor"+tag);
}
System.out.println(e.getKey() + "-" + String.valueOf(level)+"-" + tag);
// else {
// JOptionPane.showMessageDialog(null,"Battery OK : "+level+"! in the following sensor :"+tag,"Battery OK",JOptionPane.INFORMATION_MESSAGE);
// final String data1 = String.format("{\"id\":\"%s\",\"data\":{\"measured\":[{ \"status\": \"Battery OK %s in the following sensor: %s \", \"battery\": %s}]}}", fixedIdG1,level, tag,level);
//// httpPost.HttpCallPost("http://connector.dev.whysor.com/default/insert?access_token=3hosOhAeh4k0XmcuAMQGfYldvTuQDvtAj2PJJ4irKPBefD5Ijij6gnUkLtVLd4fW",data1);
// System.out.println(data1);
// }
// System.out.println($tMap);
// System.out.println(e.getKey() + " " + String.valueOf(level)+" " + tag+" "+id);
end

View File

@ -0,0 +1,7 @@
package rules;
dialect "mvel"
rule "DielectricPermittivity"
when
then
end

View File

@ -0,0 +1,7 @@
package rules;
rule "ExternalTemperature"
when
then
end

View File

@ -0,0 +1,7 @@
package rules;
dialect "mvel"
rule "Precipitation"
when
then
end

View File

@ -3,17 +3,36 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import eu.hsrw.ias.ggd.ScheduledTask;
import javax.swing.JOptionPane;
import javax.swing.JOptionPane
import HttpCall.HttpPost;
import eu.hsrw.ias.ggd.SensorData
rule "Optimal EC levels in the soil"
when
$map : Map (this["soilConductivity"]<0.57 && this["soilConductivity"]>0.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

View File

@ -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

View File

@ -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

View File

@ -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