You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2022/09/27 10:10:05 UTC

[plc4x] branch develop updated: feat(plc-simulator/cbus): unified cal data handling

This is an automated email from the ASF dual-hosted git repository.

sruehl 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 9878491a9 feat(plc-simulator/cbus): unified cal data handling
9878491a9 is described below

commit 9878491a9052fa1602239a13b46d50d041cd480e
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Tue Sep 27 12:09:57 2022 +0200

    feat(plc-simulator/cbus): unified cal data handling
---
 .../server/cbus/protocol/CBusServerAdapter.java    | 252 +++++++++++----------
 1 file changed, 133 insertions(+), 119 deletions(-)

diff --git a/plc4j/utils/plc-simulator/src/main/java/org/apache/plc4x/simulator/server/cbus/protocol/CBusServerAdapter.java b/plc4j/utils/plc-simulator/src/main/java/org/apache/plc4x/simulator/server/cbus/protocol/CBusServerAdapter.java
index 677e633cc..4fce92d72 100644
--- a/plc4j/utils/plc-simulator/src/main/java/org/apache/plc4x/simulator/server/cbus/protocol/CBusServerAdapter.java
+++ b/plc4j/utils/plc-simulator/src/main/java/org/apache/plc4x/simulator/server/cbus/protocol/CBusServerAdapter.java
@@ -133,7 +133,8 @@ public class CBusServerAdapter extends ChannelInboundHandlerAdapter {
                 LOGGER.info("Sending echo");
                 ctx.write(msg);
             }
-            handleReset((RequestReset) request);
+            LOGGER.info("Handling RequestReset\n{}", request);
+            handleReset();
         } else if (request instanceof RequestSmartConnectShortcut) {
             handleSmartConnect((RequestSmartConnectShortcut) request);
         }
