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>
<!-- https://mvnrepository.com/artifact/org.kie/kie-api -->
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-api</artifactId>
<version>${runtime.version}</version>
<scope>provided</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.kie</groupId>-->
<!-- <artifactId>kie-api</artifactId>-->
<!-- <version>${runtime.version}</version>-->
<!-- <scope>provided</scope>-->
<!-- </dependency>-->
<!-- https://mvnrepository.com/artifact/org.kie/kie-ci -->
<dependency>
<groupId>org.kie</groupId>

View File

@ -1,12 +1,14 @@
package Credentials;
public class UserCredentials {
private String username = "kevin.shehu@hochschule-rhein-waal.de";
private String password = "DK7SxFkGJgnLhnU3";
private String baseURL = "https://api.whysor.com/";
private static String homeRoot = "/Users/ksh/Documents/Work/GGD/src/main/java/Data/";
private final String username = "kevin.shehu@hochschule-rhein-waal.de";
private final String password = "DK7SxFkGJgnLhnU3";
private final String baseURL = "https://api.whysor.com/";
private static final String homeRoot = "D:\\WORK\\GGD\\src\\main\\java\\Data\\";
public static String getHomeRoot() {
return homeRoot;
}
public String getUsername() {
return username;
@ -20,25 +22,7 @@ public class UserCredentials {
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
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.TimeoutException;
public class HttpCall {
public String HttpGet (String baseURL, String token) throws InterruptedException, ExecutionException, 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)

View File

@ -21,22 +21,14 @@ public class HttpPost {
.POST(HttpRequest.BodyPublishers.ofString(body))
.setHeader("Content-Type", "application/json")
.setHeader("Accept", "application/json")
.header("Authority", "https://api.whysor.com/")
.build();
} catch (URISyntaxException e) {
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());
String result = response.thenApply(HttpResponse::body).get(5, TimeUnit.SECONDS);
return result;
}
}

View File

@ -3,7 +3,7 @@ package JSON_Unpack;
import Credentials.UserCredentials;
import Files.ReadFile;
import Files.WriteFile;
import HttpCall.HttpCall;
import HttpCall.HttpGet;
import org.json.JSONArray;
import org.json.JSONObject;
@ -30,14 +30,13 @@ public class BoxCall4Partner {
protected ArrayList<String> onCallMethod(String cName, String token) throws Exception {
ReadFile readFile = new ReadFile();
UserCredentials userCredentials = new UserCredentials();
HttpCall httpCall = new HttpCall();
HttpGet httpCall = new HttpGet();
WriteFile writingIntoFile = new WriteFile();
String baseURL = userCredentials.getBaseURL();
String homeRoot = userCredentials.getHomeRoot();
String homeRoot = UserCredentials.getHomeRoot();
String outputGeneral = GeneralCall4All.FetchAll(token);
String outputGeneral = GeneralCall4AllDevices.FetchAll(token);
JSONArray jsonarray = new JSONArray(outputGeneral);
cName = readFile.ReadFromFile(homeRoot + cName + ".txt");
@ -52,19 +51,16 @@ public class BoxCall4Partner {
if (iterator.equals(jsonExternalID)) {
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);
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) {
System.out.println("Processing..... ");
return listOfCompanyOutputs;
}
}
}
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 Files.WriteFile;
import HttpCall.HttpCall;
import HttpCall.HttpGet;
import org.json.JSONArray;
import org.json.JSONObject;
@ -29,11 +29,11 @@ public class ReadSensors4Partner {
protected ArrayList<String> SensorRead(String cName, String token) throws Exception {
UserCredentials userCredentials = new UserCredentials();
HttpCall httpCall = new HttpCall();
HttpGet httpCall = new HttpGet();
WriteFile writingIntoFile = new WriteFile();
String baseURL = userCredentials.getBaseURL();
String homeRoot = userCredentials.getHomeRoot();
String homeRoot = UserCredentials.getHomeRoot();
BoxCall4Partner boxCall4Partner = new BoxCall4Partner();
String outputBox = boxCall4Partner.FetchBox(cName,token);
@ -50,8 +50,7 @@ public class ReadSensors4Partner {
String jsonTag = jsonobject.getString("tag");
String finalTag = ","+par+"sensorTag"+par+":"+jsonTag+"}]";
String sensorOutput = httpCall.HttpGet(baseURL + "sensors/" + jsonID + "/read" + "?access_token=" + token, token);
String sensorOutput = httpCall.HttpGetCall(baseURL + "sensors/" + jsonID + "/read" + "?access_token=" + token, token);
String modification = sensorOutput.replace("}]","");
String finalSensorOutput = modification+finalTag;
list.add(finalSensorOutput);
@ -59,7 +58,6 @@ public class ReadSensors4Partner {
writingIntoFile.WriteFile(homeRoot + jsonID + ".json", sensorOutput + jsonTag);
loopTerminator++;
if(loopTerminator == jsonArray.length()){
// System.out.println("L+++++++++++++"+list);
return list;
}
}

View File

@ -6,17 +6,17 @@ import JSON_Unpack.ReadSensors4Partner;
import org.json.JSONArray;
import org.json.JSONObject;
import javax.swing.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
public class ApiCall {
private static String token = "0";
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();
@ -47,52 +47,22 @@ public class ApiCall {
JSONArray jsonArray = new JSONArray(workingArray);
HashMap<String, Double> sensors = new HashMap<String, Double>();
HashMap<String,
SensorData> 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");
// System.out.println(value);
// System.out.println(sensorTag);
// sensorsMap.setSensors(sensorTag,value);
sensors.put(sensorTag,value);
}
String sensorId = jsonobject.getString("sensorId");
// ArrayList<String> listOfCompanies = new ArrayList<>();
// 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;
finalOutput.put(sensorTag, new SensorData(sensorId, value));
}
// public static void FetchData(String companyName, String token){
// 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);
//
// }
//
//
// }
return finalOutput;
}
}

View File

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

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

View File

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

View File

@ -3,16 +3,34 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
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"
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
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