Skip to content

Commit

Permalink
refactor: Moved the PlcTagHandler from the driver to the PlcConnectio…
Browse files Browse the repository at this point in the history
…n/Protocol so we can start making connection-aware tag-handlers in the future.
  • Loading branch information
chrisdutz committed Sep 29, 2024
1 parent b0bdbfe commit 76e6ef3
Show file tree
Hide file tree
Showing 111 changed files with 1,245 additions and 1,780 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -76,11 +75,6 @@ protected List<String> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<CIPEncapsulationPacket> context) {
tm.shutdown();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -115,11 +114,6 @@ protected List<String> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -113,6 +115,11 @@ public void setConfiguration(AdsConfiguration configuration) {
this.configuration = configuration;
}

@Override
public PlcTagHandler getTagHandler() {
return new AdsTagHandler();
}

@Override
public void onConnect(ConversationContext<AmsTCPPacket> context) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -93,11 +92,6 @@ protected boolean canSubscribe() {
return true;
}

@Override
protected BacNetIpTagHandler getTagHandler() {
return new BacNetIpTagHandler();
}

@Override
protected ProtocolStackConfigurer<BVLC> getStackConfigurer() {
return SingleProtocolStackConfigurer.builder(BVLC.class, BVLC::staticParse)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -85,6 +87,11 @@ public void setConfiguration(BacNetIpConfiguration configuration) {
}
}

@Override
public PlcTagHandler getTagHandler() {
return new BacNetIpTagHandler();
}

@Override
public void onConnect(ConversationContext<BVLC> context) {
if (context.isPassive()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -80,11 +79,6 @@ protected List<String> getSupportedTransportCodes() {
return Collections.singletonList("tcp");
}

@Override
protected PlcTagHandler getTagHandler() {
return null;
}

@Override
protected ProtocolStackConfigurer<CBusCommand> getStackConfigurer() {
return SingleProtocolStackConfigurer.builder(CBusCommand.class, io ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<CBusCommand> context) {
tm.shutdown();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -42,12 +43,14 @@ public class CANDriverAdapter<C, T> extends Plc4xProtocolBase<C> {
private final Class<C> wireType;
private final Function<C, FrameData> adapter;
private final FrameHandler<C, T> frameHandler;
private final PlcTagHandler tagHandler;

public CANDriverAdapter(Plc4xCANProtocolBase<T> delegate, Class<C> wireType, Function<C, FrameData> adapter, FrameHandler<C, T> frameHandler) {
public CANDriverAdapter(Plc4xCANProtocolBase<T> delegate, Class<C> wireType, Function<C, FrameData> adapter, FrameHandler<C, T> frameHandler, PlcTagHandler tagHandler) {
this.delegate = delegate;
this.wireType = wireType;
this.adapter = adapter;
this.frameHandler = frameHandler;
this.tagHandler = tagHandler;
}

@Override
Expand All @@ -65,6 +68,11 @@ public void setContext(ConversationContext<C> context) {
delegate.setContext(new ConversationContextWrapper<>(context, wireType, adapter, frameHandler, context.getAuthentication()));
}

@Override
public PlcTagHandler getTagHandler() {
return tagHandler;
}

@Override
public void onConnect(ConversationContext<C> context) {
delegate.onConnect(new ConversationContextWrapper<>(context, wireType, adapter, frameHandler, context.getAuthentication()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -116,7 +111,8 @@ protected ProtocolStackConfigurer<Message> 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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -72,6 +74,11 @@ public void setContext(ConversationContext<GenericFrame> context) {
super.setContext(context);
}

@Override
public PlcTagHandler getTagHandler() {
return new GenericCANTagHandler();
}

@Override
public void onConnect(ConversationContext<GenericFrame> context) {
context.fireConnected();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public PlcValueType getPlcValueType() {

@Override
public List<ArrayInfo> getArrayInfo() {
if(arraySize != 1) {
if(arraySize > 1) {
return Collections.singletonList(new DefaultArrayInfo(0, arraySize));
}
return Collections.emptyList();
Expand Down
Loading

0 comments on commit 76e6ef3

Please sign in to comment.