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/01 10:50:53 UTC

[plc4x] 02/03: feat(plc-simulator/cbus): added support for installation mmi requests

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

commit 720c603a8ac5b97bfec8bf200d1f52a5185f81a9
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Thu Sep 1 12:49:50 2022 +0200

    feat(plc-simulator/cbus): added support for installation mmi requests
---
 .../apache/plc4x/java/cbus/RandomPackagesTest.java | 70 ++++++++++++-----
 .../server/cbus/protocol/CBusServerAdapter.java    | 88 ++++++++++++++++++++++
 2 files changed, 138 insertions(+), 20 deletions(-)

diff --git a/plc4j/drivers/c-bus/src/test/java/org/apache/plc4x/java/cbus/RandomPackagesTest.java b/plc4j/drivers/c-bus/src/test/java/org/apache/plc4x/java/cbus/RandomPackagesTest.java
index 03e2328fb..55533ab1a 100644
--- a/plc4j/drivers/c-bus/src/test/java/org/apache/plc4x/java/cbus/RandomPackagesTest.java
+++ b/plc4j/drivers/c-bus/src/test/java/org/apache/plc4x/java/cbus/RandomPackagesTest.java
@@ -22,6 +22,7 @@ import org.apache.plc4x.java.cbus.readwrite.*;
 import org.apache.plc4x.java.spi.generation.ReadBufferByteBased;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Disabled;
+import org.junit.jupiter.api.Nested;
 import org.junit.jupiter.api.Test;
 
 import java.nio.charset.StandardCharsets;
@@ -401,27 +402,56 @@ public class RandomPackagesTest {
         assertMessageMatches(bytes, msg);
     }
 
