forked from kevin.shehu/GGD
CHANGE: REDEFINING THE STRUCTURE
FIX: ApiCall.java ADD: ISIS IC Server REQ ADD: RULES
This commit is contained in:
98
src/main/java/eu/hsrw/ias/ggd/ApiCall.java
Normal file
98
src/main/java/eu/hsrw/ias/ggd/ApiCall.java
Normal file
@@ -0,0 +1,98 @@
|
||||
package eu.hsrw.ias.ggd;
|
||||
|
||||
import Credentials.UserCredentials;
|
||||
import Files.WriteFile;
|
||||
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 {
|
||||
|
||||
|
||||
UserCredentials userCredentials = new UserCredentials();
|
||||
final String username = userCredentials.getUsername();
|
||||
final String password = userCredentials.getPassword();
|
||||
final String homeRoot = userCredentials.getHomeRoot();
|
||||
String outputToken;
|
||||
WriteFile writingIntoFile = new WriteFile();
|
||||
Date now; // to display current time
|
||||
now = new Date();
|
||||
if (token.equals(oldId)) {
|
||||
//do nothing
|
||||
} else {
|
||||
try {
|
||||
outputToken = SecurityToken.OnCallMethod(username, password);
|
||||
JSONObject obj = new JSONObject(outputToken);
|
||||
token = obj.getString("id");
|
||||
|
||||
writingIntoFile.WriteFile(homeRoot+"securityToken.txt", token);
|
||||
oldId = token;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
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, 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);
|
||||
}
|
||||
|
||||
// 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;
|
||||
|
||||
}
|
||||
|
||||
// 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);
|
||||
//
|
||||
// }
|
||||
//
|
||||
//
|
||||
// }
|
||||
|
||||
}
|
||||
25
src/main/java/eu/hsrw/ias/ggd/Isis.java
Normal file
25
src/main/java/eu/hsrw/ias/ggd/Isis.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package eu.hsrw.ias.ggd;
|
||||
|
||||
import HttpCall.HttpPost;
|
||||
|
||||
public class Isis {
|
||||
public static String OnCallMethod(String deviceName, String sensorName) throws Exception {
|
||||
//CONFIG PARAMETERS:
|
||||
//BEGIN------------CONFIG PARAMETERS BELOW TO YOUR ENVIRONMENT---------------------------------------
|
||||
String baseURL = "https://api.dev.whysor.com/devices/readValueFromSpeech?access_token=2MreqC9fqXxFHJBEcrCL38LKhEaVcEHmEi6o45CwTV9SSgTAuZ93oiMfY3HVc2fj";
|
||||
final String body = String.format("{\"deviceName\": \"%s\", \"sensorName\": \"%s\", \"domain\": \"my.yookr.org\"}", deviceName, sensorName);
|
||||
HttpPost httpMethodPost = new HttpPost();
|
||||
httpMethodPost.HttpCallPost(baseURL,body);
|
||||
|
||||
return httpMethodPost.HttpCallPost(baseURL, body);
|
||||
}
|
||||
public static void main(String args[]){
|
||||
try {
|
||||
String output = OnCallMethod("Climate", "humidity");
|
||||
System.out.println(output);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
14
src/main/java/eu/hsrw/ias/ggd/MainExe.java
Normal file
14
src/main/java/eu/hsrw/ias/ggd/MainExe.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package eu.hsrw.ias.ggd;
|
||||
|
||||
import java.util.Timer;
|
||||
|
||||
public class MainExe {
|
||||
public static void main(String[] args){
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
53
src/main/java/eu/hsrw/ias/ggd/ScheduledTask.java
Normal file
53
src/main/java/eu/hsrw/ias/ggd/ScheduledTask.java
Normal file
@@ -0,0 +1,53 @@
|
||||
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;
|
||||
import org.kie.api.runtime.rule.FactHandle;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.TimerTask;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
// Create a class extends with TimerTask
|
||||
public class ScheduledTask extends TimerTask {
|
||||
|
||||
Date now; // to display current time
|
||||
HashMap<String, Double> outputDevices ;
|
||||
|
||||
// Add your task here
|
||||
public void run() {
|
||||
// Display current time
|
||||
now = new Date(); // initialize date
|
||||
System.out.println("Time is :" + now);
|
||||
|
||||
try {
|
||||
outputDevices = ApiCall.FetchDataFromApi();
|
||||
System.out.println("Final Size"+outputDevices.size()+outputDevices);
|
||||
KieServices ks = KieServices.Factory.get();
|
||||
KieContainer kContainer = ks.getKieClasspathContainer();
|
||||
//Get the session named kseesion-rule that we defined in kmodule.xml above.
|
||||
//Also by default the session returned is always stateful.
|
||||
KieSession kSession = kContainer.newKieSession("ksession-rule");
|
||||
FactHandle fact1;
|
||||
fact1 = kSession.insert(outputDevices);
|
||||
System.out.println(fact1);
|
||||
kSession.fireAllRules();
|
||||
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
} catch (TimeoutException e) {
|
||||
e.printStackTrace();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
26
src/main/java/eu/hsrw/ias/ggd/SecurityToken.java
Normal file
26
src/main/java/eu/hsrw/ias/ggd/SecurityToken.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package eu.hsrw.ias.ggd;
|
||||
|
||||
import HttpCall.HttpPost;
|
||||
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
public class SecurityToken {
|
||||
public static String OnCallMethod(String username, String password) throws Exception {
|
||||
//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);
|
||||
HttpPost httpMethodPost = new HttpPost();
|
||||
|
||||
return httpMethodPost.HttpCallPost(baseURL, body);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user