@@ -142,118 +143,7 @@ public class CBusServerAdapter extends ChannelInboundHandlerAdapter {
     private void handleDirectCommandAccess(ChannelHandlerContext ctx, RequestDirectCommandAccess requestDirectCommandAccess) {
         CALData calData = requestDirectCommandAccess.getCalData();
         LOGGER.info("Handling RequestDirectCommandAccess\n{}", requestDirectCommandAccess);
-
-        // TODO: handle other cal data type
-        if (calData instanceof CALDataWrite) {
-            CALDataWrite calDataWrite = (CALDataWrite) calData;
-            Runnable acknowledger = () -> {
-                CALDataAcknowledge calDataAcknowledge = new CALDataAcknowledge(CALCommandTypeContainer.CALCommandAcknowledge, null, calDataWrite.getParamNo(), (short) 0x0, requestContext);
-                CALReplyShort calReply = new CALReplyShort((byte) 0x0, calDataAcknowledge, cBusOptions, requestContext);
-                EncodedReplyCALReply encodedReply = new EncodedReplyCALReply((byte) 0x0, calReply, cBusOptions, requestContext);
-                ReplyEncodedReply replyEncodedReply = new ReplyEncodedReply((byte) 0x0, encodedReply, null, cBusOptions, requestContext);
-                ReplyOrConfirmation replyOrConfirmation = new ReplyOrConfirmationReply((byte) 0x0, replyEncodedReply, new ResponseTermination(), cBusOptions, requestContext);
-                if (requestDirectCommandAccess.getAlpha() != null) {
-                    replyOrConfirmation = new ReplyOrConfirmationConfirmation((byte) 0x0, new Confirmation(requestDirectCommandAccess.getAlpha(), null, ConfirmationType.CONFIRMATION_SUCCESSFUL), replyOrConfirmation, cBusOptions, requestContext);
-                }
-                CBusMessageToClient cBusMessageToClient = new CBusMessageToClient(replyOrConfirmation, requestContext, cBusOptions);
-                LOGGER.info("Sending ack\n{}", cBusMessageToClient);
-                ctx.writeAndFlush(cBusMessageToClient);
-            };
-            switch (calDataWrite.getParamNo().getParameterType()) {
-                case APPLICATION_ADDRESS_1:
-                    ApplicationAddress1 applicationAddress1 = ((ParameterValueApplicationAddress1) calDataWrite.getParameterValue()).getValue();
-                    monitorApplicationAddress1 = applicationAddress1.getAddress();
-                    acknowledger.run();
-                    return;
-                case APPLICATION_ADDRESS_2:
-                    ApplicationAddress2 applicationAddress2 = ((ParameterValueApplicationAddress2) calDataWrite.getParameterValue()).getValue();
-                    monitorApplicationAddress2 = applicationAddress2.getAddress();
-                    acknowledger.run();
-                    return;
-                case INTERFACE_OPTIONS_1:
-                    InterfaceOptions1 interfaceOptions1 = ((ParameterValueInterfaceOptions1) calDataWrite.getParameterValue()).getValue();
-                    idmon = interfaceOptions1.getIdmon();
-                    monitor = interfaceOptions1.getMonitor();
-                    if (monitor) startMMIMonitor(ctx);
-                    else stopMMIMonitor();
-                    smart = interfaceOptions1.getSmart();
-                    srchk = interfaceOptions1.getSrchk();
-                    // TODO: add support for xonxoff
-                    // xonxoff = interfaceOptions1.getXonXoff();
-                    connect = interfaceOptions1.getConnect();
-                    if (connect) startSALMonitor(ctx);
-                    else stopSALMonitor();
-                    buildCBusOptions();
-                    acknowledger.run();
-                    return;
-                case INTERFACE_OPTIONS_2:
-                    InterfaceOptions2 interfaceOptions2 = ((ParameterValueInterfaceOptions2) calDataWrite.getParameterValue()).getValue();
-                    // TODO: add support for burden
-                    // burden =  interfaceOptions2.getBurden();
-                    // TODO: add support for clockgen
-                    // clockgen = interfaceOptions2.getClockGen();
-                    buildCBusOptions();
-                    acknowledger.run();
-                    return;
-                case INTERFACE_OPTIONS_3:
-                    InterfaceOptions3 interfaceOptions3Value = ((ParameterValueInterfaceOptions3) calDataWrite.getParameterValue()).getValue();
-                    boolean oldExstat = exstat;
-                    exstat = interfaceOptions3Value.getExstat();
-                    if (oldExstat != exstat) {
-                        LOGGER.info("Restarting monitors");
-                        stopMMIMonitor();
-                        startMMIMonitor(ctx);
-                        stopSALMonitor();
-                        startSALMonitor(ctx);
-                    }
-                    pun = interfaceOptions3Value.getPun();
-                    // TODO: add support for localsal
-                    // localsal = interfaceOptions3Value.getLocalSal();
-                    pcn = interfaceOptions3Value.getPcn();
-                    buildCBusOptions();
-                    acknowledger.run();
-                    return;
-                case BAUD_RATE_SELECTOR:
-                    BaudRateSelector baudRateSelector = ((ParameterValueBaudRateSelector) calDataWrite.getParameterValue()).getValue();
-                    // TODO: add support for baudrate
-                    // baudrate = baudRateSelector.getValue();
-                    buildCBusOptions();
-                    acknowledger.run();
-                    return;
-                case INTERFACE_OPTIONS_1_POWER_UP_SETTINGS:
-                    InterfaceOptions1 interfaceOptions1PowerUpSettings = ((ParameterValueInterfaceOptions1PowerUpSettings) calDataWrite.getParameterValue()).getValue().getInterfaceOptions1();
-                    idmon = interfaceOptions1PowerUpSettings.getIdmon();
-                    monitor = interfaceOptions1PowerUpSettings.getMonitor();
-                    if (monitor) startMMIMonitor(ctx);
-                    else stopMMIMonitor();
-                    smart = interfaceOptions1PowerUpSettings.getSmart();
-                    srchk = interfaceOptions1PowerUpSettings.getSrchk();
-                    // TODO: add support for xonxoff
-                    // xonxoff = interfaceOptions1PowerUpSettings.getXonXoff();
-                    connect = interfaceOptions1PowerUpSettings.getConnect();
-                    if (connect) startSALMonitor(ctx);
-                    else stopSALMonitor();
-                    buildCBusOptions();
-                    acknowledger.run();
-                    return;
-                case CUSTOM_MANUFACTURER:
-                    // TODO: handle other param typed
-                    acknowledger.run();
-                    return;
-                case SERIAL_NUMBER:
-                    // TODO: handle other param typed
-                    acknowledger.run();
-                    return;
-                case CUSTOM_TYPE:
-                    // TODO: handle other param typed
-                    acknowledger.run();
-                    return;
-                default:
-                    throw new IllegalStateException("Unmapped type");
-            }
-        } else if (calData instanceof CALDataIdentify) {
-            handleCalDataIdentify(ctx, (CALDataIdentify) calData, requestDirectCommandAccess.getAlpha());
-        }
+        handleCalData(ctx, calData, requestDirectCommandAccess.getAlpha());
     }
 
     private void handleRequestCommand(ChannelHandlerContext ctx, RequestCommand requestCommand) {
@@ -286,10 +176,7 @@ public class CBusServerAdapter extends ChannelInboundHandlerAdapter {
                 return;
             }
             CALData calData = command.getCalData();
-            // TODO: handle other Datatypes
-            if (calData instanceof CALDataIdentify) {
-                handleCalDataIdentify(ctx, (CALDataIdentify) calData, requestCommand.getAlpha());
-            }
+            handleCalData(ctx, calData, requestCommand.getAlpha());
             return;
         } else if (cbusCommand instanceof CBusCommandPointToMultiPoint) {
             CBusCommandPointToMultiPoint cBusCommandPointToMultiPoint = (CBusCommandPointToMultiPoint) cbusCommand;
@@ -378,6 +265,134 @@ public class CBusServerAdapter extends ChannelInboundHandlerAdapter {
         sendStatusBytes(ctx, "First parts {}", application, (byte) 0x0, statusBytes, requestCommand.getAlpha(), cBusOptions);
     }
 
+    private void handleCalData(ChannelHandlerContext ctx, CALData calData, Alpha alpha) {
+        if (calData instanceof CALDataGetStatus) {
+            // TODO: implement me
+        } else if (calData instanceof CALDataIdentify) {
+            handleCalDataIdentify(ctx, (CALDataIdentify) calData, alpha);
+        } else if (calData instanceof CALDataRecall) {
+            // TODO: implement me
+        } else if (calData instanceof CALDataReset) {
+            CALDataReset calDataReset = (CALDataReset) calData;
+            if (smart || connect) {
+                // On reset, we need to send the echo if we had not sent it above
+                LOGGER.info("Sending echo");
+                ctx.write(calDataReset);
+            }
+            LOGGER.info("Handling CALDataReset\n{}", calDataReset);
+            handleReset();
+        } else if (calData instanceof CALDataWrite) {
+            CALDataWrite calDataWrite = (CALDataWrite) calData;
+            Runnable acknowledger = () -> {
+                CALDataAcknowledge calDataAcknowledge = new CALDataAcknowledge(CALCommandTypeContainer.CALCommandAcknowledge, null, calDataWrite.getParamNo(), (short) 0x0, requestContext);
+                CALReplyShort calReply = new CALReplyShort((byte) 0x0, calDataAcknowledge, cBusOptions, requestContext);
+                EncodedReplyCALReply encodedReply = new EncodedReplyCALReply((byte) 0x0, calReply, cBusOptions, requestContext);
+                ReplyEncodedReply replyEncodedReply = new ReplyEncodedReply((byte) 0x0, encodedReply, null, cBusOptions, requestContext);
+                ReplyOrConfirmation replyOrConfirmation = new ReplyOrConfirmationReply((byte) 0x0, replyEncodedReply, new ResponseTermination(), cBusOptions, requestContext);
+                if (alpha != null) {
+                    replyOrConfirmation = new ReplyOrConfirmationConfirmation((byte) 0x0, new Confirmation(alpha, null, ConfirmationType.CONFIRMATION_SUCCESSFUL), replyOrConfirmation, cBusOptions, requestContext);
+                }
+                CBusMessageToClient cBusMessageToClient = new CBusMessageToClient(replyOrConfirmation, requestContext, cBusOptions);
+                LOGGER.info("Sending ack\n{}", cBusMessageToClient);
+                ctx.writeAndFlush(cBusMessageToClient);
+            };
+            switch (calDataWrite.getParamNo().getParameterType()) {
+                case APPLICATION_ADDRESS_1:
+                    ApplicationAddress1 applicationAddress1 = ((ParameterValueApplicationAddress1) calDataWrite.getParameterValue()).getValue();
+                    monitorApplicationAddress1 = applicationAddress1.getAddress();
+                    acknowledger.run();
+                    return;
+                case APPLICATION_ADDRESS_2:
+                    ApplicationAddress2 applicationAddress2 = ((ParameterValueApplicationAddress2) calDataWrite.getParameterValue()).getValue();
+                    monitorApplicationAddress2 = applicationAddress2.getAddress();
+                    acknowledger.run();
+                    return;
+                case INTERFACE_OPTIONS_1:
+                    InterfaceOptions1 interfaceOptions1 = ((ParameterValueInterfaceOptions1) calDataWrite.getParameterValue()).getValue();
+                    idmon = interfaceOptions1.getIdmon();
+                    monitor = interfaceOptions1.getMonitor();
+                    if (monitor) startMMIMonitor(ctx);
+                    else stopMMIMonitor();
+                    smart = interfaceOptions1.getSmart();
+                    srchk = interfaceOptions1.getSrchk();
+                    // TODO: add support for xonxoff
+                    // xonxoff = interfaceOptions1.getXonXoff();
+                    connect = interfaceOptions1.getConnect();
+                    if (connect) startSALMonitor(ctx);
+                    else stopSALMonitor();
+                    buildCBusOptions();
+                    acknowledger.run();
+                    return;
+                case INTERFACE_OPTIONS_2:
+                    InterfaceOptions2 interfaceOptions2 = ((ParameterValueInterfaceOptions2) calDataWrite.getParameterValue()).getValue();
+                    // TODO: add support for burden
+                    // burden =  interfaceOptions2.getBurden();
+                    // TODO: add support for clockgen
+                    // clockgen = interfaceOptions2.getClockGen();
+                    buildCBusOptions();
+                    acknowledger.run();
+                    return;
+                case INTERFACE_OPTIONS_3:
+                    InterfaceOptions3 interfaceOptions3Value = ((ParameterValueInterfaceOptions3) calDataWrite.getParameterValue()).getValue();
+                    boolean oldExstat = exstat;
+                    exstat = interfaceOptions3Value.getExstat();
+                    if (oldExstat != exstat) {
+                        LOGGER.info("Restarting monitors");
+                        stopMMIMonitor();
+                        startMMIMonitor(ctx);
+                        stopSALMonitor();
+                        startSALMonitor(ctx);
+                    }
+                    pun = interfaceOptions3Value.getPun();
+                    // TODO: add support for localsal
+                    // localsal = interfaceOptions3Value.getLocalSal();
+                    pcn = interfaceOptions3Value.getPcn();
+                    buildCBusOptions();
+                    acknowledger.run();
+                    return;
+                case BAUD_RATE_SELECTOR:
+                    BaudRateSelector baudRateSelector = ((ParameterValueBaudRateSelector) calDataWrite.getParameterValue()).getValue();
+                    // TODO: add support for baudrate
+                    // baudrate = baudRateSelector.getValue();
+                    buildCBusOptions();
+                    acknowledger.run();
+                    return;
+                case INTERFACE_OPTIONS_1_POWER_UP_SETTINGS:
+                    InterfaceOptions1 interfaceOptions1PowerUpSettings = ((ParameterValueInterfaceOptions1PowerUpSettings) calDataWrite.getParameterValue()).getValue().getInterfaceOptions1();
+                    idmon = interfaceOptions1PowerUpSettings.getIdmon();
+                    monitor = interfaceOptions1PowerUpSettings.getMonitor();
+                    if (monitor) startMMIMonitor(ctx);
+                    else stopMMIMonitor();
+                    smart = interfaceOptions1PowerUpSettings.getSmart();
+                    srchk = interfaceOptions1PowerUpSettings.getSrchk();
+                    // TODO: add support for xonxoff
+                    // xonxoff = interfaceOptions1PowerUpSettings.getXonXoff();
+                    connect = interfaceOptions1PowerUpSettings.getConnect();
+                    if (connect) startSALMonitor(ctx);
+                    else stopSALMonitor();
+                    buildCBusOptions();
+                    acknowledger.run();
+                    return;
+                case CUSTOM_MANUFACTURER:
+                    // TODO: handle other param typed
+                    acknowledger.run();
+                    return;
+                case SERIAL_NUMBER:
+                    // TODO: handle other param typed
+                    acknowledger.run();
+                    return;
+                case CUSTOM_TYPE:
+                    // TODO: handle other param typed
+                    acknowledger.run();
+                    return;
+                default:
+                    throw new IllegalStateException("Unmapped type");
+            }
+        } else {
+            throw new IllegalStateException("Unmapped type: " + calData.getClass());
+        }
+    }
+
     private static CBusMessage createCBusMessageForReply(Alpha alpha, CALReply calReply, CBusOptions cBusOptions) {
         EncodedReply encodedReply = new EncodedReplyCALReply((byte) 0x0, calReply, CBusServerAdapter.cBusOptions, CBusServerAdapter.requestContext);
         ReplyEncodedReply replyEncodedReply = new ReplyEncodedReply((byte) 0xC0, encodedReply, null, CBusServerAdapter.cBusOptions, CBusServerAdapter.requestContext);
@@ -590,8 +605,7 @@ public class CBusServerAdapter extends ChannelInboundHandlerAdapter {
         buildCBusOptions();
     }
 
-    private void handleReset(RequestReset requestReset) {
-        LOGGER.info("Handling RequestReset\n{}", requestReset);
+    private void handleReset() {
         connect = false;
         smart = false;
         idmon = false;