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 2021/03/08 16:36:46 UTC

[plc4x] branch develop updated: - Fixed the Firmata driver (The analog part now works again)

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 591fd57  - Fixed the Firmata driver (The analog part now works again)
591fd57 is described below

commit 591fd571737ef1cf317d5e4a0e9af55d5ae05e9d
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Mon Mar 8 17:36:39 2021 +0100

    - Fixed the Firmata driver (The analog part now works again)
---
 .../java/firmata/readwrite/context/FirmataDriverContext.java | 11 +++++++----
 .../firmata/readwrite/protocol/FirmataProtocolLogic.java     |  5 ++++-
 .../helloplc4x/subscription/HelloPlc4xSubscription.java      | 12 +++++++++---
 3 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/plc4j/drivers/firmata/src/main/java/org/apache/plc4x/java/firmata/readwrite/context/FirmataDriverContext.java b/plc4j/drivers/firmata/src/main/java/org/apache/plc4x/java/firmata/readwrite/context/FirmataDriverContext.java
index 03577fe..35ae9dc 100644
--- a/plc4j/drivers/firmata/src/main/java/org/apache/plc4x/java/firmata/readwrite/context/FirmataDriverContext.java
+++ b/plc4j/drivers/firmata/src/main/java/org/apache/plc4x/java/firmata/readwrite/context/FirmataDriverContext.java
@@ -23,6 +23,8 @@ import org.apache.plc4x.java.api.exceptions.PlcRuntimeException;
 import org.apache.plc4x.java.api.messages.PlcSubscriptionRequest;
 import org.apache.plc4x.java.api.messages.PlcWriteRequest;
 import org.apache.plc4x.java.api.model.PlcField;
+import org.apache.plc4x.java.api.model.PlcSubscriptionField;
+import org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionField;
 import org.apache.plc4x.java.spi.values.PlcList;
 import org.apache.plc4x.java.api.value.PlcValue;
 import org.apache.plc4x.java.firmata.readwrite.*;
@@ -89,8 +91,9 @@ public class FirmataDriverContext implements DriverContext {
         Map<Integer, PinMode> requestAnalogFieldPinModes = new HashMap<>();
         for (String fieldName : subscriptionRequest.getFieldNames()) {
             final PlcField field = subscriptionRequest.getField(fieldName);
-            if(field instanceof FirmataFieldDigital) {
-                FirmataFieldDigital fieldDigital = (FirmataFieldDigital) field;
+            DefaultPlcSubscriptionField subscriptionField = (DefaultPlcSubscriptionField) field;
+            if(subscriptionField.getPlcField() instanceof FirmataFieldDigital) {
+                FirmataFieldDigital fieldDigital = (FirmataFieldDigital) subscriptionField.getPlcField();
                 PinMode fieldPinMode = (fieldDigital.getPinMode() != null) ?
                     fieldDigital.getPinMode() : PinMode.PinModeInput;
                 if(!(fieldPinMode.equals(PinMode.PinModeInput) || fieldPinMode.equals(PinMode.PinModePullup))) {
@@ -99,8 +102,8 @@ public class FirmataDriverContext implements DriverContext {
                 for(int pin = fieldDigital.getAddress(); pin < fieldDigital.getAddress() + fieldDigital.getNumberOfElements(); pin++) {
                     requestDigitalFieldPinModes.put(pin, fieldPinMode);
                 }
-            } else if(field instanceof FirmataFieldAnalog) {
-                FirmataFieldAnalog fieldAnalog = (FirmataFieldAnalog) field;
+            } else if(subscriptionField.getPlcField() instanceof FirmataFieldAnalog) {
+                FirmataFieldAnalog fieldAnalog = (FirmataFieldAnalog) subscriptionField.getPlcField();
                 for(int pin = fieldAnalog.getAddress(); pin < fieldAnalog.getAddress() + fieldAnalog.getNumberOfElements(); pin++) {
                     requestAnalogFieldPinModes.put(pin, PinMode.PinModeInput);
                 }
diff --git a/plc4j/drivers/firmata/src/main/java/org/apache/plc4x/java/firmata/readwrite/protocol/FirmataProtocolLogic.java b/plc4j/drivers/firmata/src/main/java/org/apache/plc4x/java/firmata/readwrite/protocol/FirmataProtocolLogic.java
index 89d7905..8db551c 100644
--- a/plc4j/drivers/firmata/src/main/java/org/apache/plc4x/java/firmata/readwrite/protocol/FirmataProtocolLogic.java
+++ b/plc4j/drivers/firmata/src/main/java/org/apache/plc4x/java/firmata/readwrite/protocol/FirmataProtocolLogic.java
@@ -36,6 +36,7 @@ import org.apache.plc4x.java.spi.Plc4xProtocolBase;
 import org.apache.plc4x.java.spi.messages.*;
 import org.apache.plc4x.java.spi.messages.utils.ResponseItem;
 import org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration;
+import org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionField;
 import org.apache.plc4x.java.spi.values.PlcBOOL;
 import org.apache.plc4x.java.spi.values.PlcDINT;
 import org.apache.plc4x.java.spi.values.PlcList;
@@ -118,7 +119,9 @@ public class FirmataProtocolLogic extends Plc4xProtocolBase<FirmataMessage> impl
             }
             Map<String, ResponseItem<PlcSubscriptionHandle>> result = new HashMap<>();
             for (String fieldName : subscriptionRequest.getFieldNames()) {
-                FirmataField field = (FirmataField) subscriptionRequest.getField(fieldName);
+                DefaultPlcSubscriptionField subscriptionField =
+                    (DefaultPlcSubscriptionField) subscriptionRequest.getField(fieldName);
+                FirmataField field = (FirmataField) subscriptionField.getPlcField();
                 result.put(fieldName, new ResponseItem<>(PlcResponseCode.OK,
                     new FirmataSubscriptionHandle(this, fieldName, field)));
             }
diff --git a/plc4j/examples/hello-world-plc4x-subscription/src/main/java/org/apache/plc4x/java/examples/helloplc4x/subscription/HelloPlc4xSubscription.java b/plc4j/examples/hello-world-plc4x-subscription/src/main/java/org/apache/plc4x/java/examples/helloplc4x/subscription/HelloPlc4xSubscription.java
index 94e0cc7..4455fba 100644
--- a/plc4j/examples/hello-world-plc4x-subscription/src/main/java/org/apache/plc4x/java/examples/helloplc4x/subscription/HelloPlc4xSubscription.java
+++ b/plc4j/examples/hello-world-plc4x-subscription/src/main/java/org/apache/plc4x/java/examples/helloplc4x/subscription/HelloPlc4xSubscription.java
@@ -110,9 +110,15 @@ public class HelloPlc4xSubscription {
             // 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 = new Gson().toJson(plcValue);
-                logger.info(String.format("Field '%s' value: %s", fieldName, jsonString));
+                if(plcValue.isList()) {
+                    StringBuilder sb = new StringBuilder(String.format("Field '%s' value:", fieldName));
+                    for (PlcValue value : plcValue.getList()) {
+                        sb.append(" ").append(value.getString());
+                    }
+                    logger.info(sb.toString());
+                } else {
+                    logger.info(String.format("Field '%s' value: %s", fieldName, plcValue.getString()));
+                }
             }
         }
     }