From 8572bb8a488004eb46624c41cdd77d8cbf9f4b64 Mon Sep 17 00:00:00 2001
From: Christofer Dutz <cdutz@apache.org>
Date: Thu, 3 Oct 2024 11:26:47 +0200
Subject: [PATCH] fix: Fixed the simulated driver by allowing to set the
 tag-handler without providing a "protocol"

---
 .../connection/SimulatedConnection.java       |  5 ++-
 .../simulated/ManualSimulatedDriverTest.java  | 37 +++++++++++++++++++
 .../spi/connection/AbstractPlcConnection.java | 16 ++++++++
 3 files changed, 57 insertions(+), 1 deletion(-)
 create mode 100644 plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/ManualSimulatedDriverTest.java

diff --git a/plc4j/drivers/simulated/src/main/java/org/apache/plc4x/java/simulated/connection/SimulatedConnection.java b/plc4j/drivers/simulated/src/main/java/org/apache/plc4x/java/simulated/connection/SimulatedConnection.java
index a14e41fd9a..92b320def8 100644
--- a/plc4j/drivers/simulated/src/main/java/org/apache/plc4x/java/simulated/connection/SimulatedConnection.java
+++ b/plc4j/drivers/simulated/src/main/java/org/apache/plc4x/java/simulated/connection/SimulatedConnection.java
@@ -26,8 +26,11 @@
 import org.apache.plc4x.java.api.value.PlcValue;
 import org.apache.plc4x.java.simulated.tag.SimulatedTag;
 import org.apache.plc4x.java.simulated.tag.SimulatedTagHandler;
+import org.apache.plc4x.java.spi.ConversationContext;
+import org.apache.plc4x.java.spi.Plc4xProtocolBase;
 import org.apache.plc4x.java.spi.connection.AbstractPlcConnection;
 import org.apache.plc4x.java.spi.connection.PlcTagHandler;
+import org.apache.plc4x.java.spi.generation.Message;
 import org.apache.plc4x.java.spi.messages.*;
 import org.apache.plc4x.java.spi.messages.utils.DefaultPlcResponseItem;
 import org.apache.plc4x.java.spi.messages.utils.PlcResponseItem;
@@ -66,7 +69,7 @@ public class SimulatedConnection extends AbstractPlcConnection implements PlcRea
 
     public SimulatedConnection(SimulatedDevice device) {
         super(true, true, true, true, false,
-            new DefaultPlcValueHandler(), null, null);
+            new DefaultPlcValueHandler(), new SimulatedTagHandler(), null, null);
         this.device = device;
     }
 
diff --git a/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/ManualSimulatedDriverTest.java b/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/ManualSimulatedDriverTest.java
new file mode 100644
index 0000000000..784b6836ab
--- /dev/null
+++ b/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/ManualSimulatedDriverTest.java
@@ -0,0 +1,37 @@
+/*
+ * 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.
+ */
+
+package org.apache.plc4x.java.simulated;
+
+import org.apache.plc4x.java.api.PlcConnection;
+import org.apache.plc4x.java.api.PlcDriverManager;
+import org.apache.plc4x.java.api.messages.PlcReadRequest;
+import org.apache.plc4x.java.api.messages.PlcReadResponse;
+
+public class ManualSimulatedDriverTest {
+
+    public static void main(String[] args) throws Exception {
+        try (PlcConnection connection = PlcDriverManager.getDefault().getConnectionManager().getConnection("simulated:hurz")){
+            PlcReadRequest readRequest = connection.readRequestBuilder().addTagAddress("test", "RANDOM/dummy:UINT[100]").build();
+            PlcReadResponse readResponse = readRequest.execute().get();
+            System.out.println(readResponse);
+        }
+    }
+
+}
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/AbstractPlcConnection.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/AbstractPlcConnection.java
index 45fc42bb70..fb321b60f1 100644
--- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/AbstractPlcConnection.java
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/AbstractPlcConnection.java
@@ -85,6 +85,22 @@ protected AbstractPlcConnection(boolean canPing, boolean canRead, boolean canWri
         this.authentication = authentication;
     }
 
+    protected AbstractPlcConnection(boolean canPing, boolean canRead, boolean canWrite,
+                                    boolean canSubscribe, boolean canBrowse,
+                                    PlcValueHandler valueHandler,
+                                    PlcTagHandler tagHandler,
+                                    BaseOptimizer optimizer, PlcAuthentication authentication) {
+        this.canPing = canPing;
+        this.canRead = canRead;
+        this.canWrite = canWrite;
+        this.canSubscribe = canSubscribe;
+        this.canBrowse = canBrowse;
+        this.valueHandler = valueHandler;
+        this.tagHandler = tagHandler;
+        this.optimizer = optimizer;
+        this.authentication = authentication;
+    }
+
     public void setProtocol(Plc4xProtocolBase<? extends Message> protocol) {
         this.protocol = protocol;
         this.tagHandler = protocol.getTagHandler();