ADD: NEW CHANGES

FIX : Reworked ApiCall
FIX : Reworked Rule Engine
FIX : Reworked drl files
master
Kevin Shehu 2021-10-13 13:18:10 +02:00
parent cceac4d65d
commit 1e0ec23d08
17 changed files with 124 additions and 142 deletions

12
pom.xml
View File

@ -84,12 +84,12 @@
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.kie/kie-api --> <!-- https://mvnrepository.com/artifact/org.kie/kie-api -->
<dependency> <!-- <dependency>-->
<groupId>org.kie</groupId> <!-- <groupId>org.kie</groupId>-->
<artifactId>kie-api</artifactId> <!-- <artifactId>kie-api</artifactId>-->
<version>${runtime.version}</version> <!-- <version>${runtime.version}</version>-->
<scope>provided</scope> <!-- <scope>provided</scope>-->
</dependency> <!-- </dependency>-->
<!-- https://mvnrepository.com/artifact/org.kie/kie-ci --> <!-- https://mvnrepository.com/artifact/org.kie/kie-ci -->
<dependency> <dependency>
<groupId>org.kie</groupId> <groupId>org.kie</groupId>

View File

@ -1,12 +1,14 @@
package Credentials; package Credentials;
public class UserCredentials { public class UserCredentials {
private String username = "kevin.shehu@hochschule-rhein-waal.de"; private final String username = "kevin.shehu@hochschule-rhein-waal.de";
private String password = "DK7SxFkGJgnLhnU3"; private final String password = "DK7SxFkGJgnLhnU3";
private String baseURL = "https://api.whysor.com/"; private final String baseURL = "https://api.whysor.com/";
private static String homeRoot = "/Users/ksh/Documents/Work/GGD/src/main/java/Data/"; private static final String homeRoot = "D:\\WORK\\GGD\\src\\main\\java\\Data\\";
public static String getHomeRoot() {
return homeRoot;
}
public String getUsername() { public String getUsername() {
return username; return username;
@ -20,25 +22,7 @@ public class UserCredentials {
return baseURL; return baseURL;
} }
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setBaseURL(String baseURL) {
this.baseURL = baseURL;
}
public static String getHomeRoot() {
return homeRoot;
}
public static void setHomeRoot(String homeRoot) {
UserCredentials.homeRoot = homeRoot;
}
@Override @Override
public String toString() { public String toString() {

View File

@ -1 +1 @@
2YiUD7Tx2i8XO8q38cs5FsZMjzKbk4SUA5uJzUq68uaVXrps6z94Ve0R6SjgWeXw T2JK7TpbwuBYDkJx41m4tdL6AUH6sLrTzVolA9mSLsi7A6FtHWFJv2sXVl8IYGQo

View File

@ -10,8 +10,8 @@ import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
public class HttpCall { public class HttpGet {
public String HttpGet (String baseURL, String token) throws InterruptedException, ExecutionException, TimeoutException { public String HttpGetCall (String baseURL, String token) throws InterruptedException, ExecutionException, TimeoutException {
var client = HttpClient.newBuilder() var client = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1) .version(HttpClient.Version.HTTP_1_1)
.followRedirects(HttpClient.Redirect.ALWAYS) .followRedirects(HttpClient.Redirect.ALWAYS)

View File

@ -9,7 +9,7 @@ import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class HttpPost { public class HttpPost {
public String HttpCallPost (String baseUrl, String body) throws Exception { public String HttpCallPost(String baseUrl, String body) throws Exception {
var client = HttpClient.newBuilder() var client = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1) .version(HttpClient.Version.HTTP_1_1)
@ -21,22 +21,14 @@ public class HttpPost {
.POST(HttpRequest.BodyPublishers.ofString(body)) .POST(HttpRequest.BodyPublishers.ofString(body))
.setHeader("Content-Type", "application/json") .setHeader("Content-Type", "application/json")
.setHeader("Accept", "application/json") .setHeader("Accept", "application/json")
.header("Authority", "https://api.whysor.com/")
.build(); .build();
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
e.printStackTrace(); e.printStackTrace();
} }
// client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
// .thenApply(HttpResponse::body)
// .thenAccept(System.out::println)
// .join();
CompletableFuture<HttpResponse<String>> response = client.sendAsync(request, HttpResponse.BodyHandlers.ofString()); CompletableFuture<HttpResponse<String>> response = client.sendAsync(request, HttpResponse.BodyHandlers.ofString());
String result = response.thenApply(HttpResponse::body).get(5, TimeUnit.SECONDS); String result = response.thenApply(HttpResponse::body).get(5, TimeUnit.SECONDS);
return result; return result;
} }
} }

View File

@ -3,7 +3,7 @@ package JSON_Unpack;
import Credentials.UserCredentials; import Credentials.UserCredentials;
import Files.ReadFile; import Files.ReadFile;
import Files.WriteFile; import Files.WriteFile;
import HttpCall.HttpCall; import HttpCall.HttpGet;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
@ -30,18 +30,17 @@ public class BoxCall4Partner {
protected ArrayList<String> onCallMethod(String cName, String token) throws Exception { protected ArrayList<String> onCallMethod(String cName, String token) throws Exception {
ReadFile readFile = new ReadFile(); ReadFile readFile = new ReadFile();
UserCredentials userCredentials = new UserCredentials(); UserCredentials userCredentials = new UserCredentials();
HttpCall httpCall = new HttpCall(); HttpGet httpCall = new HttpGet();
WriteFile writingIntoFile = new WriteFile(); WriteFile writingIntoFile = new WriteFile();
String baseURL = userCredentials.getBaseURL(); String baseURL = userCredentials.getBaseURL();
String homeRoot = userCredentials.getHomeRoot(); String homeRoot = UserCredentials.getHomeRoot();
String outputGeneral = GeneralCall4AllDevices.FetchAll(token);
String outputGeneral = GeneralCall4All.FetchAll(token);
JSONArray jsonarray = new JSONArray(outputGeneral); JSONArray jsonarray = new JSONArray(outputGeneral);
cName = readFile.ReadFromFile(homeRoot +cName +".txt"); cName = readFile.ReadFromFile(homeRoot + cName + ".txt");
ArrayList<String> listOfCompanyOutputs=new ArrayList<String>(); ArrayList<String> listOfCompanyOutputs = new ArrayList<String>();
String[] arrayOfCompany = cName.split(","); String[] arrayOfCompany = cName.split(",");
int loopTerminator = 0; int loopTerminator = 0;
@ -52,19 +51,16 @@ public class BoxCall4Partner {
if (iterator.equals(jsonExternalID)) { if (iterator.equals(jsonExternalID)) {
String jsonID = jsonobject.getString("id"); String jsonID = jsonobject.getString("id");
String companyOutput = httpCall.HttpGet(baseURL + "devices/" + jsonID + "/sensors" + "?access_token=" + token, token); String companyOutput = httpCall.HttpGetCall(baseURL + "devices/" + jsonID + "/sensors" + "?access_token=" + token, token);
listOfCompanyOutputs.add(companyOutput); listOfCompanyOutputs.add(companyOutput);
writingIntoFile.WriteFile(homeRoot +i+" "+ cName + ".json", companyOutput); writingIntoFile.WriteFile(homeRoot + i + " " + cName + ".json", companyOutput);
System.out.println(listOfCompanyOutputs); System.out.println(listOfCompanyOutputs);
loopTerminator++; loopTerminator++;
System.out.println(loopTerminator + " "+arrayOfCompany.length+" "+jsonarray.length()+" "+iterator); System.out.println(loopTerminator + " " + arrayOfCompany.length + " " + jsonarray.length() + " " + iterator);
} }
if (loopTerminator == arrayOfCompany.length) {
if(loopTerminator == arrayOfCompany.length ){
System.out.println("Processing..... ");
return listOfCompanyOutputs; return listOfCompanyOutputs;
} }
} }
} }
return null; return null;

View File

@ -1,23 +0,0 @@
package JSON_Unpack;
import Credentials.UserCredentials;
import Files.WriteFile;
import HttpCall.HttpCall;
import org.json.JSONArray;
//Get a list of all devices
public class GeneralCall4All {
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();
HttpCall httpCall = new HttpCall();
String fetchAll = httpCall.HttpGet(baseURL + extension + "?access_token=" + token,token);
writingIntoFile.WriteFile(homeRoot + extension + "Output.json", fetchAll);
return fetchAll;
}
}

View File

@ -0,0 +1,24 @@
package JSON_Unpack;
import Credentials.UserCredentials;
import Files.WriteFile;
import HttpCall.HttpGet;
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();
HttpGet httpCall = new HttpGet();
String fetchAll = httpCall.HttpGetCall(baseURL + extension + "?access_token=" + token, token);
writingIntoFile.WriteFile(homeRoot + extension + "Output.json", fetchAll);
return fetchAll;
}
}

View File

@ -3,7 +3,7 @@ package JSON_Unpack;
import Credentials.UserCredentials; import Credentials.UserCredentials;
import Files.WriteFile; import Files.WriteFile;
import HttpCall.HttpCall; import HttpCall.HttpGet;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
@ -29,11 +29,11 @@ public class ReadSensors4Partner {
protected ArrayList<String> SensorRead(String cName, String token) throws Exception { protected ArrayList<String> SensorRead(String cName, String token) throws Exception {
UserCredentials userCredentials = new UserCredentials(); UserCredentials userCredentials = new UserCredentials();
HttpCall httpCall = new HttpCall(); HttpGet httpCall = new HttpGet();
WriteFile writingIntoFile = new WriteFile(); WriteFile writingIntoFile = new WriteFile();
String baseURL = userCredentials.getBaseURL(); String baseURL = userCredentials.getBaseURL();
String homeRoot = userCredentials.getHomeRoot(); String homeRoot = UserCredentials.getHomeRoot();
BoxCall4Partner boxCall4Partner = new BoxCall4Partner(); BoxCall4Partner boxCall4Partner = new BoxCall4Partner();
String outputBox = boxCall4Partner.FetchBox(cName,token); String outputBox = boxCall4Partner.FetchBox(cName,token);
@ -50,8 +50,7 @@ public class ReadSensors4Partner {
String jsonTag = jsonobject.getString("tag"); String jsonTag = jsonobject.getString("tag");
String finalTag = ","+par+"sensorTag"+par+":"+jsonTag+"}]"; String finalTag = ","+par+"sensorTag"+par+":"+jsonTag+"}]";
String sensorOutput = httpCall.HttpGetCall(baseURL + "sensors/" + jsonID + "/read" + "?access_token=" + token, token);
String sensorOutput = httpCall.HttpGet(baseURL + "sensors/" + jsonID + "/read" + "?access_token=" + token, token);
String modification = sensorOutput.replace("}]",""); String modification = sensorOutput.replace("}]","");
String finalSensorOutput = modification+finalTag; String finalSensorOutput = modification+finalTag;
list.add(finalSensorOutput); list.add(finalSensorOutput);
@ -59,7 +58,6 @@ public class ReadSensors4Partner {
writingIntoFile.WriteFile(homeRoot + jsonID + ".json", sensorOutput + jsonTag); writingIntoFile.WriteFile(homeRoot + jsonID + ".json", sensorOutput + jsonTag);
loopTerminator++; loopTerminator++;
if(loopTerminator == jsonArray.length()){ if(loopTerminator == jsonArray.length()){
// System.out.println("L+++++++++++++"+list);
return list; return list;
} }
} }

View File

@ -6,17 +6,17 @@ import JSON_Unpack.ReadSensors4Partner;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import javax.swing.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
public class ApiCall { public class ApiCall {
private static String token = "0"; private static String token = "0";
private static String oldId = "1"; private static String oldId = "1";
public static HashMap<String, Double> FetchDataFromApi() throws Exception { public static HashMap<String, SensorData> FetchDataFromApi() throws Exception {
UserCredentials userCredentials = new UserCredentials(); UserCredentials userCredentials = new UserCredentials();
@ -35,64 +35,34 @@ public class ApiCall {
JSONObject obj = new JSONObject(outputToken); JSONObject obj = new JSONObject(outputToken);
token = obj.getString("id"); token = obj.getString("id");
writingIntoFile.WriteFile(homeRoot+"securityToken.txt", token); writingIntoFile.WriteFile(homeRoot + "securityToken.txt", token);
oldId = token; oldId = token;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
ReadSensors4Partner readSensors4Partner = new ReadSensors4Partner(); ReadSensors4Partner readSensors4Partner = new ReadSensors4Partner();
ArrayList<String> outputValue = (readSensors4Partner.FetchSensor4Box("Germes",token)); ArrayList<String> outputValue = (readSensors4Partner.FetchSensor4Box("Germes", token));
String workingArray = String.valueOf(outputValue).replace("[{","{").replace("}]","}"); String workingArray = String.valueOf(outputValue).replace("[{", "{").replace("}]", "}");
JSONArray jsonArray = new JSONArray(workingArray); JSONArray jsonArray = new JSONArray(workingArray);
HashMap<String, Double> sensors = new HashMap<String, Double>(); HashMap<String,
for(int j = 0; j<jsonArray.length(); j++) { SensorData> finalOutput = new HashMap();
for (int j = 0; j < jsonArray.length(); j++) {
JSONObject jsonobject = jsonArray.getJSONObject(j); JSONObject jsonobject = jsonArray.getJSONObject(j);
double value = jsonobject.getDouble("value"); double value = jsonobject.getDouble("value");
String sensorTag = jsonobject.getString("sensorTag"); String sensorTag = jsonobject.getString("sensorTag");
// System.out.println(value); String sensorId = jsonobject.getString("sensorId");
// System.out.println(sensorTag);
// sensorsMap.setSensors(sensorTag,value);
sensors.put(sensorTag,value);
}
// ArrayList<String> listOfCompanies = new ArrayList<>(); finalOutput.put(sensorTag, new SensorData(sensorId, value));
// String germesOutput = FetchData("Germes",token);
// listOfCompanies.add(FetchData(c1,token));
// listOfCompanies.add(FetchData("Heufs",token));
JOptionPane.showMessageDialog(null,sensors,"Germes sensors"+now,JOptionPane.INFORMATION_MESSAGE);
return sensors;
} }
// public static void FetchData(String companyName, String token){ return finalOutput;
// ReadSensors4Partner readSensors4Partner = new ReadSensors4Partner();
// ArrayList<String> outputValue = null; }
// try {
// outputValue = (readSensors4Partner.FetchSensor4Box(companyName,token));
// } catch (Exception e) {
// e.printStackTrace();
// }
// String workingArray = String.valueOf(outputValue).replace("[{","{").replace("}]","}");
// JSONArray jsonArray = new JSONArray(workingArray);
//
// HashMap<String, Double> sensors = new HashMap<String, Double>();
// for(int j = 0; j<jsonArray.length(); j++) {
// JSONObject jsonobject = jsonArray.getJSONObject(j);
// double value = jsonobject.getDouble("value");
// String sensorTag = jsonobject.getString("sensorTag");
//// System.out.println(value);
//// System.out.println(sensorTag);
//// sensorsMap.setSensors(sensorTag,value);
// sensors.put(sensorTag,value);
//
// }
//
//
// }
} }

View File

@ -15,7 +15,7 @@ public class Isis {
} }
public static void main(String args[]){ public static void main(String args[]){
try { try {
String output = OnCallMethod("Climate", "humidity"); String output = OnCallMethod("Greenhouse 1", "status");
System.out.println(output); System.out.println(output);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();

View File

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

View File

@ -1,6 +1,5 @@
package eu.hsrw.ias.ggd; package eu.hsrw.ias.ggd;
import eu.hsrw.ias.ggd.ApiCall;
import org.kie.api.KieServices; import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer; import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession; import org.kie.api.runtime.KieSession;
@ -16,7 +15,7 @@ import java.util.concurrent.TimeoutException;
public class ScheduledTask extends TimerTask { public class ScheduledTask extends TimerTask {
Date now; // to display current time Date now; // to display current time
HashMap<String, Double> outputDevices ; HashMap<String, SensorData> outputDevices ;
// Add your task here // Add your task here
public void run() { public void run() {

View File

@ -12,12 +12,8 @@ public class SecurityToken {
String baseURL = "https://api.whysor.com/users/login"; String baseURL = "https://api.whysor.com/users/login";
final String body = String.format("{\"email\": \"%s\", \"password\": \"%s\", \"domain\": \"my.yookr.org\"}", username, password); final String body = String.format("{\"email\": \"%s\", \"password\": \"%s\", \"domain\": \"my.yookr.org\"}", username, password);
HttpPost httpMethodPost = new HttpPost(); HttpPost httpMethodPost = new HttpPost();
return httpMethodPost.HttpCallPost(baseURL, body); return httpMethodPost.HttpCallPost(baseURL, body);
} }
} }

View File

@ -0,0 +1,28 @@
package eu.hsrw.ias.ggd;
public class SensorData {
private String sensorId;
private double value;
public SensorData(String sensorId, double value) {
this.sensorId = sensorId;
this.value = value;
}
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;
}
}

View File

@ -1,4 +1,4 @@
package Knowledgebase; package eu.hsrw.ias.ggd;
import java.util.HashMap; import java.util.HashMap;

View File

@ -3,16 +3,34 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Iterator; import java.util.Iterator;
import eu.hsrw.ias.ggd.ScheduledTask import eu.hsrw.ias.ggd.ScheduledTask
import javax.swing.JOptionPane; import eu.hsrw.ias.ggd.SensorData
import javax.swing.JOptionPane
import HttpCall.HttpPost
import Files.ReadFile
import Credentials.UserCredentials;
rule "Low Battery Level" rule "Low Battery Level"
when when
$map : Map (this["battery"]<5.5) // This is like declaring a variable tMap where is Map and contains a key with name battery
$tMap: HashMap( containsKey("battery") == 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
// level = v.getValue()
then then
System.out.println("Low Battery : "+$map.get("battery")+"! Please swap the battery soon");
JOptionPane.showMessageDialog(null,"Low Battery : "+$map.get("battery")+"! Please swap the battery soon","Low Battery Level",JOptionPane.INFORMATION_MESSAGE);
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();
httpPost.HttpCallPost("http://connector.dev.whysor.com/default/insert?access_token=3hosOhAeh4k0XmcuAMQGfYldvTuQDvtAj2PJJ4irKPBefD5Ijij6gnUkLtVLd4fW",data);
System.out.println(data);
}
System.out.println(e.getKey() + "-" + String.valueOf(level)+"-" + tag);
end end