diff --git a/pom.xml b/pom.xml index cb3f876..910a735 100644 --- a/pom.xml +++ b/pom.xml @@ -84,12 +84,12 @@ - - org.kie - kie-api - ${runtime.version} - provided - + + + + + + org.kie diff --git a/src/main/java/Credentials/UserCredentials.java b/src/main/java/Credentials/UserCredentials.java index b276e49..53c1683 100644 --- a/src/main/java/Credentials/UserCredentials.java +++ b/src/main/java/Credentials/UserCredentials.java @@ -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() { diff --git a/src/main/java/Data/securityToken.txt b/src/main/java/Data/securityToken.txt index eb9b064..9f9a149 100644 --- a/src/main/java/Data/securityToken.txt +++ b/src/main/java/Data/securityToken.txt @@ -1 +1 @@ -2YiUD7Tx2i8XO8q38cs5FsZMjzKbk4SUA5uJzUq68uaVXrps6z94Ve0R6SjgWeXw \ No newline at end of file +T2JK7TpbwuBYDkJx41m4tdL6AUH6sLrTzVolA9mSLsi7A6FtHWFJv2sXVl8IYGQo \ No newline at end of file diff --git a/src/main/java/HttpCall/HttpCall.java b/src/main/java/HttpCall/HttpGet.java similarity index 89% rename from src/main/java/HttpCall/HttpCall.java rename to src/main/java/HttpCall/HttpGet.java index bbbb548..e617a1a 100644 --- a/src/main/java/HttpCall/HttpCall.java +++ b/src/main/java/HttpCall/HttpGet.java @@ -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) diff --git a/src/main/java/HttpCall/HttpPost.java b/src/main/java/HttpCall/HttpPost.java index 25e370e..cc0cf4d 100644 --- a/src/main/java/HttpCall/HttpPost.java +++ b/src/main/java/HttpCall/HttpPost.java @@ -9,7 +9,7 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; 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() .version(HttpClient.Version.HTTP_1_1) @@ -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> 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/JSON_Unpack/BoxCall4Partner.java b/src/main/java/JSON_Unpack/BoxCall4Partner.java index 55d45e1..3c1f500 100644 --- a/src/main/java/JSON_Unpack/BoxCall4Partner.java +++ b/src/main/java/JSON_Unpack/BoxCall4Partner.java @@ -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,18 +30,17 @@ public class BoxCall4Partner { protected ArrayList 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"); - ArrayList listOfCompanyOutputs=new ArrayList(); + cName = readFile.ReadFromFile(homeRoot + cName + ".txt"); + ArrayList listOfCompanyOutputs = new ArrayList(); String[] arrayOfCompany = cName.split(","); int loopTerminator = 0; @@ -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); + writingIntoFile.WriteFile(homeRoot + i + " " + cName + ".json", companyOutput); System.out.println(listOfCompanyOutputs); loopTerminator++; - System.out.println(loopTerminator + " "+arrayOfCompany.length+" "+jsonarray.length()+" "+iterator); + System.out.println(loopTerminator + " " + arrayOfCompany.length + " " + jsonarray.length() + " " + iterator); } - - if(loopTerminator == arrayOfCompany.length ){ - System.out.println("Processing..... "); + if (loopTerminator == arrayOfCompany.length) { return listOfCompanyOutputs; } - } } return null; diff --git a/src/main/java/JSON_Unpack/GeneralCall4All.java b/src/main/java/JSON_Unpack/GeneralCall4All.java deleted file mode 100644 index e7cb6f0..0000000 --- a/src/main/java/JSON_Unpack/GeneralCall4All.java +++ /dev/null @@ -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; - } -} diff --git a/src/main/java/JSON_Unpack/GeneralCall4AllDevices.java b/src/main/java/JSON_Unpack/GeneralCall4AllDevices.java new file mode 100644 index 0000000..d5060e7 --- /dev/null +++ b/src/main/java/JSON_Unpack/GeneralCall4AllDevices.java @@ -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; + } +} diff --git a/src/main/java/JSON_Unpack/ReadSensors4Partner.java b/src/main/java/JSON_Unpack/ReadSensors4Partner.java index 9164378..2dcd829 100644 --- a/src/main/java/JSON_Unpack/ReadSensors4Partner.java +++ b/src/main/java/JSON_Unpack/ReadSensors4Partner.java @@ -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 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; } } diff --git a/src/main/java/eu/hsrw/ias/ggd/ApiCall.java b/src/main/java/eu/hsrw/ias/ggd/ApiCall.java index 1a33e52..05f543d 100644 --- a/src/main/java/eu/hsrw/ias/ggd/ApiCall.java +++ b/src/main/java/eu/hsrw/ias/ggd/ApiCall.java @@ -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 FetchDataFromApi() throws Exception { + public static HashMap FetchDataFromApi() throws Exception { UserCredentials userCredentials = new UserCredentials(); @@ -35,64 +35,34 @@ public class ApiCall { JSONObject obj = new JSONObject(outputToken); token = obj.getString("id"); - writingIntoFile.WriteFile(homeRoot+"securityToken.txt", token); + writingIntoFile.WriteFile(homeRoot + "securityToken.txt", token); oldId = token; } catch (Exception e) { e.printStackTrace(); } } ReadSensors4Partner readSensors4Partner = new ReadSensors4Partner(); - ArrayList outputValue = (readSensors4Partner.FetchSensor4Box("Germes",token)); - String workingArray = String.valueOf(outputValue).replace("[{","{").replace("}]","}"); + ArrayList outputValue = (readSensors4Partner.FetchSensor4Box("Germes", token)); + String workingArray = String.valueOf(outputValue).replace("[{", "{").replace("}]", "}"); JSONArray jsonArray = new JSONArray(workingArray); - HashMap sensors = new HashMap(); - for(int j = 0; j 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"); + + finalOutput.put(sensorTag, new SensorData(sensorId, value)); + } -// ArrayList 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; + return finalOutput; } -// public static void FetchData(String companyName, String token){ -// ReadSensors4Partner readSensors4Partner = new ReadSensors4Partner(); -// ArrayList 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 sensors = new HashMap(); -// for(int j = 0; j outputDevices ; + HashMap outputDevices ; // Add your task here public void run() { diff --git a/src/main/java/eu/hsrw/ias/ggd/SecurityToken.java b/src/main/java/eu/hsrw/ias/ggd/SecurityToken.java index 65691a7..2772fb8 100644 --- a/src/main/java/eu/hsrw/ias/ggd/SecurityToken.java +++ b/src/main/java/eu/hsrw/ias/ggd/SecurityToken.java @@ -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); } - - - } diff --git a/src/main/java/eu/hsrw/ias/ggd/SensorData.java b/src/main/java/eu/hsrw/ias/ggd/SensorData.java new file mode 100644 index 0000000..a963d58 --- /dev/null +++ b/src/main/java/eu/hsrw/ias/ggd/SensorData.java @@ -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; + } +} diff --git a/src/main/java/Knowledgebase/SensorMap.java b/src/main/java/eu/hsrw/ias/ggd/SensorMap.java similarity index 92% rename from src/main/java/Knowledgebase/SensorMap.java rename to src/main/java/eu/hsrw/ias/ggd/SensorMap.java index 3145470..758adb7 100644 --- a/src/main/java/Knowledgebase/SensorMap.java +++ b/src/main/java/eu/hsrw/ias/ggd/SensorMap.java @@ -1,4 +1,4 @@ -package Knowledgebase; +package eu.hsrw.ias.ggd; import java.util.HashMap; diff --git a/src/main/resources/rules/Battery.drl b/src/main/resources/rules/Battery.drl index 800cb9d..615ca3c 100644 --- a/src/main/resources/rules/Battery.drl +++ b/src/main/resources/rules/Battery.drl @@ -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 \ No newline at end of file