-    @Test
-    void initiateMMI() throws Exception {
-        byte[] bytes = (  "\\05FF007AFF0083g\r").getBytes(StandardCharsets.UTF_8);
-        ReadBufferByteBased readBufferByteBased = new ReadBufferByteBased(bytes);
-        cBusOptions = C_BUS_OPTIONS_WITH_SRCHK;
-        CBusMessage msg = CBusMessage.staticParse(readBufferByteBased, false, requestContext, cBusOptions);
-        assertThat(msg).isNotNull();
-        System.out.println(msg);
-
-        assertMessageMatches(bytes, msg);
+    @Nested
+    class InstallationMMI {
+        @Test
+        void initiateMMI() throws Exception {
+            byte[] bytes = ("\\05FF007AFF0083g\r").getBytes(StandardCharsets.UTF_8);
+            ReadBufferByteBased readBufferByteBased = new ReadBufferByteBased(bytes);
+            cBusOptions = C_BUS_OPTIONS_WITH_SRCHK;
+            CBusMessage msg = CBusMessage.staticParse(readBufferByteBased, false, requestContext, cBusOptions);
+            assertThat(msg).isNotNull();
+            System.out.println(msg);
+
+            assertMessageMatches(bytes, msg);
+        }
+
+        @Test
+        void initiateMMIReponse1() throws Exception {
+            byte[] bytes = ("g.86020200F900FF0094120006000000000000000008000000000000000000CA\r\n").getBytes(StandardCharsets.UTF_8);
+            ReadBufferByteBased readBufferByteBased = new ReadBufferByteBased(bytes);
+            cBusOptions = C_BUS_OPTIONS_WITH_SRCHK;
+            CBusMessage msg = CBusMessage.staticParse(readBufferByteBased, true, requestContext, cBusOptions);
+            assertThat(msg).isNotNull();
+            System.out.println(msg);
+
+            assertMessageMatches(bytes, msg);
+        }
+
+        @Test
+        void initiateMMIReponse2() throws Exception {
+            byte[] bytes = ("86020200F900FF580000000000000000000000000000000000000000000026\r\n").getBytes(StandardCharsets.UTF_8);
+            ReadBufferByteBased readBufferByteBased = new ReadBufferByteBased(bytes);
+            cBusOptions = C_BUS_OPTIONS_WITH_SRCHK;
+            CBusMessage msg = CBusMessage.staticParse(readBufferByteBased, true, requestContext, cBusOptions);
+            assertThat(msg).isNotNull();
+            System.out.println(msg);
+
+            assertMessageMatches(bytes, msg);
+        }
+
+        @Test
+        void initiateMMIReponse3() throws Exception {
+            byte[] bytes = ("86020200F700FFB00000000000000000000000000000000000000000D0\r\n").getBytes(StandardCharsets.UTF_8);
+            ReadBufferByteBased readBufferByteBased = new ReadBufferByteBased(bytes);
+            cBusOptions = C_BUS_OPTIONS_WITH_SRCHK;
+            CBusMessage msg = CBusMessage.staticParse(readBufferByteBased, true, requestContext, cBusOptions);
+            assertThat(msg).isNotNull();
+            System.out.println(msg);
+
+            assertMessageMatches(bytes, msg);
+        }
     }
 
-    @Test
-    void initiateMMIReponse() throws Exception {
-        byte[] bytes = (  "g.86020200F900FF0094120006000000000000000008000000000000000000CA\r\n").getBytes(StandardCharsets.UTF_8);
-        ReadBufferByteBased readBufferByteBased = new ReadBufferByteBased(bytes);
-        cBusOptions = C_BUS_OPTIONS_WITH_SRCHK;
-        CBusMessage msg = CBusMessage.staticParse(readBufferByteBased, true, requestContext, cBusOptions);
-        assertThat(msg).isNotNull();
-        System.out.println(msg);
 
-        assertMessageMatches(bytes, msg);
-    }
 }
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 bf790c6d6..7960ca965 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
@@ -286,6 +286,11 @@ public class CBusServerAdapter extends ChannelInboundHandlerAdapter {
                 if (statusRequest instanceof StatusRequestBinaryState) {
                     StatusRequestBinaryState statusRequestBinaryState = (StatusRequestBinaryState) statusRequest;
                     LOGGER.info("Handling StatusRequestBinaryState\n{}", statusRequestBinaryState);
+                    if (statusRequestBinaryState.getApplication() == ApplicationIdContainer.NETWORK_CONTROL) {
+                        LOGGER.info("Handling installation MMI Request");
+                        sendInstallationMMIResponse(ctx, requestCommand, statusRequestBinaryState.getApplication());
+                        return;
+                    }
                     CALReply calReply;
                     if (exstat) {
                         // TODO: map actuall values from simulator
@@ -316,6 +321,11 @@ public class CBusServerAdapter extends ChannelInboundHandlerAdapter {
                 if (statusRequest instanceof StatusRequestBinaryStateDeprecated) {
                     StatusRequestBinaryStateDeprecated statusRequestBinaryStateDeprecated = (StatusRequestBinaryStateDeprecated) statusRequest;
                     LOGGER.info("Handling StatusRequestBinaryStateDeprecated\n{}", statusRequestBinaryStateDeprecated);
+                    if (statusRequestBinaryStateDeprecated.getApplication() == ApplicationIdContainer.NETWORK_CONTROL) {
+                        LOGGER.info("Handling installation MMI Request");
+                        sendInstallationMMIResponse(ctx, requestCommand, statusRequestBinaryStateDeprecated.getApplication());
+                        return;
+                    }
                     CALReply calReply;
                     if (exstat) {
                         // TODO: map actuall values from simulator
@@ -397,6 +407,84 @@ public class CBusServerAdapter extends ChannelInboundHandlerAdapter {
         }
     }
 
+    private static void sendInstallationMMIResponse(ChannelHandlerContext ctx, RequestCommand requestCommand, ApplicationIdContainer application) {
+        LOGGER.info("Send installation mmis");
+        {
+            byte blockStart = 0x0;
+            List<StatusByte> unitStatusBytes = new LinkedList<>();
+            for (int i = blockStart; i <= 88 - 4; i = i + 4) {
+                LOGGER.debug("Handling units 0-88 {},{},{},{}", i, (i + 1), (i + 2), (i + 3));
+                unitStatusBytes.add(
+                    new StatusByte(
+                        AVAILABLE_UNITS.contains((byte) (i + 0)) ? GAVState.ON : GAVState.DOES_NOT_EXIST,
+                        AVAILABLE_UNITS.contains((byte) (i + 1)) ? GAVState.ON : GAVState.DOES_NOT_EXIST,
+                        AVAILABLE_UNITS.contains((byte) (i + 2)) ? GAVState.ON : GAVState.DOES_NOT_EXIST,
+                        AVAILABLE_UNITS.contains((byte) (i + 3)) ? GAVState.ON : GAVState.DOES_NOT_EXIST
+                    )
+                );
+            }
+            LOGGER.debug("Produced {}, status bytes which equates to {} status", unitStatusBytes.size(), unitStatusBytes.size() * 4);
+            CALData calData = new CALDataStatusExtended(CALCommandTypeContainer.CALCommandReply_22Bytes, null, StatusCoding.BINARY_BY_THIS_SERIAL_INTERFACE, application, blockStart, unitStatusBytes, null, requestContext);
+            CALReply calReply = new CALReplyShort((byte) 0x0, calData, cBusOptions, requestContext);
+            EncodedReply encodedReply = new EncodedReplyCALReply((byte) 0x0, calReply, cBusOptions, requestContext);
+            ReplyEncodedReply replyEncodedReply = new ReplyEncodedReply((byte) 0xC0, encodedReply, null, cBusOptions, requestContext);
+            ReplyOrConfirmation replyOrConfirmation = new ReplyOrConfirmationReply((byte) 0xFF, replyEncodedReply, new ResponseTermination(), cBusOptions, requestContext);
+            Alpha alpha = requestCommand.getAlpha();
+            if (alpha != null) {
+                Confirmation confirmation = new Confirmation(alpha, null, ConfirmationType.CONFIRMATION_SUCCESSFUL);
+                replyOrConfirmation = new ReplyOrConfirmationConfirmation(alpha.getCharacter(), confirmation, replyOrConfirmation, cBusOptions, requestContext);
+            }
+            CBusMessage response = new CBusMessageToClient(replyOrConfirmation, requestContext, cBusOptions);
+            ctx.writeAndFlush(response);
+        }
+        {
+            byte blockStart = 88;
+            List<StatusByte> unitStatusBytes = new LinkedList<>();
+            for (int i = 88; i <= 88 + 88 - 4; i = i + 4) {
+                LOGGER.debug("Handling units 88-176 {},{},{},{}", i, (i + 1), (i + 2), (i + 3));
+                unitStatusBytes.add(
+                    new StatusByte(
+                        AVAILABLE_UNITS.contains((byte) (i + 0)) ? GAVState.ON : GAVState.DOES_NOT_EXIST,
+                        AVAILABLE_UNITS.contains((byte) (i + 1)) ? GAVState.ON : GAVState.DOES_NOT_EXIST,
+                        AVAILABLE_UNITS.contains((byte) (i + 2)) ? GAVState.ON : GAVState.DOES_NOT_EXIST,
+                        AVAILABLE_UNITS.contains((byte) (i + 3)) ? GAVState.ON : GAVState.DOES_NOT_EXIST
+                    )
+                );
+            }
+            LOGGER.debug("Produced {}, status bytes which equates to {} status", unitStatusBytes.size(), unitStatusBytes.size() * 4);
+            CALData calData = new CALDataStatusExtended(CALCommandTypeContainer.CALCommandReply_22Bytes, null, StatusCoding.BINARY_BY_THIS_SERIAL_INTERFACE, application, blockStart, unitStatusBytes, null, requestContext);
+            CALReply calReply = new CALReplyShort((byte) 0x0, calData, cBusOptions, requestContext);
+            EncodedReply encodedReply = new EncodedReplyCALReply((byte) 0x0, calReply, cBusOptions, requestContext);
+            ReplyEncodedReply replyEncodedReply = new ReplyEncodedReply((byte) 0xC0, encodedReply, null, cBusOptions, requestContext);
+            ReplyOrConfirmation replyOrConfirmation = new ReplyOrConfirmationReply((byte) 0xFF, replyEncodedReply, new ResponseTermination(), cBusOptions, requestContext);
+            CBusMessage response = new CBusMessageToClient(replyOrConfirmation, requestContext, cBusOptions);
+            ctx.writeAndFlush(response);
+        }
+        {
+            byte blockStart = (byte) 176;
+            List<StatusByte> unitStatusBytes = new LinkedList<>();
+            for (int i = 176; i <= 176 + 80 - 4; i = i + 4) {
+                LOGGER.debug("Handling units 176-256 {},{},{},{}", i, (i + 1), (i + 2), (i + 3));
+                unitStatusBytes.add(
+                    new StatusByte(
+                        AVAILABLE_UNITS.contains((byte) (i + 0)) ? GAVState.ON : GAVState.DOES_NOT_EXIST,
+                        AVAILABLE_UNITS.contains((byte) (i + 1)) ? GAVState.ON : GAVState.DOES_NOT_EXIST,
+                        AVAILABLE_UNITS.contains((byte) (i + 2)) ? GAVState.ON : GAVState.DOES_NOT_EXIST,
+                        AVAILABLE_UNITS.contains((byte) (i + 3)) ? GAVState.ON : GAVState.DOES_NOT_EXIST
+                    )
+                );
+            }
+            LOGGER.debug("Produced {}, status bytes which equates to {} status", unitStatusBytes.size(), unitStatusBytes.size() * 4);
+            CALData calData = new CALDataStatusExtended(CALCommandTypeContainer.CALCommandReply_21Bytes, null, StatusCoding.BINARY_BY_THIS_SERIAL_INTERFACE, application, blockStart, unitStatusBytes, null, requestContext);
+            CALReply calReply = new CALReplyShort((byte) 0x0, calData, cBusOptions, requestContext);
+            EncodedReply encodedReply = new EncodedReplyCALReply((byte) 0x0, calReply, cBusOptions, requestContext);
+            ReplyEncodedReply replyEncodedReply = new ReplyEncodedReply((byte) 0xC0, encodedReply, null, cBusOptions, requestContext);
+            ReplyOrConfirmation replyOrConfirmation = new ReplyOrConfirmationReply((byte) 0xFF, replyEncodedReply, new ResponseTermination(), cBusOptions, requestContext);
+            CBusMessage response = new CBusMessageToClient(replyOrConfirmation, requestContext, cBusOptions);
+            ctx.writeAndFlush(response);
+        }
+    }
+
     private void handleCalDataIdentify(ChannelHandlerContext ctx, CALDataIdentify calDataIdentify, Alpha alpha) {
         short numBytes;
         IdentifyReplyCommand identifyReplyCommand;