ADD NEW STRUCUTE

master
Kevin Shehu 2021-05-05 14:21:30 +02:00
parent fd96876544
commit e51ce37fe3
23 changed files with 354 additions and 106 deletions

View File

@ -3,7 +3,7 @@
The service of decision support system is inferring knowledge from the available data of the greenhouse, the user input, and historical data what a user/the gardener should do & which action a gardener should execute to be successful with the growing process of the plants.
These actions are called goals.
The list of inferred active goals is offered to other services of the system.
The approach of the DSS is based on three main concepts:
The approach of the DSS is based on three MainExe concepts:
**Fact knowledge (Knowledgebase)**

29
pom.xml
View File

@ -60,6 +60,35 @@
</dependency>
<!-- https://mvnrepository.com/artifact/org.drools/drools-core -->
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-core</artifactId>
<version>7.48.0.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.drools/drools-compiler -->
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
<version>7.48.0.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.drools/drools-decisiontables -->
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-decisiontables</artifactId>
<version>7.48.0.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.kie/kie-api -->
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-api</artifactId>
<version>7.48.0.Final</version>
<scope>provided</scope>
</dependency>
</dependencies>

View File

@ -1,14 +1,18 @@
import org.eclipse.jetty.http.HttpHeader;
import Credentials.UserCredentials;
import Files.ReadFile;
import Files.WriteFile;
import HttpCall.HttpCall;
import HttpCall.HttpPost;
import org.apache.jena.base.Sys;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.net.PasswordAuthentication;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpHeaders;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.concurrent.CompletableFuture;
@ -16,63 +20,63 @@ import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class ApiCall {
private static String token ="0";
private static String oldId="1";
private static String token = "0";
private static String oldId = "1";
public static String FetchDataFromApi(String extension) throws InterruptedException, ExecutionException, TimeoutException {
UserCredentials userCredentials = new UserCredentials();
final String username = userCredentials.getUsername();
final String password = userCredentials.getPassword();
String baseURL = userCredentials.getBaseURL();
String outputToken;
WriteFile writingIntoFile = new WriteFile();
HttpCall httpCall = new HttpCall();
String devicesResult = "";
final String username = "kevin.shehu@hochschule-rhein-waal.de";
final String password = "DK7SxFkGJgnLhnU3";
String baseURL = "https://api.dev.whysor.com/";
String outputToken = null;
if (token.equals(oldId)){
if (token.equals(oldId)) {
//do nothing
}
else {
SecurityToken securityToken = new SecurityToken();
} else {
try {
outputToken = securityToken.OnCallMethod(username,password);
outputToken = SecurityToken.OnCallMethod(username, password);
JSONObject obj = new JSONObject(outputToken);
token = obj.getString("id");
writingIntoFile.WriteFile("/Users/ksh/Documents/Work/GGD/src/main/java/Data/securityToken.txt", token);
oldId = token;
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (TimeoutException e) {
} catch (Exception e) {
e.printStackTrace();
}
}
try {
devicesResult = httpCall.HttpGet(baseURL + extension + "?access_token=" + token,token);
JSONArray jsonarray = new JSONArray(devicesResult);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
String id = jsonobject.getString("id");
System.out.println(id);
writingIntoFile.WriteFile("/Users/ksh/Documents/Work/GGD/src/main/java/Data/devicesID.txt", id);
}
System.out.println(token);
var client = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
.followRedirects(HttpClient.Redirect.ALWAYS)
.build();
HttpRequest request = null;
} catch (Exception e) {
e.printStackTrace();
}
try {
request = HttpRequest.newBuilder(new URI(baseURL+extension))
.POST(HttpRequest.BodyPublishers.ofString(extension))
.setHeader("Authorization" , token)
.setHeader("Content-Type", "application/json")
.setHeader("Accept", "application/json")
.build();
try {
writingIntoFile.WriteFile("/Users/ksh/Documents/Work/GGD/src/main/java/Data/" + extension + "Output.json", devicesResult);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return devicesResult;
} catch (URISyntaxException e) {
e.printStackTrace();
}
CompletableFuture<HttpResponse<String>> response = client.sendAsync(request, HttpResponse.BodyHandlers.ofString());
String result = response.thenApply(HttpResponse::body).get(5, TimeUnit.SECONDS);
return result;
}

View File

@ -0,0 +1,42 @@
package Credentials;
public class UserCredentials {
private String username = "kevin.shehu@hochschule-rhein-waal.de";
private String password = "DK7SxFkGJgnLhnU3";
private String baseURL = "https://api.dev.whysor.com/";
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public String getBaseURL() {
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;
}
@Override
public String toString() {
return "UserCredentials{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", baseURL='" + baseURL + '\'' +
'}';
}
}

View File

@ -0,0 +1 @@
e5728985-f4f5-4b9a-ae32-9d6ae40f9f53

View File

@ -0,0 +1 @@
[{"id":"087c830b-1173-4b5b-bdbc-c139856cf6aa","externalDomain":["kpn-lora","modelapi"],"externalId":"70B3D57050004C5A","name":"70B3D57050004C5A","description":"6kg sensor","additionalInfo":{"location":{"latitude":51.289406,"longitude":6.042}},"deviceTemplateId":"7be8a213-65e9-4084-b73b-9d5cfcc46cc0","organizationId":"46224b7f-bf6e-4a14-8844-c8d9258a58bf"},{"id":"09d69136-7bdb-420e-83eb-8185d39b06c3","externalDomain":["kpn-lora","modelapi"],"externalId":"70B3D570500046AA","name":"70B3D570500046AA","description":"6kg sensor","additionalInfo":{},"deviceTemplateId":"7be8a213-65e9-4084-b73b-9d5cfcc46cc0","organizationId":"46224b7f-bf6e-4a14-8844-c8d9258a58bf"},{"id":"142ad259-c570-49cc-bf58-99105231c051","externalDomain":["kpn-lora","modelapi"],"externalId":"70B3D57050004657","name":"70B3D57050004657","description":"6kg sensor","additionalInfo":{},"deviceTemplateId":"7be8a213-65e9-4084-b73b-9d5cfcc46cc0","organizationId":"46224b7f-bf6e-4a14-8844-c8d9258a58bf"},{"id":"1f6184e2-3cfb-43e7-83a4-ea175c8590f4","externalDomain":["kpn-lora","modelapi"],"externalId":"70B3D5705000473D","name":"70B3D5705000473D","description":"","additionalInfo":{},"deviceTemplateId":"7be8a213-65e9-4084-b73b-9d5cfcc46cc0","organizationId":"46224b7f-bf6e-4a14-8844-c8d9258a58bf"},{"id":"21c94566-202d-4c12-af0d-035b8db19d32","externalDomain":["kpn-lora","modelapi"],"externalId":"70B3D57050004A7B","name":"70B3D57050004A7B","description":"","additionalInfo":{},"deviceTemplateId":"7be8a213-65e9-4084-b73b-9d5cfcc46cc0","organizationId":"46224b7f-bf6e-4a14-8844-c8d9258a58bf"},{"id":"98e242b8-e49d-4eb5-a56e-a5665c05b3a3","externalDomain":["kpn-lora","modelapi"],"externalId":"70B3D57050004619","name":"70B3D57050004619","description":"","additionalInfo":{},"deviceTemplateId":"7be8a213-65e9-4084-b73b-9d5cfcc46cc0","organizationId":"46224b7f-bf6e-4a14-8844-c8d9258a58bf"},{"id":"b4f3ca19-1648-4df1-a83d-089c8064a6ff","externalDomain":["kpn-lora","modelapi"],"externalId":"70B3D57050004636","name":"70B3D57050004636","description":"6kg sensor","additionalInfo":{},"deviceTemplateId":"7be8a213-65e9-4084-b73b-9d5cfcc46cc0","organizationId":"46224b7f-bf6e-4a14-8844-c8d9258a58bf"},{"id":"b61c30d1-9281-4fc7-ab70-a43861fe0264","externalDomain":["kpn-lora","modelapi"],"externalId":"70B3D5705000471F","name":"70B3D5705000471F","description":"","additionalInfo":{},"deviceTemplateId":"7be8a213-65e9-4084-b73b-9d5cfcc46cc0","organizationId":"46224b7f-bf6e-4a14-8844-c8d9258a58bf"},{"id":"c97e36da-8274-43aa-a226-7e1bf8b943b0","externalDomain":["kpn-lora","modelapi"],"externalId":"70B3D5705000473C","name":"70B3D5705000473C","description":"","additionalInfo":{},"deviceTemplateId":"7be8a213-65e9-4084-b73b-9d5cfcc46cc0","organizationId":"46224b7f-bf6e-4a14-8844-c8d9258a58bf"},{"id":"e5728985-f4f5-4b9a-ae32-9d6ae40f9f53","externalDomain":["kpn-lora","modelapi"],"externalId":"70B3D5705000461C","name":"70B3D5705000461C","description":"6kg sensor","additionalInfo":{"location":{"latitude":51.28905,"longitude":6.042781}},"deviceTemplateId":"7be8a213-65e9-4084-b73b-9d5cfcc46cc0","organizationId":"46224b7f-bf6e-4a14-8844-c8d9258a58bf"}]

View File

@ -0,0 +1 @@
ZcUXmzWkBB5eJSzcllxFlUpgWh7ujC2T6TdB9YdXr5ddERrjiHsp5rmPYrpG5sCU

View File

@ -0,0 +1,28 @@
package Files;
import java.io.File; // Import the File class
import java.io.FileNotFoundException; // Import this class to handle errors
import java.util.Scanner; // Import the Scanner class to read text files
public class ReadFile {
public String data;
public String ReadFromFile(String path){
try {
File myObj = new File(path);
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
data = myReader.nextLine();
System.out.println(data);
}
myReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
return data;
}
}

View File

@ -0,0 +1,38 @@
package Files;
import java.io.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
public class WriteFile {
BufferedWriter buffer = null;
public void WriteFile(String path, String input) throws Exception{
try {
File file = new File(path);
if (!file.exists()) {
file.createNewFile();
}
FileWriter fileWriter = new FileWriter(path);
buffer = new BufferedWriter(fileWriter);
buffer.write(input);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (buffer != null)
buffer.flush();
// buffer.close();
} catch (Exception ex) {
System.out.println("Error in closing the BufferedWriter" + ex);
}
}
}
}

View File

@ -0,0 +1,40 @@
package HttpCall;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.concurrent.CompletableFuture;
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 {
var client = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
.followRedirects(HttpClient.Redirect.ALWAYS)
.build();
HttpRequest request = null;
try {
request = HttpRequest.newBuilder(new URI(baseURL))
.setHeader("Authorization", token)
// .setHeader("access_token", token)
.setHeader("Content-Type", "application/json")
.setHeader("Accept", "application/json")
.header("Authority", "https://api.dev.whysor.com/")
.build();
} catch (URISyntaxException e) {
e.printStackTrace();
}
CompletableFuture<HttpResponse<String>> response = client.sendAsync(request, HttpResponse.BodyHandlers.ofString());
String result = response.thenApply(HttpResponse::body).get(5, TimeUnit.SECONDS);
return result;
}
}

View File

@ -0,0 +1,42 @@
package HttpCall;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
public class HttpPost {
public String HttpCallPost (String baseUrl, String body) throws Exception {
var client = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
.followRedirects(HttpClient.Redirect.ALWAYS)
.build();
HttpRequest request = null;
try {
request = HttpRequest.newBuilder(new URI(baseUrl))
.POST(HttpRequest.BodyPublishers.ofString(body))
.setHeader("Content-Type", "application/json")
.setHeader("Accept", "application/json")
.header("Authority", "https://api.dev.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

@ -32,7 +32,7 @@ public class JSON2RDF {
}
}
// @CommandLine.Parameters(paramLabel = "D:\\WORK\\GGD\\src\\main\\java\\Data", description = "json file")
// @CommandLine.Parameters(paramLabel = "D:\\WORK\\GGD\\src\\MainExe\\java\\Data", description = "json file")
@CommandLine.Option(names = { "--input-charset" }, description = "Input charset (default: ${DEFAULT-VALUE})")
private final Charset inputCharset = StandardCharsets.UTF_8;

View File

@ -2,17 +2,12 @@ import java.util.Timer;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
public class main {
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 ); // Create Repetitively task for every 1 secs
time.schedule(scheduledTask, 0, 1000 * 60 * 1 ); // Create Repetitively task for every 1 secs
}
}

View File

@ -1,16 +0,0 @@
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.query.ResultSetFormatter;
import org.apache.jena.rdfconnection.RDFConnectionFuseki;
import org.apache.jena.rdfconnection.RDFConnectionRemoteBuilder;
public class RDFConnectionExample {
public static void main(String ...args) {
RDFConnectionRemoteBuilder builder = RDFConnectionFuseki.create()
.destination("http://localhost:3030/fuseki");
}
}

View File

@ -0,0 +1,5 @@
package RuleEngine;
public class KalunaRules {
}

View File

@ -1,3 +1,11 @@
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.TimerTask;
import java.util.Date;
import java.util.concurrent.ExecutionException;
@ -7,16 +15,20 @@ import java.util.concurrent.TimeoutException;
public class ScheduledTask extends TimerTask {
Date now; // to display current time
BufferedWriter bw = null;
// Add your task here
public void run() {
// Display current time
now = new Date(); // initialize date
System.out.println("Time is :" + now);
ApiCall apiCall = new ApiCall();
String output = null;
String outputDevices = "";
String devicesExtension = "devices";
String sensorExtension = "sensors"; //read and readMultiple
try {
output = apiCall.FetchDataFromApi("devices");
outputDevices = ApiCall.FetchDataFromApi(devicesExtension);
// outputDevices = ApiCall.FetchDataFromApi(sensorExtension);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
@ -24,6 +36,9 @@ public class ScheduledTask extends TimerTask {
} catch (TimeoutException e) {
e.printStackTrace();
}
System.out.println("Output :" + output); // Display current output
System.out.println("Output :" + outputDevices); // Display current output
}
}

View File

@ -1,50 +1,24 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.concurrent.CompletableFuture;
import HttpCall.HttpPost;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class SecurityToken {
public static String OnCallMethod(String username, String password) throws InterruptedException, ExecutionException, TimeoutException {
public static String OnCallMethod(String username, String password) throws Exception {
//CONFIG PARAMETERS:
//BEGIN------------CONFIG PARAMETERS BELOW TO YOUR ENVIRONMENT---------------------------------------
String baseURL = "https://api.dev.whysor.com/users/login";
final String body= String.format("{\"email\": \"%s\", \"password\": \"%s\", \"domain\": \"my.dev.yookr.org\"}", username, password);
final String body = String.format("{\"email\": \"%s\", \"password\": \"%s\", \"domain\": \"my.dev.yookr.org\"}", username, password);
HttpPost httpMethodPost = new HttpPost();
var client = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
.followRedirects(HttpClient.Redirect.ALWAYS)
.build();
HttpRequest request = null;
return httpMethodPost.HttpCallPost(baseURL, body);
try {
request = HttpRequest.newBuilder(new URI(baseURL))
.POST(HttpRequest.BodyPublishers.ofString(body))
.setHeader("Content-Type", "application/json")
.setHeader("Accept", "application/json")
.header("Authority","https://api.dev.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;
// return HttpResponse.BodyHandlers.ofFile(Paths.get("D:\\WORK\\GGD\\src\\main\\java\\Data\\securityToken.json"));
}
}

View File

@ -0,0 +1,24 @@
package Sensors;
public class Sensors {
public enum SensorType {
temperature, humidity
}
public enum Type {
KALUNA, TULIPS, TOMATOs
}
private int sensorId;
private String sensorDescription;
private int sensorBattery;
private int sensorOutput;
private int latitude;
private int longitude;
}

View File

@ -0,0 +1,25 @@
import Credentials.UserCredentials;
import Files.ReadFile;
import org.json.simple.JSONArray;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
public class SpecificApiCall {
public static String FetchSpecificDataWithID(String extension) {
JSONParser parser = new JSONParser();
UserCredentials userCredentials = new UserCredentials();
ReadFile fileReader = new ReadFile();
String token = fileReader.ReadFromFile("/Users/ksh/Documents/Work/GGD/src/MainExe/java/Data/securityToken.txt");
return token;
}
}