diff --git a/plc4j/api/src/main/java/org/apache/plc4x/java/api/exceptions/PlcTagNotFoundException.java b/plc4j/api/src/main/java/org/apache/plc4x/java/api/exceptions/PlcTagNotFoundException.java new file mode 100644 index 00000000000..e417d18b9c4 --- /dev/null +++ b/plc4j/api/src/main/java/org/apache/plc4x/java/api/exceptions/PlcTagNotFoundException.java @@ -0,0 +1,43 @@ +/* + * 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 + * + * https://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.api.exceptions; + +/** + * Indicates an invalid tag address. + */ +public class PlcTagNotFoundException extends PlcRuntimeException { + + private static final long serialVersionUID = 1L; + private final String tagToBeParsed; + + public PlcTagNotFoundException(String tagToBeParsed) { + super(tagToBeParsed + " not found"); + this.tagToBeParsed = tagToBeParsed; + } + + public PlcTagNotFoundException(String tagToBeParsed, Throwable cause) { + super(tagToBeParsed + " not found", cause); + this.tagToBeParsed = tagToBeParsed; + } + + public String getTagToBeParsed() { + return tagToBeParsed; + } + +} diff --git a/plc4j/drivers/ab-eth/src/main/java/org/apache/plc4x/java/abeth/AbEthDriver.java b/plc4j/drivers/ab-eth/src/main/java/org/apache/plc4x/java/abeth/AbEthDriver.java index 22e673b82f6..877d8b8274b 100644 --- a/plc4j/drivers/ab-eth/src/main/java/org/apache/plc4x/java/abeth/AbEthDriver.java +++ b/plc4j/drivers/ab-eth/src/main/java/org/apache/plc4x/java/abeth/AbEthDriver.java @@ -22,7 +22,6 @@ import org.apache.plc4x.java.abeth.configuration.AbEthConfiguration; import org.apache.plc4x.java.abeth.configuration.AbEthTcpTransportConfiguration; import org.apache.plc4x.java.abeth.tag.AbEthTag; -import org.apache.plc4x.java.abeth.tag.AbEthTagHandler; import org.apache.plc4x.java.abeth.protocol.AbEthProtocolLogic; import org.apache.plc4x.java.abeth.readwrite.CIPEncapsulationPacket; import org.apache.plc4x.java.spi.configuration.PlcConnectionConfiguration; @@ -76,11 +75,6 @@ protected List getSupportedTransportCodes() { return Collections.singletonList("tcp"); } - @Override - protected AbEthTagHandler getTagHandler() { - return new AbEthTagHandler(); - } - /** * This protocol doesn't have a disconnect procedure, so there is no need to wait for a login to finish. * @return false diff --git a/plc4j/drivers/ab-eth/src/main/java/org/apache/plc4x/java/abeth/protocol/AbEthProtocolLogic.java b/plc4j/drivers/ab-eth/src/main/java/org/apache/plc4x/java/abeth/protocol/AbEthProtocolLogic.java index 8cb9eb0c485..aa8bb611f90 100644 --- a/plc4j/drivers/ab-eth/src/main/java/org/apache/plc4x/java/abeth/protocol/AbEthProtocolLogic.java +++ b/plc4j/drivers/ab-eth/src/main/java/org/apache/plc4x/java/abeth/protocol/AbEthProtocolLogic.java @@ -21,6 +21,7 @@ import org.apache.plc4x.java.abeth.configuration.AbEthConfiguration; import org.apache.plc4x.java.abeth.readwrite.*; import org.apache.plc4x.java.abeth.tag.AbEthTag; +import org.apache.plc4x.java.abeth.tag.AbEthTagHandler; import org.apache.plc4x.java.api.messages.PlcReadRequest; import org.apache.plc4x.java.api.messages.PlcReadResponse; import org.apache.plc4x.java.api.messages.PlcResponse; @@ -30,6 +31,7 @@ import org.apache.plc4x.java.spi.ConversationContext; import org.apache.plc4x.java.spi.Plc4xProtocolBase; import org.apache.plc4x.java.spi.configuration.HasConfiguration; +import org.apache.plc4x.java.spi.connection.PlcTagHandler; import org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse; import org.apache.plc4x.java.spi.messages.utils.DefaultPlcResponseItem; import org.apache.plc4x.java.spi.messages.utils.PlcResponseItem; @@ -68,6 +70,11 @@ public void setConfiguration(AbEthConfiguration configuration) { this.tm = new RequestTransactionManager(1); } + @Override + public PlcTagHandler getTagHandler() { + return new AbEthTagHandler(); + } + @Override public void close(ConversationContext context) { tm.shutdown(); diff --git a/plc4j/drivers/ads/src/main/java/org/apache/plc4x/java/ads/AdsPlcDriver.java b/plc4j/drivers/ads/src/main/java/org/apache/plc4x/java/ads/AdsPlcDriver.java index f7b4a24c310..641bc781afb 100644 --- a/plc4j/drivers/ads/src/main/java/org/apache/plc4x/java/ads/AdsPlcDriver.java +++ b/plc4j/drivers/ads/src/main/java/org/apache/plc4x/java/ads/AdsPlcDriver.java @@ -22,7 +22,6 @@ import org.apache.plc4x.java.ads.configuration.AdsConfiguration; import org.apache.plc4x.java.ads.configuration.AdsTcpTransportConfiguration; import org.apache.plc4x.java.ads.discovery.AdsPlcDiscoverer; -import org.apache.plc4x.java.ads.tag.AdsTagHandler; import org.apache.plc4x.java.ads.protocol.AdsProtocolLogic; import org.apache.plc4x.java.ads.readwrite.AmsTCPPacket; import org.apache.plc4x.java.spi.configuration.PlcConnectionConfiguration; @@ -115,11 +114,6 @@ protected List getSupportedTransportCodes() { return Collections.singletonList("tcp"); } - @Override - protected AdsTagHandler getTagHandler() { - return new AdsTagHandler(); - } - /** * This protocol doesn't have a disconnect procedure, so there is no need to wait for a login to finish. * @return false diff --git a/plc4j/drivers/ads/src/main/java/org/apache/plc4x/java/ads/protocol/AdsProtocolLogic.java b/plc4j/drivers/ads/src/main/java/org/apache/plc4x/java/ads/protocol/AdsProtocolLogic.java index 3a6ff4f4c6d..b35c0391ece 100644 --- a/plc4j/drivers/ads/src/main/java/org/apache/plc4x/java/ads/protocol/AdsProtocolLogic.java +++ b/plc4j/drivers/ads/src/main/java/org/apache/plc4x/java/ads/protocol/AdsProtocolLogic.java @@ -24,6 +24,7 @@ import org.apache.plc4x.java.ads.model.AdsSubscriptionHandle; import org.apache.plc4x.java.ads.readwrite.*; import org.apache.plc4x.java.ads.tag.AdsTag; +import org.apache.plc4x.java.ads.tag.AdsTagHandler; import org.apache.plc4x.java.ads.tag.DirectAdsStringTag; import org.apache.plc4x.java.ads.tag.DirectAdsTag; import org.apache.plc4x.java.ads.tag.SymbolicAdsTag; @@ -40,6 +41,7 @@ import org.apache.plc4x.java.spi.ConversationContext; import org.apache.plc4x.java.spi.Plc4xProtocolBase; import org.apache.plc4x.java.spi.configuration.HasConfiguration; +import org.apache.plc4x.java.spi.connection.PlcTagHandler; import org.apache.plc4x.java.spi.generation.*; import org.apache.plc4x.java.spi.messages.*; import org.apache.plc4x.java.spi.messages.utils.DefaultPlcResponseItem; @@ -113,6 +115,11 @@ public void setConfiguration(AdsConfiguration configuration) { this.configuration = configuration; } + @Override + public PlcTagHandler getTagHandler() { + return new AdsTagHandler(); + } + @Override public void onConnect(ConversationContext context) { diff --git a/plc4j/drivers/ads/src/test/java/org/apache/plc4x/protocol/ads/ManualAdsBrowse.java b/plc4j/drivers/ads/src/test/java/org/apache/plc4x/protocol/ads/ManualAdsBrowse.java index f9240a3c871..5c850d372ea 100644 --- a/plc4j/drivers/ads/src/test/java/org/apache/plc4x/protocol/ads/ManualAdsBrowse.java +++ b/plc4j/drivers/ads/src/test/java/org/apache/plc4x/protocol/ads/ManualAdsBrowse.java @@ -21,14 +21,15 @@ import org.apache.plc4x.java.api.PlcConnection; import org.apache.plc4x.java.api.PlcDriverManager; -import org.apache.plc4x.java.api.messages.PlcBrowseResponse; public class ManualAdsBrowse { public static void main(String[] args) throws Exception { try (PlcConnection connection = PlcDriverManager.getDefault().getConnectionManager().getConnection("ads:tcp://192.168.23.20:48898?target-ams-port=851&source-ams-port=65534&source-ams-net-id=192.168.23.220.1.1&target-ams-net-id=192.168.23.20.1.1")){ - PlcBrowseResponse browseResponse = connection.browseRequestBuilder().addQuery("all", "*").build().execute().get(); - System.out.println(browseResponse); + connection.browseRequestBuilder().addQuery("all", "*").build().executeWithInterceptor(item -> { + System.out.printf("- %s%n", item.getTag().getAddressString()); + return true; + }).get(); } } } diff --git a/plc4j/drivers/ads/src/test/java/org/apache/plc4x/protocol/ads/ManualAdsDriverTest.java b/plc4j/drivers/ads/src/test/java/org/apache/plc4x/protocol/ads/ManualAdsDriverTest.java index 07dcfd8b8f0..d623a701c00 100644 --- a/plc4j/drivers/ads/src/test/java/org/apache/plc4x/protocol/ads/ManualAdsDriverTest.java +++ b/plc4j/drivers/ads/src/test/java/org/apache/plc4x/protocol/ads/ManualAdsDriverTest.java @@ -18,7 +18,6 @@ */ package org.apache.plc4x.protocol.ads; -import org.apache.plc4x.java.api.types.PlcResponseCode; import org.apache.plc4x.java.api.value.PlcValue; import org.apache.plc4x.java.spi.values.*; import org.apache.plc4x.test.manual.ManualTest; @@ -27,6 +26,7 @@ import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; +import java.time.OffsetDateTime; import java.util.HashMap; import java.util.Map; @@ -130,12 +130,12 @@ public static void main(String[] args) throws Exception { children.put("hurz_LTIME", new PlcLTIME(Duration.parse("PT24015H23M12.034002044S"))); children.put("hurz_DATE", new PlcDATE(LocalDate.parse("1978-03-28"))); children.put("hurz_TIME_OF_DAY", new PlcTIME_OF_DAY(LocalTime.parse("15:36:30.123"))); - children.put("hurz_DATE_AND_TIME", new PlcDATE_AND_TIME(LocalDateTime.parse("1996-05-06T15:36:30"))); + children.put("hurz_DATE_AND_TIME", new PlcDATE_AND_TIME(LocalDateTime.parse("1996-05-06T15:36:30").atOffset(OffsetDateTime.now().getOffset()).toLocalDateTime())); test.addTestCase("MAIN.hurz_Struct", new PlcStruct(children)); - test.addTestCase("MAIN.thisVariableDoesntExist", PlcResponseCode.NOT_FOUND); + //test.addTestCase("MAIN.thisVariableDoesntExist", PlcResponseCode.NOT_FOUND); // TODO: Add some complex array path - test.addTestCase("...3323(/987", PlcResponseCode.INVALID_ADDRESS); - test.addTestCase("MAIN.hurz_UDT_array[4].hurz_INT_array[2].someProperty", PlcResponseCode.NOT_FOUND); + //test.addTestCase("...3323(/987", PlcResponseCode.INVALID_ADDRESS); + //test.addTestCase("MAIN.hurz_UDT_array[4].hurz_INT_array[2].someProperty", PlcResponseCode.NOT_FOUND); test.run(); } diff --git a/plc4j/drivers/bacnet/src/main/java/org/apache/plc4x/java/bacnetip/BacNetIpDriver.java b/plc4j/drivers/bacnet/src/main/java/org/apache/plc4x/java/bacnetip/BacNetIpDriver.java index 12acec0c5a3..ccffd6ddf5a 100644 --- a/plc4j/drivers/bacnet/src/main/java/org/apache/plc4x/java/bacnetip/BacNetIpDriver.java +++ b/plc4j/drivers/bacnet/src/main/java/org/apache/plc4x/java/bacnetip/BacNetIpDriver.java @@ -25,7 +25,6 @@ import org.apache.plc4x.java.bacnetip.configuration.BacNetPcapReplayTransportConfiguration; import org.apache.plc4x.java.bacnetip.configuration.BacNetRawSocketTransportConfiguration; import org.apache.plc4x.java.bacnetip.configuration.BacNetUdpTransportConfiguration; -import org.apache.plc4x.java.bacnetip.tag.BacNetIpTagHandler; import org.apache.plc4x.java.bacnetip.protocol.BacNetIpProtocolLogic; import org.apache.plc4x.java.bacnetip.readwrite.BVLC; import org.apache.plc4x.java.spi.connection.GeneratedDriverBase; @@ -93,11 +92,6 @@ protected boolean canSubscribe() { return true; } - @Override - protected BacNetIpTagHandler getTagHandler() { - return new BacNetIpTagHandler(); - } - @Override protected ProtocolStackConfigurer getStackConfigurer() { return SingleProtocolStackConfigurer.builder(BVLC.class, BVLC::staticParse) diff --git a/plc4j/drivers/bacnet/src/main/java/org/apache/plc4x/java/bacnetip/protocol/BacNetIpProtocolLogic.java b/plc4j/drivers/bacnet/src/main/java/org/apache/plc4x/java/bacnetip/protocol/BacNetIpProtocolLogic.java index fd92227bcac..ecf8b79d403 100644 --- a/plc4j/drivers/bacnet/src/main/java/org/apache/plc4x/java/bacnetip/protocol/BacNetIpProtocolLogic.java +++ b/plc4j/drivers/bacnet/src/main/java/org/apache/plc4x/java/bacnetip/protocol/BacNetIpProtocolLogic.java @@ -32,9 +32,11 @@ import org.apache.plc4x.java.bacnetip.ede.model.EdeModel; import org.apache.plc4x.java.bacnetip.tag.BacNetIpTag; import org.apache.plc4x.java.bacnetip.readwrite.*; +import org.apache.plc4x.java.bacnetip.tag.BacNetIpTagHandler; import org.apache.plc4x.java.spi.ConversationContext; import org.apache.plc4x.java.spi.Plc4xProtocolBase; import org.apache.plc4x.java.spi.configuration.HasConfiguration; +import org.apache.plc4x.java.spi.connection.PlcTagHandler; import org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionEvent; import org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionResponse; import org.apache.plc4x.java.spi.messages.PlcSubscriber; @@ -85,6 +87,11 @@ public void setConfiguration(BacNetIpConfiguration configuration) { } } + @Override + public PlcTagHandler getTagHandler() { + return new BacNetIpTagHandler(); + } + @Override public void onConnect(ConversationContext context) { if (context.isPassive()) { diff --git a/plc4j/drivers/c-bus/src/main/java/org/apache/plc4x/java/cbus/CBusDriver.java b/plc4j/drivers/c-bus/src/main/java/org/apache/plc4x/java/cbus/CBusDriver.java index c6fed8e1329..1065a50c1bf 100644 --- a/plc4j/drivers/c-bus/src/main/java/org/apache/plc4x/java/cbus/CBusDriver.java +++ b/plc4j/drivers/c-bus/src/main/java/org/apache/plc4x/java/cbus/CBusDriver.java @@ -28,7 +28,6 @@ import org.apache.plc4x.java.cbus.protocol.CBusProtocolLogic; import org.apache.plc4x.java.cbus.readwrite.CBusCommand; import org.apache.plc4x.java.spi.connection.GeneratedDriverBase; -import org.apache.plc4x.java.spi.connection.PlcTagHandler; import org.apache.plc4x.java.spi.connection.ProtocolStackConfigurer; import org.apache.plc4x.java.spi.connection.SingleProtocolStackConfigurer; @@ -80,11 +79,6 @@ protected List getSupportedTransportCodes() { return Collections.singletonList("tcp"); } - @Override - protected PlcTagHandler getTagHandler() { - return null; - } - @Override protected ProtocolStackConfigurer getStackConfigurer() { return SingleProtocolStackConfigurer.builder(CBusCommand.class, io -> diff --git a/plc4j/drivers/c-bus/src/main/java/org/apache/plc4x/java/cbus/protocol/CBusProtocolLogic.java b/plc4j/drivers/c-bus/src/main/java/org/apache/plc4x/java/cbus/protocol/CBusProtocolLogic.java index 7bed821a080..41d27fe5a6e 100644 --- a/plc4j/drivers/c-bus/src/main/java/org/apache/plc4x/java/cbus/protocol/CBusProtocolLogic.java +++ b/plc4j/drivers/c-bus/src/main/java/org/apache/plc4x/java/cbus/protocol/CBusProtocolLogic.java @@ -22,6 +22,7 @@ import org.apache.plc4x.java.cbus.readwrite.CBusCommand; import org.apache.plc4x.java.spi.ConversationContext; import org.apache.plc4x.java.spi.Plc4xProtocolBase; +import org.apache.plc4x.java.spi.connection.PlcTagHandler; import org.apache.plc4x.java.spi.context.DriverContext; import org.apache.plc4x.java.spi.messages.*; import org.apache.plc4x.java.spi.transaction.RequestTransactionManager; @@ -48,6 +49,11 @@ public void setDriverContext(DriverContext driverContext) { this.tm = new RequestTransactionManager(1); } + @Override + public PlcTagHandler getTagHandler() { + return null; + } + @Override public void close(ConversationContext context) { tm.shutdown(); diff --git a/plc4j/drivers/can/src/main/java/org/apache/plc4x/java/can/adapter/CANDriverAdapter.java b/plc4j/drivers/can/src/main/java/org/apache/plc4x/java/can/adapter/CANDriverAdapter.java index 13eecb84536..b0fe115c3f6 100644 --- a/plc4j/drivers/can/src/main/java/org/apache/plc4x/java/can/adapter/CANDriverAdapter.java +++ b/plc4j/drivers/can/src/main/java/org/apache/plc4x/java/can/adapter/CANDriverAdapter.java @@ -32,6 +32,7 @@ import org.apache.plc4x.java.can.adapter.conversation.ConversationContextWrapper; import org.apache.plc4x.java.spi.ConversationContext; import org.apache.plc4x.java.spi.Plc4xProtocolBase; +import org.apache.plc4x.java.spi.connection.PlcTagHandler; import org.apache.plc4x.java.spi.context.DriverContext; import org.apache.plc4x.java.transport.can.CANTransport.FrameHandler; import org.apache.plc4x.java.transport.can.FrameData; @@ -42,12 +43,14 @@ public class CANDriverAdapter extends Plc4xProtocolBase { private final Class wireType; private final Function adapter; private final FrameHandler frameHandler; + private final PlcTagHandler tagHandler; - public CANDriverAdapter(Plc4xCANProtocolBase delegate, Class wireType, Function adapter, FrameHandler frameHandler) { + public CANDriverAdapter(Plc4xCANProtocolBase delegate, Class wireType, Function adapter, FrameHandler frameHandler, PlcTagHandler tagHandler) { this.delegate = delegate; this.wireType = wireType; this.adapter = adapter; this.frameHandler = frameHandler; + this.tagHandler = tagHandler; } @Override @@ -65,6 +68,11 @@ public void setContext(ConversationContext context) { delegate.setContext(new ConversationContextWrapper<>(context, wireType, adapter, frameHandler, context.getAuthentication())); } + @Override + public PlcTagHandler getTagHandler() { + return tagHandler; + } + @Override public void onConnect(ConversationContext context) { delegate.onConnect(new ConversationContextWrapper<>(context, wireType, adapter, frameHandler, context.getAuthentication())); diff --git a/plc4j/drivers/can/src/main/java/org/apache/plc4x/java/can/generic/GenericCANDriver.java b/plc4j/drivers/can/src/main/java/org/apache/plc4x/java/can/generic/GenericCANDriver.java index 5efc8735452..2c7c1413f96 100644 --- a/plc4j/drivers/can/src/main/java/org/apache/plc4x/java/can/generic/GenericCANDriver.java +++ b/plc4j/drivers/can/src/main/java/org/apache/plc4x/java/can/generic/GenericCANDriver.java @@ -18,12 +18,12 @@ */ package org.apache.plc4x.java.can.generic; +import org.apache.plc4x.java.can.generic.tag.GenericCANTagHandler; import org.apache.plc4x.java.spi.configuration.PlcConnectionConfiguration; import org.apache.plc4x.java.api.exceptions.PlcRuntimeException; import org.apache.plc4x.java.can.adapter.CANDriverAdapter; import org.apache.plc4x.java.can.generic.configuration.GenericCANConfiguration; import org.apache.plc4x.java.can.generic.context.GenericCANDriverContext; -import org.apache.plc4x.java.can.generic.tag.GenericCANTagHandler; import org.apache.plc4x.java.can.generic.protocol.GenericCANProtocolLogic; import org.apache.plc4x.java.can.generic.transport.GenericCANFrameDataHandler; import org.apache.plc4x.java.spi.configuration.ConfigurationFactory; @@ -79,11 +79,6 @@ protected boolean canWrite() { return true; } - @Override - protected GenericCANTagHandler getTagHandler() { - return new GenericCANTagHandler(); - } - /** * This protocol doesn't have a disconnect procedure, so there is no need to wait for a login to finish. * @return false @@ -116,7 +111,8 @@ protected ProtocolStackConfigurer getStackConfigurer(Transport transpor ConfigurationFactory.configure(cfg, protocolLogic); return new CANDriverAdapter<>(protocolLogic, canTransport.getMessageType(), canTransport.adapter(), - new GenericCANFrameDataHandler(canTransport::getTransportFrameBuilder) + new GenericCANFrameDataHandler(canTransport::getTransportFrameBuilder), + new GenericCANTagHandler() ); }) .withDriverContext(cfg -> new GenericCANDriverContext()) diff --git a/plc4j/drivers/can/src/main/java/org/apache/plc4x/java/can/generic/protocol/GenericCANProtocolLogic.java b/plc4j/drivers/can/src/main/java/org/apache/plc4x/java/can/generic/protocol/GenericCANProtocolLogic.java index da8c717af0c..c7bdcb9cb98 100644 --- a/plc4j/drivers/can/src/main/java/org/apache/plc4x/java/can/generic/protocol/GenericCANProtocolLogic.java +++ b/plc4j/drivers/can/src/main/java/org/apache/plc4x/java/can/generic/protocol/GenericCANProtocolLogic.java @@ -29,9 +29,11 @@ import org.apache.plc4x.java.api.value.PlcValue; import org.apache.plc4x.java.can.adapter.Plc4xCANProtocolBase; import org.apache.plc4x.java.can.generic.tag.GenericCANTag; +import org.apache.plc4x.java.can.generic.tag.GenericCANTagHandler; import org.apache.plc4x.java.can.generic.transport.GenericFrame; import org.apache.plc4x.java.genericcan.readwrite.DataItem; import org.apache.plc4x.java.spi.ConversationContext; +import org.apache.plc4x.java.spi.connection.PlcTagHandler; import org.apache.plc4x.java.spi.context.DriverContext; import org.apache.plc4x.java.spi.generation.*; import org.apache.plc4x.java.spi.messages.*; @@ -72,6 +74,11 @@ public void setContext(ConversationContext context) { super.setContext(context); } + @Override + public PlcTagHandler getTagHandler() { + return new GenericCANTagHandler(); + } + @Override public void onConnect(ConversationContext context) { context.fireConnected(); diff --git a/plc4j/drivers/can/src/main/java/org/apache/plc4x/java/can/generic/tag/GenericCANTag.java b/plc4j/drivers/can/src/main/java/org/apache/plc4x/java/can/generic/tag/GenericCANTag.java index c21db4dfe10..ea8a2d34fef 100644 --- a/plc4j/drivers/can/src/main/java/org/apache/plc4x/java/can/generic/tag/GenericCANTag.java +++ b/plc4j/drivers/can/src/main/java/org/apache/plc4x/java/can/generic/tag/GenericCANTag.java @@ -65,7 +65,7 @@ public PlcValueType getPlcValueType() { @Override public List getArrayInfo() { - if(arraySize != 1) { + if(arraySize > 1) { return Collections.singletonList(new DefaultArrayInfo(0, arraySize)); } return Collections.emptyList(); diff --git a/plc4j/drivers/canopen/src/main/java/org/apache/plc4x/java/canopen/CANOpenPlcDriver.java b/plc4j/drivers/canopen/src/main/java/org/apache/plc4x/java/canopen/CANOpenPlcDriver.java index a99eddffdf6..6d7d6f08dea 100644 --- a/plc4j/drivers/canopen/src/main/java/org/apache/plc4x/java/canopen/CANOpenPlcDriver.java +++ b/plc4j/drivers/canopen/src/main/java/org/apache/plc4x/java/canopen/CANOpenPlcDriver.java @@ -18,6 +18,7 @@ */ package org.apache.plc4x.java.canopen; +import org.apache.plc4x.java.canopen.tag.CANOpenTagHandler; import org.apache.plc4x.java.spi.configuration.PlcConnectionConfiguration; import org.apache.plc4x.java.api.exceptions.PlcRuntimeException; import org.apache.plc4x.java.api.model.PlcTag; @@ -25,7 +26,6 @@ import org.apache.plc4x.java.can.adapter.CANDriverAdapter; import org.apache.plc4x.java.canopen.configuration.CANOpenConfiguration; import org.apache.plc4x.java.canopen.context.CANOpenDriverContext; -import org.apache.plc4x.java.canopen.tag.CANOpenTagHandler; import org.apache.plc4x.java.canopen.protocol.CANOpenProtocolLogic; import org.apache.plc4x.java.canopen.transport.CANOpenFrameDataHandler; import org.apache.plc4x.java.spi.configuration.ConfigurationFactory; @@ -82,11 +82,6 @@ protected Optional getDefaultTransportCode() { return Optional.of("socketcan"); } - @Override - protected CANOpenTagHandler getTagHandler() { - return new CANOpenTagHandler(); - } - @Override protected PlcValueHandler getValueHandler() { return new DefaultPlcValueHandler() { @@ -132,7 +127,8 @@ protected ProtocolStackConfigurer getStackConfigurer(Transport transpor ConfigurationFactory.configure(cfg, protocolLogic); return new CANDriverAdapter<>(protocolLogic, canTransport.getMessageType(), canTransport.adapter(), - new CANOpenFrameDataHandler(canTransport::getTransportFrameBuilder) + new CANOpenFrameDataHandler(canTransport::getTransportFrameBuilder), + new CANOpenTagHandler() ); }) .withDriverContext(cfg -> new CANOpenDriverContext()) diff --git a/plc4j/drivers/canopen/src/main/java/org/apache/plc4x/java/canopen/protocol/CANOpenProtocolLogic.java b/plc4j/drivers/canopen/src/main/java/org/apache/plc4x/java/canopen/protocol/CANOpenProtocolLogic.java index a899b408c55..aaa74359e4c 100644 --- a/plc4j/drivers/canopen/src/main/java/org/apache/plc4x/java/canopen/protocol/CANOpenProtocolLogic.java +++ b/plc4j/drivers/canopen/src/main/java/org/apache/plc4x/java/canopen/protocol/CANOpenProtocolLogic.java @@ -56,6 +56,7 @@ import org.apache.plc4x.java.canopen.readwrite.NMTStateRequest; import org.apache.plc4x.java.spi.ConversationContext; import org.apache.plc4x.java.spi.configuration.HasConfiguration; +import org.apache.plc4x.java.spi.connection.PlcTagHandler; import org.apache.plc4x.java.spi.context.DriverContext; import org.apache.plc4x.java.spi.generation.*; import org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse; @@ -125,6 +126,11 @@ public void setDriverContext(DriverContext driverContext) { this.tm = new RequestTransactionManager(1); } + @Override + public PlcTagHandler getTagHandler() { + return new CANOpenTagHandler(); + } + @Override public void close(ConversationContext context) { tm.shutdown(); diff --git a/plc4j/drivers/ctrlx/src/main/java/org/apache/plc4x/java/ctrlx/readwrite/connection/CtrlXConnection.java b/plc4j/drivers/ctrlx/src/main/java/org/apache/plc4x/java/ctrlx/readwrite/connection/CtrlXConnection.java index df09c0bb757..f5b27bb503a 100644 --- a/plc4j/drivers/ctrlx/src/main/java/org/apache/plc4x/java/ctrlx/readwrite/connection/CtrlXConnection.java +++ b/plc4j/drivers/ctrlx/src/main/java/org/apache/plc4x/java/ctrlx/readwrite/connection/CtrlXConnection.java @@ -19,18 +19,19 @@ package org.apache.plc4x.java.ctrlx.readwrite.connection; -import com.hrakaroo.glob.GlobPattern; import com.hrakaroo.glob.MatchingEngine; import org.apache.plc4x.java.api.PlcConnection; import org.apache.plc4x.java.api.exceptions.PlcConnectionException; import org.apache.plc4x.java.api.exceptions.PlcInvalidTagException; import org.apache.plc4x.java.api.exceptions.PlcProtocolException; +import org.apache.plc4x.java.api.exceptions.PlcRuntimeException; import org.apache.plc4x.java.api.messages.*; import org.apache.plc4x.java.api.metadata.PlcConnectionMetadata; import org.apache.plc4x.java.api.model.PlcQuery; import org.apache.plc4x.java.api.model.PlcTag; import org.apache.plc4x.java.api.types.PlcResponseCode; import org.apache.plc4x.java.api.types.PlcValueType; +import org.apache.plc4x.java.api.value.PlcValue; import org.apache.plc4x.java.ctrlx.readwrite.rest.datalayer.ApiClient; import org.apache.plc4x.java.ctrlx.readwrite.rest.datalayer.ApiException; import org.apache.plc4x.java.ctrlx.readwrite.rest.datalayer.api.DataLayerInformationAndSettingsApi; @@ -42,6 +43,8 @@ import org.apache.plc4x.java.ctrlx.readwrite.tag.CtrlXTagHandler; import org.apache.plc4x.java.ctrlx.readwrite.utils.ApiClientFactory; import org.apache.plc4x.java.spi.messages.*; +import org.apache.plc4x.java.spi.values.DefaultPlcValueHandler; +import org.apache.plc4x.java.spi.values.PlcValueHandler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -60,6 +63,7 @@ public class CtrlXConnection implements PlcConnection, PlcPinger, PlcBrowser { private final String password; private final ExecutorService executorService; + private PlcValueHandler valueHandler; private ApiClient apiClient; private NodesApi nodesApi; @@ -72,6 +76,18 @@ public CtrlXConnection(String baseUrl, String username, String password) { this.username = username; this.password = password; this.executorService = Executors.newFixedThreadPool(10); + this.valueHandler = new DefaultPlcValueHandler(); + } + + @Override + public Optional parseTagValue(PlcTag tag, Object... values) { + PlcValue plcValue; + try { + plcValue = valueHandler.newPlcValue(tag, values); + } catch (Exception e) { + throw new PlcRuntimeException("Error parsing tag value " + tag, e); + } + return Optional.of(plcValue); } @Override diff --git a/plc4j/drivers/eip/src/main/java/org/apache/plc4x/java/eip/base/EIPDriver.java b/plc4j/drivers/eip/src/main/java/org/apache/plc4x/java/eip/base/EIPDriver.java index abf99de0192..02f4be1d48a 100644 --- a/plc4j/drivers/eip/src/main/java/org/apache/plc4x/java/eip/base/EIPDriver.java +++ b/plc4j/drivers/eip/src/main/java/org/apache/plc4x/java/eip/base/EIPDriver.java @@ -29,7 +29,6 @@ import org.apache.plc4x.java.eip.base.configuration.EipTcpTransportConfiguration; import org.apache.plc4x.java.eip.base.tag.EipTag; import org.apache.plc4x.java.eip.base.protocol.EipProtocolLogic; -import org.apache.plc4x.java.eip.base.tag.EipTagHandler; import org.apache.plc4x.java.eip.readwrite.EipPacket; import org.apache.plc4x.java.spi.configuration.ConfigurationFactory; import org.apache.plc4x.java.spi.configuration.HasConfiguration; @@ -90,11 +89,6 @@ protected List getSupportedTransportCodes() { return Collections.singletonList("tcp"); } - @Override - protected PlcTagHandler getTagHandler() { - return new EipTagHandler(); - } - @Override public PlcDiscoveryRequest.Builder discoveryRequestBuilder() { return new DefaultPlcDiscoveryRequest.Builder(new EipPlcDiscoverer()); @@ -215,7 +209,6 @@ public PlcConnection getConnection(String connectionString) throws PlcConnection return new DefaultNettyPlcConnection( canPing(), canRead(), canWrite(), canSubscribe(), canBrowse(), - getTagHandler(), getValueHandler(), configuration, channelFactory, diff --git a/plc4j/drivers/eip/src/main/java/org/apache/plc4x/java/eip/base/protocol/EipProtocolLogic.java b/plc4j/drivers/eip/src/main/java/org/apache/plc4x/java/eip/base/protocol/EipProtocolLogic.java index 0793c8583d7..f5465540c65 100644 --- a/plc4j/drivers/eip/src/main/java/org/apache/plc4x/java/eip/base/protocol/EipProtocolLogic.java +++ b/plc4j/drivers/eip/src/main/java/org/apache/plc4x/java/eip/base/protocol/EipProtocolLogic.java @@ -30,11 +30,13 @@ import org.apache.plc4x.java.api.value.PlcValue; import org.apache.plc4x.java.eip.base.configuration.EIPConfiguration; import org.apache.plc4x.java.eip.base.tag.EipTag; +import org.apache.plc4x.java.eip.base.tag.EipTagHandler; import org.apache.plc4x.java.eip.logix.configuration.LogixConfiguration; import org.apache.plc4x.java.eip.readwrite.*; import org.apache.plc4x.java.spi.ConversationContext; import org.apache.plc4x.java.spi.Plc4xProtocolBase; import org.apache.plc4x.java.spi.configuration.HasConfiguration; +import org.apache.plc4x.java.spi.connection.PlcTagHandler; import org.apache.plc4x.java.spi.generation.ParseException; import org.apache.plc4x.java.spi.generation.ReadBufferByteBased; import org.apache.plc4x.java.spi.generation.SerializationException; @@ -147,6 +149,11 @@ public void setConfiguration(EIPConfiguration configuration) { this.tm = new RequestTransactionManager(1); } + @Override + public PlcTagHandler getTagHandler() { + return new EipTagHandler(); + } + @Override public void close(ConversationContext context) { tm.shutdown(); diff --git a/plc4j/drivers/eip/src/main/java/org/apache/plc4x/java/eip/logix/LogixDriver.java b/plc4j/drivers/eip/src/main/java/org/apache/plc4x/java/eip/logix/LogixDriver.java index 4e0d4c2b94d..3a1d971cc1b 100644 --- a/plc4j/drivers/eip/src/main/java/org/apache/plc4x/java/eip/logix/LogixDriver.java +++ b/plc4j/drivers/eip/src/main/java/org/apache/plc4x/java/eip/logix/LogixDriver.java @@ -26,7 +26,6 @@ import org.apache.plc4x.java.eip.logix.configuration.LogixConfiguration; import org.apache.plc4x.java.eip.logix.configuration.LogixTcpTransportConfiguration; import org.apache.plc4x.java.eip.readwrite.EipPacket; -import org.apache.plc4x.java.eip.base.tag.EipTagHandler; import org.apache.plc4x.java.spi.connection.*; import java.util.Collections; @@ -71,11 +70,6 @@ protected List getSupportedTransportCodes() { return Collections.singletonList("tcp"); } - @Override - protected PlcTagHandler getTagHandler() { - return new EipTagHandler(); - } - @Override protected boolean awaitDisconnectComplete() { return true; diff --git a/plc4j/drivers/firmata/src/main/java/org/apache/plc4x/java/firmata/readwrite/FirmataDriver.java b/plc4j/drivers/firmata/src/main/java/org/apache/plc4x/java/firmata/readwrite/FirmataDriver.java index 631a532fa37..8c38c2d8188 100644 --- a/plc4j/drivers/firmata/src/main/java/org/apache/plc4x/java/firmata/readwrite/FirmataDriver.java +++ b/plc4j/drivers/firmata/src/main/java/org/apache/plc4x/java/firmata/readwrite/FirmataDriver.java @@ -24,10 +24,8 @@ import org.apache.plc4x.java.firmata.readwrite.configuration.FirmataConfiguration; import org.apache.plc4x.java.firmata.readwrite.context.FirmataDriverContext; import org.apache.plc4x.java.firmata.readwrite.tag.FirmataTag; -import org.apache.plc4x.java.firmata.readwrite.tag.FirmataTagHandler; import org.apache.plc4x.java.firmata.readwrite.protocol.FirmataProtocolLogic; import org.apache.plc4x.java.spi.connection.GeneratedDriverBase; -import org.apache.plc4x.java.spi.connection.PlcTagHandler; import org.apache.plc4x.java.spi.connection.ProtocolStackConfigurer; import org.apache.plc4x.java.spi.connection.SingleProtocolStackConfigurer; @@ -74,11 +72,6 @@ protected boolean canSubscribe() { return true; } - @Override - protected PlcTagHandler getTagHandler() { - return new FirmataTagHandler(); - } - /** * This protocol doesn't have a disconnect procedure, so there is no need to wait for a login to finish. * @return false 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 225fe97563c..839ecd52936 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 @@ -31,8 +31,10 @@ import org.apache.plc4x.java.firmata.readwrite.tag.FirmataTag; import org.apache.plc4x.java.firmata.readwrite.tag.FirmataTagAnalog; import org.apache.plc4x.java.firmata.readwrite.tag.FirmataTagDigital; +import org.apache.plc4x.java.firmata.readwrite.tag.FirmataTagHandler; import org.apache.plc4x.java.spi.ConversationContext; import org.apache.plc4x.java.spi.Plc4xProtocolBase; +import org.apache.plc4x.java.spi.connection.PlcTagHandler; import org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionEvent; import org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionResponse; import org.apache.plc4x.java.spi.messages.DefaultPlcWriteResponse; @@ -70,6 +72,11 @@ public class FirmataProtocolLogic extends Plc4xProtocolBase impl private final Map> consumers = new ConcurrentHashMap<>(); + @Override + public PlcTagHandler getTagHandler() { + return new FirmataTagHandler(); + } + @Override public void onConnect(ConversationContext context) { LOGGER.debug("Sending Firmata Reset Command"); diff --git a/plc4j/drivers/iec-60870/src/main/java/org/apache/plc4x/java/iec608705104/readwrite/Iec60870514PlcDriver.java b/plc4j/drivers/iec-60870/src/main/java/org/apache/plc4x/java/iec608705104/readwrite/Iec60870514PlcDriver.java index 85ad63e5a59..bd0e5c80453 100644 --- a/plc4j/drivers/iec-60870/src/main/java/org/apache/plc4x/java/iec608705104/readwrite/Iec60870514PlcDriver.java +++ b/plc4j/drivers/iec-60870/src/main/java/org/apache/plc4x/java/iec608705104/readwrite/Iec60870514PlcDriver.java @@ -24,7 +24,6 @@ import org.apache.plc4x.java.iec608705104.readwrite.configuration.Iec608705014Configuration; import org.apache.plc4x.java.iec608705104.readwrite.configuration.Iec608705014TcpTransportConfiguration; import org.apache.plc4x.java.iec608705104.readwrite.protocol.Iec608705104Protocol; -import org.apache.plc4x.java.iec608705104.readwrite.tag.Iec608705104TagHandler; import org.apache.plc4x.java.spi.connection.GeneratedDriverBase; import org.apache.plc4x.java.spi.connection.ProtocolStackConfigurer; import org.apache.plc4x.java.spi.connection.SingleProtocolStackConfigurer; @@ -82,11 +81,6 @@ protected List getSupportedTransportCodes() { return Collections.singletonList("tcp"); } - @Override - protected Iec608705104TagHandler getTagHandler() { - return new Iec608705104TagHandler(); - } - /** * This protocol doesn't have a disconnect procedure, so there is no need to wait for a login to finish. * @return false diff --git a/plc4j/drivers/iec-60870/src/main/java/org/apache/plc4x/java/iec608705104/readwrite/protocol/Iec608705104Protocol.java b/plc4j/drivers/iec-60870/src/main/java/org/apache/plc4x/java/iec608705104/readwrite/protocol/Iec608705104Protocol.java index f97c5bf03c6..61e4a41e2cb 100644 --- a/plc4j/drivers/iec-60870/src/main/java/org/apache/plc4x/java/iec608705104/readwrite/protocol/Iec608705104Protocol.java +++ b/plc4j/drivers/iec-60870/src/main/java/org/apache/plc4x/java/iec608705104/readwrite/protocol/Iec608705104Protocol.java @@ -31,9 +31,11 @@ import org.apache.plc4x.java.iec608705104.readwrite.messages.Iec608705104PlcSubscriptionEvent; import org.apache.plc4x.java.iec608705104.readwrite.model.Iec608705104SubscriptionHandle; import org.apache.plc4x.java.iec608705104.readwrite.tag.Iec608705104Tag; +import org.apache.plc4x.java.iec608705104.readwrite.tag.Iec608705104TagHandler; import org.apache.plc4x.java.spi.ConversationContext; import org.apache.plc4x.java.spi.Plc4xProtocolBase; import org.apache.plc4x.java.spi.configuration.HasConfiguration; +import org.apache.plc4x.java.spi.connection.PlcTagHandler; import org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionResponse; import org.apache.plc4x.java.spi.messages.PlcBrowser; import org.apache.plc4x.java.spi.messages.PlcSubscriber; @@ -71,6 +73,11 @@ public void setConfiguration(Iec608705014Configuration configuration) { this.configuration = configuration; } + @Override + public PlcTagHandler getTagHandler() { + return new Iec608705104TagHandler(); + } + @Override public void close(ConversationContext context) { tm.shutdown(); diff --git a/plc4j/drivers/knxnetip/src/main/java/org/apache/plc4x/java/knxnetip/KnxNetIpDriver.java b/plc4j/drivers/knxnetip/src/main/java/org/apache/plc4x/java/knxnetip/KnxNetIpDriver.java index 99c3378dac9..5718f34b8cc 100644 --- a/plc4j/drivers/knxnetip/src/main/java/org/apache/plc4x/java/knxnetip/KnxNetIpDriver.java +++ b/plc4j/drivers/knxnetip/src/main/java/org/apache/plc4x/java/knxnetip/KnxNetIpDriver.java @@ -30,11 +30,9 @@ import org.apache.plc4x.java.knxnetip.context.KnxNetIpDriverContext; import org.apache.plc4x.java.knxnetip.tag.KnxNetIpTag; import org.apache.plc4x.java.knxnetip.readwrite.KnxNetIpMessage; -import org.apache.plc4x.java.knxnetip.tag.KnxNetIpTagHandler; import org.apache.plc4x.java.knxnetip.protocol.KnxNetIpProtocolLogic; import org.apache.plc4x.java.spi.messages.DefaultPlcDiscoveryRequest; import org.apache.plc4x.java.spi.connection.GeneratedDriverBase; -import org.apache.plc4x.java.spi.connection.PlcTagHandler; import org.apache.plc4x.java.spi.connection.ProtocolStackConfigurer; import org.apache.plc4x.java.spi.connection.SingleProtocolStackConfigurer; import org.apache.plc4x.java.spi.optimizer.BaseOptimizer; @@ -122,11 +120,6 @@ protected BaseOptimizer getOptimizer() { return new SingleTagOptimizer(); } - @Override - protected PlcTagHandler getTagHandler() { - return new KnxNetIpTagHandler(); - } - @Override protected boolean awaitDisconnectComplete() { return true; } diff --git a/plc4j/drivers/knxnetip/src/main/java/org/apache/plc4x/java/knxnetip/protocol/KnxNetIpProtocolLogic.java b/plc4j/drivers/knxnetip/src/main/java/org/apache/plc4x/java/knxnetip/protocol/KnxNetIpProtocolLogic.java index b968ab22e02..784876cce76 100644 --- a/plc4j/drivers/knxnetip/src/main/java/org/apache/plc4x/java/knxnetip/protocol/KnxNetIpProtocolLogic.java +++ b/plc4j/drivers/knxnetip/src/main/java/org/apache/plc4x/java/knxnetip/protocol/KnxNetIpProtocolLogic.java @@ -32,8 +32,10 @@ import org.apache.plc4x.java.knxnetip.model.KnxNetIpSubscriptionHandle; import org.apache.plc4x.java.knxnetip.readwrite.*; import org.apache.plc4x.java.knxnetip.tag.KnxNetIpTag; +import org.apache.plc4x.java.knxnetip.tag.KnxNetIpTagHandler; import org.apache.plc4x.java.spi.ConversationContext; import org.apache.plc4x.java.spi.Plc4xProtocolBase; +import org.apache.plc4x.java.spi.connection.PlcTagHandler; import org.apache.plc4x.java.spi.context.DriverContext; import org.apache.plc4x.java.spi.generation.*; import org.apache.plc4x.java.spi.messages.*; @@ -81,6 +83,11 @@ public void setDriverContext(DriverContext driverContext) { this.tm = new RequestTransactionManager(1); } + @Override + public PlcTagHandler getTagHandler() { + return new KnxNetIpTagHandler(); + } + @Override public void close(ConversationContext context) { tm.shutdown(); diff --git a/plc4j/drivers/knxnetip/src/test/java/org/apache/plc4x/java/knxnetip/ManualKnxNetIp.java b/plc4j/drivers/knxnetip/src/test/java/org/apache/plc4x/java/knxnetip/ManualKnxNetIp.java index 7e3a1b32eaa..9cadd0191d6 100644 --- a/plc4j/drivers/knxnetip/src/test/java/org/apache/plc4x/java/knxnetip/ManualKnxNetIp.java +++ b/plc4j/drivers/knxnetip/src/test/java/org/apache/plc4x/java/knxnetip/ManualKnxNetIp.java @@ -38,8 +38,8 @@ public class ManualKnxNetIp { // */*/101: Power Line 1 public static void main(String[] args) throws Exception { - //final PlcConnection connection = new PlcDriverManager().getConnection("knxnet-ip://192.168.42.11?knxproj-file-path=/Users/christofer.dutz/Projects/Apache/PLC4X-Documents/KNX/Stettiner%20Str.%2013/StettinerStr-Soll-Ist-Temperatur.knxproj"); - final PlcConnection connection = new DefaultPlcDriverManager().getConnection("knxnet-ip:pcap:///Users/christofer.dutz/Projects/Apache/PLC4X-Documents/KNX/Recording-01.03.2020-2.pcapng?knxproj-file-path=/Users/christofer.dutz/Projects/Apache/PLC4X-Documents/KNX/Stettiner%20Str.%2013/StettinerStr-Soll-Ist-Temperatur.knxproj"); + final PlcConnection connection = new DefaultPlcDriverManager().getConnection("knxnet-ip://192.168.42.11?knxproj-file-path=/Users/christofer.dutz/Projects/Apache/PLC4X-Documents/KNX/Stettiner%20Str.%2013/StettinerStr-Soll-Ist-Temperatur.knxproj"); + //final PlcConnection connection = new DefaultPlcDriverManager().getConnection("knxnet-ip:pcap:///Users/christofer.dutz/Projects/Apache/PLC4X-Documents/KNX/Recording-01.03.2020-2.pcapng?knxproj-file-path=/Users/christofer.dutz/Projects/Apache/PLC4X-Documents/KNX/Stettiner%20Str.%2013/StettinerStr-Soll-Ist-Temperatur.knxproj"); // Make sure we hang up correctly when terminating. Runtime.getRuntime().addShutdownHook(new Thread(() -> { try { diff --git a/plc4j/drivers/mock/src/main/java/org/apache/plc4x/java/mock/connection/MockConnection.java b/plc4j/drivers/mock/src/main/java/org/apache/plc4x/java/mock/connection/MockConnection.java index b93259abfaa..b3c4d3a3712 100644 --- a/plc4j/drivers/mock/src/main/java/org/apache/plc4x/java/mock/connection/MockConnection.java +++ b/plc4j/drivers/mock/src/main/java/org/apache/plc4x/java/mock/connection/MockConnection.java @@ -21,6 +21,7 @@ import org.apache.commons.lang3.Validate; import org.apache.plc4x.java.api.PlcConnection; import org.apache.plc4x.java.api.authentication.PlcAuthentication; +import org.apache.plc4x.java.api.exceptions.PlcRuntimeException; import org.apache.plc4x.java.api.messages.*; import org.apache.plc4x.java.api.metadata.PlcConnectionMetadata; import org.apache.plc4x.java.api.model.PlcConsumerRegistration; @@ -32,6 +33,7 @@ import org.apache.plc4x.java.spi.messages.*; import org.apache.plc4x.java.spi.messages.utils.PlcResponseItem; import org.apache.plc4x.java.spi.values.DefaultPlcValueHandler; +import org.apache.plc4x.java.spi.values.PlcValueHandler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -48,6 +50,8 @@ public class MockConnection implements PlcConnection, PlcReader, PlcWriter, PlcS private static final Logger LOGGER = LoggerFactory.getLogger(MockConnection.class); + private PlcValueHandler valueHandler; + private final PlcAuthentication authentication; private MockDevice device; @@ -63,9 +67,21 @@ public MockDevice getDevice() { private final MockTagHandler mockTagHandler = new MockTagHandler(); public void setDevice(MockDevice device) { LOGGER.info("Set Mock Device on Mock Connection {} with device {}", this, device); + this.valueHandler = new DefaultPlcValueHandler(); this.device = device; } + @Override + public Optional parseTagValue(PlcTag tag, Object... values) { + PlcValue plcValue; + try { + plcValue = valueHandler.newPlcValue(tag, values); + } catch (Exception e) { + throw new PlcRuntimeException("Error parsing tag value " + tag, e); + } + return Optional.of(plcValue); + } + @Override public void connect() { // do nothing diff --git a/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/ascii/ModbusAsciiDriver.java b/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/ascii/ModbusAsciiDriver.java index e79dc8cc9c8..20006d99fc9 100644 --- a/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/ascii/ModbusAsciiDriver.java +++ b/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/ascii/ModbusAsciiDriver.java @@ -27,7 +27,6 @@ import org.apache.plc4x.java.modbus.ascii.config.ModbusAsciiConfiguration; import org.apache.plc4x.java.modbus.ascii.protocol.ModbusAsciiProtocolLogic; import org.apache.plc4x.java.modbus.base.tag.ModbusTag; -import org.apache.plc4x.java.modbus.base.tag.ModbusTagHandler; import org.apache.plc4x.java.modbus.readwrite.DriverType; import org.apache.plc4x.java.modbus.readwrite.ModbusAsciiADU; import org.apache.plc4x.java.modbus.tcp.config.ModbusTcpTransportConfiguration; @@ -119,11 +118,6 @@ protected BaseOptimizer getOptimizer() { return new SingleTagOptimizer(); } - @Override - protected ModbusTagHandler getTagHandler() { - return new ModbusTagHandler(); - } - @Override protected ProtocolStackConfigurer getStackConfigurer() { return SingleProtocolStackConfigurer.builder(ModbusAsciiADU.class, new ModbusAsciiInput(), new ModbusAsciiOutput()) diff --git a/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/ascii/protocol/ModbusAsciiProtocolLogic.java b/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/ascii/protocol/ModbusAsciiProtocolLogic.java index 8a79ecf5ac3..28470bba765 100644 --- a/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/ascii/protocol/ModbusAsciiProtocolLogic.java +++ b/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/ascii/protocol/ModbusAsciiProtocolLogic.java @@ -26,10 +26,12 @@ import org.apache.plc4x.java.modbus.ascii.config.ModbusAsciiConfiguration; import org.apache.plc4x.java.modbus.base.tag.ModbusTag; import org.apache.plc4x.java.modbus.base.protocol.ModbusProtocolLogic; +import org.apache.plc4x.java.modbus.base.tag.ModbusTagHandler; import org.apache.plc4x.java.modbus.readwrite.*; import org.apache.plc4x.java.modbus.types.ModbusByteOrder; import org.apache.plc4x.java.spi.ConversationContext; import org.apache.plc4x.java.spi.configuration.HasConfiguration; +import org.apache.plc4x.java.spi.connection.PlcTagHandler; import org.apache.plc4x.java.spi.generation.ParseException; import org.apache.plc4x.java.spi.messages.*; import org.apache.plc4x.java.spi.messages.utils.DefaultPlcResponseItem; @@ -53,6 +55,11 @@ public void setConfiguration(ModbusAsciiConfiguration configuration) { this.tm = new RequestTransactionManager(1); } + @Override + public PlcTagHandler getTagHandler() { + return new ModbusTagHandler(); + } + @Override public void close(ConversationContext context) { tm.shutdown(); diff --git a/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/rtu/ModbusRtuDriver.java b/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/rtu/ModbusRtuDriver.java index 392968317e7..19a0e872ddb 100644 --- a/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/rtu/ModbusRtuDriver.java +++ b/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/rtu/ModbusRtuDriver.java @@ -24,7 +24,6 @@ import org.apache.plc4x.java.spi.configuration.PlcConnectionConfiguration; import org.apache.plc4x.java.spi.configuration.PlcTransportConfiguration; import org.apache.plc4x.java.modbus.base.tag.ModbusTag; -import org.apache.plc4x.java.modbus.base.tag.ModbusTagHandler; import org.apache.plc4x.java.modbus.readwrite.DriverType; import org.apache.plc4x.java.modbus.readwrite.ModbusRtuADU; import org.apache.plc4x.java.modbus.rtu.config.ModbusRtuConfiguration; @@ -117,11 +116,6 @@ protected BaseOptimizer getOptimizer() { return new SingleTagOptimizer(); } - @Override - protected ModbusTagHandler getTagHandler() { - return new ModbusTagHandler(); - } - @Override protected ProtocolStackConfigurer getStackConfigurer() { return SingleProtocolStackConfigurer.builder(ModbusRtuADU.class, io -> (ModbusRtuADU) ModbusRtuADU.staticParse(io, DriverType.MODBUS_RTU, true)) diff --git a/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/rtu/protocol/ModbusRtuProtocolLogic.java b/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/rtu/protocol/ModbusRtuProtocolLogic.java index 6f36dea2069..fb560d5aa48 100644 --- a/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/rtu/protocol/ModbusRtuProtocolLogic.java +++ b/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/rtu/protocol/ModbusRtuProtocolLogic.java @@ -25,11 +25,13 @@ import org.apache.plc4x.java.api.value.PlcValue; import org.apache.plc4x.java.modbus.base.tag.ModbusTag; import org.apache.plc4x.java.modbus.base.protocol.ModbusProtocolLogic; +import org.apache.plc4x.java.modbus.base.tag.ModbusTagHandler; import org.apache.plc4x.java.modbus.readwrite.*; import org.apache.plc4x.java.modbus.rtu.config.ModbusRtuConfiguration; import org.apache.plc4x.java.modbus.types.ModbusByteOrder; import org.apache.plc4x.java.spi.ConversationContext; import org.apache.plc4x.java.spi.configuration.HasConfiguration; +import org.apache.plc4x.java.spi.connection.PlcTagHandler; import org.apache.plc4x.java.spi.generation.ParseException; import org.apache.plc4x.java.spi.messages.*; import org.apache.plc4x.java.spi.messages.utils.DefaultPlcResponseItem; @@ -53,6 +55,11 @@ public void setConfiguration(ModbusRtuConfiguration configuration) { this.tm = new RequestTransactionManager(1); } + @Override + public PlcTagHandler getTagHandler() { + return new ModbusTagHandler(); + } + @Override public void close(ConversationContext context) { tm.shutdown(); diff --git a/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/tcp/ModbusTcpDriver.java b/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/tcp/ModbusTcpDriver.java index d49c0d452fd..a0bd18b9c66 100644 --- a/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/tcp/ModbusTcpDriver.java +++ b/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/tcp/ModbusTcpDriver.java @@ -29,7 +29,6 @@ import org.apache.plc4x.java.modbus.tcp.config.ModbusTcpConfiguration; import org.apache.plc4x.java.modbus.tcp.config.ModbusTcpTransportConfiguration; import org.apache.plc4x.java.modbus.tcp.discovery.ModbusPlcDiscoverer; -import org.apache.plc4x.java.modbus.base.tag.ModbusTagHandler; import org.apache.plc4x.java.modbus.readwrite.ModbusTcpADU; import org.apache.plc4x.java.modbus.tcp.protocol.ModbusTcpProtocolLogic; import org.apache.plc4x.java.spi.messages.DefaultPlcDiscoveryRequest; @@ -122,11 +121,6 @@ protected BaseOptimizer getOptimizer() { return new /*SingleTagOptimizer();/*/ModbusOptimizer(); } - @Override - protected ModbusTagHandler getTagHandler() { - return new ModbusTagHandler(); - } - @Override protected ProtocolStackConfigurer getStackConfigurer() { return SingleProtocolStackConfigurer.builder(ModbusTcpADU.class, (io) -> (ModbusTcpADU) ModbusTcpADU.staticParse(io, DriverType.MODBUS_TCP, true)) diff --git a/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/tcp/protocol/ModbusTcpProtocolLogic.java b/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/tcp/protocol/ModbusTcpProtocolLogic.java index 5de6596b7e1..6c30f28a29b 100644 --- a/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/tcp/protocol/ModbusTcpProtocolLogic.java +++ b/plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/tcp/protocol/ModbusTcpProtocolLogic.java @@ -31,6 +31,7 @@ import org.apache.plc4x.java.modbus.types.ModbusByteOrder; import org.apache.plc4x.java.spi.ConversationContext; import org.apache.plc4x.java.spi.configuration.HasConfiguration; +import org.apache.plc4x.java.spi.connection.PlcTagHandler; import org.apache.plc4x.java.spi.generation.ParseException; import org.apache.plc4x.java.spi.messages.*; import org.apache.plc4x.java.spi.messages.utils.DefaultPlcResponseItem; @@ -55,6 +56,11 @@ public void setConfiguration(ModbusTcpConfiguration configuration) { this.tm = new RequestTransactionManager(1); } + @Override + public PlcTagHandler getTagHandler() { + return new ModbusTagHandler(); + } + @Override public void close(ConversationContext context) { tm.shutdown(); diff --git a/plc4j/drivers/modbus/src/test/java/org/apache/plc4x/java/modbus/ModbusEncodeTest.java b/plc4j/drivers/modbus/src/test/java/org/apache/plc4x/java/modbus/ModbusEncodeTest.java index 7311be64d35..8e6426ccec9 100644 --- a/plc4j/drivers/modbus/src/test/java/org/apache/plc4x/java/modbus/ModbusEncodeTest.java +++ b/plc4j/drivers/modbus/src/test/java/org/apache/plc4x/java/modbus/ModbusEncodeTest.java @@ -32,7 +32,7 @@ public class ModbusEncodeTest { @Test public void testEncodeBooleanBOOL() { Boolean[] object = {true,false,true,false,true,false,true,true,false}; - ModbusTagCoil coils = ModbusTagCoil.of("coil:8:BOOL"); + ModbusTagCoil coils = ModbusTagCoil.of("coil:8:BOOL[9]"); PlcList list = (PlcList) new DefaultPlcValueHandler().newPlcValue(coils, object); Assertions.assertEquals("[true,false,true,false,true,false,true,true,false]", list.toString()); } @@ -40,135 +40,135 @@ public void testEncodeBooleanBOOL() { @Test public void testEncodeIntegerSINT() { Integer[] object = {1,-1,127,-128,5,6,7,8}; - ModbusTagHoldingRegister holdingregister = ModbusTagHoldingRegister.of("holding-register:8:SINT{unit-id: 10}"); - Assertions.assertEquals((short) 10, holdingregister.getUnitId()); - PlcList list = (PlcList) new DefaultPlcValueHandler().newPlcValue(holdingregister, object); + ModbusTagHoldingRegister holdingRegister = ModbusTagHoldingRegister.of("holding-register:8:SINT[8]{unit-id: 10}"); + Assertions.assertEquals((short) 10, holdingRegister.getUnitId()); + PlcList list = (PlcList) new DefaultPlcValueHandler().newPlcValue(holdingRegister, object); Assertions.assertEquals("[1,-1,127,-128,5,6,7,8]", list.toString()); } @Test public void testEncodeIntegerUSINT() { Integer[] object = {1,255,0,4,5,6,7,8}; - ModbusTagHoldingRegister holdingregister = ModbusTagHoldingRegister.of("holding-register:8:USINT"); - PlcList list = (PlcList) new DefaultPlcValueHandler().newPlcValue(holdingregister, object); + ModbusTagHoldingRegister holdingRegister = ModbusTagHoldingRegister.of("holding-register:8:USINT[8]"); + PlcList list = (PlcList) new DefaultPlcValueHandler().newPlcValue(holdingRegister, object); Assertions.assertEquals("[1,255,0,4,5,6,7,8]", list.toString()); } @Test public void testEncodeIntegerBYTE() { Integer[] object = {1,255,0,4,5,6,7,8}; - ModbusTagHoldingRegister holdingregister = ModbusTagHoldingRegister.of("holding-register:8:BYTE"); - PlcList list = (PlcList) new DefaultPlcValueHandler().newPlcValue(holdingregister, object); + ModbusTagHoldingRegister holdingRegister = ModbusTagHoldingRegister.of("holding-register:8:BYTE[8]"); + PlcList list = (PlcList) new DefaultPlcValueHandler().newPlcValue(holdingRegister, object); Assertions.assertEquals("[1,255,0,4,5,6,7,8]", list.toString()); } @Test public void testEncodeIntegerINT() { Integer[] object = {1,-1,32000,-32000,5,6,7}; - ModbusTagHoldingRegister holdingregister = ModbusTagHoldingRegister.of("holding-register:7:INT"); - PlcList list = (PlcList) new DefaultPlcValueHandler().newPlcValue(holdingregister, object); + ModbusTagHoldingRegister holdingRegister = ModbusTagHoldingRegister.of("holding-register:7:INT[7]"); + PlcList list = (PlcList) new DefaultPlcValueHandler().newPlcValue(holdingRegister, object); Assertions.assertEquals("[1,-1,32000,-32000,5,6,7]", list.toString()); } @Test public void testEncodeIntegerUINT() { Integer[] object = {1,65535,10,55000,5,6,7}; - ModbusTagHoldingRegister holdingregister = ModbusTagHoldingRegister.of("holding-register:7:UINT"); - PlcList list = (PlcList) new DefaultPlcValueHandler().newPlcValue(holdingregister, object); + ModbusTagHoldingRegister holdingRegister = ModbusTagHoldingRegister.of("holding-register:7:UINT[7]"); + PlcList list = (PlcList) new DefaultPlcValueHandler().newPlcValue(holdingRegister, object); Assertions.assertEquals("[1,65535,10,55000,5,6,7]", list.toString()); } @Test public void testEncodeIntegerWORD() { Integer[] object = {1,65535,10,55000,5,6,7}; - ModbusTagHoldingRegister holdingregister = ModbusTagHoldingRegister.of("holding-register:7:WORD"); - PlcList list = (PlcList) new DefaultPlcValueHandler().newPlcValue(holdingregister, object); + ModbusTagHoldingRegister holdingRegister = ModbusTagHoldingRegister.of("holding-register:7:WORD[7]"); + PlcList list = (PlcList) new DefaultPlcValueHandler().newPlcValue(holdingRegister, object); Assertions.assertEquals("[1,65535,10,55000,5,6,7]", list.toString()); } @Test public void testEncodeIntegerDINT() { Integer[] object = {1,655354775,-2147483648,2147483647,5,6,7}; - ModbusTagHoldingRegister holdingregister = ModbusTagHoldingRegister.of("holding-register:7:DINT"); - PlcList list = (PlcList) new DefaultPlcValueHandler().newPlcValue(holdingregister, object); + ModbusTagHoldingRegister holdingRegister = ModbusTagHoldingRegister.of("holding-register:7:DINT[7]"); + PlcList list = (PlcList) new DefaultPlcValueHandler().newPlcValue(holdingRegister, object); Assertions.assertEquals("[1,655354775,-2147483648,2147483647,5,6,7]", list.toString()); } @Test public void testEncodeLongUDINT() { Long[] object = {1L,655354775L,0L,4294967295L,5L,6L,7L}; - ModbusTagHoldingRegister holdingregister = ModbusTagHoldingRegister.of("holding-register:7:UDINT"); - PlcList list = (PlcList) new DefaultPlcValueHandler().newPlcValue(holdingregister, object); + ModbusTagHoldingRegister holdingRegister = ModbusTagHoldingRegister.of("holding-register:7:UDINT[7]"); + PlcList list = (PlcList) new DefaultPlcValueHandler().newPlcValue(holdingRegister, object); Assertions.assertEquals("[1,655354775,0,4294967295,5,6,7]", list.toString()); } @Test public void testEncodeLongDWORD() { Long[] object = {1L,655354775L,0L,4294967295L,5L,6L,7L}; - ModbusTagHoldingRegister holdingregister = ModbusTagHoldingRegister.of("holding-register:7:DWORD"); - PlcList list = (PlcList) new DefaultPlcValueHandler().newPlcValue(holdingregister, object); + ModbusTagHoldingRegister holdingRegister = ModbusTagHoldingRegister.of("holding-register:7:DWORD[7]"); + PlcList list = (PlcList) new DefaultPlcValueHandler().newPlcValue(holdingRegister, object); Assertions.assertEquals("[1,655354775,0,4294967295,5,6,7]", list.toString()); } @Test public void testEncodeLongLINT() { Long[] object = {1L,655354775L,-9223372036854775808L,9223372036854775807L,5L,6L,7L}; - ModbusTagHoldingRegister holdingregister = ModbusTagHoldingRegister.of("holding-register:7:LINT"); - PlcList list = (PlcList) new DefaultPlcValueHandler().newPlcValue(holdingregister, object); + ModbusTagHoldingRegister holdingRegister = ModbusTagHoldingRegister.of("holding-register:7:LINT[7]"); + PlcList list = (PlcList) new DefaultPlcValueHandler().newPlcValue(holdingRegister, object); Assertions.assertEquals("[1,655354775,-9223372036854775808,9223372036854775807,5,6,7]", list.toString()); } @Test public void testEncodeBigIntegerULINT() { BigInteger[] object = {BigInteger.valueOf(1L),BigInteger.valueOf(655354775L),BigInteger.valueOf(0),new BigInteger("18446744073709551615"),BigInteger.valueOf(5L),BigInteger.valueOf(6L),BigInteger.valueOf(7L)}; - ModbusTagHoldingRegister holdingregister = ModbusTagHoldingRegister.of("holding-register:7:ULINT"); - PlcList list = (PlcList) new DefaultPlcValueHandler().newPlcValue(holdingregister, object); + ModbusTagHoldingRegister holdingRegister = ModbusTagHoldingRegister.of("holding-register:7:ULINT[7]"); + PlcList list = (PlcList) new DefaultPlcValueHandler().newPlcValue(holdingRegister, object); Assertions.assertEquals("[1,655354775,0,18446744073709551615,5,6,7]", list.toString()); } @Test public void testEncodeBigIntegerLWORD() { BigInteger[] object = {BigInteger.valueOf(1L),BigInteger.valueOf(655354775L),BigInteger.valueOf(0),new BigInteger("18446744073709551615"),BigInteger.valueOf(5L),BigInteger.valueOf(6L),BigInteger.valueOf(7L)}; - ModbusTagHoldingRegister holdingregister = ModbusTagHoldingRegister.of("holding-register:7:LWORD"); - PlcList list = (PlcList) new DefaultPlcValueHandler().newPlcValue(holdingregister, object); + ModbusTagHoldingRegister holdingRegister = ModbusTagHoldingRegister.of("holding-register:7:LWORD[7]"); + PlcList list = (PlcList) new DefaultPlcValueHandler().newPlcValue(holdingRegister, object); Assertions.assertEquals("[1,655354775,0,18446744073709551615,5,6,7]", list.toString()); } @Test public void testEncodeFloatREAL() { Float[] object = {1.1f,1000.1f,100000.1f,3.4028232E38f,-3.4028232E38f,-1f,10384759934840.0f}; - ModbusTagHoldingRegister holdingregister = ModbusTagHoldingRegister.of("holding-register:7:REAL"); - PlcList list = (PlcList) new DefaultPlcValueHandler().newPlcValue(holdingregister, object); + ModbusTagHoldingRegister holdingRegister = ModbusTagHoldingRegister.of("holding-register:7:REAL[7]"); + PlcList list = (PlcList) new DefaultPlcValueHandler().newPlcValue(holdingRegister, object); //! When using Java 19 it seems the toString method uses a different precision than the previous versions, //! so we need to check differently in this case. for (int i = 0; i < list.getLength(); i++) { PlcValue plcValue = list.getIndex(i); Float referenceValue = object[i]; - Assertions.assertEquals(referenceValue, plcValue.getFloat()); + Assertions.assertEquals(referenceValue, plcValue.getFloat(), 0.001); } } @Test public void testEncodeDoubleLREAL() { Double[] object = {1.1,1000.1,100000.1,1.7E308,-1.7E308,-1d,10384759934840.0}; - ModbusTagHoldingRegister holdingregister = ModbusTagHoldingRegister.of("holding-register:7:LREAL"); - PlcList list = (PlcList) new DefaultPlcValueHandler().newPlcValue(holdingregister, object); + ModbusTagHoldingRegister holdingRegister = ModbusTagHoldingRegister.of("holding-register:7:LREAL[7]"); + PlcList list = (PlcList) new DefaultPlcValueHandler().newPlcValue(holdingRegister, object); Assertions.assertEquals("[1.1,1000.1,100000.1,1.7E308,-1.7E308,-1.0,1.038475993484E13]", list.toString()); } /*@Test public void testEncodeStringSTRING() { String[] object = {"Hello Toddy!"}; - ModbusTagHoldingRegister holdingregister = ModbusTagHoldingRegister.of("holding-register:8:STRING"); - PlcList list = (PlcList) handler.encodeString(holdingregister, object); + ModbusTagHoldingRegister holdingRegister = ModbusTagHoldingRegister.of("holding-register:8:STRING"); + PlcList list = (PlcList) handler.encodeString(holdingRegister, object); Assertions.assertEquals("[H,e,l,l,o, ,T,o,d,d,y,!]", list.toString()); } @Test public void testEncodeStringWSTRING() { String[] object = {"Hello Toddy!"}; - ModbusTagHoldingRegister holdingregister = ModbusTagdHoldingRegister.of("holding-register:8:WSTRING"); - PlcList list = (PlcList) handler.encodeString(holdingregister, object); + ModbusTagHoldingRegister holdingRegister = ModbusTagdHoldingRegister.of("holding-register:8:WSTRING"); + PlcList list = (PlcList) handler.encodeString(holdingRegister, object); Assertions.assertEquals("[H,e,l,l,o, ,T,o,d,d,y,!]", list.toString()); } */ } diff --git a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/OpcuaPlcDriver.java b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/OpcuaPlcDriver.java index 2a8d8f483dc..51488739cb9 100644 --- a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/OpcuaPlcDriver.java +++ b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/OpcuaPlcDriver.java @@ -25,7 +25,6 @@ import org.apache.plc4x.java.opcua.optimizer.OpcuaOptimizer; import org.apache.plc4x.java.opcua.protocol.OpcuaProtocolLogic; import org.apache.plc4x.java.opcua.readwrite.OpcuaAPU; -import org.apache.plc4x.java.opcua.tag.OpcuaPlcTagHandler; import org.apache.plc4x.java.opcua.tag.OpcuaTag; import org.apache.plc4x.java.spi.connection.GeneratedDriverBase; import org.apache.plc4x.java.spi.connection.ProtocolStackConfigurer; @@ -83,11 +82,6 @@ protected OpcuaOptimizer getOptimizer() { return new OpcuaOptimizer(); } - @Override - protected OpcuaPlcTagHandler getTagHandler() { - return new OpcuaPlcTagHandler(); - } - @Override protected boolean fireDiscoverEvent() { return true; diff --git a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaProtocolLogic.java b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaProtocolLogic.java index 2741efb8cc6..e2794a4aced 100644 --- a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaProtocolLogic.java +++ b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/protocol/OpcuaProtocolLogic.java @@ -37,10 +37,12 @@ import org.apache.plc4x.java.opcua.context.OpcuaDriverContext; import org.apache.plc4x.java.opcua.context.SecureChannel; import org.apache.plc4x.java.opcua.readwrite.*; +import org.apache.plc4x.java.opcua.tag.OpcuaPlcTagHandler; import org.apache.plc4x.java.opcua.tag.OpcuaTag; import org.apache.plc4x.java.spi.ConversationContext; import org.apache.plc4x.java.spi.Plc4xProtocolBase; import org.apache.plc4x.java.spi.configuration.HasConfiguration; +import org.apache.plc4x.java.spi.connection.PlcTagHandler; import org.apache.plc4x.java.spi.context.DriverContext; import org.apache.plc4x.java.spi.messages.*; import org.apache.plc4x.java.spi.messages.utils.DefaultPlcResponseItem; @@ -93,6 +95,11 @@ public void setConfiguration(OpcuaConfiguration configuration) { this.configuration = configuration; } + @Override + public PlcTagHandler getTagHandler() { + return new OpcuaPlcTagHandler(); + } + @Override public void close(ConversationContext context) { tm.shutdown(); diff --git a/plc4j/drivers/open-protocol/src/main/java/org/apache/plc4x/java/openprotocol/OpenProtocolDriver.java b/plc4j/drivers/open-protocol/src/main/java/org/apache/plc4x/java/openprotocol/OpenProtocolDriver.java index dacd6674406..95a3d0027b2 100644 --- a/plc4j/drivers/open-protocol/src/main/java/org/apache/plc4x/java/openprotocol/OpenProtocolDriver.java +++ b/plc4j/drivers/open-protocol/src/main/java/org/apache/plc4x/java/openprotocol/OpenProtocolDriver.java @@ -26,7 +26,6 @@ import org.apache.plc4x.java.openprotocol.protocol.OpenProtocolProtocolLogic; import org.apache.plc4x.java.openprotocol.readwrite.OpenProtocolMessage; import org.apache.plc4x.java.openprotocol.tag.OpenProtocolTag; -import org.apache.plc4x.java.openprotocol.tag.OpenProtocolTagHandler; import org.apache.plc4x.java.spi.connection.GeneratedDriverBase; import org.apache.plc4x.java.spi.connection.ProtocolStackConfigurer; import org.apache.plc4x.java.spi.connection.SingleProtocolStackConfigurer; @@ -99,11 +98,6 @@ protected BaseOptimizer getOptimizer() { return new SingleTagOptimizer(); } - @Override - protected OpenProtocolTagHandler getTagHandler() { - return new OpenProtocolTagHandler(); - } - @Override protected ProtocolStackConfigurer getStackConfigurer() { return SingleProtocolStackConfigurer.builder(OpenProtocolMessage.class, io -> OpenProtocolMessage.staticParse(io, 1)) diff --git a/plc4j/drivers/open-protocol/src/main/java/org/apache/plc4x/java/openprotocol/protocol/OpenProtocolProtocolLogic.java b/plc4j/drivers/open-protocol/src/main/java/org/apache/plc4x/java/openprotocol/protocol/OpenProtocolProtocolLogic.java index deafb87871c..0a639c09358 100644 --- a/plc4j/drivers/open-protocol/src/main/java/org/apache/plc4x/java/openprotocol/protocol/OpenProtocolProtocolLogic.java +++ b/plc4j/drivers/open-protocol/src/main/java/org/apache/plc4x/java/openprotocol/protocol/OpenProtocolProtocolLogic.java @@ -22,8 +22,10 @@ import org.apache.plc4x.java.api.model.PlcConsumerRegistration; import org.apache.plc4x.java.api.model.PlcSubscriptionHandle; import org.apache.plc4x.java.openprotocol.readwrite.OpenProtocolMessage; +import org.apache.plc4x.java.openprotocol.tag.OpenProtocolTagHandler; import org.apache.plc4x.java.spi.ConversationContext; import org.apache.plc4x.java.spi.Plc4xProtocolBase; +import org.apache.plc4x.java.spi.connection.PlcTagHandler; import org.apache.plc4x.java.spi.messages.PlcSubscriber; import org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration; @@ -33,6 +35,11 @@ public class OpenProtocolProtocolLogic extends Plc4xProtocolBase implements PlcSubscriber { + @Override + public PlcTagHandler getTagHandler() { + return new OpenProtocolTagHandler(); + } + @Override public void onConnect(ConversationContext context) { } diff --git a/plc4j/drivers/plc4x/src/main/java/org/apache/plc4x/java/plc4x/Plc4xDriver.java b/plc4j/drivers/plc4x/src/main/java/org/apache/plc4x/java/plc4x/Plc4xDriver.java index ec7a61bcc96..cb738d53397 100644 --- a/plc4j/drivers/plc4x/src/main/java/org/apache/plc4x/java/plc4x/Plc4xDriver.java +++ b/plc4j/drivers/plc4x/src/main/java/org/apache/plc4x/java/plc4x/Plc4xDriver.java @@ -23,11 +23,9 @@ import org.apache.plc4x.java.spi.configuration.PlcTransportConfiguration; import org.apache.plc4x.java.plc4x.config.Plc4xConfiguration; import org.apache.plc4x.java.plc4x.config.Plc4xTcpTransportConfiguration; -import org.apache.plc4x.java.plc4x.tag.Plc4XTagHandler; import org.apache.plc4x.java.plc4x.protocol.Plc4xProtocolLogic; import org.apache.plc4x.java.plc4x.readwrite.Plc4xMessage; import org.apache.plc4x.java.spi.connection.GeneratedDriverBase; -import org.apache.plc4x.java.spi.connection.PlcTagHandler; import org.apache.plc4x.java.spi.connection.ProtocolStackConfigurer; import org.apache.plc4x.java.spi.connection.SingleProtocolStackConfigurer; @@ -73,11 +71,6 @@ protected List getSupportedTransportCodes() { return Collections.singletonList("tcp"); } - @Override - protected PlcTagHandler getTagHandler() { - return new Plc4XTagHandler(); - } - /** * This protocol doesn't have a disconnect procedure, so there is no need to wait for a login to finish. * @return false diff --git a/plc4j/drivers/plc4x/src/main/java/org/apache/plc4x/java/plc4x/protocol/Plc4xProtocolLogic.java b/plc4j/drivers/plc4x/src/main/java/org/apache/plc4x/java/plc4x/protocol/Plc4xProtocolLogic.java index 6890d71f64b..4348367f641 100644 --- a/plc4j/drivers/plc4x/src/main/java/org/apache/plc4x/java/plc4x/protocol/Plc4xProtocolLogic.java +++ b/plc4j/drivers/plc4x/src/main/java/org/apache/plc4x/java/plc4x/protocol/Plc4xProtocolLogic.java @@ -23,10 +23,12 @@ import org.apache.plc4x.java.api.value.PlcValue; import org.apache.plc4x.java.plc4x.config.Plc4xConfiguration; import org.apache.plc4x.java.plc4x.readwrite.*; +import org.apache.plc4x.java.plc4x.tag.Plc4XTagHandler; import org.apache.plc4x.java.plc4x.tag.Plc4xTag; import org.apache.plc4x.java.spi.ConversationContext; import org.apache.plc4x.java.spi.Plc4xProtocolBase; import org.apache.plc4x.java.spi.configuration.HasConfiguration; +import org.apache.plc4x.java.spi.connection.PlcTagHandler; import org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse; import org.apache.plc4x.java.spi.messages.DefaultPlcWriteResponse; import org.apache.plc4x.java.spi.messages.utils.DefaultPlcResponseItem; @@ -61,6 +63,11 @@ public void setConfiguration(Plc4xConfiguration configuration) { this.connectionId = 0; } + @Override + public PlcTagHandler getTagHandler() { + return new Plc4XTagHandler(); + } + @Override public void close(ConversationContext context) { tm.shutdown(); diff --git a/plc4j/drivers/profinet-ng/src/main/java/org/apache/plc4x/java/profinet/ProfinetDriver.java b/plc4j/drivers/profinet-ng/src/main/java/org/apache/plc4x/java/profinet/ProfinetDriver.java index d9062540e7e..34ff1380da5 100644 --- a/plc4j/drivers/profinet-ng/src/main/java/org/apache/plc4x/java/profinet/ProfinetDriver.java +++ b/plc4j/drivers/profinet-ng/src/main/java/org/apache/plc4x/java/profinet/ProfinetDriver.java @@ -34,7 +34,6 @@ import org.apache.plc4x.java.profinet.protocol.ProfinetProtocolLogic; import org.apache.plc4x.java.profinet.readwrite.*; import org.apache.plc4x.java.profinet.tag.ProfinetTag; -import org.apache.plc4x.java.profinet.tag.ProfinetTagHandler; import org.apache.plc4x.java.spi.configuration.ConfigurationFactory; import org.apache.plc4x.java.spi.connection.GeneratedDriverBase; import org.apache.plc4x.java.spi.connection.ProtocolStackConfigurer; @@ -166,11 +165,6 @@ protected BaseOptimizer getOptimizer() { return new SingleTagOptimizer(); } - @Override - protected ProfinetTagHandler getTagHandler() { - return new ProfinetTagHandler(); - } - @Override protected ProtocolStackConfigurer getStackConfigurer() { return SingleProtocolStackConfigurer.builder(Ethernet_Frame.class, Ethernet_Frame::staticParse) diff --git a/plc4j/drivers/profinet-ng/src/main/java/org/apache/plc4x/java/profinet/discovery/ProfinetDiscoverer.java b/plc4j/drivers/profinet-ng/src/main/java/org/apache/plc4x/java/profinet/discovery/ProfinetDiscoverer.java index 3bf4ebad92c..d81582c7088 100644 --- a/plc4j/drivers/profinet-ng/src/main/java/org/apache/plc4x/java/profinet/discovery/ProfinetDiscoverer.java +++ b/plc4j/drivers/profinet-ng/src/main/java/org/apache/plc4x/java/profinet/discovery/ProfinetDiscoverer.java @@ -33,7 +33,7 @@ import org.apache.plc4x.java.spi.messages.DefaultPlcDiscoveryItem; import org.apache.plc4x.java.spi.messages.DefaultPlcDiscoveryResponse; import org.apache.plc4x.java.spi.messages.PlcDiscoverer; -import org.apache.plc4x.java.spi.values.PlcValues; +import org.apache.plc4x.java.spi.values.PlcSTRING; import org.apache.plc4x.java.transport.rawsocket.RawSocketTransport; import org.pcap4j.core.NotOpenException; import org.pcap4j.core.PcapHandle; @@ -230,16 +230,16 @@ public void handlePnDcpPacket(PnDcp_Pdu pdu, EthernetPacket ethernetPacket) { } Map attributes = new HashMap<>(); - attributes.put("ipAddress", PlcValues.of(remoteAddress)); - attributes.put("subnetMask", PlcValues.of(remoteSubnetMask)); - attributes.put("macAddress", PlcValues.of(srcAddr.toString())); - attributes.put("localMacAddress", PlcValues.of(dstAddr.toString())); - attributes.put("deviceTypeName", PlcValues.of(deviceTypeName)); - attributes.put("deviceName", PlcValues.of(deviceName)); - attributes.put("vendorId", PlcValues.of(vendorId)); - attributes.put("deviceId", PlcValues.of(deviceId)); - attributes.put("role", PlcValues.of(role)); - attributes.put("packetType", PlcValues.of("dcp")); + attributes.put("ipAddress", new PlcSTRING(remoteAddress)); + attributes.put("subnetMask", new PlcSTRING(remoteSubnetMask)); + attributes.put("macAddress", new PlcSTRING(srcAddr.toString())); + attributes.put("localMacAddress", new PlcSTRING(dstAddr.toString())); + attributes.put("deviceTypeName", new PlcSTRING(deviceTypeName)); + attributes.put("deviceName", new PlcSTRING(deviceName)); + attributes.put("vendorId", new PlcSTRING(vendorId)); + attributes.put("deviceId", new PlcSTRING(deviceId)); + attributes.put("role", new PlcSTRING(role)); + attributes.put("packetType", new PlcSTRING("dcp")); String name = deviceTypeName + " - " + deviceName; diff --git a/plc4j/drivers/profinet-ng/src/main/java/org/apache/plc4x/java/profinet/protocol/ProfinetProtocolLogic.java b/plc4j/drivers/profinet-ng/src/main/java/org/apache/plc4x/java/profinet/protocol/ProfinetProtocolLogic.java index 49d8df00af1..d492f36a6ee 100644 --- a/plc4j/drivers/profinet-ng/src/main/java/org/apache/plc4x/java/profinet/protocol/ProfinetProtocolLogic.java +++ b/plc4j/drivers/profinet-ng/src/main/java/org/apache/plc4x/java/profinet/protocol/ProfinetProtocolLogic.java @@ -33,10 +33,12 @@ import org.apache.plc4x.java.profinet.packets.PnDcpPacketFactory; import org.apache.plc4x.java.profinet.readwrite.*; import org.apache.plc4x.java.profinet.tag.ProfinetTag; +import org.apache.plc4x.java.profinet.tag.ProfinetTagHandler; import org.apache.plc4x.java.profinet.utils.ProfinetDataTypeMapper; import org.apache.plc4x.java.spi.ConversationContext; import org.apache.plc4x.java.spi.Plc4xProtocolBase; import org.apache.plc4x.java.spi.configuration.HasConfiguration; +import org.apache.plc4x.java.spi.connection.PlcTagHandler; import org.apache.plc4x.java.spi.context.DriverContext; import org.apache.plc4x.java.spi.messages.DefaultPlcBrowseItem; import org.apache.plc4x.java.spi.messages.DefaultPlcBrowseResponse; @@ -78,6 +80,11 @@ public void setConfiguration(ProfinetConfiguration configuration) { this.configuration = configuration; } + @Override + public PlcTagHandler getTagHandler() { + return new ProfinetTagHandler(); + } + @Override public void onConnect(ConversationContext context) { super.onConnect(context); diff --git a/plc4j/drivers/profinet/src/main/java/org/apache/plc4x/java/profinet/ProfinetDriver.java b/plc4j/drivers/profinet/src/main/java/org/apache/plc4x/java/profinet/ProfinetDriver.java index c058298c6ce..f9e3b1f34db 100644 --- a/plc4j/drivers/profinet/src/main/java/org/apache/plc4x/java/profinet/ProfinetDriver.java +++ b/plc4j/drivers/profinet/src/main/java/org/apache/plc4x/java/profinet/ProfinetDriver.java @@ -30,7 +30,6 @@ import org.apache.plc4x.java.profinet.protocol.ProfinetProtocolLogic; import org.apache.plc4x.java.profinet.readwrite.Ethernet_Frame; import org.apache.plc4x.java.profinet.tag.ProfinetTag; -import org.apache.plc4x.java.profinet.tag.ProfinetTagHandler; import org.apache.plc4x.java.spi.connection.GeneratedDriverBase; import org.apache.plc4x.java.spi.connection.ProtocolStackConfigurer; import org.apache.plc4x.java.spi.messages.DefaultPlcDiscoveryRequest; @@ -135,11 +134,6 @@ protected BaseOptimizer getOptimizer() { return new SingleTagOptimizer(); } - @Override - protected ProfinetTagHandler getTagHandler() { - return new ProfinetTagHandler(); - } - @Override protected ProtocolStackConfigurer getStackConfigurer() { return SingleProtocolStackConfigurer.builder(Ethernet_Frame.class, Ethernet_Frame::staticParse) diff --git a/plc4j/drivers/profinet/src/main/java/org/apache/plc4x/java/profinet/protocol/ProfinetProtocolLogic.java b/plc4j/drivers/profinet/src/main/java/org/apache/plc4x/java/profinet/protocol/ProfinetProtocolLogic.java index d1a903aa3d7..a30c1fbe91a 100644 --- a/plc4j/drivers/profinet/src/main/java/org/apache/plc4x/java/profinet/protocol/ProfinetProtocolLogic.java +++ b/plc4j/drivers/profinet/src/main/java/org/apache/plc4x/java/profinet/protocol/ProfinetProtocolLogic.java @@ -32,9 +32,11 @@ import org.apache.plc4x.java.profinet.discovery.ProfinetPlcDiscoverer; import org.apache.plc4x.java.profinet.readwrite.*; import org.apache.plc4x.java.profinet.tag.ProfinetTag; +import org.apache.plc4x.java.profinet.tag.ProfinetTagHandler; import org.apache.plc4x.java.spi.ConversationContext; import org.apache.plc4x.java.spi.Plc4xProtocolBase; import org.apache.plc4x.java.spi.configuration.HasConfiguration; +import org.apache.plc4x.java.spi.connection.PlcTagHandler; 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; @@ -110,6 +112,11 @@ public void setContext(ConversationContext context) { } } + @Override + public PlcTagHandler getTagHandler() { + return new ProfinetTagHandler(); + } + /** * Start a PN or LLDP discovery to find PN devices on the network. * We pass in a driverContext handler, that checks if an incoming response has the information needed for diff --git a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/S7Driver.java b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/S7Driver.java index 517fa721b94..f814cd3a49c 100644 --- a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/S7Driver.java +++ b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/S7Driver.java @@ -29,7 +29,6 @@ import org.apache.plc4x.java.s7.readwrite.protocol.S7HGeneratedDriverBase; import org.apache.plc4x.java.s7.readwrite.protocol.S7HSingleProtocolStackConfigurer; import org.apache.plc4x.java.s7.readwrite.protocol.S7ProtocolLogic; -import org.apache.plc4x.java.s7.readwrite.tag.S7PlcTagHandler; import org.apache.plc4x.java.s7.readwrite.tag.S7Tag; import org.apache.plc4x.java.spi.connection.ProtocolStackConfigurer; import org.apache.plc4x.java.spi.messages.DefaultPlcDiscoveryRequest; @@ -98,11 +97,6 @@ protected BaseOptimizer getOptimizer() { return new S7Optimizer(); } - @Override - protected S7PlcTagHandler getTagHandler() { - return new S7PlcTagHandler(); - } - @Override public PlcDiscoveryRequest.Builder discoveryRequestBuilder() { // TODO: This should actually happen in the execute method of the discoveryRequest and not here ... diff --git a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/connection/S7HMuxImpl.java b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/connection/S7HMuxImpl.java index 122ce4b309d..83703f2ab21 100644 --- a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/connection/S7HMuxImpl.java +++ b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/connection/S7HMuxImpl.java @@ -102,11 +102,11 @@ public class S7HMuxImpl extends MessageToMessageCodec implemen */ public final static AttributeKey RETRY_TIME = AttributeKey.valueOf("RETRY_TIME"); - ChannelHandlerContext embed_ctx = null; - protected Channel embeded_channel = null; - protected Channel tcp_channel = null; - protected Channel primary_channel = null; - protected Channel secondary_channel = null; + ChannelHandlerContext embedCtx = null; + protected Channel embededChannel = null; + protected Channel tcpChannel = null; + protected Channel primaryChannel = null; + protected Channel secondaryChannel = null; /* * From S7ProtocolLogic @@ -114,13 +114,15 @@ public class S7HMuxImpl extends MessageToMessageCodec implemen * the Embedded channel when we created it. */ @Override - protected void encode(ChannelHandlerContext ctx, ByteBuf outbb, List list) throws Exception { - logger.debug("ENCODE: " + outbb.toString()); - if ((embed_ctx == null) && (ctx.channel() instanceof EmbeddedChannel)) embed_ctx = ctx; - if ((tcp_channel != null) && (embed_ctx == ctx)) { - tcp_channel.writeAndFlush(outbb.copy()); + protected void encode(ChannelHandlerContext ctx, ByteBuf outBB, List list) { + logger.debug("ENCODE: {}", outBB.toString()); + if ((embedCtx == null) && (ctx.channel() instanceof EmbeddedChannel)) { + embedCtx = ctx; + } + if ((tcpChannel != null) && (embedCtx == ctx)) { + tcpChannel.writeAndFlush(outBB.copy()); } else { - list.add(outbb.copy()); + list.add(outBB.copy()); } } @@ -131,7 +133,7 @@ protected void encode(ChannelHandlerContext ctx, ByteBuf outbb, List lis */ @Override protected void decode(ChannelHandlerContext ctx, ByteBuf inbb, List list) throws Exception { - embed_ctx.fireChannelRead(inbb.copy()); + embedCtx.fireChannelRead(inbb.copy()); } @Override @@ -165,16 +167,16 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exc logger.info("{} userEventTriggered: {} Event: {}", LocalTime.now(), ctx.name(), evt); if (evt instanceof ConnectEvent) { try { - tcp_channel.pipeline().remove("watchdog"); + tcpChannel.pipeline().remove("watchdog"); } catch (Exception ex) { logger.info(ex.toString()); } try { - tcp_channel.pipeline().addFirst("watchdog", new ReadTimeoutHandler(30)); - if (tcp_channel.isActive()) { - embeded_channel.attr(IS_CONNECTED).set(true); + tcpChannel.pipeline().addFirst("watchdog", new ReadTimeoutHandler(30)); + if (tcpChannel.isActive()) { + embededChannel.attr(IS_CONNECTED).set(true); } else { - embeded_channel.attr(IS_CONNECTED).set(false); + embededChannel.attr(IS_CONNECTED).set(false); } } catch (Exception ex) { logger.info(ex.toString()); @@ -209,39 +211,39 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { super.channelUnregistered(ctx); logger.debug("{} channelUnregistered: {}", LocalTime.now(), ctx.name()); - String strCanal = (tcp_channel == primary_channel) ? "PRIMARY" : "SECONDARY"; - logger.debug("Unregistered of channel: " + strCanal); + String strCanal = (tcpChannel == primaryChannel) ? "PRIMARY" : "SECONDARY"; + logger.debug("Unregistered of channel: {}", strCanal); //TODO: If embedded channel is closed, we need close all channels - if (ctx == embed_ctx) return; + if (ctx == embedCtx) return; - if (tcp_channel == ctx.channel()) - embeded_channel.attr(IS_CONNECTED).set(false); + if (tcpChannel == ctx.channel()) + embededChannel.attr(IS_CONNECTED).set(false); - logger.info(embed_ctx.executor().toString()); + logger.info(embedCtx.executor().toString()); - if ((tcp_channel == primary_channel) && - (primary_channel == ctx.channel())) - if ((!primary_channel.isActive()) && - (secondary_channel != null)) { - if (secondary_channel.isActive()) - synchronized (tcp_channel) { + if ((tcpChannel == primaryChannel) && + (primaryChannel == ctx.channel())) + if ((!primaryChannel.isActive()) && + (secondaryChannel != null)) { + if (secondaryChannel.isActive()) + synchronized (tcpChannel) { logger.info("Using secondary TCP channel."); - tcp_channel = secondary_channel; - embeded_channel.attr(IS_PRIMARY).set(false); - embeded_channel.pipeline().fireUserEventTriggered(new ConnectEvent()); + tcpChannel = secondaryChannel; + embededChannel.attr(IS_PRIMARY).set(false); + embededChannel.pipeline().fireUserEventTriggered(new ConnectEvent()); } } - if ((tcp_channel == secondary_channel) && - (secondary_channel == ctx.channel())) - if ((!secondary_channel.isActive() && - (primary_channel.isActive()))) { - synchronized (tcp_channel) { + if ((tcpChannel == secondaryChannel) && + (secondaryChannel == ctx.channel())) + if ((!secondaryChannel.isActive() && + (primaryChannel.isActive()))) { + synchronized (tcpChannel) { logger.info("Using primary TCP channel."); - tcp_channel = primary_channel; - embeded_channel.attr(IS_PRIMARY).set(true); - embeded_channel.pipeline().fireUserEventTriggered(new ConnectEvent()); + tcpChannel = primaryChannel; + embededChannel.attr(IS_PRIMARY).set(true); + embededChannel.pipeline().fireUserEventTriggered(new ConnectEvent()); } } } @@ -249,32 +251,32 @@ public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { @Override public void setEmbeddedChannel(Channel embeded_channel) { - this.embeded_channel = embeded_channel; - this.embeded_channel.attr(IS_CONNECTED).set(false); - this.embeded_channel.attr(IS_PRIMARY).set(true); - this.embeded_channel.attr(READ_TIME_OUT).set(8); - this.embeded_channel.attr(IS_PING_ACTIVE).set(false); - this.embeded_channel.attr(PING_TIME).set(-1); - this.embeded_channel.attr(RETRY_TIME).set(8); + this.embededChannel = embeded_channel; + this.embededChannel.attr(IS_CONNECTED).set(false); + this.embededChannel.attr(IS_PRIMARY).set(true); + this.embededChannel.attr(READ_TIME_OUT).set(8); + this.embededChannel.attr(IS_PING_ACTIVE).set(false); + this.embededChannel.attr(PING_TIME).set(-1); + this.embededChannel.attr(RETRY_TIME).set(8); } public void setPrimaryChannel(Channel primary_channel) { - if ((this.primary_channel == null) && (tcp_channel == null)) { + if ((this.primaryChannel == null) && (tcpChannel == null)) { if (primary_channel != null) { - this.primary_channel = primary_channel; - tcp_channel = primary_channel; - embeded_channel.attr(IS_PRIMARY).set(true); + this.primaryChannel = primary_channel; + tcpChannel = primary_channel; + embededChannel.attr(IS_PRIMARY).set(true); } - } else if ((!this.primary_channel.isActive()) && (tcp_channel == secondary_channel)) { - this.primary_channel = primary_channel; - } else if ((!this.primary_channel.isActive()) && (tcp_channel == this.primary_channel)) { - synchronized (tcp_channel) { - tcp_channel.close(); - this.primary_channel = primary_channel; - tcp_channel = primary_channel; - embeded_channel.attr(IS_PRIMARY).set(true); - if (tcp_channel.isActive()) { - embed_ctx.fireUserEventTriggered(new ConnectEvent()); + } else if ((!this.primaryChannel.isActive()) && (tcpChannel == secondaryChannel)) { + this.primaryChannel = primary_channel; + } else if ((!this.primaryChannel.isActive()) && (tcpChannel == this.primaryChannel)) { + synchronized (tcpChannel) { + tcpChannel.close(); + this.primaryChannel = primary_channel; + tcpChannel = primary_channel; + embededChannel.attr(IS_PRIMARY).set(true); + if (tcpChannel.isActive()) { + embedCtx.fireUserEventTriggered(new ConnectEvent()); } } } @@ -282,25 +284,25 @@ public void setPrimaryChannel(Channel primary_channel) { @Override public void setSecondaryChannel(Channel secondary_channel) { - if ((this.primary_channel == null) && (tcp_channel == null)) { + if ((this.primaryChannel == null) && (tcpChannel == null)) { if (secondary_channel != null) { - this.secondary_channel = secondary_channel; - tcp_channel = secondary_channel; - embeded_channel.attr(IS_PRIMARY).set(false); + this.secondaryChannel = secondary_channel; + tcpChannel = secondary_channel; + embededChannel.attr(IS_PRIMARY).set(false); } - } else if ((this.secondary_channel == null) || (tcp_channel == primary_channel)) { - this.secondary_channel = secondary_channel; - } else if ((!this.secondary_channel.isActive()) && (tcp_channel == primary_channel)) { - this.secondary_channel = secondary_channel; - } else if ((!this.secondary_channel.isActive()) && (tcp_channel == this.secondary_channel)) { - synchronized (tcp_channel) { - tcp_channel.close(); - this.secondary_channel = secondary_channel; - tcp_channel = secondary_channel; - embeded_channel.attr(IS_PRIMARY).set(false); + } else if ((this.secondaryChannel == null) || (tcpChannel == primaryChannel)) { + this.secondaryChannel = secondary_channel; + } else if ((!this.secondaryChannel.isActive()) && (tcpChannel == primaryChannel)) { + this.secondaryChannel = secondary_channel; + } else if ((!this.secondaryChannel.isActive()) && (tcpChannel == this.secondaryChannel)) { + synchronized (tcpChannel) { + tcpChannel.close(); + this.secondaryChannel = secondary_channel; + tcpChannel = secondary_channel; + embededChannel.attr(IS_PRIMARY).set(false); } - if (tcp_channel.isActive()) { - embed_ctx.fireUserEventTriggered(new ConnectEvent()); + if (tcpChannel.isActive()) { + embedCtx.fireUserEventTriggered(new ConnectEvent()); } } } diff --git a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/discovery/S7PlcDiscoverer.java b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/discovery/S7PlcDiscoverer.java index 50f623c7292..9a487666dc9 100644 --- a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/discovery/S7PlcDiscoverer.java +++ b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/discovery/S7PlcDiscoverer.java @@ -46,7 +46,7 @@ import org.apache.plc4x.java.spi.messages.DefaultPlcDiscoveryItem; import org.apache.plc4x.java.spi.messages.DefaultPlcDiscoveryResponse; import org.apache.plc4x.java.spi.messages.PlcDiscoverer; -import org.apache.plc4x.java.spi.values.PlcValues; +import org.apache.plc4x.java.spi.values.PlcSTRING; import org.pcap4j.core.NotOpenException; import org.pcap4j.core.PcapHandle; import org.pcap4j.core.PcapNativeException; @@ -286,16 +286,16 @@ public void handlePnDcpPacket(PnDcp_Pdu pdu, EthernetPacket ethernetPacket) { // Only add devices we know support the S7Comm protocol. if(supportsS7Comm) { Map attributes = new HashMap<>(); - attributes.put("ipAddress", PlcValues.of(remoteAddress)); - attributes.put("subnetMask", PlcValues.of(remoteSubnetMask)); - attributes.put("macAddress", PlcValues.of(srcAddr.toString())); - attributes.put("localMacAddress", PlcValues.of(dstAddr.toString())); - attributes.put("deviceTypeName", PlcValues.of(deviceTypeName)); - attributes.put("deviceName", PlcValues.of(deviceName)); - attributes.put("vendorId", PlcValues.of(vendorId)); - attributes.put("deviceId", PlcValues.of(deviceId)); - attributes.put("role", PlcValues.of(role)); - attributes.put("packetType", PlcValues.of("dcp")); + attributes.put("ipAddress", new PlcSTRING(remoteAddress)); + attributes.put("subnetMask", new PlcSTRING(remoteSubnetMask)); + attributes.put("macAddress", new PlcSTRING(srcAddr.toString())); + attributes.put("localMacAddress", new PlcSTRING(dstAddr.toString())); + attributes.put("deviceTypeName", new PlcSTRING(deviceTypeName)); + attributes.put("deviceName", new PlcSTRING(deviceName)); + attributes.put("vendorId", new PlcSTRING(vendorId)); + attributes.put("deviceId", new PlcSTRING(deviceId)); + attributes.put("role", new PlcSTRING(role)); + attributes.put("packetType", new PlcSTRING("dcp")); String name = deviceTypeName + " - " + deviceName; diff --git a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7HGeneratedDriverBase.java b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7HGeneratedDriverBase.java index 11be0f145c7..f721e664fda 100644 --- a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7HGeneratedDriverBase.java +++ b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7HGeneratedDriverBase.java @@ -29,7 +29,6 @@ import org.apache.plc4x.java.spi.configuration.ConfigurationFactory; import org.apache.plc4x.java.spi.connection.ChannelFactory; import org.apache.plc4x.java.spi.connection.GeneratedDriverBase; -import org.apache.plc4x.java.spi.connection.PlcTagHandler; import org.apache.plc4x.java.spi.connection.ProtocolStackConfigurer; import org.apache.plc4x.java.spi.transport.Transport; import org.slf4j.Logger; @@ -162,7 +161,6 @@ public PlcConnection getConnection(String connectionString) throws PlcConnection return new S7HPlcConnection( canPing(), canRead(), canWrite(), canSubscribe(), canBrowse(), - getTagHandler(), getValueHandler(), configuration, channelFactory, @@ -176,11 +174,6 @@ public PlcConnection getConnection(String connectionString) throws PlcConnection getAuthentication()); } - @Override - protected PlcTagHandler getTagHandler() { - throw new UnsupportedOperationException("getTagHandler, Not supported yet."); - } - @Override protected ProtocolStackConfigurer getStackConfigurer() { throw new UnsupportedOperationException("getStackConfigurer, Not supported yet."); diff --git a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7HMuxImpl.java b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7HMuxImpl.java index 26248b1c52a..1a987940d0e 100644 --- a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7HMuxImpl.java +++ b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7HMuxImpl.java @@ -114,24 +114,24 @@ public class S7HMuxImpl extends MessageToMessageCodec implemen */ final static AttributeKey RETRY_TIME = AttributeKey.valueOf("RETRY_TIME"); - ChannelHandlerContext embed_ctx = null; - protected Channel embeded_channel = null; - protected Channel tcp_channel = null; - protected Channel primary_channel = null; - protected Channel secondary_channel = null; + ChannelHandlerContext embedCtx = null; + protected Channel embededChannel = null; + protected Channel tcpChannel = null; + protected Channel primaryChannel = null; + protected Channel secondaryChannel = null; /* * From S7ProtocolLogic * TODO: Evaluate if the "embed_ctx" is really required since we set - * the Embeded channel when we created it. + * the Embeded channel when we created it. */ @Override - protected void encode(ChannelHandlerContext ctx, ByteBuf outbb, List list) { - if ((embed_ctx == null) && (ctx.channel() instanceof EmbeddedChannel)) embed_ctx = ctx; - if ((tcp_channel != null) && (embed_ctx == ctx)) { - tcp_channel.writeAndFlush(outbb.copy()); + protected void encode(ChannelHandlerContext ctx, ByteBuf outBB, List list) { + if ((embedCtx == null) && (ctx.channel() instanceof EmbeddedChannel)) embedCtx = ctx; + if ((tcpChannel != null) && (embedCtx == ctx)) { + tcpChannel.writeAndFlush(outBB.copy()); } else { - list.add(outbb.copy()); + list.add(outBB.copy()); } } @@ -141,8 +141,8 @@ protected void encode(ChannelHandlerContext ctx, ByteBuf outbb, List lis * the pipeline of the channel "embeded_channel" */ @Override - protected void decode(ChannelHandlerContext ctx, ByteBuf inbb, List list) throws Exception { - embed_ctx.fireChannelRead(inbb.copy()); + protected void decode(ChannelHandlerContext ctx, ByteBuf inBB, List list) throws Exception { + embedCtx.fireChannelRead(inBB.copy()); } @Override @@ -177,19 +177,19 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exc logger.info(LocalTime.now() + " userEventTriggered: " + ctx.name() + " Event: " + evt); if (evt instanceof ConnectedEvent) { try { - tcp_channel.pipeline().remove("watchdog"); + tcpChannel.pipeline().remove("watchdog"); } catch (Exception ex) { logger.info(ex.toString()); } try { - if ((embeded_channel.attr(READ_TIME_OUT).get() > 0) && - embeded_channel.attr(IS_PING_ACTIVE).get()) - tcp_channel.pipeline().addFirst("watchdog", - new ReadTimeoutHandler(embeded_channel.attr(READ_TIME_OUT).get())); - if (tcp_channel.isActive()) { - embeded_channel.attr(IS_CONNECTED).set(true); + if ((embededChannel.attr(READ_TIME_OUT).get() > 0) && + embededChannel.attr(IS_PING_ACTIVE).get()) + tcpChannel.pipeline().addFirst("watchdog", + new ReadTimeoutHandler(embededChannel.attr(READ_TIME_OUT).get())); + if (tcpChannel.isActive()) { + embededChannel.attr(IS_CONNECTED).set(true); } else { - embeded_channel.attr(IS_CONNECTED).set(false); + embededChannel.attr(IS_CONNECTED).set(false); } } catch (Exception ex) { logger.info(ex.toString()); @@ -223,45 +223,45 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { @Override public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { super.channelUnregistered(ctx); - logger.debug(LocalTime.now().toString() + " channelUnregistered: " + ctx.name()); - String strCanal = (tcp_channel == primary_channel) ? "PRIMARY" : "SECONDARY"; - logger.info("Unregistered of channel: " + strCanal); + logger.debug("{} channelUnregistered: {}", LocalTime.now(), ctx.name()); + String strCanal = (tcpChannel == primaryChannel) ? "PRIMARY" : "SECONDARY"; + logger.info("Unregistered of channel: {}", strCanal); //TODO: If embedded channel is closed, we need close all channels - if (ctx == embed_ctx) return; + if (ctx == embedCtx) return; - if (tcp_channel == ctx.channel()) { - embeded_channel.attr(IS_CONNECTED).set(false); - embeded_channel.attr(WAS_CONNECTED).set(true); - embeded_channel.pipeline().fireUserEventTriggered(new DisconnectedEvent()); + if (tcpChannel == ctx.channel()) { + embededChannel.attr(IS_CONNECTED).set(false); + embededChannel.attr(WAS_CONNECTED).set(true); + embededChannel.pipeline().fireUserEventTriggered(new DisconnectedEvent()); } - logger.info(embed_ctx.executor().toString()); + logger.info(embedCtx.executor().toString()); - if ((tcp_channel == primary_channel) && - (primary_channel == ctx.channel())) - if ((!primary_channel.isActive()) && - (secondary_channel != null)) - if (secondary_channel.isActive()) { - synchronized (tcp_channel) { + if ((tcpChannel == primaryChannel) && + (primaryChannel == ctx.channel())) + if ((!primaryChannel.isActive()) && + (secondaryChannel != null)) + if (secondaryChannel.isActive()) { + synchronized (tcpChannel) { logger.info("Using secondary TCP channel."); - tcp_channel = secondary_channel; - embeded_channel.attr(IS_PRIMARY).set(false); - embeded_channel.pipeline().fireUserEventTriggered(new ConnectEvent()); + tcpChannel = secondaryChannel; + embededChannel.attr(IS_PRIMARY).set(false); + embededChannel.pipeline().fireUserEventTriggered(new ConnectEvent()); } } ; - if ((tcp_channel == secondary_channel) && - (secondary_channel == ctx.channel())) - if ((!secondary_channel.isActive()) && - (primary_channel != null)) - if (primary_channel.isActive()) { - synchronized (tcp_channel) { + if ((tcpChannel == secondaryChannel) && + (secondaryChannel == ctx.channel())) + if ((!secondaryChannel.isActive()) && + (primaryChannel != null)) + if (primaryChannel.isActive()) { + synchronized (tcpChannel) { logger.info("Using primary TCP channel."); - tcp_channel = primary_channel; - embeded_channel.attr(IS_PRIMARY).set(true); - embeded_channel.pipeline().fireUserEventTriggered(new ConnectEvent()); + tcpChannel = primaryChannel; + embededChannel.attr(IS_PRIMARY).set(true); + embededChannel.pipeline().fireUserEventTriggered(new ConnectEvent()); } } @@ -271,43 +271,43 @@ public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { @Override public void setEmbededhannel(Channel embeded_channel, PlcConnectionConfiguration configuration) { final S7Configuration conf = (S7Configuration) configuration; - this.embeded_channel = embeded_channel; - this.embeded_channel.attr(IS_CONNECTED).set(false); - this.embeded_channel.attr(WAS_CONNECTED).set(false); - this.embeded_channel.attr(IS_PRIMARY).set(true); + this.embededChannel = embeded_channel; + this.embededChannel.attr(IS_CONNECTED).set(false); + this.embededChannel.attr(WAS_CONNECTED).set(false); + this.embededChannel.attr(IS_PRIMARY).set(true); //From the URL - this.embeded_channel.attr(READ_TIME_OUT).set(conf.getReadTimeout()); - this.embeded_channel.attr(IS_PING_ACTIVE).set(conf.getPing()); - this.embeded_channel.attr(PING_TIME).set(conf.getPingTime()); - this.embeded_channel.attr(RETRY_TIME).set(conf.getRetryTime()); + this.embededChannel.attr(READ_TIME_OUT).set(conf.getReadTimeout()); + this.embededChannel.attr(IS_PING_ACTIVE).set(conf.getPing()); + this.embededChannel.attr(PING_TIME).set(conf.getPingTime()); + this.embededChannel.attr(RETRY_TIME).set(conf.getRetryTime()); } @Override public void setPrimaryChannel(Channel primary_channel) { - if ((this.primary_channel == null) && (tcp_channel == null)) { + if ((this.primaryChannel == null) && (tcpChannel == null)) { if (primary_channel != null) { - this.primary_channel = primary_channel; - tcp_channel = primary_channel; - embeded_channel.attr(IS_PRIMARY).set(true); + this.primaryChannel = primary_channel; + tcpChannel = primary_channel; + embededChannel.attr(IS_PRIMARY).set(true); } - } else if ((this.primary_channel == null) || - ((tcp_channel == secondary_channel)) && (tcp_channel.isActive())) { - this.primary_channel = primary_channel; + } else if ((this.primaryChannel == null) || + ((tcpChannel == secondaryChannel)) && (tcpChannel.isActive())) { + this.primaryChannel = primary_channel; - } else if ((!this.primary_channel.isActive()) && (tcp_channel == secondary_channel)) { - this.primary_channel = primary_channel; + } else if ((!this.primaryChannel.isActive()) && (tcpChannel == secondaryChannel)) { + this.primaryChannel = primary_channel; - } else if (((!this.primary_channel.isActive()) && (tcp_channel == this.primary_channel)) || + } else if (((!this.primaryChannel.isActive()) && (tcpChannel == this.primaryChannel)) || (primary_channel.isActive())) { - synchronized (tcp_channel) { - tcp_channel.close(); - this.primary_channel = primary_channel; - tcp_channel = primary_channel; - embeded_channel.attr(IS_PRIMARY).set(true); - - if (tcp_channel.isActive()) { - embed_ctx.fireUserEventTriggered(new ConnectEvent()); + synchronized (tcpChannel) { + tcpChannel.close(); + this.primaryChannel = primary_channel; + tcpChannel = primary_channel; + embededChannel.attr(IS_PRIMARY).set(true); + + if (tcpChannel.isActive()) { + embedCtx.fireUserEventTriggered(new ConnectEvent()); } } } else if (primary_channel.isActive()) { @@ -317,37 +317,37 @@ public void setPrimaryChannel(Channel primary_channel) { @Override public void setSecondaryChannel(Channel secondary_channel) { - if ((this.primary_channel == null) && (tcp_channel == null)) { + if ((this.primaryChannel == null) && (tcpChannel == null)) { if (secondary_channel != null) { - this.secondary_channel = secondary_channel; - tcp_channel = secondary_channel; - embeded_channel.attr(IS_PRIMARY).set(false); + this.secondaryChannel = secondary_channel; + tcpChannel = secondary_channel; + embededChannel.attr(IS_PRIMARY).set(false); } - } else if ((this.secondary_channel == null) || - ((tcp_channel == primary_channel)) && (tcp_channel.isActive())) { - this.secondary_channel = secondary_channel; + } else if ((this.secondaryChannel == null) || + ((tcpChannel == primaryChannel)) && (tcpChannel.isActive())) { + this.secondaryChannel = secondary_channel; - } else if ((!this.secondary_channel.isActive()) && (tcp_channel == primary_channel)) { - this.secondary_channel = secondary_channel; + } else if ((!this.secondaryChannel.isActive()) && (tcpChannel == primaryChannel)) { + this.secondaryChannel = secondary_channel; - } else if (((!this.secondary_channel.isActive()) && (tcp_channel == this.secondary_channel)) || + } else if (((!this.secondaryChannel.isActive()) && (tcpChannel == this.secondaryChannel)) || (secondary_channel.isActive())) { - synchronized (tcp_channel) { - tcp_channel.close(); - this.secondary_channel = secondary_channel; - tcp_channel = secondary_channel; - embeded_channel.attr(IS_PRIMARY).set(false); + synchronized (tcpChannel) { + tcpChannel.close(); + this.secondaryChannel = secondary_channel; + tcpChannel = secondary_channel; + embededChannel.attr(IS_PRIMARY).set(false); } - if (tcp_channel.isActive()) { - embed_ctx.fireUserEventTriggered(new ConnectEvent()); + if (tcpChannel.isActive()) { + embedCtx.fireUserEventTriggered(new ConnectEvent()); } } } @Override public Channel getTCPChannel() { - return tcp_channel; + return tcpChannel; } diff --git a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7HPlcConnection.java b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7HPlcConnection.java index 00950d4871b..ea0a1e258b6 100644 --- a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7HPlcConnection.java +++ b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7HPlcConnection.java @@ -91,7 +91,6 @@ public S7HPlcConnection( boolean canWrite, boolean canSubscribe, boolean canBrowse, - PlcTagHandler tagHandler, PlcValueHandler valueHandler, PlcConnectionConfiguration configuration, ChannelFactory channelFactory, @@ -108,7 +107,6 @@ public S7HPlcConnection( canWrite, canSubscribe, canBrowse, - tagHandler, valueHandler, configuration, channelFactory, diff --git a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7ProtocolLogic.java b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7ProtocolLogic.java index 580e259f0e1..b2223174126 100644 --- a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7ProtocolLogic.java +++ b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7ProtocolLogic.java @@ -41,6 +41,7 @@ import org.apache.plc4x.java.s7.utils.S7ParamErrorCode; import org.apache.plc4x.java.spi.ConversationContext; import org.apache.plc4x.java.spi.Plc4xProtocolBase; +import org.apache.plc4x.java.spi.connection.PlcTagHandler; import org.apache.plc4x.java.spi.context.DriverContext; import org.apache.plc4x.java.spi.generation.*; import org.apache.plc4x.java.spi.messages.*; @@ -145,6 +146,11 @@ public void setDriverContext(DriverContext driverContext) { eventLogic.start(); } + @Override + public PlcTagHandler getTagHandler() { + return new S7PlcTagHandler(); + } + @Override public void close(ConversationContext context) { // TODO: Find out how to close this prior to Java 19 diff --git a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/tag/S7Tag.java b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/tag/S7Tag.java index 6f9265c21e8..c494dc881d5 100644 --- a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/tag/S7Tag.java +++ b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/tag/S7Tag.java @@ -148,7 +148,11 @@ public static boolean matches(String tagString) { public static S7Tag of(String tagString) { Matcher matcher; if ((matcher = DATA_BLOCK_ADDRESS_PATTERN.matcher(tagString)).matches()) { - TransportSize dataType = TransportSize.valueOf(matcher.group(DATA_TYPE)); + String dataTypeName = matcher.group(DATA_TYPE); + if("RAW_BYTE_ARRAY".equals(dataTypeName)) { + dataTypeName = "BYTE"; + } + TransportSize dataType = TransportSize.valueOf(dataTypeName); MemoryArea memoryArea = MemoryArea.DATA_BLOCKS; Short transferSizeCode = getSizeCode(matcher.group(TRANSFER_SIZE_CODE)); int blockNumber = checkDataBlockNumber(Integer.parseInt(matcher.group(BLOCK_NUMBER))); @@ -171,7 +175,11 @@ public static S7Tag of(String tagString) { return new S7Tag(dataType, memoryArea, blockNumber, byteOffset, bitOffset, numElements); } else if ((matcher = DATA_BLOCK_SHORT_PATTERN.matcher(tagString)).matches()) { - TransportSize dataType = TransportSize.valueOf(matcher.group(DATA_TYPE)); + String dataTypeName = matcher.group(DATA_TYPE); + if("RAW_BYTE_ARRAY".equals(dataTypeName)) { + dataTypeName = "BYTE"; + } + TransportSize dataType = TransportSize.valueOf(dataTypeName); MemoryArea memoryArea = MemoryArea.DATA_BLOCKS; int blockNumber = checkDataBlockNumber(Integer.parseInt(matcher.group(BLOCK_NUMBER))); int byteOffset = checkByteOffset(Integer.parseInt(matcher.group(BYTE_OFFSET))); diff --git a/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/readwrite/DatatypesTest.java b/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/readwrite/DatatypesTest.java index 3f05a90bdb2..45d58e33091 100644 --- a/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/readwrite/DatatypesTest.java +++ b/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/readwrite/DatatypesTest.java @@ -56,7 +56,7 @@ public static void main(String[] args) throws Exception { builder.addTagAddress("lreal-value", "%DB2:70:LREAL"); // 3.14159265358979 builder.addTagAddress("lreal-array", "%DB2:78:LREAL[2]"); // 1.2345, -1.2345 builder.addTagAddress("string-value", "%DB2:94:STRING(10)"); // "Hurz" - // When reading a sized STRING string array, this has to be translated into multiple items + // When reading a sized STRING array, this has to be translated into multiple items //builder.addField("string-array", "%DB2:350:STRING(10)[2]"); // "Wolf", "Lamm" builder.addTagAddress("time-value", "%DB2:862:TIME"); // 1234ms builder.addTagAddress("time-array", "%DB2:866:TIME[2]"); // 123ms, 234ms diff --git a/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/readwrite/ManualS71200DriverTest.java b/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/readwrite/ManualS71200DriverTest.java index b9ba2262807..c02494d9be1 100644 --- a/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/readwrite/ManualS71200DriverTest.java +++ b/plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/readwrite/ManualS71200DriverTest.java @@ -68,7 +68,7 @@ public class ManualS71200DriverTest extends ManualTest { */ public ManualS71200DriverTest(String connectionString) { - super(connectionString); + super(connectionString, true, true, true, true, 100); } public static void main(String[] args) throws Exception { @@ -95,7 +95,7 @@ public static void main(String[] args) throws Exception { test.addTestCase("%DB4:70:DATE", new PlcDATE(LocalDate.parse("1998-03-28"))); test.addTestCase("%DB4:72:TIME_OF_DAY", new PlcTIME_OF_DAY(LocalTime.parse("15:36:30.123"))); test.addTestCase("%DB4:908:CHAR[5]", new PlcList(Arrays.asList(new PlcCHAR("w"), new PlcCHAR("i"), new PlcCHAR("e"), new PlcCHAR("s"), new PlcCHAR("e")))); - test.addTestCase("%DB4:914:BYTE[11]", new PlcRawByteArray(new byte[] {(byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5, (byte) 6, (byte) 7, (byte) 8, (byte) 9, (byte) 10, (byte) 11})); + test.addTestCase("%DB4:914:RAW_BYTE_ARRAY[11]", new PlcRawByteArray(new byte[] {(byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5, (byte) 6, (byte) 7, (byte) 8, (byte) 9, (byte) 10, (byte) 11})); long start = System.currentTimeMillis(); test.run(); 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 5b284bd2e10..a14e41fd9a4 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 @@ -27,6 +27,7 @@ import org.apache.plc4x.java.simulated.tag.SimulatedTag; import org.apache.plc4x.java.simulated.tag.SimulatedTagHandler; import org.apache.plc4x.java.spi.connection.AbstractPlcConnection; +import org.apache.plc4x.java.spi.connection.PlcTagHandler; 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; @@ -65,10 +66,15 @@ public class SimulatedConnection extends AbstractPlcConnection implements PlcRea public SimulatedConnection(SimulatedDevice device) { super(true, true, true, true, false, - new SimulatedTagHandler(), new DefaultPlcValueHandler(), null, null); + new DefaultPlcValueHandler(), null, null); this.device = device; } + @Override + public PlcTagHandler getPlcTagHandler() { + return new SimulatedTagHandler(); + } + @Override public void connect() { connected = true; @@ -111,10 +117,14 @@ public CompletableFuture read(PlcReadRequest readRequest) { public CompletableFuture write(PlcWriteRequest writeRequest) { Map tags = new HashMap<>(); for (String tagName : writeRequest.getTagNames()) { - SimulatedTag tag = (SimulatedTag) writeRequest.getTag(tagName); - PlcValue value = writeRequest.getPlcValue(tagName); - device.set(tag, value); - tags.put(tagName, PlcResponseCode.OK); + if(writeRequest.getTagResponseCode(tagName) == PlcResponseCode.OK) { + SimulatedTag tag = (SimulatedTag) writeRequest.getTag(tagName); + PlcValue value = writeRequest.getPlcValue(tagName); + device.set(tag, value); + tags.put(tagName, PlcResponseCode.OK); + } else { + tags.put(tagName, writeRequest.getTagResponseCode(tagName)); + } } PlcWriteResponse response = new DefaultPlcWriteResponse(writeRequest, tags); return CompletableFuture.completedFuture(response); diff --git a/plc4j/drivers/simulated/src/main/java/org/apache/plc4x/java/simulated/tag/SimulatedTagHandler.java b/plc4j/drivers/simulated/src/main/java/org/apache/plc4x/java/simulated/tag/SimulatedTagHandler.java index 20981fa7e8d..0e7039f60e6 100644 --- a/plc4j/drivers/simulated/src/main/java/org/apache/plc4x/java/simulated/tag/SimulatedTagHandler.java +++ b/plc4j/drivers/simulated/src/main/java/org/apache/plc4x/java/simulated/tag/SimulatedTagHandler.java @@ -7,7 +7,7 @@ * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * - * https://www.apache.org/licenses/LICENSE-2.0 + * 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 @@ -16,11 +16,12 @@ * specific language governing permissions and limitations * under the License. */ + package org.apache.plc4x.java.simulated.tag; import org.apache.plc4x.java.api.exceptions.PlcInvalidTagException; -import org.apache.plc4x.java.api.model.PlcTag; import org.apache.plc4x.java.api.model.PlcQuery; +import org.apache.plc4x.java.api.model.PlcTag; import org.apache.plc4x.java.spi.connection.PlcTagHandler; public class SimulatedTagHandler implements PlcTagHandler { diff --git a/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/connection/SimulatedConnectionTest.java b/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/connection/SimulatedConnectionTest.java index ed4cb33891c..0b2bb2c8103 100644 --- a/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/connection/SimulatedConnectionTest.java +++ b/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/connection/SimulatedConnectionTest.java @@ -163,7 +163,7 @@ void subscription() throws Exception { .addTagAddress("state", "STATE/state:STRING", "initialstate") .addTagAddress("event", "STATE/event:STRING", "initialevent") .build(); - SUT.write(plcWriteRequest).get(1, TimeUnit.SECONDS); + PlcWriteResponse plcWriteResponse = SUT.write(plcWriteRequest).get(1, TimeUnit.SECONDS); // Note: as we don't have a subscription yet, no callback will be executed LOGGER.trace("subscribe"); diff --git a/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/tag/SimularedTagHandlerTest.java b/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/tag/SimularedTagHandlerTest.java deleted file mode 100644 index c570d570151..00000000000 --- a/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/tag/SimularedTagHandlerTest.java +++ /dev/null @@ -1,119 +0,0 @@ -/* - * 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 - * - * https://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.tag; - -import org.apache.plc4x.java.simulated.tag.SimulatedTag; -import org.apache.plc4x.java.simulated.tag.SimulatedTagHandler; -import org.assertj.core.api.WithAssertions; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; - -@ExtendWith(MockitoExtension.class) -class SimularedTagHandlerTest implements WithAssertions { - - final SimulatedTagHandler SUT = new SimulatedTagHandler(); - - @Mock - SimulatedTag plcTag; - - @Test - void createTag() { - assertThat(SUT.parseTag("STATE/bar:DINT")).isNotNull(); - } - - /*@Test - void encodeBoolean() { - assertThat(SUT.encodeBoolean(plcTag, new Boolean[0])).isNotNull(); - } - - @Test - void encodeByte() { - when(plcTag.getPlcDataType()).thenReturn("Byte"); - assertThat(SUT.encodeByte(plcTag, new Byte[0])).isNotNull(); - } - - @Test - void encodeShort() { - when(plcTag.getPlcDataType()).thenReturn("Short"); - assertThat(SUT.encodeShort(plcTag, new Short[0])).isNotNull(); - } - - @Test - void encodeInteger() { - when(plcTag.getPlcDataType()).thenReturn("Integer"); - assertThat(SUT.encodeInteger(plcTag, new Integer[0])).isNotNull(); - } - - @Test - void encodeBigInteger() { - when(plcTag.getPlcDataType()).thenReturn("BigInteger"); - assertThat(SUT.encodeBigInteger(plcTag, new BigInteger[0])).isNotNull(); - } - - @Test - void encodeLong() { - when(plcTag.getPlcDataType()).thenReturn("Long"); - assertThat(SUT.encodeLong(plcTag, new Long[0])).isNotNull(); - } - - @Test - void encodeFloat() { - when(plcTag.getPlcDataType()).thenReturn("Float"); - assertThat(SUT.encodeFloat(plcTag, new Float[0])).isNotNull(); - } - - @Test - void encodeBigDecimal() { - when(plcTag.getPlcDataType()).thenReturn("BigDecimal"); - assertThat(SUT.encodeBigDecimal(plcTag, new BigDecimal[0])).isNotNull(); - } - - @Test - void encodeDouble() { - when(plcTag.getPlcDataType()).thenReturn("Double"); - assertThat(SUT.encodeDouble(plcTag, new Double[0])).isNotNull(); - } - - @Test - void encodeString() { - when(plcTag.getPlcDataType()).thenReturn("String"); - assertThat(SUT.encodeString(plcTag, new String[0])).isNotNull(); - } - - @Test - void encodeTime() { - when(plcTag.getPlcDataType()).thenReturn("LocalTime"); - assertThat(SUT.encodeTime(plcTag, new LocalTime[0])).isNotNull(); - } - - @Test - void encodeDate() { - when(plcTag.getPlcDataType()).thenReturn("LocalDate"); - assertThat(SUT.encodeDate(plcTag, new LocalDate[0])).isNotNull(); - } - - @Test - void encodeDateTime() { - when(plcTag.getPlcDataType()).thenReturn("LocalDateTime"); - assertThat(SUT.encodeDateTime(plcTag, new LocalDateTime[0])).isNotNull(); - }*/ - -} diff --git a/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/tag/SimulatedTagTest.java b/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/tag/SimulatedTagTest.java index a8a9b658f24..6bedd43518a 100644 --- a/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/tag/SimulatedTagTest.java +++ b/plc4j/drivers/simulated/src/test/java/org/apache/plc4x/java/simulated/tag/SimulatedTagTest.java @@ -20,7 +20,6 @@ import nl.jqno.equalsverifier.EqualsVerifier; import org.apache.plc4x.java.api.exceptions.PlcInvalidTagException; -import org.apache.plc4x.java.simulated.tag.SimulatedTag; import org.apache.plc4x.java.simulated.types.SimulatedTagType; import org.junit.jupiter.api.Test; diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/Plc4xNettyWrapper.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/Plc4xNettyWrapper.java index 838c9f8e4c9..519799ab8bd 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/Plc4xNettyWrapper.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/Plc4xNettyWrapper.java @@ -188,7 +188,9 @@ protected void decode(ChannelHandlerContext channelHandlerContext, T payload, Li } catch (Exception e) { logger.trace("Failure while processing payload {} with handler {}", message, registration, e); BiConsumer biConsumer = registration.getErrorConsumer(); - biConsumer.accept(message, e); + if(biConsumer != null) { + biConsumer.accept(message, e); + } registration.confirmError(); } return; @@ -218,7 +220,6 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exc /** * Performs registration of packet handler and makes sure that its timeout will be handled properly. - * * Since timeouts are controlled by {@link TimeoutManager} there is a need to decorate handler * operations so both sides know what's going on. * diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/Plc4xProtocolBase.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/Plc4xProtocolBase.java index 95ace093fd4..2030ad6bae4 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/Plc4xProtocolBase.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/Plc4xProtocolBase.java @@ -20,6 +20,7 @@ import org.apache.commons.lang3.NotImplementedException; import org.apache.plc4x.java.api.messages.*; +import org.apache.plc4x.java.spi.connection.PlcTagHandler; import org.apache.plc4x.java.spi.context.DriverContext; import java.util.concurrent.CompletableFuture; @@ -54,6 +55,8 @@ public void onDiscover(ConversationContext context) { // Intentionally do nothing here } + public abstract PlcTagHandler getTagHandler(); + /** * TODO document me *

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 a6126fe6dda..6ac2ef2e8b1 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 @@ -57,29 +57,21 @@ public abstract class AbstractPlcConnection implements PlcConnection, PlcConnect private boolean canWrite = false; private boolean canSubscribe = false; private boolean canBrowse = false; - private PlcTagHandler tagHandler; - private PlcValueHandler valueHandler; + private final PlcValueHandler valueHandler; + private final BaseOptimizer optimizer; + private final PlcAuthentication authentication; private Plc4xProtocolBase protocol; - private BaseOptimizer optimizer; - private PlcAuthentication authentication; - - /** - * @deprecated only for compatibility reasons. - */ - @Deprecated - protected AbstractPlcConnection() { - } + private PlcTagHandler tagHandler; protected AbstractPlcConnection(boolean canPing, boolean canRead, boolean canWrite, boolean canSubscribe, boolean canBrowse, - PlcTagHandler tagHandler, PlcValueHandler valueHandler, + PlcValueHandler valueHandler, BaseOptimizer optimizer, PlcAuthentication authentication) { this.canPing = canPing; this.canRead = canRead; this.canWrite = canWrite; this.canSubscribe = canSubscribe; this.canBrowse = canBrowse; - this.tagHandler = tagHandler; this.valueHandler = valueHandler; this.optimizer = optimizer; this.authentication = authentication; @@ -87,6 +79,7 @@ protected AbstractPlcConnection(boolean canPing, boolean canRead, boolean canWri public void setProtocol(Plc4xProtocolBase protocol) { this.protocol = protocol; + this.tagHandler = protocol.getTagHandler(); } public Plc4xProtocolBase getProtocol() { @@ -105,6 +98,7 @@ public CompletableFuture ping() { return future; } + @Override public boolean isReadSupported() { return canRead; diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/DefaultNettyPlcConnection.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/DefaultNettyPlcConnection.java index b97d94d347e..686fb36a3a1 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/DefaultNettyPlcConnection.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/DefaultNettyPlcConnection.java @@ -66,7 +66,6 @@ public DefaultNettyPlcConnection(boolean canPing, boolean canWrite, boolean canSubscribe, boolean canBrowse, - PlcTagHandler tagHandler, PlcValueHandler valueHandler, PlcConnectionConfiguration configuration, ChannelFactory channelFactory, @@ -77,7 +76,7 @@ public DefaultNettyPlcConnection(boolean canPing, ProtocolStackConfigurer stackConfigurer, BaseOptimizer optimizer, PlcAuthentication authentication) { - super(canPing, canRead, canWrite, canSubscribe, canBrowse, tagHandler, valueHandler, optimizer, authentication); + super(canPing, canRead, canWrite, canSubscribe, canBrowse, valueHandler, optimizer, authentication); this.configuration = configuration; this.channelFactory = channelFactory; this.fireDiscoverEvent = fireDiscoverEvent; diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/GeneratedDriverBase.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/GeneratedDriverBase.java index 9e1dfe76fbb..c8a85f2c22b 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/GeneratedDriverBase.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/connection/GeneratedDriverBase.java @@ -285,8 +285,6 @@ protected BaseOptimizer getOptimizer() { return null; } - protected abstract PlcTagHandler getTagHandler(); - protected PlcValueHandler getValueHandler() { return new DefaultPlcValueHandler(); } @@ -400,7 +398,6 @@ public PlcConnection getConnection(String connectionString, PlcAuthentication au return new DefaultNettyPlcConnection( canPing(), canRead(), canWrite(), canSubscribe(), canBrowse(), - getTagHandler(), getValueHandler(), configuration, channelFactory, diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcBrowseRequest.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcBrowseRequest.java index 14e523c2a2e..efc6bb8c2ef 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcBrowseRequest.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcBrowseRequest.java @@ -28,6 +28,7 @@ import java.util.LinkedHashMap; import java.util.LinkedHashSet; +import java.util.Objects; import java.util.concurrent.CompletableFuture; import java.util.function.Supplier; @@ -80,8 +81,8 @@ public static class Builder implements PlcBrowseRequest.Builder { private final LinkedHashMap> queries; public Builder(PlcBrowser browser, PlcTagHandler tagHandler) { - this.browser = browser; - this.tagHandler = tagHandler; + this.browser = Objects.requireNonNull(browser); + this.tagHandler = Objects.requireNonNull(tagHandler); queries = new LinkedHashMap<>(); } diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcDiscoveryRequest.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcDiscoveryRequest.java index a782df78f53..9990f1ba5c8 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcDiscoveryRequest.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcDiscoveryRequest.java @@ -25,6 +25,7 @@ import java.util.LinkedHashMap; import java.util.Map; +import java.util.Objects; import java.util.concurrent.CompletableFuture; public class DefaultPlcDiscoveryRequest implements PlcDiscoveryRequest, Serializable { @@ -73,7 +74,7 @@ public static class Builder implements PlcDiscoveryRequest.Builder { private final LinkedHashMap queries; public Builder(PlcDiscoverer discoverer) { - this.discoverer = discoverer; + this.discoverer = Objects.requireNonNull(discoverer); queries = new LinkedHashMap<>(); } diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcReadRequest.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcReadRequest.java index dcb4c73bb5d..92c64c4ebbc 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcReadRequest.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcReadRequest.java @@ -19,6 +19,7 @@ package org.apache.plc4x.java.spi.messages; import org.apache.plc4x.java.api.exceptions.PlcRuntimeException; +import org.apache.plc4x.java.api.exceptions.PlcTagNotFoundException; import org.apache.plc4x.java.api.messages.PlcReadRequest; import org.apache.plc4x.java.api.messages.PlcReadResponse; import org.apache.plc4x.java.api.messages.PlcTagRequest; @@ -36,6 +37,7 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.concurrent.CompletableFuture; import java.util.function.Supplier; import java.util.stream.Collectors; @@ -86,7 +88,7 @@ public PlcResponseCode getTagResponseCode(String tagName) { @Override public List getTags() { - return tags.values().stream().map(PlcTagItem::getTag).collect(Collectors.toList()); + return tags.values().stream().filter(plcTagItem -> plcTagItem instanceof DefaultPlcTagItem).map(PlcTagItem::getTag).collect(Collectors.toList()); } public PlcReader getReader() { @@ -122,8 +124,8 @@ public static class Builder implements PlcReadRequest.Builder { private final Map> tagItems; public Builder(PlcReader reader, PlcTagHandler tagHandler) { - this.reader = reader; - this.tagHandler = tagHandler; + this.reader = Objects.requireNonNull(reader); + this.tagHandler = Objects.requireNonNull(tagHandler); tagItems = new LinkedHashMap<>(); } @@ -136,6 +138,8 @@ public PlcReadRequest.Builder addTagAddress(String name, String tagAddress) { try { PlcTag tag = tagHandler.parseTag(tagAddress); return new DefaultPlcTagItem(tag); + } catch (PlcTagNotFoundException e) { + return new DefaultPlcTagErrorItem(PlcResponseCode.NOT_FOUND); } catch (Exception e) { return new DefaultPlcTagErrorItem(PlcResponseCode.INVALID_ADDRESS); } diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcSubscriptionRequest.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcSubscriptionRequest.java index 7264b78209a..acd6bd23ddd 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcSubscriptionRequest.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcSubscriptionRequest.java @@ -111,8 +111,8 @@ public static class Builder implements PlcSubscriptionRequest.Builder { private final LinkedHashMap>> preRegisteredConsumers; public Builder(PlcSubscriber subscriber, PlcTagHandler tagHandler) { - this.subscriber = subscriber; - this.tagHandler = tagHandler; + this.subscriber = Objects.requireNonNull(subscriber); + this.tagHandler = Objects.requireNonNull(tagHandler); this.tags = new TreeMap<>(); this.preRegisteredConsumers = new LinkedHashMap<>(); } diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcUnsubscriptionRequest.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcUnsubscriptionRequest.java index a90aeea8729..64570741f83 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcUnsubscriptionRequest.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcUnsubscriptionRequest.java @@ -71,7 +71,7 @@ public static class Builder implements PlcUnsubscriptionRequest.Builder { private final List plcSubscriptionHandles; public Builder(PlcSubscriber subscriber) { - this.subscriber = subscriber; + this.subscriber = Objects.requireNonNull(subscriber); plcSubscriptionHandles = new ArrayList<>(); } diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcWriteRequest.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcWriteRequest.java index eb9296eb599..64f3c21ec7b 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcWriteRequest.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcWriteRequest.java @@ -150,9 +150,9 @@ public static class Builder implements PlcWriteRequest.Builder { private final Map> tagValues; public Builder(PlcWriter writer, PlcTagHandler tagHandler, PlcValueHandler valueHandler) { - this.writer = writer; - this.tagHandler = tagHandler; - this.valueHandler = valueHandler; + this.writer = Objects.requireNonNull(writer); + this.tagHandler = Objects.requireNonNull(tagHandler); + this.valueHandler = Objects.requireNonNull(valueHandler); tagValues = new TreeMap<>(); } diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/DefaultPlcValueHandler.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/DefaultPlcValueHandler.java index 33863ef3996..1cd73fdbc63 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/DefaultPlcValueHandler.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/DefaultPlcValueHandler.java @@ -26,6 +26,11 @@ import org.apache.plc4x.java.api.types.PlcValueType; import org.apache.plc4x.java.api.value.PlcValue; +import java.math.BigInteger; +import java.time.Duration; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -76,13 +81,30 @@ public static PlcValue of(PlcTag tag, Object[] values) { } } - private static PlcList ofElements(PlcValueType type, List arrayInfos, Object[] values) { + private static PlcValue ofElements(PlcValueType type, List arrayInfos, Object[] values) { ArrayInfo arrayInfo = arrayInfos.get(0); + + if(values.length == 1) { + if(values[0] instanceof Object[]) { + values = (Object[]) values[0]; + } else if (values[0] instanceof Collection) { + values = ((Collection) values[0]).toArray(); + } else if(values[0] instanceof PlcList) { + values = ((PlcList) values[0]).getList().toArray(); + } else if(values[0] instanceof PlcRawByteArray) { + PlcRawByteArray plcRawByteArray = (PlcRawByteArray) values[0]; + if(plcRawByteArray.getRaw().length != arrayInfo.getSize()) { + throw new PlcRuntimeException(String.format("Expecting %d items, but got %d", arrayInfo.getSize(), plcRawByteArray.getRaw().length)); + } + return plcRawByteArray; + } + } + List plcValues = new ArrayList<>(arrayInfo.getSize()); // In the last layer we'll create a list of PlcValues if(arrayInfos.size() == 1) { - if((values.length != 1) || ((values[0] instanceof List) && (((List) values[0]).size() != 1))) { - throw new PlcRuntimeException("Expecting only one item"); + if(values.length != arrayInfo.getSize()) { + throw new PlcRuntimeException(String.format("Expecting %d items, but got %d", arrayInfo.getSize(), values.length)); } // TODO: Add some size-checks here ... for (Object value : values) { @@ -93,13 +115,17 @@ private static PlcList ofElements(PlcValueType type, List arrayInfos, else { // TODO: Add some size-checks here ... for (Object value : values) { - plcValues.add(ofElements(type, arrayInfos.subList(1, arrayInfos.size()), values)); + plcValues.add(ofElements(type, arrayInfos.subList(1, arrayInfos.size()), (Object[]) value)); } } return new PlcList(plcValues); } private static PlcValue ofElement(PlcValueType type, Object value) { + // This is a temporary hack for drivers that don't have type information in their tags (ADS) + if(type == null) { + return of(value); + } switch (type) { case BOOL: return PlcBOOL.of(value); @@ -178,6 +204,71 @@ private static PlcValue ofElement(PlcValueType type, Object value) { } } + /** + * This is a legacy helper that should help with protocols, that currently don't have PlcValueType information + * available at the request-building-time. Such as the ADS driver. We should remove this option as soon as all + * drivers are fully PlcValueType-aware. + * @param value value + * @return PlcValue for the given type + */ + private static PlcValue of(Object value) { + /*if (values.length != 1) { + PlcList list = new PlcList(); + for (Object value : values) { + list.add(of(new Object[]{value})); + } + return list; + } + Object value = values[0];*/ + if (value instanceof Boolean) { + return PlcBOOL.of(value); + } + if (value instanceof Byte) { + return PlcSINT.of(value); + } + if (value instanceof byte[]) { + return PlcRawByteArray.of(value); + } + if (value instanceof Short) { + return PlcINT.of(value); + } + if (value instanceof Integer) { + return PlcDINT.of(value); + } + if (value instanceof Long) { + return PlcLINT.of(value); + } + if (value instanceof BigInteger) { + return PlcLINT.of(value); + } + if (value instanceof Float) { + return PlcREAL.of(value); + } + if (value instanceof Double) { + return PlcLREAL.of(value); + } + if (value instanceof Duration) { + return new PlcTIME((Duration) value); + } + if (value instanceof LocalTime) { + return new PlcTIME_OF_DAY((LocalTime) value); + } + if (value instanceof LocalDate) { + return new PlcDATE((LocalDate) value); + } + if (value instanceof LocalDateTime) { + return new PlcDATE_AND_TIME((LocalDateTime) value); + } + if (value instanceof String) { + return new PlcSTRING((String) value); + } + if (value instanceof PlcValue) { + return (PlcValue) value; + } + throw new PlcUnsupportedDataTypeException("Data Type " + value.getClass() + + " Is not supported"); + } + /* public static PlcValue customDataType(Object[] values) { return of(values); }*/ diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcBINT.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcBINT.java deleted file mode 100644 index 1432e405ab4..00000000000 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcBINT.java +++ /dev/null @@ -1,237 +0,0 @@ -/* - * 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 - * - * https://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.spi.values; - -import org.apache.plc4x.java.api.types.PlcValueType; -import org.apache.plc4x.java.spi.generation.SerializationException; -import org.apache.plc4x.java.spi.generation.WriteBuffer; - -import java.math.BigDecimal; -import java.math.BigInteger; - -public class PlcBINT extends PlcIECValue { - - public static PlcBINT of(Object value) { - if (value instanceof Boolean) { - return new PlcBINT((Boolean) value); - } else if (value instanceof Byte) { - return new PlcBINT((Byte) value); - } else if (value instanceof Short) { - return new PlcBINT((Short) value); - } else if (value instanceof Integer) { - return new PlcBINT((Integer) value); - } else if (value instanceof Long) { - return new PlcBINT((Long) value); - } else if (value instanceof Float) { - return new PlcBINT((Float) value); - } else if (value instanceof Double) { - return new PlcBINT((Double) value); - } else if (value instanceof BigInteger) { - return new PlcBINT((BigInteger) value); - } else if (value instanceof BigDecimal) { - return new PlcBINT((BigDecimal) value); - } else { - return new PlcBINT(value.toString()); - } - } - - public PlcBINT(Boolean value) { - this.value = value ? BigInteger.valueOf(1) : BigInteger.valueOf(0); - this.isNullable = false; - } - - public PlcBINT(Byte value) { - this.value = BigInteger.valueOf(value); - this.isNullable = false; - } - - public PlcBINT(Short value) { - this.value = BigInteger.valueOf(value); - this.isNullable = false; - } - - public PlcBINT(Integer value) { - this.value = BigInteger.valueOf(value); - this.isNullable = false; - } - - public PlcBINT(Long value) { - this.value = BigInteger.valueOf(value); - this.isNullable = false; - } - - public PlcBINT(Float value) { - this.value = BigDecimal.valueOf(value).toBigInteger(); - this.isNullable = false; - } - - public PlcBINT(Double value) { - this.value = BigDecimal.valueOf(value).toBigInteger(); - this.isNullable = false; - } - - public PlcBINT(BigInteger value) { - this.value = value; - this.isNullable = false; - } - - public PlcBINT(BigDecimal value) { - this.value = value.toBigInteger(); - this.isNullable = false; - } - - public PlcBINT(String value) { - this.value = new BigInteger(value.trim()); - this.isNullable = false; - } - - @Override - public PlcValueType getPlcValueType() { - return PlcValueType.ULINT; - } - - @Override - public boolean isBoolean() { - return true; - } - - @Override - public boolean getBoolean() { - return (value != null) && !value.equals(BigInteger.ZERO); - } - - @Override - public boolean isByte() { - return (value != null) && (value.compareTo(BigInteger.valueOf(Byte.MAX_VALUE)) <= 0) && (value.compareTo(BigInteger.valueOf(Byte.MIN_VALUE)) >= 0); - } - - @Override - public byte getByte() { - return value.byteValue(); - } - - @Override - public boolean isShort() { - return (value != null) && (value.compareTo(BigInteger.valueOf(Short.MAX_VALUE)) <= 0) && (value.compareTo(BigInteger.valueOf(Short.MIN_VALUE)) >= 0); - } - - @Override - public short getShort() { - return value.shortValue(); - } - - @Override - public boolean isInteger() { - return (value != null) && (value.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) <= 0) && (value.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) >= 0); - } - - @Override - public int getInteger() { - return value.intValue(); - } - - @Override - public boolean isLong() { - return (value != null) && (value.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) <= 0) && (value.compareTo(BigInteger.valueOf(Long.MIN_VALUE)) >= 0); - } - - @Override - public long getLong() { - return value.longValue(); - } - - @Override - public boolean isBigInteger() { - return true; - } - - @Override - public BigInteger getBigInteger() { - return value; - } - - @Override - public boolean isFloat() { - return true; - } - - @Override - public float getFloat() { - return value.floatValue(); - } - - @Override - public boolean isDouble() { - return true; - } - - @Override - public double getDouble() { - return value.doubleValue(); - } - - @Override - public boolean isBigDecimal() { - return true; - } - - @Override - public BigDecimal getBigDecimal() { - return new BigDecimal(value); - } - - @Override - public boolean isString() { - return true; - } - - @Override - public String getString() { - return toString(); - } - - @Override - public String toString() { - return value.toString(); - } - - @Override - public byte[] getRaw() { - return getBytes(); - } - - public byte[] getBytes() { - byte[] tmp = value.toByteArray(); - byte[] bytes = new byte[8]; - for (int i = 0; i < bytes.length; i++) { - if (i >= (bytes.length - tmp.length)) { - bytes[i] = tmp[i - (bytes.length - tmp.length)]; - } else { - bytes[i] = 0x00; - } - } - return bytes; - } - - @Override - public void serialize(WriteBuffer writeBuffer) throws SerializationException { - writeBuffer.writeBigInteger(getClass().getSimpleName(), 64, value); - } - -} diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcBOOL.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcBOOL.java index 3749e1a3868..a6dbada59a1 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcBOOL.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcBOOL.java @@ -28,12 +28,12 @@ public class PlcBOOL extends PlcIECValue { - private static final String VALUE_OUT_OF_RANGE = "Value of type %s is out of range %d - %d for a %s Value"; - static final int minValue = 0; - static final int maxValue = 1; + private static final String VALUE_OUT_OF_RANGE = "Value of is required for a %s Value"; public static PlcBOOL of(Object value) { - if (value instanceof Boolean) { + if (value instanceof PlcBOOL) { + return (PlcBOOL) value; + } else if (value instanceof Boolean) { return new PlcBOOL((Boolean) value); } else if (value instanceof Byte) { return new PlcBOOL((Byte) value); @@ -62,66 +62,66 @@ public PlcBOOL(Boolean value) { } public PlcBOOL(Byte value) { - if ((value == null) || (value < minValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if (value == null) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, this.getClass().getSimpleName())); } - this.value = value >= 1; + this.value = value != 0; this.isNullable = true; } public PlcBOOL(Short value) { - if ((value == null) || (value < minValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if (value == null) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, this.getClass().getSimpleName())); } - this.value = value >= 1; + this.value = value != 0; this.isNullable = true; } public PlcBOOL(Integer value) { - if ((value == null) || (value < minValue)) { - throw new PlcInvalidTagException(String.format("Value of type %d is out of range %d - %d for a %s Value", value, minValue, maxValue, this.getClass().getSimpleName())); + if (value == null) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, this.getClass().getSimpleName())); } - this.value = value >= 1; + this.value = value != 0; this.isNullable = true; } public PlcBOOL(Long value) { - if ((value == null) || (value < minValue)) { - throw new PlcInvalidTagException(String.format("Value of type %d is out of range %d - %d for a %s Value", value, minValue, maxValue, this.getClass().getSimpleName())); + if (value == null) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, this.getClass().getSimpleName())); } - this.value = value >= 1; + this.value = value != 0; this.isNullable = true; } public PlcBOOL(Float value) { - if ((value == null) || (value < minValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if (value == null) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, this.getClass().getSimpleName())); } - this.value = value >= 1; + this.value = value != 0.0; this.isNullable = true; } public PlcBOOL(Double value) { - if ((value == null) || (value < minValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if (value == null) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, this.getClass().getSimpleName())); } - this.value = value >= 1; + this.value = value != 0.0d; this.isNullable = true; } public PlcBOOL(BigInteger value) { - if ((value == null) || (value.compareTo(BigInteger.valueOf(minValue)) < 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if (value == null) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, this.getClass().getSimpleName())); } - this.value = value.compareTo(BigInteger.valueOf(maxValue)) >= 0; + this.value = value.compareTo(BigInteger.ZERO) != 0; this.isNullable = true; } public PlcBOOL(BigDecimal value) { - if ((value == null) || (value.compareTo(BigDecimal.valueOf(minValue)) < 0) || (value.scale() > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if (value == null) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, this.getClass().getSimpleName())); } - this.value = value.compareTo(BigDecimal.valueOf(maxValue)) >= 0; + this.value = value.compareTo(BigDecimal.ZERO) != 0; this.isNullable = true; } @@ -130,7 +130,7 @@ public PlcBOOL(String value) { this.value = parseValue(value); this.isNullable = false; } catch (RuntimeException e) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName()), e); + throw new PlcInvalidTagException(String.format("Value %s could not be parsed to %s Value", value, this.getClass().getSimpleName()), e); } } diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcBREAL.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcBREAL.java deleted file mode 100644 index 8bf785be526..00000000000 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcBREAL.java +++ /dev/null @@ -1,238 +0,0 @@ -/* - * 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 - * - * https://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.spi.values; - -import org.apache.plc4x.java.api.types.PlcValueType; -import org.apache.plc4x.java.spi.generation.SerializationException; -import org.apache.plc4x.java.spi.generation.WriteBuffer; - -import java.math.BigDecimal; -import java.math.BigInteger; - -public class PlcBREAL extends PlcIECValue { - - public static PlcBREAL of(Object value) { - if (value instanceof Boolean) { - return new PlcBREAL((Boolean) value); - } else if (value instanceof Byte) { - return new PlcBREAL((Byte) value); - } else if (value instanceof Short) { - return new PlcBREAL((Short) value); - } else if (value instanceof Integer) { - return new PlcBREAL((Integer) value); - } else if (value instanceof Long) { - return new PlcBREAL((Long) value); - } else if (value instanceof Float) { - return new PlcBREAL((Float) value); - } else if (value instanceof Double) { - return new PlcBREAL((Double) value); - } else if (value instanceof BigInteger) { - return new PlcBREAL((BigInteger) value); - } else if (value instanceof BigDecimal) { - return new PlcBREAL((BigDecimal) value); - } else { - return new PlcBREAL(value.toString()); - } - } - - public PlcBREAL(Boolean value) { - this.value = value ? BigDecimal.valueOf(1) : BigDecimal.valueOf(0); - this.isNullable = false; - } - - public PlcBREAL(Byte value) { - this.value = BigDecimal.valueOf(value); - this.isNullable = false; - } - - public PlcBREAL(Short value) { - this.value = BigDecimal.valueOf(value); - this.isNullable = false; - } - - public PlcBREAL(Integer value) { - this.value = BigDecimal.valueOf(value); - this.isNullable = false; - } - - public PlcBREAL(Long value) { - this.value = BigDecimal.valueOf(value); - this.isNullable = false; - } - - public PlcBREAL(Float value) { - this.value = BigDecimal.valueOf(value); - this.isNullable = false; - } - - public PlcBREAL(Double value) { - this.value = BigDecimal.valueOf(value); - this.isNullable = false; - } - - public PlcBREAL(BigInteger value) { - this.value = new BigDecimal(value); - this.isNullable = false; - } - - public PlcBREAL(BigDecimal value) { - this.value = value; - this.isNullable = false; - } - - public PlcBREAL(String value) { - this.value = new BigDecimal(value.trim()); - this.isNullable = false; - } - - @Override - public PlcValueType getPlcValueType() { - return PlcValueType.ULINT; - } - - @Override - public boolean isBoolean() { - return true; - } - - @Override - public boolean getBoolean() { - return (value != null) && !value.equals(BigDecimal.ZERO); - } - - @Override - public boolean isByte() { - return (value != null) && (value.compareTo(BigDecimal.valueOf(Byte.MAX_VALUE)) <= 0) && (value.compareTo(BigDecimal.valueOf(Byte.MIN_VALUE)) >= 0); - } - - @Override - public byte getByte() { - return value.byteValue(); - } - - @Override - public boolean isShort() { - return (value != null) && (value.compareTo(BigDecimal.valueOf(Short.MAX_VALUE)) <= 0) && (value.compareTo(BigDecimal.valueOf(Short.MIN_VALUE)) >= 0); - } - - @Override - public short getShort() { - return value.shortValue(); - } - - @Override - public boolean isInteger() { - return (value != null) && (value.compareTo(BigDecimal.valueOf(Integer.MAX_VALUE)) <= 0) && (value.compareTo(BigDecimal.valueOf(Integer.MIN_VALUE)) >= 0); - } - - @Override - public int getInteger() { - return value.intValue(); - } - - @Override - public boolean isLong() { - return (value != null) && (value.compareTo(BigDecimal.valueOf(Long.MAX_VALUE)) <= 0) && (value.compareTo(BigDecimal.valueOf(Long.MIN_VALUE)) >= 0); - } - - @Override - public long getLong() { - return value.longValue(); - } - - @Override - public boolean isBigInteger() { - return true; - } - - @Override - public BigInteger getBigInteger() { - return value.toBigInteger(); - } - - @Override - public boolean isFloat() { - return true; - } - - @Override - public float getFloat() { - return value.floatValue(); - } - - @Override - public boolean isDouble() { - return true; - } - - @Override - public double getDouble() { - return value.doubleValue(); - } - - @Override - public boolean isBigDecimal() { - return true; - } - - @Override - public BigDecimal getBigDecimal() { - return value; - } - - @Override - public boolean isString() { - return true; - } - - @Override - public String getString() { - return toString(); - } - - @Override - public String toString() { - return value.toString(); - } - - @Override - public byte[] getRaw() { - return getBytes(); - } - - public byte[] getBytes() { - // TODO: Not sure if this is correct ... - byte[] tmp = value.unscaledValue().toByteArray(); - byte[] bytes = new byte[8]; - for (int i = 0; i < bytes.length; i++) { - if (i >= (bytes.length - tmp.length)) { - bytes[i] = tmp[i - (bytes.length - tmp.length)]; - } else { - bytes[i] = 0x00; - } - } - return bytes; - } - - @Override - public void serialize(WriteBuffer writeBuffer) throws SerializationException { - writeBuffer.writeBigDecimal(getClass().getSimpleName(), 64, value); - } - -} diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcBYTE.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcBYTE.java index e3bd6aa69a5..e481c560eea 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcBYTE.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcBYTE.java @@ -30,11 +30,13 @@ public class PlcBYTE extends PlcIECValue { private static final String VALUE_OUT_OF_RANGE = "Value of type %s is out of range %d - %d for a %s Value"; - static final Short minValue = 0; - static final Short maxValue = (short) Byte.MAX_VALUE * 2 + 1; + public static final Short MIN_VALUE = 0; + public static final Short MAX_VALUE = (short) Byte.MAX_VALUE * 2 + 1; public static PlcBYTE of(Object value) { - if (value instanceof Boolean) { + if (value instanceof PlcBYTE) { + return (PlcBYTE) value; + } else if (value instanceof Boolean) { return new PlcBYTE((Boolean) value); } else if (value instanceof Byte) { return new PlcBYTE((Byte) value); @@ -64,64 +66,64 @@ public PlcBYTE(Boolean value) { } public PlcBYTE(Byte value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.shortValue(); this.isNullable = false; } public PlcBYTE(Short value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value; this.isNullable = false; } public PlcBYTE(Integer value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.shortValue(); this.isNullable = false; } public PlcBYTE(Long value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.shortValue(); this.isNullable = false; } public PlcBYTE(Float value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.shortValue(); this.isNullable = false; } public PlcBYTE(Double value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.shortValue(); this.isNullable = false; } public PlcBYTE(BigInteger value) { - if ((value.compareTo(BigInteger.valueOf(minValue)) < 0) || (value.compareTo(BigInteger.valueOf(maxValue)) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value.compareTo(BigInteger.valueOf(MIN_VALUE)) < 0) || (value.compareTo(BigInteger.valueOf(MAX_VALUE)) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.shortValue(); this.isNullable = true; } public PlcBYTE(BigDecimal value) { - if ((value.compareTo(BigDecimal.valueOf(minValue)) < 0) || (value.compareTo(BigDecimal.valueOf(maxValue)) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value.compareTo(BigDecimal.valueOf(MIN_VALUE)) < 0) || (value.compareTo(BigDecimal.valueOf(MAX_VALUE)) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.shortValue(); this.isNullable = true; @@ -130,19 +132,19 @@ public PlcBYTE(BigDecimal value) { public PlcBYTE(String value) { try { short val = Short.parseShort(value.trim()); - if ((val < minValue) || (val > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((val < MIN_VALUE) || (val > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = val; this.isNullable = false; } catch (Exception e) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } } public PlcBYTE(short value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value; this.isNullable = false; diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcCHAR.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcCHAR.java index 84ab9dd94d7..d32e8f8f84e 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcCHAR.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcCHAR.java @@ -31,11 +31,13 @@ public class PlcCHAR extends PlcIECValue { private static final String VALUE_OUT_OF_RANGE = "Value of type %s is out of range %d - %d for a %s Value"; - static final Short minValue = 0; - static final Short maxValue = (short) Byte.MAX_VALUE * 2 + 1; + public static final Short MIN_VALUE = 0; + public static final Short MAX_VALUE = (short) Byte.MAX_VALUE * 2 + 1; public static PlcCHAR of(Object value) { - if (value instanceof Boolean) { + if (value instanceof PlcCHAR) { + return (PlcCHAR) value; + } else if (value instanceof Boolean) { return new PlcCHAR((Boolean) value); } else if (value instanceof Byte) { return new PlcCHAR((Byte) value); @@ -69,91 +71,91 @@ public PlcCHAR(Boolean value) { public PlcCHAR(Character value) { super(); Integer val = (int) value; - if ((val >= minValue) && (val <= maxValue)) { + if ((val >= MIN_VALUE) && (val <= MAX_VALUE)) { this.value = val.shortValue(); this.isNullable = false; } else { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } } public PlcCHAR(Byte value) { super(); - if ((value >= minValue) && (value <= maxValue)) { + if ((value >= MIN_VALUE) && (value <= MAX_VALUE)) { this.value = value.shortValue(); this.isNullable = false; } else { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } } public PlcCHAR(Short value) { super(); - if ((value >= minValue) && (value <= maxValue)) { + if ((value >= MIN_VALUE) && (value <= MAX_VALUE)) { this.value = value; this.isNullable = false; } else { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } } public PlcCHAR(Integer value) { super(); - if ((value >= minValue) && (value <= maxValue)) { + if ((value >= MIN_VALUE) && (value <= MAX_VALUE)) { this.value = value.shortValue(); this.isNullable = false; } else { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } } public PlcCHAR(Long value) { super(); - if ((value >= minValue) && (value <= maxValue)) { + if ((value >= MIN_VALUE) && (value <= MAX_VALUE)) { this.value = value.shortValue(); this.isNullable = false; } else { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } } public PlcCHAR(Float value) { super(); - if ((value >= minValue) && (value <= maxValue)) { + if ((value >= MIN_VALUE) && (value <= MAX_VALUE)) { this.value = value.shortValue(); this.isNullable = false; } else { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } } public PlcCHAR(Double value) { super(); - if ((value >= minValue) && (value <= maxValue)) { + if ((value >= MIN_VALUE) && (value <= MAX_VALUE)) { this.value = value.shortValue(); this.isNullable = false; } else { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } } public PlcCHAR(BigInteger value) { super(); - if ((value.compareTo(BigInteger.valueOf(minValue)) >= 0) && (value.compareTo(BigInteger.valueOf(maxValue)) <= 0)) { + if ((value.compareTo(BigInteger.valueOf(MIN_VALUE)) >= 0) && (value.compareTo(BigInteger.valueOf(MAX_VALUE)) <= 0)) { this.value = value.shortValue(); this.isNullable = true; } else { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } } public PlcCHAR(BigDecimal value) { super(); - if ((value.compareTo(BigDecimal.valueOf(minValue)) >= 0) && (value.compareTo(BigDecimal.valueOf(maxValue)) <= 0) && (value.scale() <= 0)) { + if ((value.compareTo(BigDecimal.valueOf(MIN_VALUE)) >= 0) && (value.compareTo(BigDecimal.valueOf(MAX_VALUE)) <= 0) && (value.scale() <= 0)) { this.value = value.shortValue(); this.isNullable = true; } else { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } } @@ -166,24 +168,24 @@ public PlcCHAR(String value) { s = " "; } short val = (short) s.charAt(0); - if ((val >= minValue) && (val <= maxValue)) { + if ((val >= MIN_VALUE) && (val <= MAX_VALUE)) { this.value = val; this.isNullable = false; } else { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } } catch (Exception e) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } } public PlcCHAR(short value) { super(); - if ((value >= minValue) && (value <= maxValue)) { + if ((value >= MIN_VALUE) && (value <= MAX_VALUE)) { this.value = value; this.isNullable = false; } else { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } } diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcDATE.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcDATE.java index 6f68370e138..50904a35553 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcDATE.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcDATE.java @@ -35,7 +35,9 @@ public class PlcDATE extends PlcIECValue { public static PlcDATE of(Object value) { - if (value instanceof LocalDate) { + if (value instanceof PlcDATE) { + return (PlcDATE) value; + } else if (value instanceof LocalDate) { return new PlcDATE((LocalDate) value); } else if (value instanceof Byte) { return new PlcDATE((Byte) value); @@ -60,13 +62,13 @@ public static PlcDATE of(Object value) { public static PlcDATE ofSecondsSinceEpoch(long secondsSinceEpoch) { return new PlcDATE(LocalDateTime.ofInstant( - Instant.ofEpochSecond(secondsSinceEpoch), OffsetDateTime.now().getOffset()).toLocalDate()); + Instant.ofEpochSecond(secondsSinceEpoch), ZoneOffset.UTC).toLocalDate()); } public static PlcDATE ofDaysSinceEpoch(int daysSinceEpoch) { // 86400 = 24 hours x 60 Minutes x 60 Seconds return new PlcDATE(LocalDateTime.ofInstant( - Instant.ofEpochSecond(((long) daysSinceEpoch) * 86400), OffsetDateTime.now().getOffset()).toLocalDate()); + Instant.ofEpochSecond(((long) daysSinceEpoch) * 86400), ZoneOffset.UTC).toLocalDate()); } public PlcDATE(LocalDate value) { @@ -77,54 +79,54 @@ public PlcDATE(LocalDate value) { public PlcDATE(Byte daysSinceEpoch) { // REMARK: Yes, I'm using LocalDataTime.ofInstant as LocalDate.ofInstant is marked "JDK 1.9" this.value = LocalDateTime.ofInstant( - Instant.ofEpochSecond(((long) daysSinceEpoch) * 86400), OffsetDateTime.now().getOffset()).toLocalDate(); + Instant.ofEpochSecond(((long) daysSinceEpoch) * 86400), ZoneOffset.UTC).toLocalDate(); this.isNullable = false; } public PlcDATE(Short daysSinceEpoch) { // REMARK: Yes, I'm using LocalDataTime.ofInstant as LocalDate.ofInstant is marked "JDK 1.9" this.value = LocalDateTime.ofInstant( - Instant.ofEpochSecond(((long) daysSinceEpoch) * 86400), OffsetDateTime.now().getOffset()).toLocalDate(); + Instant.ofEpochSecond(((long) daysSinceEpoch) * 86400), ZoneOffset.UTC).toLocalDate(); this.isNullable = false; } public PlcDATE(Integer daysSinceEpoch) { // REMARK: Yes, I'm using LocalDataTime.ofInstant as LocalDate.ofInstant is marked "JDK 1.9" this.value = LocalDateTime.ofInstant( - Instant.ofEpochSecond(((long) daysSinceEpoch) * 86400), OffsetDateTime.now().getOffset()).toLocalDate(); + Instant.ofEpochSecond(((long) daysSinceEpoch) * 86400), ZoneOffset.UTC).toLocalDate(); this.isNullable = false; } public PlcDATE(Long daysSinceEpoch) { // REMARK: Yes, I'm using LocalDataTime.ofInstant as LocalDate.ofInstant is marked "JDK 1.9" this.value = LocalDateTime.ofInstant( - Instant.ofEpochSecond((daysSinceEpoch) * 86400), OffsetDateTime.now().getOffset()).toLocalDate(); + Instant.ofEpochSecond((daysSinceEpoch) * 86400), ZoneOffset.UTC).toLocalDate(); this.isNullable = false; } public PlcDATE(Float daysSinceEpoch) { // REMARK: Yes, I'm using LocalDataTime.ofInstant as LocalDate.ofInstant is marked "JDK 1.9" this.value = LocalDateTime.ofInstant( - Instant.ofEpochSecond(daysSinceEpoch.longValue() * 86400), OffsetDateTime.now().getOffset()).toLocalDate(); + Instant.ofEpochSecond(daysSinceEpoch.longValue() * 86400), ZoneOffset.UTC).toLocalDate(); this.isNullable = false; } public PlcDATE(Double daysSinceEpoch) { // REMARK: Yes, I'm using LocalDataTime.ofInstant as LocalDate.ofInstant is marked "JDK 1.9" this.value = LocalDateTime.ofInstant( - Instant.ofEpochSecond(daysSinceEpoch.longValue() * 86400), OffsetDateTime.now().getOffset()).toLocalDate(); + Instant.ofEpochSecond(daysSinceEpoch.longValue() * 86400), ZoneOffset.UTC).toLocalDate(); this.isNullable = false; } public PlcDATE(BigInteger daysSinceEpoch) { this.value = LocalDateTime.ofInstant( - Instant.ofEpochSecond(daysSinceEpoch.longValue() * 86400), OffsetDateTime.now().getOffset()).toLocalDate(); + Instant.ofEpochSecond(daysSinceEpoch.longValue() * 86400), ZoneOffset.UTC).toLocalDate(); this.isNullable = false; } public PlcDATE(BigDecimal daysSinceEpoch) { this.value = LocalDateTime.ofInstant( - Instant.ofEpochSecond(daysSinceEpoch.longValue() * 86400), OffsetDateTime.now().getOffset()).toLocalDate(); + Instant.ofEpochSecond(daysSinceEpoch.longValue() * 86400), ZoneOffset.UTC).toLocalDate(); this.isNullable = false; } @@ -134,11 +136,11 @@ public PlcValueType getPlcValueType() { } public long getSecondsSinceEpoch() { - return value.atStartOfDay(OffsetDateTime.now().getOffset()).toEpochSecond(); + return value.atStartOfDay(ZoneOffset.UTC).toEpochSecond(); } public int getDaysSinceEpoch() { - return (int) (value.atStartOfDay(OffsetDateTime.now().getOffset()).toEpochSecond() / 86400); + return (int) (value.atStartOfDay(ZoneOffset.UTC).toEpochSecond() / 86400); } @Override @@ -148,7 +150,7 @@ public boolean isLong() { @Override public long getLong() { - Instant instant = value.atStartOfDay(OffsetDateTime.now().getOffset()).toInstant(); + Instant instant = value.atStartOfDay(ZoneOffset.UTC).toInstant(); return (instant.toEpochMilli() / 1000); } diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcDATE_AND_LTIME.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcDATE_AND_LTIME.java index 8d8402da75b..6c88b2b409d 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcDATE_AND_LTIME.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcDATE_AND_LTIME.java @@ -30,13 +30,14 @@ import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; -import java.time.OffsetDateTime; import java.time.ZoneOffset; public class PlcDATE_AND_LTIME extends PlcIECValue { public static PlcDATE_AND_LTIME of(Object value) { - if (value instanceof LocalDateTime) { + if (value instanceof PlcDATE_AND_LTIME) { + return (PlcDATE_AND_LTIME) value; + } else if (value instanceof LocalDateTime) { return new PlcDATE_AND_LTIME((LocalDateTime) value); } else if (value instanceof Byte) { return new PlcDATE_AND_LTIME((Byte) value); @@ -59,63 +60,67 @@ public static PlcDATE_AND_LTIME of(Object value) { } } - public static PlcDATE_AND_LTIME ofNanosecondsSinceEpoch(BigInteger nanosecondsSinceEpoch) { - return new PlcDATE_AND_LTIME(nanosecondsSinceEpoch); - } - - public PlcDATE_AND_LTIME(Byte millisecondsSinceEpoch) { - long daysSinceEpoch = 0; - long millisecondsOfDay = millisecondsSinceEpoch.longValue(); - this.value = LocalDateTime.of(LocalDate.ofEpochDay(daysSinceEpoch), LocalTime.ofNanoOfDay(millisecondsOfDay * 1000000)); + public PlcDATE_AND_LTIME(Byte nanosecondsSinceEpoch) { + long secondsSinceEpoch = 0; + long nannoSecondsOfSecond = nanosecondsSinceEpoch % 1000000000; + this.value = LocalDateTime.ofEpochSecond(secondsSinceEpoch, (int) nannoSecondsOfSecond, + ZoneOffset.UTC); this.isNullable = false; } - public PlcDATE_AND_LTIME(Short millisecondsSinceEpoch) { - long daysSinceEpoch = 0; - long millisecondsOfDay = millisecondsSinceEpoch.longValue(); - this.value = LocalDateTime.of(LocalDate.ofEpochDay(daysSinceEpoch), LocalTime.ofNanoOfDay(millisecondsOfDay * 1000000)); + public PlcDATE_AND_LTIME(Short nanosecondsSinceEpoch) { + long secondsSinceEpoch = 0; + long nannoSecondsOfSecond = nanosecondsSinceEpoch % 1000000000; + this.value = LocalDateTime.ofEpochSecond(secondsSinceEpoch, (int) nannoSecondsOfSecond, + ZoneOffset.UTC); this.isNullable = false; } - public PlcDATE_AND_LTIME(Integer millisecondsSinceEpoch) { - long daysSinceEpoch = millisecondsSinceEpoch.longValue() / 86400000; - long millisecondsOfDay = millisecondsSinceEpoch.longValue() % 86400000; - this.value = LocalDateTime.of(LocalDate.ofEpochDay(daysSinceEpoch), LocalTime.ofNanoOfDay(millisecondsOfDay * 1000000)); + public PlcDATE_AND_LTIME(Integer nanosecondsSinceEpoch) { + long secondsSinceEpoch = nanosecondsSinceEpoch.longValue() / 1000000000; + long nannoSecondsOfSecond = nanosecondsSinceEpoch % 1000000000; + this.value = LocalDateTime.ofEpochSecond(secondsSinceEpoch, (int) nannoSecondsOfSecond, + ZoneOffset.UTC); this.isNullable = false; } - public PlcDATE_AND_LTIME(Long millisecondsSinceEpoch) { - long daysSinceEpoch = millisecondsSinceEpoch / 86400000; - long millisecondsOfDay = millisecondsSinceEpoch % 86400000; - this.value = LocalDateTime.of(LocalDate.ofEpochDay(daysSinceEpoch), LocalTime.ofNanoOfDay(millisecondsOfDay * 1000000)); + public PlcDATE_AND_LTIME(Long nanosecondsSinceEpoch) { + long secondsSinceEpoch = nanosecondsSinceEpoch / 1000000000; + long nannoSecondsOfSecond = nanosecondsSinceEpoch % 1000000000; + this.value = LocalDateTime.ofEpochSecond(secondsSinceEpoch, (int) nannoSecondsOfSecond, + ZoneOffset.UTC); this.isNullable = false; } - public PlcDATE_AND_LTIME(Float millisecondsSinceEpoch) { - long daysSinceEpoch = millisecondsSinceEpoch.longValue() / 86400000; - long millisecondsOfDay = millisecondsSinceEpoch.longValue() % 86400000; - this.value = LocalDateTime.of(LocalDate.ofEpochDay(daysSinceEpoch), LocalTime.ofNanoOfDay(millisecondsOfDay * 1000000)); + public PlcDATE_AND_LTIME(Float nanosecondsSinceEpoch) { + long secondsSinceEpoch = nanosecondsSinceEpoch.longValue() / 1000000000; + long nannoSecondsOfSecond = nanosecondsSinceEpoch.longValue() % 1000000000; + this.value = LocalDateTime.ofEpochSecond(secondsSinceEpoch, (int) nannoSecondsOfSecond, + ZoneOffset.UTC); this.isNullable = false; } - public PlcDATE_AND_LTIME(Double millisecondsSinceEpoch) { - long daysSinceEpoch = millisecondsSinceEpoch.longValue() / 86400000; - long millisecondsOfDay = millisecondsSinceEpoch.longValue() % 86400000; - this.value = LocalDateTime.of(LocalDate.ofEpochDay(daysSinceEpoch), LocalTime.ofNanoOfDay(millisecondsOfDay * 1000000)); + public PlcDATE_AND_LTIME(Double nanosecondsSinceEpoch) { + long secondsSinceEpoch = nanosecondsSinceEpoch.longValue() / 1000000000; + long nannoSecondsOfSecond = nanosecondsSinceEpoch.longValue() % 1000000000; + this.value = LocalDateTime.ofEpochSecond(secondsSinceEpoch, (int) nannoSecondsOfSecond, + ZoneOffset.UTC); this.isNullable = false; } - public PlcDATE_AND_LTIME(BigInteger millisecondsSinceEpoch) { - long daysSinceEpoch = millisecondsSinceEpoch.longValue() / 86400000; - long millisecondsOfDay = millisecondsSinceEpoch.longValue() % 86400000; - this.value = LocalDateTime.of(LocalDate.ofEpochDay(daysSinceEpoch), LocalTime.ofNanoOfDay(millisecondsOfDay * 1000000)); + public PlcDATE_AND_LTIME(BigInteger nanosecondsSinceEpoch) { + long secondsSinceEpoch = nanosecondsSinceEpoch.longValue() / 1000000000; + long nannoSecondsOfSecond = nanosecondsSinceEpoch.longValue() % 1000000000; + this.value = LocalDateTime.ofEpochSecond(secondsSinceEpoch, (int) nannoSecondsOfSecond, + ZoneOffset.UTC); this.isNullable = false; } - public PlcDATE_AND_LTIME(BigDecimal millisecondsSinceEpoch) { - long daysSinceEpoch = millisecondsSinceEpoch.longValue() / 86400000; - long millisecondsOfDay = millisecondsSinceEpoch.longValue() % 86400000; - this.value = LocalDateTime.of(LocalDate.ofEpochDay(daysSinceEpoch), LocalTime.ofNanoOfDay(millisecondsOfDay * 1000000)); + public PlcDATE_AND_LTIME(BigDecimal nanosecondsSinceEpoch) { + long secondsSinceEpoch = nanosecondsSinceEpoch.longValue() / 1000000000; + long nannoSecondsOfSecond = nanosecondsSinceEpoch.longValue() % 1000000000; + this.value = LocalDateTime.ofEpochSecond(secondsSinceEpoch, (int) nannoSecondsOfSecond, + ZoneOffset.UTC); this.isNullable = false; } @@ -124,6 +129,11 @@ public PlcDATE_AND_LTIME(LocalDateTime value) { this.isNullable = false; } + + public static PlcDATE_AND_LTIME ofNanosecondsSinceEpoch(BigInteger nanosecondsSinceEpoch) { + return new PlcDATE_AND_LTIME(nanosecondsSinceEpoch.longValue()); + } + public static PlcDATE_AND_LTIME ofSegments(int year, int month, int day, int hour, int minutes, int seconds, long nannosecondsOfSecond) { return new PlcDATE_AND_LTIME(LocalDateTime.of(year, month, day, hour, minutes, seconds, (int) nannosecondsOfSecond)); } @@ -134,7 +144,7 @@ public PlcValueType getPlcValueType() { } public BigInteger getNanosecondsSinceEpoch() { - Instant instant = getDateTime().toInstant(OffsetDateTime.now().getOffset()); + Instant instant = getDateTime().toInstant(ZoneOffset.UTC); return BigInteger.valueOf(instant.getEpochSecond()).multiply(BigInteger.valueOf(1000_000_000)).add(BigInteger.valueOf(instant.getNano())); } @@ -145,7 +155,7 @@ public boolean isLong() { @Override public long getLong() { - Instant instant = value.atZone(OffsetDateTime.now().getOffset()).toInstant(); + Instant instant = value.atZone(ZoneOffset.UTC).toInstant(); return instant.getEpochSecond(); } diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcDATE_AND_TIME.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcDATE_AND_TIME.java index 5d97a21ab7d..60b4416bf81 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcDATE_AND_TIME.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcDATE_AND_TIME.java @@ -30,13 +30,14 @@ import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; -import java.time.OffsetDateTime; import java.time.ZoneOffset; public class PlcDATE_AND_TIME extends PlcIECValue { public static PlcDATE_AND_TIME of(Object value) { - if (value instanceof LocalDateTime) { + if (value instanceof PlcDATE_AND_TIME) { + return (PlcDATE_AND_TIME) value; + } else if (value instanceof LocalDateTime) { return new PlcDATE_AND_TIME((LocalDateTime) value); } else if (value instanceof Byte) { return new PlcDATE_AND_TIME((Byte) value); @@ -61,7 +62,7 @@ public static PlcDATE_AND_TIME of(Object value) { public static PlcDATE_AND_TIME ofSecondsSinceEpoch(long secondsSinceEpoch) { return new PlcDATE_AND_TIME(LocalDateTime.ofEpochSecond(secondsSinceEpoch, 0, - OffsetDateTime.now().getOffset())); + ZoneOffset.UTC)); } public static PlcDATE_AND_TIME ofSegments(int year, int month, int day, int hour, int minutes, int seconds, int nanoseconds) { @@ -70,49 +71,49 @@ public static PlcDATE_AND_TIME ofSegments(int year, int month, int day, int hour public PlcDATE_AND_TIME(Byte secondsSinceEpoch) { this.value = LocalDateTime.ofEpochSecond(secondsSinceEpoch, 0, - OffsetDateTime.now().getOffset()); + ZoneOffset.UTC); this.isNullable = false; } public PlcDATE_AND_TIME(Short secondsSinceEpoch) { this.value = LocalDateTime.ofEpochSecond(secondsSinceEpoch, 0, - OffsetDateTime.now().getOffset()); + ZoneOffset.UTC); this.isNullable = false; } public PlcDATE_AND_TIME(Integer secondsSinceEpoch) { this.value = LocalDateTime.ofEpochSecond(secondsSinceEpoch, 0, - OffsetDateTime.now().getOffset()); + ZoneOffset.UTC); this.isNullable = false; } public PlcDATE_AND_TIME(Long secondsSinceEpoch) { this.value = LocalDateTime.ofEpochSecond(secondsSinceEpoch, 0, - OffsetDateTime.now().getOffset()); + ZoneOffset.UTC); this.isNullable = false; } public PlcDATE_AND_TIME(Float secondsSinceEpoch) { this.value = LocalDateTime.ofEpochSecond(secondsSinceEpoch.longValue(), 0, - OffsetDateTime.now().getOffset()); + ZoneOffset.UTC); this.isNullable = false; } public PlcDATE_AND_TIME(Double secondsSinceEpoch) { this.value = LocalDateTime.ofEpochSecond(secondsSinceEpoch.longValue(), 0, - OffsetDateTime.now().getOffset()); + ZoneOffset.UTC); this.isNullable = false; } public PlcDATE_AND_TIME(BigInteger secondsSinceEpoch) { this.value = LocalDateTime.ofEpochSecond(secondsSinceEpoch.longValue(), 0, - OffsetDateTime.now().getOffset()); + ZoneOffset.UTC); this.isNullable = false; } public PlcDATE_AND_TIME(BigDecimal secondsSinceEpoch) { this.value = LocalDateTime.ofEpochSecond(secondsSinceEpoch.longValue(), 0, - OffsetDateTime.now().getOffset()); + ZoneOffset.UTC); this.isNullable = false; } @@ -132,7 +133,7 @@ public PlcValueType getPlcValueType() { } public long getSecondsSinceEpoch() { - Instant instant = getDateTime().toInstant(OffsetDateTime.now().getOffset()); + Instant instant = getDateTime().toInstant(ZoneOffset.UTC); return instant.getEpochSecond(); } @@ -175,7 +176,7 @@ public boolean isLong() { @Override public long getLong() { - Instant instant = value.atZone(OffsetDateTime.now().getOffset()).toInstant(); + Instant instant = value.atZone(ZoneOffset.UTC).toInstant(); return instant.getEpochSecond(); } diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcDINT.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcDINT.java index 8ecf36ff0cd..f747e285d33 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcDINT.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcDINT.java @@ -29,11 +29,13 @@ public class PlcDINT extends PlcIECValue { private static final String VALUE_OUT_OF_RANGE = "Value of type %s is out of range %d - %d for a %s Value"; - static final Integer minValue = Integer.MIN_VALUE; - static final Integer maxValue = Integer.MAX_VALUE; + public static final Integer MIN_VALUE = Integer.MIN_VALUE; + public static final Integer MAX_VALUE = Integer.MAX_VALUE; public static PlcDINT of(Object value) { - if (value instanceof Boolean) { + if (value instanceof PlcDINT) { + return (PlcDINT) value; + } else if (value instanceof Boolean) { return new PlcDINT((Boolean) value); } else if (value instanceof Byte) { return new PlcDINT((Byte) value); @@ -77,40 +79,40 @@ public PlcDINT(Integer value) { } public PlcDINT(Long value) { - if (value < minValue || value > maxValue) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if (value < MIN_VALUE || value > MAX_VALUE) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.intValue(); this.isNullable = false; } public PlcDINT(Float value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.intValue(); this.isNullable = false; } public PlcDINT(Double value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.intValue(); this.isNullable = false; } public PlcDINT(BigInteger value) { - if ((value.compareTo(BigInteger.valueOf(minValue)) < 0) || (value.compareTo(BigInteger.valueOf(maxValue)) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value.compareTo(BigInteger.valueOf(MIN_VALUE)) < 0) || (value.compareTo(BigInteger.valueOf(MAX_VALUE)) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.intValue(); this.isNullable = true; } public PlcDINT(BigDecimal value) { - if ((value.compareTo(BigDecimal.valueOf(minValue)) < 0) || (value.compareTo(BigDecimal.valueOf(maxValue)) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value.compareTo(BigDecimal.valueOf(MIN_VALUE)) < 0) || (value.compareTo(BigDecimal.valueOf(MAX_VALUE)) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.intValue(); this.isNullable = true; @@ -121,7 +123,7 @@ public PlcDINT(String value) { this.value = Integer.parseInt(value.trim()); this.isNullable = false; } catch (Exception e) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName()), e); + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName()), e); } } diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcDWORD.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcDWORD.java index f66b721de42..2ab06b560d8 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcDWORD.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcDWORD.java @@ -30,11 +30,13 @@ public class PlcDWORD extends PlcIECValue { private static final String VALUE_OUT_OF_RANGE = "Value of type %s is out of range %d - %d for a %s Value"; - static final Long minValue = (long) 0; - static final Long maxValue = (long) Integer.MAX_VALUE * 2 + 1; + public static final Long MIN_VALUE = (long) 0; + public static final Long MAX_VALUE = (long) Integer.MAX_VALUE * 2 + 1; public static PlcDWORD of(Object value) { - if (value instanceof Boolean) { + if (value instanceof PlcDWORD) { + return (PlcDWORD) value; + } else if (value instanceof Boolean) { return new PlcDWORD((Boolean) value); } else if (value instanceof Byte) { return new PlcDWORD((Byte) value); @@ -64,64 +66,64 @@ public PlcDWORD(Boolean value) { } public PlcDWORD(Byte value) { - if (value < minValue || value > maxValue) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if (value < MIN_VALUE || value > MAX_VALUE) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.longValue(); this.isNullable = false; } public PlcDWORD(Short value) { - if (value < minValue || value > maxValue) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if (value < MIN_VALUE || value > MAX_VALUE) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.longValue(); this.isNullable = false; } public PlcDWORD(Integer value) { - if (value < minValue || value > maxValue) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if (value < MIN_VALUE || value > MAX_VALUE) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.longValue(); this.isNullable = false; } public PlcDWORD(Long value) { - if (value < minValue || value > maxValue) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if (value < MIN_VALUE || value > MAX_VALUE) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.longValue(); this.isNullable = false; } public PlcDWORD(Float value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.longValue(); this.isNullable = false; } public PlcDWORD(Double value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.longValue(); this.isNullable = false; } public PlcDWORD(BigInteger value) { - if ((value.compareTo(BigInteger.valueOf(minValue)) < 0) || (value.compareTo(BigInteger.valueOf(maxValue)) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value.compareTo(BigInteger.valueOf(MIN_VALUE)) < 0) || (value.compareTo(BigInteger.valueOf(MAX_VALUE)) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.longValue(); this.isNullable = true; } public PlcDWORD(BigDecimal value) { - if ((value.compareTo(BigDecimal.valueOf(minValue)) < 0) || (value.compareTo(BigDecimal.valueOf(maxValue)) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value.compareTo(BigDecimal.valueOf(MIN_VALUE)) < 0) || (value.compareTo(BigDecimal.valueOf(MAX_VALUE)) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.longValue(); this.isNullable = true; @@ -130,19 +132,19 @@ public PlcDWORD(BigDecimal value) { public PlcDWORD(String value) { try { long val = Long.parseLong(value.trim()); - if (val < minValue || val > maxValue) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if (val < MIN_VALUE || val > MAX_VALUE) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = val; this.isNullable = false; } catch (Exception e) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName()), e); + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName()), e); } } public PlcDWORD(long value) { - if (value < minValue || value > maxValue) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if (value < MIN_VALUE || value > MAX_VALUE) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value; this.isNullable = false; diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcINT.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcINT.java index 4bb42079f1a..656fbeedeea 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcINT.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcINT.java @@ -29,11 +29,13 @@ public class PlcINT extends PlcIECValue { private static final String VALUE_OUT_OF_RANGE = "Value of type %s is out of range %d - %d for a %s Value"; - static final Short minValue = Short.MIN_VALUE; - static final Short maxValue = Short.MAX_VALUE; + public static final Short MIN_VALUE = Short.MIN_VALUE; + public static final Short MAX_VALUE = Short.MAX_VALUE; public static PlcINT of(Object value) { - if (value instanceof Boolean) { + if (value instanceof PlcINT) { + return (PlcINT) value; + } else if (value instanceof Boolean) { return new PlcINT((Boolean) value); } else if (value instanceof Byte) { return new PlcINT((Byte) value); @@ -72,8 +74,8 @@ public PlcINT(Short value) { } public PlcINT(Integer value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } else { this.value = value.shortValue(); this.isNullable = false; @@ -81,40 +83,40 @@ public PlcINT(Integer value) { } public PlcINT(Long value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.shortValue(); this.isNullable = false; } public PlcINT(Float value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.shortValue(); this.isNullable = false; } public PlcINT(Double value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.shortValue(); this.isNullable = false; } public PlcINT(BigInteger value) { - if ((value.compareTo(BigInteger.valueOf(minValue)) < 0) || (value.compareTo(BigInteger.valueOf(maxValue)) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value.compareTo(BigInteger.valueOf(MIN_VALUE)) < 0) || (value.compareTo(BigInteger.valueOf(MAX_VALUE)) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.shortValue(); this.isNullable = true; } public PlcINT(BigDecimal value) { - if ((value.compareTo(BigDecimal.valueOf(minValue)) < 0) || (value.compareTo(BigDecimal.valueOf(maxValue)) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value.compareTo(BigDecimal.valueOf(MIN_VALUE)) < 0) || (value.compareTo(BigDecimal.valueOf(MAX_VALUE)) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.shortValue(); this.isNullable = true; @@ -125,7 +127,7 @@ public PlcINT(String value) { this.value = Short.valueOf(value.trim()); this.isNullable = false; } catch (Exception e) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } } diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcLDATE.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcLDATE.java index e2d52d3e54e..79cbc77b239 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcLDATE.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcLDATE.java @@ -29,13 +29,14 @@ import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; -import java.time.OffsetDateTime; import java.time.ZoneOffset; public class PlcLDATE extends PlcIECValue { public static PlcLDATE of(Object value) { - if (value instanceof LocalDate) { + if (value instanceof PlcLDATE) { + return (PlcLDATE) value; + } else if (value instanceof LocalDate) { return new PlcLDATE((LocalDate) value); } else if (value instanceof Byte) { return new PlcLDATE((Byte) value); @@ -62,7 +63,7 @@ public static PlcLDATE ofNanosecondsSinceEpoch(BigInteger nanosecondsSinceEpoch) BigInteger epochSecond = nanosecondsSinceEpoch.divide(BigInteger.valueOf(1000_000)); BigInteger nanoOfSecond = nanosecondsSinceEpoch.mod(BigInteger.valueOf(1000_000)); return new PlcLDATE(LocalDateTime.ofEpochSecond(epochSecond.longValue(), nanoOfSecond.intValue(), - OffsetDateTime.now().getOffset()).toLocalDate()); + ZoneOffset.UTC).toLocalDate()); } public PlcLDATE(LocalDate value) { @@ -73,56 +74,56 @@ public PlcLDATE(LocalDate value) { public PlcLDATE(Byte secondsSinceEpoch) { // REMARK: Yes, I'm using LocalDataTime.ofInstant as LocalDate.ofInstant is marked "JDK 1.9" this.value = LocalDateTime.ofEpochSecond((long) secondsSinceEpoch, 0, - OffsetDateTime.now().getOffset()).toLocalDate(); + ZoneOffset.UTC).toLocalDate(); this.isNullable = false; } public PlcLDATE(Short secondsSinceEpoch) { // REMARK: Yes, I'm using LocalDataTime.ofInstant as LocalDate.ofInstant is marked "JDK 1.9" this.value = LocalDateTime.ofEpochSecond((long) secondsSinceEpoch, 0, - OffsetDateTime.now().getOffset()).toLocalDate(); + ZoneOffset.UTC).toLocalDate(); this.isNullable = false; } public PlcLDATE(Integer secondsSinceEpoch) { // REMARK: Yes, I'm using LocalDataTime.ofInstant as LocalDate.ofInstant is marked "JDK 1.9" this.value = LocalDateTime.ofEpochSecond((long) secondsSinceEpoch, 0, - OffsetDateTime.now().getOffset()).toLocalDate(); + ZoneOffset.UTC).toLocalDate(); this.isNullable = false; } public PlcLDATE(Long secondsSinceEpoch) { // REMARK: Yes, I'm using LocalDataTime.ofInstant as LocalDate.ofInstant is marked "JDK 1.9" this.value = LocalDateTime.ofEpochSecond((long) secondsSinceEpoch, 0, - OffsetDateTime.now().getOffset()).toLocalDate(); + ZoneOffset.UTC).toLocalDate(); this.isNullable = false; } public PlcLDATE(Float secondsSinceEpoch) { // REMARK: Yes, I'm using LocalDataTime.ofInstant as LocalDate.ofInstant is marked "JDK 1.9" this.value = LocalDateTime.ofEpochSecond(secondsSinceEpoch.longValue(), 0, - OffsetDateTime.now().getOffset()).toLocalDate(); + ZoneOffset.UTC).toLocalDate(); this.isNullable = false; } public PlcLDATE(Double secondsSinceEpoch) { // REMARK: Yes, I'm using LocalDataTime.ofInstant as LocalDate.ofInstant is marked "JDK 1.9" this.value = LocalDateTime.ofEpochSecond(secondsSinceEpoch.longValue(), 0, - OffsetDateTime.now().getOffset()).toLocalDate(); + ZoneOffset.UTC).toLocalDate(); this.isNullable = false; } public PlcLDATE(BigInteger secondsSinceEpoch) { // REMARK: Yes, I'm using LocalDataTime.ofInstant as LocalDate.ofInstant is marked "JDK 1.9" this.value = LocalDateTime.ofEpochSecond(secondsSinceEpoch.longValue(), 0, - OffsetDateTime.now().getOffset()).toLocalDate(); + ZoneOffset.UTC).toLocalDate(); this.isNullable = false; } public PlcLDATE(BigDecimal secondsSinceEpoch) { // REMARK: Yes, I'm using LocalDataTime.ofInstant as LocalDate.ofInstant is marked "JDK 1.9" this.value = LocalDateTime.ofEpochSecond(secondsSinceEpoch.longValue(), 0, - OffsetDateTime.now().getOffset()).toLocalDate(); + ZoneOffset.UTC).toLocalDate(); this.isNullable = false; } @@ -132,7 +133,7 @@ public PlcValueType getPlcValueType() { } public BigInteger getNanosecondsSinceEpoch() { - Instant instant = getDateTime().toInstant(OffsetDateTime.now().getOffset()); + Instant instant = getDateTime().toInstant(ZoneOffset.UTC); return BigInteger.valueOf(instant.getEpochSecond()).multiply(BigInteger.valueOf(1000_000_000)).add(BigInteger.valueOf(instant.getNano())); } @@ -143,7 +144,7 @@ public boolean isLong() { @Override public long getLong() { - Instant instant = value.atStartOfDay(OffsetDateTime.now().getOffset()).toInstant(); + Instant instant = value.atStartOfDay(ZoneOffset.UTC).toInstant(); return (instant.toEpochMilli() / 1000); } diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcLDATE_AND_TIME.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcLDATE_AND_TIME.java index 4cc9fa6c892..6d620ac7318 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcLDATE_AND_TIME.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcLDATE_AND_TIME.java @@ -18,7 +18,6 @@ */ package org.apache.plc4x.java.spi.values; -import org.apache.plc4x.java.api.exceptions.PlcRuntimeException; import org.apache.plc4x.java.api.types.PlcValueType; import org.apache.plc4x.java.spi.codegen.WithOption; import org.apache.plc4x.java.spi.generation.SerializationException; @@ -31,13 +30,14 @@ import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; -import java.time.OffsetDateTime; import java.time.ZoneOffset; public class PlcLDATE_AND_TIME extends PlcIECValue { public static PlcLDATE_AND_TIME of(Object value) { - if (value instanceof LocalDateTime) { + if (value instanceof PlcLDATE_AND_TIME) { + return (PlcLDATE_AND_TIME) value; + } else if (value instanceof LocalDateTime) { return new PlcLDATE_AND_TIME((LocalDateTime) value); } else if (value instanceof Byte) { return new PlcLDATE_AND_TIME((Byte) value); @@ -64,7 +64,7 @@ public static PlcLDATE_AND_TIME ofNanosecondsSinceEpoch(BigInteger nanosecondsSi BigInteger epochSecond = nanosecondsSinceEpoch.divide(BigInteger.valueOf(1000_000)); BigInteger nanoOfSecond = nanosecondsSinceEpoch.mod(BigInteger.valueOf(1000_000)); return new PlcLDATE_AND_TIME(LocalDateTime.ofEpochSecond(epochSecond.longValue(), nanoOfSecond.intValue(), - OffsetDateTime.now().getOffset())); + ZoneOffset.UTC)); } public PlcLDATE_AND_TIME(Byte millisecondsSinceEpoch) { @@ -134,7 +134,7 @@ public PlcValueType getPlcValueType() { } public BigInteger getNanosecondsSinceEpoch() { - Instant instant = getDateTime().toInstant(OffsetDateTime.now().getOffset()); + Instant instant = getDateTime().toInstant(ZoneOffset.UTC); return BigInteger.valueOf(instant.getEpochSecond()).multiply(BigInteger.valueOf(1000_000_000)).add(BigInteger.valueOf(instant.getNano())); } @@ -145,7 +145,7 @@ public boolean isLong() { @Override public long getLong() { - Instant instant = value.atZone(OffsetDateTime.now().getOffset()).toInstant(); + Instant instant = value.atZone(ZoneOffset.UTC).toInstant(); return instant.getEpochSecond(); } diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcLINT.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcLINT.java index 67f4fc004aa..db1ff3fbd28 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcLINT.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcLINT.java @@ -29,11 +29,13 @@ public class PlcLINT extends PlcIECValue { private static final String VALUE_OUT_OF_RANGE = "Value of type %s is out of range %d - %d for a %s Value"; - static final Long minValue = Long.MIN_VALUE; - static final Long maxValue = Long.MAX_VALUE; + public static final Long MIN_VALUE = Long.MIN_VALUE; + public static final Long MAX_VALUE = Long.MAX_VALUE; public static PlcLINT of(Object value) { - if (value instanceof Boolean) { + if (value instanceof PlcLINT) { + return (PlcLINT) value; + } else if (value instanceof Boolean) { return new PlcLINT((Boolean) value); } else if (value instanceof Byte) { return new PlcLINT((Byte) value); @@ -82,32 +84,32 @@ public PlcLINT(Long value) { } public PlcLINT(Float value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.longValue(); this.isNullable = false; } public PlcLINT(Double value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.longValue(); this.isNullable = false; } public PlcLINT(BigInteger value) { - if ((value.compareTo(BigInteger.valueOf(minValue)) < 0) || (value.compareTo(BigInteger.valueOf(maxValue)) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value.compareTo(BigInteger.valueOf(MIN_VALUE)) < 0) || (value.compareTo(BigInteger.valueOf(MAX_VALUE)) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.longValue(); this.isNullable = true; } public PlcLINT(BigDecimal value) { - if ((value.compareTo(BigDecimal.valueOf(minValue)) < 0) || (value.compareTo(BigDecimal.valueOf(maxValue)) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value.compareTo(BigDecimal.valueOf(MIN_VALUE)) < 0) || (value.compareTo(BigDecimal.valueOf(MAX_VALUE)) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.longValue(); this.isNullable = true; @@ -118,7 +120,7 @@ public PlcLINT(String value) { this.value = Long.parseLong(value.trim()); this.isNullable = false; } catch (Exception e) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } } diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcLREAL.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcLREAL.java index 9cd9d647dba..b5c8222e41c 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcLREAL.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcLREAL.java @@ -28,12 +28,14 @@ public class PlcLREAL extends PlcIECValue { - private static final String VALUE_OUT_OF_RANGE = "Value of type %s is out of range %d - %d for a %s Value"; - static final Double minValue = -Double.MAX_VALUE; - static final Double maxValue = Double.MAX_VALUE; + private static final String VALUE_OUT_OF_RANGE = "Value of type %s is out of range %f - %f for a %s Value"; + public static final Double MIN_VALUE = -Double.MAX_VALUE; + public static final Double MAX_VALUE = Double.MAX_VALUE; public static PlcLREAL of(Object value) { - if (value instanceof Boolean) { + if (value instanceof PlcLREAL) { + return (PlcLREAL) value; + } else if (value instanceof Boolean) { return new PlcLREAL((Boolean) value); } else if (value instanceof Byte) { return new PlcLREAL((Byte) value); @@ -76,6 +78,11 @@ public PlcLREAL(Integer value) { this.isNullable = false; } + public PlcLREAL(Long value) { + this.value = value.doubleValue(); + this.isNullable = false; + } + public PlcLREAL(Float value) { this.value = value.doubleValue(); this.isNullable = false; @@ -88,16 +95,16 @@ public PlcLREAL(Double value) { public PlcLREAL(BigInteger value) { BigDecimal val = new BigDecimal(value); - if ((val.compareTo(BigDecimal.valueOf(minValue)) < 0) || (val.compareTo(BigDecimal.valueOf(maxValue)) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((val.compareTo(BigDecimal.valueOf(MIN_VALUE)) < 0) || (val.compareTo(BigDecimal.valueOf(MAX_VALUE)) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = val.doubleValue(); this.isNullable = true; } public PlcLREAL(BigDecimal value) { - if ((value.compareTo(BigDecimal.valueOf(minValue)) < 0) || (value.compareTo(BigDecimal.valueOf(maxValue)) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value.compareTo(BigDecimal.valueOf(MIN_VALUE)) < 0) || (value.compareTo(BigDecimal.valueOf(MAX_VALUE)) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.doubleValue(); this.isNullable = true; @@ -108,7 +115,7 @@ public PlcLREAL(String value) { this.value = Double.parseDouble(value.trim()); this.isNullable = false; } catch (Exception e) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } } diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcLTIME.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcLTIME.java index 74e9395fe4e..4e2c2a7491e 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcLTIME.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcLTIME.java @@ -32,7 +32,9 @@ public class PlcLTIME extends PlcIECValue { public static PlcLTIME of(Object value) { - if (value instanceof Duration) { + if (value instanceof PlcLTIME) { + return (PlcLTIME) value; + } else if (value instanceof Duration) { return new PlcLTIME((Duration) value); } else if (value instanceof Byte) { return new PlcLTIME((Byte) value); diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcLTIME_OF_DAY.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcLTIME_OF_DAY.java index f9c1f45b6b0..4d3242161a7 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcLTIME_OF_DAY.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcLTIME_OF_DAY.java @@ -31,7 +31,9 @@ public class PlcLTIME_OF_DAY extends PlcIECValue { public static PlcLTIME_OF_DAY of(Object value) { - if (value instanceof LocalTime) { + if (value instanceof PlcLTIME_OF_DAY) { + return (PlcLTIME_OF_DAY) value; + } else if (value instanceof LocalTime) { return new PlcLTIME_OF_DAY((LocalTime) value); } else if (value instanceof Byte) { return new PlcLTIME_OF_DAY((Byte) value); diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcLWORD.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcLWORD.java index 6ecd1afce33..bceed5dcd8e 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcLWORD.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcLWORD.java @@ -30,11 +30,13 @@ public class PlcLWORD extends PlcIECValue { private static final String VALUE_OUT_OF_RANGE = "Value of type %s is out of range %d - %d for a %s Value"; - static final BigInteger minValue = BigInteger.valueOf(0); - static final BigInteger maxValue = BigInteger.valueOf(Long.MAX_VALUE).multiply(BigInteger.valueOf(2)).add(BigInteger.valueOf(1)); + public static final BigInteger MIN_VALUE = BigInteger.valueOf(0); + public static final BigInteger MAX_VALUE = BigInteger.valueOf(Long.MAX_VALUE).multiply(BigInteger.valueOf(2)).add(BigInteger.valueOf(1)); public static PlcLWORD of(Object value) { - if (value instanceof Boolean) { + if (value instanceof PlcLWORD) { + return (PlcLWORD) value; + } else if (value instanceof Boolean) { return new PlcLWORD((Boolean) value); } else if (value instanceof Byte) { return new PlcLWORD((Byte) value); @@ -64,8 +66,8 @@ public PlcLWORD(Boolean value) { public PlcLWORD(Byte value) { BigInteger val = BigInteger.valueOf(value); - if ((val.compareTo(minValue) < 0) || (val.compareTo(maxValue) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((val.compareTo(MIN_VALUE) < 0) || (val.compareTo(MAX_VALUE) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = val; this.isNullable = false; @@ -73,8 +75,8 @@ public PlcLWORD(Byte value) { public PlcLWORD(Short value) { BigInteger val = BigInteger.valueOf(value); - if ((val.compareTo(minValue) < 0) || (val.compareTo(maxValue) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((val.compareTo(MIN_VALUE) < 0) || (val.compareTo(MAX_VALUE) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = val; this.isNullable = false; @@ -82,8 +84,8 @@ public PlcLWORD(Short value) { public PlcLWORD(Integer value) { BigInteger val = BigInteger.valueOf(value); - if ((val.compareTo(minValue) < 0) || (val.compareTo(maxValue) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((val.compareTo(MIN_VALUE) < 0) || (val.compareTo(MAX_VALUE) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = val; this.isNullable = false; @@ -91,8 +93,8 @@ public PlcLWORD(Integer value) { public PlcLWORD(Long value) { BigInteger val = BigInteger.valueOf(value); - if ((val.compareTo(minValue) < 0) || (val.compareTo(maxValue) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((val.compareTo(MIN_VALUE) < 0) || (val.compareTo(MAX_VALUE) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = val; this.isNullable = false; @@ -101,32 +103,32 @@ public PlcLWORD(Long value) { public PlcLWORD(Float value) { try { BigInteger val = BigDecimal.valueOf(value).toBigInteger(); - if ((val.compareTo(minValue) < 0) || (val.compareTo(maxValue) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((val.compareTo(MIN_VALUE) < 0) || (val.compareTo(MAX_VALUE) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = val; this.isNullable = false; } catch (Exception e) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName()), e); + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName()), e); } } public PlcLWORD(Double value) { try { BigInteger val = BigDecimal.valueOf(value).toBigInteger(); - if ((val.compareTo(minValue) < 0) || (val.compareTo(maxValue) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((val.compareTo(MIN_VALUE) < 0) || (val.compareTo(MAX_VALUE) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = val; this.isNullable = false; } catch (Exception e) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName()), e); + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName()), e); } } public PlcLWORD(BigInteger value) { - if ((value.compareTo(minValue) < 0) || (value.compareTo(maxValue) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value.compareTo(MIN_VALUE) < 0) || (value.compareTo(MAX_VALUE) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value; this.isNullable = false; @@ -135,26 +137,26 @@ public PlcLWORD(BigInteger value) { public PlcLWORD(BigDecimal value) { try { BigInteger val = value.toBigInteger(); - if ((val.compareTo(minValue) < 0) || (val.compareTo(maxValue) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((val.compareTo(MIN_VALUE) < 0) || (val.compareTo(MAX_VALUE) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = val; this.isNullable = false; } catch (Exception e) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName()), e); + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName()), e); } } public PlcLWORD(String value) { try { BigInteger val = new BigInteger(value.trim()); - if ((val.compareTo(minValue) < 0) || (val.compareTo(maxValue) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((val.compareTo(MIN_VALUE) < 0) || (val.compareTo(MAX_VALUE) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = val; this.isNullable = false; } catch (Exception e) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName()), e); + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName()), e); } } diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcREAL.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcREAL.java index 2cc2717a112..3e1df5abf15 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcREAL.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcREAL.java @@ -29,11 +29,13 @@ public class PlcREAL extends PlcIECValue { private static final String VALUE_OUT_OF_RANGE = "Value of type %s is out of range %f - %f for a %s Value"; - static final Float minValue = -Float.MAX_VALUE; - static final Float maxValue = Float.MAX_VALUE; + public static final Float MIN_VALUE = -Float.MAX_VALUE; + public static final Float MAX_VALUE = Float.MAX_VALUE; public static PlcREAL of(Object value) { - if (value instanceof Boolean) { + if (value instanceof PlcREAL) { + return (PlcREAL) value; + } else if (value instanceof Boolean) { return new PlcREAL((Boolean) value); } else if (value instanceof Byte) { return new PlcREAL((Byte) value); @@ -76,14 +78,19 @@ public PlcREAL(Integer value) { this.isNullable = false; } + public PlcREAL(Long value) { + this.value = value.floatValue(); + this.isNullable = false; + } + public PlcREAL(Float value) { this.value = value; this.isNullable = false; } public PlcREAL(Double value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.floatValue(); this.isNullable = false; @@ -91,16 +98,16 @@ public PlcREAL(Double value) { public PlcREAL(BigInteger value) { BigDecimal val = new BigDecimal(value); - if ((val.compareTo(BigDecimal.valueOf(minValue)) < 0) || (val.compareTo(BigDecimal.valueOf(maxValue)) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((val.compareTo(BigDecimal.valueOf(MIN_VALUE)) < 0) || (val.compareTo(BigDecimal.valueOf(MAX_VALUE)) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = val.floatValue(); this.isNullable = true; } public PlcREAL(BigDecimal value) { - if ((value.compareTo(BigDecimal.valueOf(minValue)) < 0) || (value.compareTo(BigDecimal.valueOf(maxValue)) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value.compareTo(BigDecimal.valueOf(MIN_VALUE)) < 0) || (value.compareTo(BigDecimal.valueOf(MAX_VALUE)) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.floatValue(); this.isNullable = true; @@ -111,7 +118,7 @@ public PlcREAL(String value) { this.value = Float.parseFloat(value.trim()); this.isNullable = false; } catch (Exception e) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName()), e); + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName()), e); } } diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcRawByteArray.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcRawByteArray.java index 691f4e2edde..9cf8fd096d9 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcRawByteArray.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcRawByteArray.java @@ -26,7 +26,9 @@ public class PlcRawByteArray extends PlcIECValue { public static PlcRawByteArray of(Object value) { - if (value instanceof byte[]) { + if (value instanceof PlcRawByteArray) { + return (PlcRawByteArray) value; + } else if (value instanceof byte[]) { return new PlcRawByteArray((byte[]) value); } throw new IllegalArgumentException("Only byte[] supported here"); diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcSINT.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcSINT.java index 122842db282..27c4da0abbf 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcSINT.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcSINT.java @@ -29,11 +29,13 @@ public class PlcSINT extends PlcIECValue { private static final String VALUE_OUT_OF_RANGE = "Value of type %s is out of range %d - %d for a %s Value"; - static final Byte minValue = Byte.MIN_VALUE; - static final Byte maxValue = Byte.MAX_VALUE; + public static final Byte MIN_VALUE = Byte.MIN_VALUE; + public static final Byte MAX_VALUE = Byte.MAX_VALUE; public static PlcSINT of(Object value) { - if (value instanceof Boolean) { + if (value instanceof PlcSINT) { + return (PlcSINT) value; + } else if (value instanceof Boolean) { return new PlcSINT((Boolean) value); } else if (value instanceof Byte) { return new PlcSINT((Byte) value); @@ -67,56 +69,56 @@ public PlcSINT(Byte value) { } public PlcSINT(Short value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.byteValue(); this.isNullable = false; } public PlcSINT(Integer value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.byteValue(); this.isNullable = false; } public PlcSINT(Long value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.byteValue(); this.isNullable = false; } public PlcSINT(Float value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.byteValue(); this.isNullable = false; } public PlcSINT(Double value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.byteValue(); this.isNullable = false; } public PlcSINT(BigInteger value) { - if ((value.compareTo(BigInteger.valueOf(minValue)) < 0) || (value.compareTo(BigInteger.valueOf(maxValue)) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value.compareTo(BigInteger.valueOf(MIN_VALUE)) < 0) || (value.compareTo(BigInteger.valueOf(MAX_VALUE)) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.byteValue(); this.isNullable = true; } public PlcSINT(BigDecimal value) { - if ((value.compareTo(BigDecimal.valueOf(minValue)) < 0) || (value.compareTo(BigDecimal.valueOf(maxValue)) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value.compareTo(BigDecimal.valueOf(MIN_VALUE)) < 0) || (value.compareTo(BigDecimal.valueOf(MAX_VALUE)) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.byteValue(); this.isNullable = true; @@ -127,7 +129,7 @@ public PlcSINT(String value) { this.value = Byte.valueOf(value.trim()); this.isNullable = false; } catch (Exception e) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName()), e); + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName()), e); } } diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcSTRING.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcSTRING.java index cbcfaf7940a..279bc88849e 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcSTRING.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcSTRING.java @@ -30,7 +30,9 @@ public class PlcSTRING extends PlcIECValue { public static PlcSTRING of(Object value) { - if (value instanceof String) { + if (value instanceof PlcSTRING) { + return (PlcSTRING) value; + } else if (value instanceof String) { return new PlcSTRING((String) value); } return new PlcSTRING(String.valueOf(value)); @@ -201,7 +203,7 @@ public int getLength() { @Override public String toString() { - return "\"" + value + "\""; + return value; } @Override diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcTIME.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcTIME.java index 32e83ac694d..730ce80e8e1 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcTIME.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcTIME.java @@ -30,7 +30,9 @@ public class PlcTIME extends PlcIECValue { public static PlcTIME of(Object value) { - if (value instanceof Duration) { + if (value instanceof PlcTIME) { + return (PlcTIME) value; + } else if (value instanceof Duration) { return new PlcTIME((Duration) value); } else if (value instanceof Byte) { return new PlcTIME((Byte) value); diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcTIME_OF_DAY.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcTIME_OF_DAY.java index 75fe8f08e84..76f3854b3f6 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcTIME_OF_DAY.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcTIME_OF_DAY.java @@ -31,7 +31,9 @@ public class PlcTIME_OF_DAY extends PlcIECValue { public static PlcTIME_OF_DAY of(Object value) { - if (value instanceof LocalTime) { + if (value instanceof PlcTIME_OF_DAY) { + return (PlcTIME_OF_DAY) value; + } else if (value instanceof LocalTime) { return new PlcTIME_OF_DAY((LocalTime) value); } else if (value instanceof Byte) { return new PlcTIME_OF_DAY((Byte) value); diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcUBINT.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcUBINT.java deleted file mode 100644 index de982e4e557..00000000000 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcUBINT.java +++ /dev/null @@ -1,292 +0,0 @@ -/* - * 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 - * - * https://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.spi.values; - -import org.apache.plc4x.java.api.exceptions.PlcInvalidTagException; -import org.apache.plc4x.java.api.types.PlcValueType; -import org.apache.plc4x.java.spi.generation.SerializationException; -import org.apache.plc4x.java.spi.generation.WriteBuffer; - -import java.math.BigDecimal; -import java.math.BigInteger; - -public class PlcUBINT extends PlcIECValue { - - private static final String VALUE_OUT_OF_RANGE = "Value of type %s is out of range %d - infinity for a %s Value"; - static final BigInteger minValue = BigInteger.valueOf(0); - - public static PlcUBINT of(Object value) { - if (value instanceof Boolean) { - return new PlcUBINT((Boolean) value); - } else if (value instanceof Byte) { - return new PlcUBINT((Byte) value); - } else if (value instanceof Short) { - return new PlcUBINT((Short) value); - } else if (value instanceof Integer) { - return new PlcUBINT((Integer) value); - } else if (value instanceof Long) { - return new PlcUBINT((Long) value); - } else if (value instanceof Float) { - return new PlcUBINT((Float) value); - } else if (value instanceof Double) { - return new PlcUBINT((Double) value); - } else if (value instanceof BigInteger) { - return new PlcUBINT((BigInteger) value); - } else if (value instanceof BigDecimal) { - return new PlcUBINT((BigDecimal) value); - } else { - return new PlcUBINT(value.toString()); - } - } - - public PlcUBINT(Boolean value) { - this.value = value ? BigInteger.valueOf(1) : BigInteger.valueOf(0); - this.isNullable = false; - } - - public PlcUBINT(Byte value) { - BigInteger val = BigInteger.valueOf(value); - if (val.compareTo(minValue) < 0) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, this.getClass().getSimpleName())); - } - this.value = val; - this.isNullable = false; - } - - public PlcUBINT(Short value) { - BigInteger val = BigInteger.valueOf(value); - if (val.compareTo(minValue) < 0) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, this.getClass().getSimpleName())); - } - this.value = val; - this.isNullable = false; - } - - public PlcUBINT(Integer value) { - BigInteger val = BigInteger.valueOf(value); - if (val.compareTo(minValue) < 0) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, this.getClass().getSimpleName())); - } - this.value = val; - this.isNullable = false; - } - - public PlcUBINT(Long value) { - BigInteger val = BigInteger.valueOf(value); - if (val.compareTo(minValue) < 0) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, this.getClass().getSimpleName())); - } - this.value = val; - this.isNullable = false; - } - - public PlcUBINT(Float value) { - try { - BigInteger val = BigDecimal.valueOf(value).toBigInteger(); - if (val.compareTo(minValue) < 0) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, this.getClass().getSimpleName())); - } - this.value = val; - this.isNullable = false; - } catch (Exception e) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, this.getClass().getSimpleName()), e); - } - } - - public PlcUBINT(Double value) { - try { - BigInteger val = BigDecimal.valueOf(value).toBigInteger(); - if (val.compareTo(minValue) < 0) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, this.getClass().getSimpleName())); - } - this.value = val; - this.isNullable = false; - } catch (Exception e) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, this.getClass().getSimpleName()), e); - } - } - - public PlcUBINT(BigInteger value) { - if (value.compareTo(minValue) < 0) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, this.getClass().getSimpleName())); - } - this.value = value; - this.isNullable = false; - } - - public PlcUBINT(BigDecimal value) { - try { - BigInteger val = value.toBigInteger(); - if (val.compareTo(minValue) < 0) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, this.getClass().getSimpleName())); - } - this.value = val; - this.isNullable = false; - } catch (Exception e) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, this.getClass().getSimpleName()), e); - } - } - - public PlcUBINT(String value) { - try { - BigInteger val = new BigInteger(value.trim()); - if (val.compareTo(minValue) < 0) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, this.getClass().getSimpleName())); - } - this.value = val; - this.isNullable = false; - } catch (Exception e) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, this.getClass().getSimpleName()), e); - } - } - - @Override - public PlcValueType getPlcValueType() { - return PlcValueType.ULINT; - } - - @Override - public boolean isBoolean() { - return true; - } - - @Override - public boolean getBoolean() { - return (value != null) && !value.equals(BigInteger.ZERO); - } - - @Override - public boolean isByte() { - return (value != null) && (value.compareTo(BigInteger.valueOf(Byte.MAX_VALUE)) <= 0) && (value.compareTo(BigInteger.valueOf(Byte.MIN_VALUE)) >= 0); - } - - @Override - public byte getByte() { - return value.byteValue(); - } - - @Override - public boolean isShort() { - return (value != null) && (value.compareTo(BigInteger.valueOf(Short.MAX_VALUE)) <= 0) && (value.compareTo(BigInteger.valueOf(Short.MIN_VALUE)) >= 0); - } - - @Override - public short getShort() { - return value.shortValue(); - } - - @Override - public boolean isInteger() { - return (value != null) && (value.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) <= 0) && (value.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) >= 0); - } - - @Override - public int getInteger() { - return value.intValue(); - } - - @Override - public boolean isLong() { - return (value != null) && (value.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) <= 0) && (value.compareTo(BigInteger.valueOf(Long.MIN_VALUE)) >= 0); - } - - @Override - public long getLong() { - return value.longValue(); - } - - @Override - public boolean isBigInteger() { - return true; - } - - @Override - public BigInteger getBigInteger() { - return value; - } - - @Override - public boolean isFloat() { - return true; - } - - @Override - public float getFloat() { - return value.floatValue(); - } - - @Override - public boolean isDouble() { - return true; - } - - @Override - public double getDouble() { - return value.doubleValue(); - } - - @Override - public boolean isBigDecimal() { - return true; - } - - @Override - public BigDecimal getBigDecimal() { - return new BigDecimal(value); - } - - @Override - public boolean isString() { - return true; - } - - @Override - public String getString() { - return toString(); - } - - @Override - public String toString() { - return value.toString(); - } - - @Override - public byte[] getRaw() { - return getBytes(); - } - - public byte[] getBytes() { - byte[] tmp = value.toByteArray(); - byte[] bytes = new byte[8]; - for (int i = 0; i < bytes.length; i++) { - if (i >= (bytes.length - tmp.length)) { - bytes[i] = tmp[i - (bytes.length - tmp.length)]; - } else { - bytes[i] = 0x00; - } - } - return bytes; - } - - @Override - public void serialize(WriteBuffer writeBuffer) throws SerializationException { - writeBuffer.writeBigInteger(getClass().getSimpleName(), 64, value); - } - -} diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcUDINT.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcUDINT.java index e36e086a4ef..eea7f77b30f 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcUDINT.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcUDINT.java @@ -29,11 +29,13 @@ public class PlcUDINT extends PlcIECValue { private static final String VALUE_OUT_OF_RANGE = "Value of type %s is out of range %d - %d for a %s Value"; - static final Long minValue = (long) 0; - static final Long maxValue = (long) Integer.MAX_VALUE * 2 + 1; + public static final Long MIN_VALUE = (long) 0; + public static final Long MAX_VALUE = (long) Integer.MAX_VALUE * 2 + 1; public static PlcUDINT of(Object value) { - if (value instanceof Boolean) { + if (value instanceof PlcUDINT) { + return (PlcUDINT) value; + } else if (value instanceof Boolean) { return new PlcUDINT((Boolean) value); } else if (value instanceof Byte) { return new PlcUDINT((Byte) value); @@ -62,57 +64,57 @@ public PlcUDINT(Boolean value) { } public PlcUDINT(Byte value) { - if (value < minValue || value > maxValue) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if (value < MIN_VALUE || value > MAX_VALUE) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.longValue(); this.isNullable = false; } public PlcUDINT(Short value) { - if (value < minValue || value > maxValue) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if (value < MIN_VALUE || value > MAX_VALUE) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.longValue(); this.isNullable = false; } public PlcUDINT(Integer value) { - if (value < minValue || value > maxValue) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if (value < MIN_VALUE || value > MAX_VALUE) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.longValue(); this.isNullable = false; } public PlcUDINT(Long value) { - if (value < minValue || value > maxValue) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if (value < MIN_VALUE || value > MAX_VALUE) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value; this.isNullable = false; } public PlcUDINT(Float value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.longValue(); this.isNullable = false; } public PlcUDINT(Double value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.longValue(); this.isNullable = false; } public PlcUDINT(BigInteger value) { - if ((value.compareTo(BigInteger.valueOf(minValue)) < 0) || (value.compareTo(BigInteger.valueOf(maxValue)) > 0)) { + if ((value.compareTo(BigInteger.valueOf(MIN_VALUE)) < 0) || (value.compareTo(BigInteger.valueOf(MAX_VALUE)) > 0)) { throw new PlcInvalidTagException("Value of type " + value + - " is out of range " + minValue + " - " + maxValue + " for a " + + " is out of range " + MIN_VALUE + " - " + MAX_VALUE + " for a " + this.getClass().getSimpleName() + " Value"); } this.value = value.longValue(); @@ -120,8 +122,8 @@ public PlcUDINT(BigInteger value) { } public PlcUDINT(BigDecimal value) { - if ((value.compareTo(BigDecimal.valueOf(minValue)) < 0) || (value.compareTo(BigDecimal.valueOf(maxValue)) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value.compareTo(BigDecimal.valueOf(MIN_VALUE)) < 0) || (value.compareTo(BigDecimal.valueOf(MAX_VALUE)) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.longValue(); this.isNullable = true; @@ -130,20 +132,20 @@ public PlcUDINT(BigDecimal value) { public PlcUDINT(String value) { try { long val = Long.parseLong(value.trim()); - if (val >= minValue && val <= maxValue) { + if (val >= MIN_VALUE && val <= MAX_VALUE) { this.value = val; this.isNullable = false; } else { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } } catch (Exception e) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } } public PlcUDINT(long value) { - if (value < minValue || value > maxValue) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if (value < MIN_VALUE || value > MAX_VALUE) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value; this.isNullable = false; diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcUINT.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcUINT.java index 5d9eebceb58..3615653d951 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcUINT.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcUINT.java @@ -29,11 +29,13 @@ public class PlcUINT extends PlcIECValue { private static final String VALUE_OUT_OF_RANGE = "Value of type %s is out of range %d - %d for a %s Value"; - static final Integer minValue = 0; - static final Integer maxValue = Short.MAX_VALUE * 2 + 1; + public static final Integer MIN_VALUE = 0; + public static final Integer MAX_VALUE = Short.MAX_VALUE * 2 + 1; public static PlcUINT of(Object value) { - if (value instanceof Boolean) { + if (value instanceof PlcUINT) { + return (PlcUINT) value; + } else if (value instanceof Boolean) { return new PlcUINT((Boolean) value); } else if (value instanceof Byte) { return new PlcUINT((Byte) value); @@ -67,56 +69,56 @@ public PlcUINT(Byte value) { } public PlcUINT(Short value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.intValue(); this.isNullable = false; } public PlcUINT(Integer value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value; this.isNullable = false; } public PlcUINT(Long value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.intValue(); this.isNullable = false; } public PlcUINT(Float value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.intValue(); this.isNullable = false; } public PlcUINT(Double value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.intValue(); this.isNullable = false; } public PlcUINT(BigInteger value) { - if ((value.compareTo(BigInteger.valueOf(minValue)) < 0) || (value.compareTo(BigInteger.valueOf(maxValue)) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value.compareTo(BigInteger.valueOf(MIN_VALUE)) < 0) || (value.compareTo(BigInteger.valueOf(MAX_VALUE)) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.intValue(); this.isNullable = true; } public PlcUINT(BigDecimal value) { - if ((value.compareTo(BigDecimal.valueOf(minValue)) < 0) || (value.compareTo(BigDecimal.valueOf(maxValue)) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value.compareTo(BigDecimal.valueOf(MIN_VALUE)) < 0) || (value.compareTo(BigDecimal.valueOf(MAX_VALUE)) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.intValue(); this.isNullable = true; @@ -125,19 +127,19 @@ public PlcUINT(BigDecimal value) { public PlcUINT(String value) { try { int val = Integer.parseInt(value.trim()); - if ((val < minValue) || (val > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((val < MIN_VALUE) || (val > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = val; this.isNullable = false; } catch (Exception e) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } } public PlcUINT(int value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value; this.isNullable = false; diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcULINT.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcULINT.java index ca7415baf19..479fa04316f 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcULINT.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcULINT.java @@ -29,11 +29,13 @@ public class PlcULINT extends PlcIECValue { private static final String VALUE_OUT_OF_RANGE = "Value of type %s is out of range %d - %d for a %s Value"; - static final BigInteger minValue = BigInteger.valueOf(0); - static final BigInteger maxValue = BigInteger.valueOf(Long.MAX_VALUE).multiply(BigInteger.valueOf(2)).add(BigInteger.valueOf(1)); + public static final BigInteger MIN_VALUE = BigInteger.valueOf(0); + public static final BigInteger MAX_VALUE = BigInteger.valueOf(Long.MAX_VALUE).multiply(BigInteger.valueOf(2)).add(BigInteger.valueOf(1)); public static PlcULINT of(Object value) { - if (value instanceof Boolean) { + if (value instanceof PlcULINT) { + return (PlcULINT) value; + } else if (value instanceof Boolean) { return new PlcULINT((Boolean) value); } else if (value instanceof Byte) { return new PlcULINT((Byte) value); @@ -63,8 +65,8 @@ public PlcULINT(Boolean value) { public PlcULINT(Byte value) { BigInteger val = BigInteger.valueOf(value); - if ((val.compareTo(minValue) < 0) || (val.compareTo(maxValue) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((val.compareTo(MIN_VALUE) < 0) || (val.compareTo(MAX_VALUE) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = val; this.isNullable = false; @@ -72,8 +74,8 @@ public PlcULINT(Byte value) { public PlcULINT(Short value) { BigInteger val = BigInteger.valueOf(value); - if ((val.compareTo(minValue) < 0) || (val.compareTo(maxValue) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((val.compareTo(MIN_VALUE) < 0) || (val.compareTo(MAX_VALUE) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = val; this.isNullable = false; @@ -81,8 +83,8 @@ public PlcULINT(Short value) { public PlcULINT(Integer value) { BigInteger val = BigInteger.valueOf(value); - if ((val.compareTo(minValue) < 0) || (val.compareTo(maxValue) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((val.compareTo(MIN_VALUE) < 0) || (val.compareTo(MAX_VALUE) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = val; this.isNullable = false; @@ -90,8 +92,8 @@ public PlcULINT(Integer value) { public PlcULINT(Long value) { BigInteger val = BigInteger.valueOf(value); - if ((val.compareTo(minValue) < 0) || (val.compareTo(maxValue) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((val.compareTo(MIN_VALUE) < 0) || (val.compareTo(MAX_VALUE) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = val; this.isNullable = false; @@ -100,32 +102,32 @@ public PlcULINT(Long value) { public PlcULINT(Float value) { try { BigInteger val = BigDecimal.valueOf(value).toBigInteger(); - if ((val.compareTo(minValue) < 0) || (val.compareTo(maxValue) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((val.compareTo(MIN_VALUE) < 0) || (val.compareTo(MAX_VALUE) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = val; this.isNullable = false; } catch (Exception e) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName()), e); + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName()), e); } } public PlcULINT(Double value) { try { BigInteger val = BigDecimal.valueOf(value).toBigInteger(); - if ((val.compareTo(minValue) < 0) || (val.compareTo(maxValue) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((val.compareTo(MIN_VALUE) < 0) || (val.compareTo(MAX_VALUE) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = val; this.isNullable = false; } catch (Exception e) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName()), e); + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName()), e); } } public PlcULINT(BigInteger value) { - if ((value.compareTo(minValue) < 0) || (value.compareTo(maxValue) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value.compareTo(MIN_VALUE) < 0) || (value.compareTo(MAX_VALUE) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value; this.isNullable = false; @@ -134,26 +136,26 @@ public PlcULINT(BigInteger value) { public PlcULINT(BigDecimal value) { try { BigInteger val = value.toBigInteger(); - if ((val.compareTo(minValue) < 0) || (val.compareTo(maxValue) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((val.compareTo(MIN_VALUE) < 0) || (val.compareTo(MAX_VALUE) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = val; this.isNullable = false; } catch (Exception e) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName()), e); + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName()), e); } } public PlcULINT(String value) { try { BigInteger val = new BigInteger(value.trim()); - if ((val.compareTo(minValue) < 0) || (val.compareTo(maxValue) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((val.compareTo(MIN_VALUE) < 0) || (val.compareTo(MAX_VALUE) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = val; this.isNullable = false; } catch (Exception e) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName()), e); + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName()), e); } } diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcUSINT.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcUSINT.java index 3ccfd168b63..c496ea832fc 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcUSINT.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcUSINT.java @@ -29,11 +29,13 @@ public class PlcUSINT extends PlcIECValue { private static final String VALUE_OUT_OF_RANGE = "Value of type %s is out of range %d - %d for a %s Value"; - static final Short minValue = 0; - static final Short maxValue = (short) Byte.MAX_VALUE * 2 + 1; + public static final Short MIN_VALUE = 0; + public static final Short MAX_VALUE = (short) Byte.MAX_VALUE * 2 + 1; public static PlcUSINT of(Object value) { - if (value instanceof Boolean) { + if (value instanceof PlcUSINT) { + return (PlcUSINT) value; + } else if (value instanceof Boolean) { return new PlcUSINT((Boolean) value); } else if (value instanceof Byte) { return new PlcUSINT((Byte) value); @@ -62,64 +64,64 @@ public PlcUSINT(Boolean value) { } public PlcUSINT(Byte value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.shortValue(); this.isNullable = false; } public PlcUSINT(Short value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value; this.isNullable = false; } public PlcUSINT(Integer value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.shortValue(); this.isNullable = false; } public PlcUSINT(Long value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.shortValue(); this.isNullable = false; } public PlcUSINT(Float value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.shortValue(); this.isNullable = false; } public PlcUSINT(Double value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.shortValue(); this.isNullable = false; } public PlcUSINT(BigInteger value) { - if ((value.compareTo(BigInteger.valueOf(minValue)) < 0) || (value.compareTo(BigInteger.valueOf(maxValue)) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value.compareTo(BigInteger.valueOf(MIN_VALUE)) < 0) || (value.compareTo(BigInteger.valueOf(MAX_VALUE)) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.shortValue(); this.isNullable = true; } public PlcUSINT(BigDecimal value) { - if ((value.compareTo(BigDecimal.valueOf(minValue)) < 0) || (value.compareTo(BigDecimal.valueOf(maxValue)) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value.compareTo(BigDecimal.valueOf(MIN_VALUE)) < 0) || (value.compareTo(BigDecimal.valueOf(MAX_VALUE)) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.shortValue(); this.isNullable = true; @@ -128,19 +130,19 @@ public PlcUSINT(BigDecimal value) { public PlcUSINT(String value) { try { short val = Short.parseShort(value.trim()); - if ((val < minValue) || (val > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((val < MIN_VALUE) || (val > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = val; this.isNullable = false; } catch (Exception e) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName()), e); + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName()), e); } } public PlcUSINT(short value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value; this.isNullable = false; diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcWCHAR.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcWCHAR.java index 7e3d448548c..f63eff5bd5d 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcWCHAR.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcWCHAR.java @@ -31,11 +31,13 @@ public class PlcWCHAR extends PlcIECValue { private static final String VALUE_OUT_OF_RANGE = "Value of type %s is out of range %d - %d for a %s Value"; - static final Integer minValue = 0; - static final Integer maxValue = Short.MAX_VALUE * 2 + 1; + public static final Integer MIN_VALUE = 0; + public static final Integer MAX_VALUE = Short.MAX_VALUE * 2 + 1; public static PlcWCHAR of(Object value) { - if (value instanceof Boolean) { + if (value instanceof PlcWCHAR) { + return (PlcWCHAR) value; + } else if (value instanceof Boolean) { return new PlcWCHAR((Boolean) value); } else if (value instanceof Byte) { return new PlcWCHAR((Byte) value); @@ -70,32 +72,32 @@ public PlcWCHAR(Byte value) { public PlcWCHAR(Character value) { int val = (int) value; - if ((val < minValue) || (val > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((val < MIN_VALUE) || (val > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = val; this.isNullable = false; } public PlcWCHAR(Short value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.intValue(); this.isNullable = false; } public PlcWCHAR(Integer value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value; this.isNullable = false; } public PlcWCHAR(Long value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } else { this.value = value.intValue(); this.isNullable = false; @@ -103,32 +105,32 @@ public PlcWCHAR(Long value) { } public PlcWCHAR(Float value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.intValue(); this.isNullable = false; } public PlcWCHAR(Double value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.intValue(); this.isNullable = false; } public PlcWCHAR(BigInteger value) { - if ((value.compareTo(BigInteger.valueOf(minValue)) < 0) || (value.compareTo(BigInteger.valueOf(maxValue)) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value.compareTo(BigInteger.valueOf(MIN_VALUE)) < 0) || (value.compareTo(BigInteger.valueOf(MAX_VALUE)) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.intValue(); this.isNullable = true; } public PlcWCHAR(BigDecimal value) { - if ((value.compareTo(BigDecimal.valueOf(minValue)) < 0) || (value.compareTo(BigDecimal.valueOf(maxValue)) > 0) || (value.scale() > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value.compareTo(BigDecimal.valueOf(MIN_VALUE)) < 0) || (value.compareTo(BigDecimal.valueOf(MAX_VALUE)) > 0) || (value.scale() > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.intValue(); this.isNullable = true; @@ -142,19 +144,19 @@ public PlcWCHAR(String value) { s = " "; } int val = (int) s.charAt(0); - if ((val < minValue) || (val > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((val < MIN_VALUE) || (val > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = val; this.isNullable = false; } catch (Exception e) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName()), e); + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName()), e); } } public PlcWCHAR(int value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value; this.isNullable = false; diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcWORD.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcWORD.java index 4558ea58007..d47e302907d 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcWORD.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcWORD.java @@ -30,11 +30,13 @@ public class PlcWORD extends PlcIECValue { private static final String VALUE_OUT_OF_RANGE = "Value of type %s is out of range %d - %d for a %s Value"; - static final Integer minValue = 0; - static final Integer maxValue = Short.MAX_VALUE * 2 + 1; + public static final Integer MIN_VALUE = 0; + public static final Integer MAX_VALUE = Short.MAX_VALUE * 2 + 1; public static PlcWORD of(Object value) { - if (value instanceof Boolean) { + if (value instanceof PlcWORD) { + return (PlcWORD) value; + } else if (value instanceof Boolean) { return new PlcWORD((Boolean) value); } else if (value instanceof Byte) { return new PlcWORD((Byte) value); @@ -68,56 +70,56 @@ public PlcWORD(Byte value) { } public PlcWORD(Short value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.intValue(); this.isNullable = false; } public PlcWORD(Integer value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value; this.isNullable = false; } public PlcWORD(Long value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.intValue(); this.isNullable = false; } public PlcWORD(Float value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.intValue(); this.isNullable = false; } public PlcWORD(Double value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.intValue(); this.isNullable = false; } public PlcWORD(BigInteger value) { - if ((value.compareTo(BigInteger.valueOf(minValue)) < 0) || (value.compareTo(BigInteger.valueOf(maxValue)) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value.compareTo(BigInteger.valueOf(MIN_VALUE)) < 0) || (value.compareTo(BigInteger.valueOf(MAX_VALUE)) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.intValue(); this.isNullable = true; } public PlcWORD(BigDecimal value) { - if ((value.compareTo(BigDecimal.valueOf(minValue)) < 0) || (value.compareTo(BigDecimal.valueOf(maxValue)) > 0)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value.compareTo(BigDecimal.valueOf(MIN_VALUE)) < 0) || (value.compareTo(BigDecimal.valueOf(MAX_VALUE)) > 0)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value.intValue(); this.isNullable = true; @@ -126,19 +128,19 @@ public PlcWORD(BigDecimal value) { public PlcWORD(String value) { try { int val = Integer.parseInt(value.trim()); - if ((val < minValue) || (val > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((val < MIN_VALUE) || (val > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = val; this.isNullable = false; } catch (Exception e) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName()), e); + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName()), e); } } public PlcWORD(int value) { - if ((value < minValue) || (value > maxValue)) { - throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName())); + if ((value < MIN_VALUE) || (value > MAX_VALUE)) { + throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, MIN_VALUE, MAX_VALUE, this.getClass().getSimpleName())); } this.value = value; this.isNullable = false; diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcWSTRING.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcWSTRING.java index 25d25c80567..5cb769546d7 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcWSTRING.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/values/PlcWSTRING.java @@ -30,7 +30,9 @@ public class PlcWSTRING extends PlcIECValue { public static PlcWSTRING of(Object value) { - if (value instanceof String) { + if (value instanceof PlcWSTRING) { + return (PlcWSTRING) value; + } else if (value instanceof String) { return new PlcWSTRING((String) value); } return new PlcWSTRING(String.valueOf(value)); @@ -201,7 +203,7 @@ public int getLength() { @Override public String toString() { - return "\"" + value + "\""; + return value; } @Override diff --git a/plc4j/spi/src/test/java/org/apache/plc4x/java/spi/connection/DefaultNettyPlcConnectionTest.java b/plc4j/spi/src/test/java/org/apache/plc4x/java/spi/connection/DefaultNettyPlcConnectionTest.java index 8fea8da1b17..66d410ef032 100644 --- a/plc4j/spi/src/test/java/org/apache/plc4x/java/spi/connection/DefaultNettyPlcConnectionTest.java +++ b/plc4j/spi/src/test/java/org/apache/plc4x/java/spi/connection/DefaultNettyPlcConnectionTest.java @@ -161,6 +161,11 @@ public TestProtocolBase(GateKeeper discover, GateKeeper connect, GateKeeper disc this.disconnect = disconnect; } + @Override + public PlcTagHandler getTagHandler() { + return null; + } + @Override public void onDiscover(ConversationContext context) { logger.info("On Discover"); diff --git a/plc4j/spi/src/test/java/org/apache/plc4x/java/spi/connection/PlcConnectionFactory.java b/plc4j/spi/src/test/java/org/apache/plc4x/java/spi/connection/PlcConnectionFactory.java index 6af52531935..ff65f434790 100644 --- a/plc4j/spi/src/test/java/org/apache/plc4x/java/spi/connection/PlcConnectionFactory.java +++ b/plc4j/spi/src/test/java/org/apache/plc4x/java/spi/connection/PlcConnectionFactory.java @@ -41,7 +41,7 @@ PlcConnectionFactory doNotAwaitForDisconnect() { DefaultNettyPlcConnection create(ChannelFactory channelFactory, ProtocolStackConfigurer stackConfigurer) { return new DefaultNettyPlcConnection( true, true, true, true, true, - null, null, null, channelFactory, + null, null, channelFactory, fireDiscovery, // force discovery true, // await setup awaitDisconnect, // await disconnect diff --git a/plc4j/spi/src/test/java/org/apache/plc4x/java/spi/connection/TestProtocol.java b/plc4j/spi/src/test/java/org/apache/plc4x/java/spi/connection/TestProtocol.java index 59f4f94950b..ed2d7abc98b 100644 --- a/plc4j/spi/src/test/java/org/apache/plc4x/java/spi/connection/TestProtocol.java +++ b/plc4j/spi/src/test/java/org/apache/plc4x/java/spi/connection/TestProtocol.java @@ -25,6 +25,11 @@ public class TestProtocol extends Plc4xProtocolBase { + @Override + public PlcTagHandler getTagHandler() { + return null; + } + @Override public void onDiscover(ConversationContext context) { context.fireDiscovered(null); diff --git a/plc4j/spi/src/test/java/org/apache/plc4x/java/spi/values/DefaultPlcValueHandlerTest.java b/plc4j/spi/src/test/java/org/apache/plc4x/java/spi/values/DefaultPlcValueHandlerTest.java index a3357a9c838..8edce84a327 100644 --- a/plc4j/spi/src/test/java/org/apache/plc4x/java/spi/values/DefaultPlcValueHandlerTest.java +++ b/plc4j/spi/src/test/java/org/apache/plc4x/java/spi/values/DefaultPlcValueHandlerTest.java @@ -28,6 +28,7 @@ import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import java.lang.reflect.Field; import java.math.BigDecimal; import java.math.BigInteger; import java.time.Duration; @@ -358,7 +359,7 @@ private static Stream getSingleElementPlcValues() { Arguments.of(new MockTag("mock", PlcValueType.LDATE), LocalDate.ofEpochDay(1234), new PlcLDATE(LocalDate.ofEpochDay(1234))), Arguments.of(new MockTag("mock", PlcValueType.LDATE), (byte) 123, new PlcLDATE(LocalDate.parse("1970-01-01"))), Arguments.of(new MockTag("mock", PlcValueType.LDATE), (short) 12345, new PlcLDATE(LocalDate.parse("1970-01-01"))), - Arguments.of(new MockTag("mock", PlcValueType.LDATE), 1234567890, new PlcLDATE(LocalDate.parse("2009-02-14"))), + Arguments.of(new MockTag("mock", PlcValueType.LDATE), 1234567890, new PlcLDATE(LocalDate.parse("2009-02-13"))), Arguments.of(new MockTag("mock", PlcValueType.LDATE), 12345678901L, new PlcLDATE(LocalDate.parse("2361-03-21"))), Arguments.of(new MockTag("mock", PlcValueType.LDATE), (float) 123456.56, new PlcLDATE(LocalDate.parse("1970-01-02"))), Arguments.of(new MockTag("mock", PlcValueType.LDATE), (double) 12345678.9, new PlcLDATE(LocalDate.parse("1970-05-23"))), @@ -392,26 +393,26 @@ private static Stream getSingleElementPlcValues() { // DATE_AND_TIME values (Numeric values are interpreted as seconds since epoch) Arguments.of(new MockTag("mock", PlcValueType.DATE_AND_TIME), LocalDateTime.ofEpochSecond(1234, 0, OffsetDateTime.now().getOffset()), new PlcDATE_AND_TIME(LocalDateTime.parse("1970-01-01T02:20:34"))), - Arguments.of(new MockTag("mock", PlcValueType.DATE_AND_TIME), (byte) 123, new PlcDATE_AND_TIME(LocalDateTime.parse("1970-01-01T02:02:03"))), - Arguments.of(new MockTag("mock", PlcValueType.DATE_AND_TIME), (short) 12345, new PlcDATE_AND_TIME(LocalDateTime.parse("1970-01-01T05:25:45"))), - Arguments.of(new MockTag("mock", PlcValueType.DATE_AND_TIME), 12345678, new PlcDATE_AND_TIME(LocalDateTime.parse("1970-05-23T23:21:18"))), - Arguments.of(new MockTag("mock", PlcValueType.DATE_AND_TIME), 12345678L, new PlcDATE_AND_TIME(LocalDateTime.parse("1970-05-23T23:21:18"))), - Arguments.of(new MockTag("mock", PlcValueType.DATE_AND_TIME), (float) 123456.56, new PlcDATE_AND_TIME(LocalDateTime.parse("1970-01-02T12:17:36"))), - Arguments.of(new MockTag("mock", PlcValueType.DATE_AND_TIME), (double) 12345678.9, new PlcDATE_AND_TIME(LocalDateTime.parse("1970-05-23T23:21:18"))), - Arguments.of(new MockTag("mock", PlcValueType.DATE_AND_TIME), BigInteger.valueOf(12345678L), new PlcDATE_AND_TIME(LocalDateTime.parse("1970-05-23T23:21:18"))), - Arguments.of(new MockTag("mock", PlcValueType.DATE_AND_TIME), BigDecimal.valueOf(12345678L), new PlcDATE_AND_TIME(LocalDateTime.parse("1970-05-23T23:21:18"))), + Arguments.of(new MockTag("mock", PlcValueType.DATE_AND_TIME), (byte) 123, new PlcDATE_AND_TIME(LocalDateTime.parse("1970-01-01T00:02:03"))), + Arguments.of(new MockTag("mock", PlcValueType.DATE_AND_TIME), (short) 12345, new PlcDATE_AND_TIME(LocalDateTime.parse("1970-01-01T03:25:45"))), + Arguments.of(new MockTag("mock", PlcValueType.DATE_AND_TIME), 12345678, new PlcDATE_AND_TIME(LocalDateTime.parse("1970-05-23T21:21:18"))), + Arguments.of(new MockTag("mock", PlcValueType.DATE_AND_TIME), 12345678L, new PlcDATE_AND_TIME(LocalDateTime.parse("1970-05-23T21:21:18"))), + Arguments.of(new MockTag("mock", PlcValueType.DATE_AND_TIME), (float) 123456.56, new PlcDATE_AND_TIME(LocalDateTime.parse("1970-01-02T10:17:36"))), + Arguments.of(new MockTag("mock", PlcValueType.DATE_AND_TIME), (double) 12345678.9, new PlcDATE_AND_TIME(LocalDateTime.parse("1970-05-23T21:21:18"))), + Arguments.of(new MockTag("mock", PlcValueType.DATE_AND_TIME), BigInteger.valueOf(12345678L), new PlcDATE_AND_TIME(LocalDateTime.parse("1970-05-23T21:21:18"))), + Arguments.of(new MockTag("mock", PlcValueType.DATE_AND_TIME), BigDecimal.valueOf(12345678L), new PlcDATE_AND_TIME(LocalDateTime.parse("1970-05-23T21:21:18"))), Arguments.of(new MockTag("mock", PlcValueType.DATE_AND_TIME), "1978-03-28T12:34:56", new PlcDATE_AND_TIME(LocalDateTime.parse("1978-03-28T12:34:56"))), // DATE_AND_LTIME values (Numeric values are interpreted as milliseconds since epoch) Arguments.of(new MockTag("mock", PlcValueType.DATE_AND_LTIME), LocalDateTime.ofEpochSecond(1234, 5678, OffsetDateTime.now().getOffset()), new PlcDATE_AND_LTIME(LocalDateTime.parse("1970-01-01T02:20:34.000005678"))), - Arguments.of(new MockTag("mock", PlcValueType.DATE_AND_LTIME), (byte) 123, new PlcDATE_AND_LTIME(LocalDateTime.parse("1970-01-01T00:00:00.123"))), - Arguments.of(new MockTag("mock", PlcValueType.DATE_AND_LTIME), (short) 12345, new PlcDATE_AND_LTIME(LocalDateTime.parse("1970-01-01T00:00:12.345"))), - Arguments.of(new MockTag("mock", PlcValueType.DATE_AND_LTIME), 1234567890, new PlcDATE_AND_LTIME(LocalDateTime.parse("1970-01-15T06:56:07.890"))), - Arguments.of(new MockTag("mock", PlcValueType.DATE_AND_LTIME), 12345678901L, new PlcDATE_AND_LTIME(LocalDateTime.parse("1970-05-23T21:21:18.901"))), - Arguments.of(new MockTag("mock", PlcValueType.DATE_AND_LTIME), (float) 123456.56, new PlcDATE_AND_LTIME(LocalDateTime.parse("1970-01-01T00:02:03.456"))), - Arguments.of(new MockTag("mock", PlcValueType.DATE_AND_LTIME), (double) 12345678.9, new PlcDATE_AND_LTIME(LocalDateTime.parse("1970-01-01T03:25:45.678"))), - Arguments.of(new MockTag("mock", PlcValueType.DATE_AND_LTIME), BigInteger.valueOf(12345678901L), new PlcDATE_AND_LTIME(LocalDateTime.parse("1970-05-23T21:21:18.901"))), - Arguments.of(new MockTag("mock", PlcValueType.DATE_AND_LTIME), BigDecimal.valueOf(12345678901L), new PlcDATE_AND_LTIME(LocalDateTime.parse("1970-05-23T21:21:18.901"))), + Arguments.of(new MockTag("mock", PlcValueType.DATE_AND_LTIME), (byte) 123, new PlcDATE_AND_LTIME(LocalDateTime.parse("1970-01-01T00:00:00.000000123"))), + Arguments.of(new MockTag("mock", PlcValueType.DATE_AND_LTIME), (short) 12345, new PlcDATE_AND_LTIME(LocalDateTime.parse("1970-01-01T00:00:00.000012345"))), + Arguments.of(new MockTag("mock", PlcValueType.DATE_AND_LTIME), 1234567890, new PlcDATE_AND_LTIME(LocalDateTime.parse("1970-01-01T00:00:01.234567890"))), + Arguments.of(new MockTag("mock", PlcValueType.DATE_AND_LTIME), 12345678901L, new PlcDATE_AND_LTIME(LocalDateTime.parse("1970-01-01T00:00:12.345678901"))), + Arguments.of(new MockTag("mock", PlcValueType.DATE_AND_LTIME), (float) 123456.56, new PlcDATE_AND_LTIME(LocalDateTime.parse("1970-01-01T00:00:00.000123456"))), + Arguments.of(new MockTag("mock", PlcValueType.DATE_AND_LTIME), (double) 12345678.9, new PlcDATE_AND_LTIME(LocalDateTime.parse("1970-01-01T00:00:00.012345678"))), + Arguments.of(new MockTag("mock", PlcValueType.DATE_AND_LTIME), BigInteger.valueOf(12345678901L), new PlcDATE_AND_LTIME(LocalDateTime.parse("1970-01-01T00:00:12.345678901"))), + Arguments.of(new MockTag("mock", PlcValueType.DATE_AND_LTIME), BigDecimal.valueOf(12345678901L), new PlcDATE_AND_LTIME(LocalDateTime.parse("1970-01-01T00:00:12.345678901"))), Arguments.of(new MockTag("mock", PlcValueType.DATE_AND_LTIME), "1978-03-28T01:02:03", new PlcDATE_AND_LTIME(LocalDateTime.parse("1978-03-28T01:02:03"))), // LDATE_AND_TIME values @@ -433,7 +434,7 @@ private static Stream getSingleElementPlcValues() { @ParameterizedTest @MethodSource("getSingleElementPlcValues") - void newPlcValue(PlcTag plcTag, Object input, PlcValue expected) { + void testSingleElementPlcValues(PlcTag plcTag, Object input, PlcValue expected) { PlcValueHandler sut = new DefaultPlcValueHandler(); PlcValue plcValue = sut.newPlcValue(plcTag, input); @@ -442,6 +443,88 @@ void newPlcValue(PlcTag plcTag, Object input, PlcValue expected) { assertEquals(expected, plcValue); } + private static Stream getPlcValueTypesRanges() { + return Stream.of( + Arguments.of(PlcBYTE.class), + Arguments.of(PlcWORD.class), + Arguments.of(PlcDWORD.class), + // TODO: Need to find out how to test these ... + //Arguments.of(PlcLWORD.class), + Arguments.of(PlcUSINT.class), + Arguments.of(PlcUINT.class), + Arguments.of(PlcUDINT.class), + // TODO: Need to find out how to test these ... + //Arguments.of(PlcULINT.class), + Arguments.of(PlcSINT.class), + Arguments.of(PlcINT.class), + Arguments.of(PlcDINT.class), + // TODO: Need to find out how to test these ... + //Arguments.of(PlcLINT.class), + //Arguments.of(PlcREAL.class), + //Arguments.of(PlcLREAL.class), + Arguments.of(PlcCHAR.class), + Arguments.of(PlcWCHAR.class) + ); + } + + @ParameterizedTest + @MethodSource("getPlcValueTypesRanges") + void testPlcValueTypesRanges(Class plcValueType) throws Exception { + Field minValueField = plcValueType.getField("MIN_VALUE"); + Number minValue = (Number) minValueField.get(null); + Field maxValueField = plcValueType.getField("MAX_VALUE"); + Number maxValue = (Number) maxValueField.get(null); + + // Set value to one less than the lower bound (Should fail) + try { + plcValueType.getConstructor(Long.class).newInstance(minValue.longValue() - 1); + fail("Set value to one less than the lower bound should have failed"); + } catch (Exception e) { + // We want an exception here ... + } + + // Set value to exactly the lower bound (Should succeed) + try { + PlcValue plcValue = plcValueType.getConstructor(Long.class).newInstance(minValue.longValue()); + assertEquals(minValue.longValue(), plcValue.getLong()); + } catch (Exception e) { + fail("Set value to exactly the lower bound should have succeeded"); + } + + // Set value to one more than the lower bound (Should succeed) + try { + PlcValue plcValue = plcValueType.getConstructor(Long.class).newInstance(minValue.longValue() + 1); + assertEquals(minValue.longValue() + 1, plcValue.getLong()); + } catch (Exception e) { + fail("Set value to one more than the lower bound should have succeeded"); + } + + // Set value to one less than the upper bound (Should succeed) + try { + PlcValue plcValue = plcValueType.getConstructor(Long.class).newInstance(maxValue.longValue() - 1); + assertEquals(maxValue.longValue() - 1, plcValue.getLong()); + } catch (Exception e) { + fail("Set value to one less than the upper bound should have succeeded"); + } + + // Set value to exactly the upper bound (Should succeed) + try { + PlcValue plcValue = plcValueType.getConstructor(Long.class).newInstance(maxValue.longValue()); + assertEquals(maxValue.longValue(), plcValue.getLong()); + } catch (Exception e) { + fail("Set value to exactly the upper bound should have succeeded"); + } + + // Set value to one more than the upper bound (Should fail) + try { + plcValueType.getConstructor(Long.class).newInstance(maxValue.longValue() + 1); + fail("Set value to one more than the upper bound should have failed"); + } catch (Exception e) { + // We want an exception here ... + } + } + + @Test void newPlcValues() { } diff --git a/plc4j/utils/test-utils/src/main/java/org/apache/plc4x/test/manual/ManualTest.java b/plc4j/utils/test-utils/src/main/java/org/apache/plc4x/test/manual/ManualTest.java index 5126b67a990..d0ece31aca0 100644 --- a/plc4j/utils/test-utils/src/main/java/org/apache/plc4x/test/manual/ManualTest.java +++ b/plc4j/utils/test-utils/src/main/java/org/apache/plc4x/test/manual/ManualTest.java @@ -29,7 +29,6 @@ import org.apache.plc4x.java.spi.values.PlcList; import org.apache.plc4x.java.spi.values.PlcRawByteArray; import org.apache.plc4x.java.spi.values.PlcStruct; -import org.apache.plc4x.java.spi.values.PlcValues; import org.junit.jupiter.api.Assertions; import java.util.ArrayList; @@ -145,16 +144,10 @@ public void run() throws Exception { // Try writing the value to the PLC. if (testWrite) { System.out.println(" - Writing: " + tagName); - PlcValue plcValue; - if (testCase.expectedReadValue instanceof PlcValue) { - plcValue = ((PlcValue) testCase.expectedReadValue); - } else { - plcValue = PlcValues.of(testCase.expectedReadValue); - } // Prepare the write request PlcWriteRequest writeRequest = plcConnection.writeRequestBuilder().addTagAddress( - tagName, testCase.address, plcValue).build(); + tagName, testCase.address, testCase.expectedReadValue).build(); // Execute the write request long startTime = System.currentTimeMillis();