You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cd...@apache.org on 2020/08/11 17:48:59 UTC
[plc4x] branch develop updated: - Added a tiny bit of documentation
- Made the program send a different structure of JSON objects
This is an automated email from the ASF dual-hosted git repository.
cdutz pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git
The following commit(s) were added to refs/heads/develop by this push:
new 9ee5d54 - Added a tiny bit of documentation - Made the program send a different structure of JSON objects
9ee5d54 is described below
commit 9ee5d548c08bf5d65da91f01197d7e2b8365a55b
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Tue Aug 11 19:48:45 2020 +0200
- Added a tiny bit of documentation
- Made the program send a different structure of JSON objects
---
plc4j/examples/hello-webservice/README.adoc | 24 ++++++++++++++++++++
.../examples/hellowebservice/HelloWebservice.java | 26 +++++++++++++++++-----
2 files changed, 44 insertions(+), 6 deletions(-)
diff --git a/plc4j/examples/hello-webservice/README.adoc b/plc4j/examples/hello-webservice/README.adoc
new file mode 100644
index 0000000..d7eb380
--- /dev/null
+++ b/plc4j/examples/hello-webservice/README.adoc
@@ -0,0 +1,24 @@
+//
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+== Hello World: Webservice
+
+This little example generally uses the PLC4X subscription API and for every subscription event, it sends a JSON formatted request to a remote webservice.
+
+While generally the example should work with any sort of driver, the handling of incoming events is highly tied to the KNX driver using a `knxproj` file to decode the incoming messages.
+
+So if you would like to use it with any other sort of driver, you will probably need to update the HelloWebservice.ValueChangeHandler.accept method and adjust that to your usecase.
diff --git a/plc4j/examples/hello-webservice/src/main/java/org/apache/plc4x/java/examples/hellowebservice/HelloWebservice.java b/plc4j/examples/hello-webservice/src/main/java/org/apache/plc4x/java/examples/hellowebservice/HelloWebservice.java
index da5bfe5..f0a670f 100644
--- a/plc4j/examples/hello-webservice/src/main/java/org/apache/plc4x/java/examples/hellowebservice/HelloWebservice.java
+++ b/plc4j/examples/hello-webservice/src/main/java/org/apache/plc4x/java/examples/hellowebservice/HelloWebservice.java
@@ -20,6 +20,7 @@ package org.apache.plc4x.java.examples.hellowebservice;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
+import com.google.gson.JsonObject;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
@@ -32,12 +33,14 @@ import org.apache.plc4x.java.api.messages.PlcSubscriptionEvent;
import org.apache.plc4x.java.api.messages.PlcSubscriptionRequest;
import org.apache.plc4x.java.api.messages.PlcSubscriptionResponse;
import org.apache.plc4x.java.api.model.PlcSubscriptionHandle;
-import org.apache.plc4x.java.api.value.PlcValue;
+import org.apache.plc4x.java.api.value.PlcStruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
+import java.time.ZonedDateTime;
+import java.time.format.DateTimeFormatter;
import java.util.Scanner;
import java.util.function.Consumer;
@@ -114,12 +117,14 @@ public class HelloWebservice {
private final String webserviceUrl;
private final Gson gson;
+ private final DateTimeFormatter formatter;
public ValueChangeHandler(String webserviceUrl) {
this.webserviceUrl = webserviceUrl;
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setPrettyPrinting();
gson = gsonBuilder.create();
+ formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'hh:mm:ssZ");
}
@Override
@@ -128,15 +133,24 @@ public class HelloWebservice {
// Iterate over all the fields in this event and then simply output
// them to the console in a JSON format.
for (String fieldName : plcSubscriptionEvent.getFieldNames()) {
- final PlcValue plcValue = plcSubscriptionEvent.getPlcValue(fieldName);
-
- // Simply convert the event into a JSON object.
- String jsonString = gson.toJson(plcValue);
+ final PlcStruct plcValue = (PlcStruct) plcSubscriptionEvent.getPlcValue(fieldName);
+
+ // Create a JSON object that fits the structure of my remote webservice.
+ JsonObject output = new JsonObject();
+ output.addProperty("time", ZonedDateTime.now().format(formatter));
+ output.addProperty("device-id", plcValue.getValue("sourceAddress").getString());
+ output.addProperty("target-id", plcValue.getValue("targetAddress").getString());
+ output.addProperty("type", plcValue.getValue("description").getString());
+ output.addProperty("category", plcValue.getValue("function").getString());
+ JsonObject data = new JsonObject();
+ data.addProperty("unit-of-measurement", plcValue.getValue("unitOfMeasurement").getString());
+ data.addProperty("value", plcValue.getValue("value").getString());
+ output.add("data", data);
// Send the the json payload to the remote webservice.
HttpPost post = new HttpPost(webserviceUrl);
try {
- post.setEntity(new StringEntity(jsonString));
+ post.setEntity(new StringEntity(gson.toJson(output)));
} catch (UnsupportedEncodingException e) {
logger.error("Error encoding json string entity", e);
}