Skip to content

Commit 0edb45b

Browse files
committed
Update CAN bus interactions to match new plc4x 0.13 API behavior.
Signed-off-by: Łukasz Dywicki <[email protected]>
1 parent 8924850 commit 0edb45b

File tree

10 files changed

+92
-83
lines changed

10 files changed

+92
-83
lines changed

bundles/org.connectorio.addons.binding.canbus/src/main/java/org/connectorio/addons/binding/canbus/internal/DefaultCanConnection.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.apache.plc4x.java.api.value.PlcValue;
1414
import org.apache.plc4x.java.can.generic.tag.GenericCANTag;
1515
import org.apache.plc4x.java.genericcan.readwrite.GenericCANDataType;
16+
import org.apache.plc4x.java.spi.values.PlcRawByteArray;
1617
import org.apache.plc4x.java.spi.values.PlcUSINT;
1718
import org.apache.plc4x.java.spi.values.PlcValues;
1819
import org.connectorio.addons.binding.canbus.CanConnection;
@@ -35,12 +36,7 @@ public CompletableFuture<Void> write(int cob, byte[] data) {
3536
}
3637

3738
private PlcValue encode(byte[] data) {
38-
List<PlcUSINT> values = new ArrayList<>();
39-
for (byte b : data) {
40-
values.add(new PlcUSINT(b));
41-
}
42-
43-
return PlcValues.of(values);
39+
return new PlcRawByteArray(data);
4440
}
4541

4642
@Override

bundles/org.connectorio.addons.binding.canbus/src/main/java/org/connectorio/addons/binding/canbus/internal/discovery/CANInterfaceDiscoveryDelegate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
/**
4242
* Basic discovery service which rely on network API to discover CAN interface.
4343
*/
44-
@Component(service = {DiscoveryService.class, NetworkInterfaceStateCallback.class})
44+
@Component(service = {DiscoveryService.class, NetworkInterfaceStateCallback.class}, immediate = true)
4545
public class CANInterfaceDiscoveryDelegate extends AbstractDiscoveryService implements DiscoveryService,
4646
NetworkInterfaceStateCallback {
4747

bundles/org.connectorio.addons.binding.canopen.ta/src/main/java/org/connectorio/addons/binding/canopen/ta/internal/handler/protocol/TAOperations.java

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.apache.plc4x.java.spi.generation.ParseException;
4949
import org.apache.plc4x.java.spi.generation.ReadBuffer;
5050
import org.apache.plc4x.java.spi.generation.ReadBufferByteBased;
51+
import org.apache.plc4x.java.spi.values.PlcRawByteArray;
5152
import org.apache.plc4x.java.spi.values.PlcUSINT;
5253
import org.apache.plc4x.java.spi.values.PlcValues;
5354
import org.connectorio.addons.binding.canopen.ta.internal.type.TAString;
@@ -144,31 +145,31 @@ public CompletableFuture<?> reload(int nodeId) {
144145
}
145146

146147
public CompletableFuture<? extends PlcWriteResponse> login(int nodeId, int clientId) {
147-
return connection.writeRequestBuilder().addTag("mpdo", new CANOpenPDOTag(clientId, CANOpenService.RECEIVE_PDO_3, CANOpenDataType.RECORD), PlcValues.of(
148-
new PlcUSINT((0x80 + nodeId)),
149-
new PlcUSINT(0x00),
150-
new PlcUSINT(0x1F),
151-
new PlcUSINT(0x00),
152-
new PlcUSINT(nodeId),
153-
new PlcUSINT(clientId),
154-
new PlcUSINT(0x80),
155-
new PlcUSINT(0x12)
156-
)).build().execute().whenComplete((response, error) -> {
148+
return connection.writeRequestBuilder().addTag("mpdo", new CANOpenPDOTag(clientId, CANOpenService.RECEIVE_PDO_3, CANOpenDataType.RECORD), new PlcRawByteArray(new byte[] {
149+
(byte) (0x80 + nodeId),
150+
(byte) 0x00,
151+
(byte) 0x1F,
152+
(byte) 0x00,
153+
(byte) nodeId,
154+
(byte) clientId,
155+
(byte) 0x80,
156+
(byte) 0x12
157+
})).build().execute().whenComplete((response, error) -> {
157158
logger.debug("Dispatched login request to node {}", nodeId, error);
158159
});
159160
}
160161

161162
public CompletableFuture<? extends PlcWriteResponse> logout(int nodeId, int clientId) {
162-
return connection.writeRequestBuilder().addTagAddress("mpdo", "RECEIVE_PDO_3:" + clientId + ":RECORD", PlcValues.of(
163-
new PlcUSINT((0x80 + nodeId)),
164-
new PlcUSINT(0x01),
165-
new PlcUSINT(0x1F),
166-
new PlcUSINT(0x00),
167-
new PlcUSINT(nodeId),
168-
new PlcUSINT(clientId),
169-
new PlcUSINT(0x80),
170-
new PlcUSINT(0x12)
171-
)).build().execute().whenComplete((response, error) -> {
163+
return connection.writeRequestBuilder().addTagAddress("mpdo", "RECEIVE_PDO_3:" + clientId + ":RECORD", new PlcRawByteArray(new byte[]{
164+
(byte) (0x80 + nodeId),
165+
(byte) 0x01,
166+
(byte) 0x1F,
167+
(byte) 0x00,
168+
(byte) nodeId,
169+
(byte) clientId,
170+
(byte) 0x80,
171+
(byte) 0x12
172+
})).build().execute().whenComplete((response, error) -> {
172173
logger.debug("Dispatched logout request to node {}", nodeId, error);
173174
});
174175
}

bundles/org.connectorio.addons.binding.canopen.ta/src/main/java/org/connectorio/addons/binding/canopen/ta/tapi/dev/TADevice.java

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.function.Function;
3232
import org.apache.commons.codec.binary.Hex;
3333
import org.apache.plc4x.java.canopen.readwrite.CANOpenService;
34+
import org.apache.plc4x.java.spi.values.PlcRawByteArray;
3435
import org.apache.plc4x.java.spi.values.PlcUSINT;
3536
import org.apache.plc4x.java.spi.values.PlcValues;
3637
import org.connectorio.addons.binding.canopen.api.CoNode;
@@ -249,29 +250,29 @@ public void reload() {
249250
}
250251

251252
public void login() {
252-
node.getConnection().send(clientId, CANOpenService.RECEIVE_PDO_3, PlcValues.of(
253-
new PlcUSINT(0x80 + node.getNodeId()),
254-
new PlcUSINT(0x00),
255-
new PlcUSINT(0x1F),
256-
new PlcUSINT(0x00),
257-
new PlcUSINT(node.getNodeId()),
258-
new PlcUSINT(clientId),
259-
new PlcUSINT(0x80),
260-
new PlcUSINT(0x12)
261-
));
253+
node.getConnection().send(clientId, CANOpenService.RECEIVE_PDO_3, new PlcRawByteArray(new byte[] {
254+
(byte) (0x80 + node.getNodeId()),
255+
(byte) 0x00,
256+
(byte) 0x1F,
257+
(byte) 0x00,
258+
(byte) node.getNodeId(),
259+
(byte) clientId,
260+
(byte) 0x80,
261+
(byte) 0x12
262+
}));
262263
}
263264

264265
public void logout() {
265-
node.getConnection().send(clientId, CANOpenService.RECEIVE_PDO_3, PlcValues.of(
266-
new PlcUSINT(0x80 + node.getNodeId()),
267-
new PlcUSINT(0x01),
268-
new PlcUSINT(0x1F),
269-
new PlcUSINT(0x00),
270-
new PlcUSINT(node.getNodeId()),
271-
new PlcUSINT(clientId),
272-
new PlcUSINT(0x80),
273-
new PlcUSINT(0x12)
274-
));
266+
node.getConnection().send(clientId, CANOpenService.RECEIVE_PDO_3, new PlcRawByteArray(new byte[]{
267+
(byte) (0x80 + node.getNodeId()),
268+
(byte) 0x01,
269+
(byte) 0x1F,
270+
(byte) 0x00,
271+
(byte) node.getNodeId(),
272+
(byte) clientId,
273+
(byte) 0x80,
274+
(byte) 0x12
275+
}));
275276
}
276277

277278
public void close() {
@@ -423,16 +424,16 @@ protected void subscribe(int nodeId, CANOpenService service, Consumer<byte[]> co
423424

424425
private void reloadOutputs() {
425426
logger.info("Reload {} outputs.", node.getNodeId());
426-
node.getConnection().send(0, CANOpenService.TRANSMIT_PDO_4, PlcValues.of(
427-
new PlcUSINT(0x80 + node.getNodeId()),
428-
new PlcUSINT(0x01),
429-
new PlcUSINT(0x4e),
430-
new PlcUSINT(0x01),
431-
new PlcUSINT(0x01),
432-
new PlcUSINT(0x00),
433-
new PlcUSINT(0x00),
434-
new PlcUSINT(0x00)
435-
));
427+
node.getConnection().send(0, CANOpenService.TRANSMIT_PDO_4, new PlcRawByteArray(new byte[]{
428+
(byte) (0x80 + node.getNodeId()),
429+
(byte) 0x01,
430+
(byte) 0x4e,
431+
(byte) 0x01,
432+
(byte) 0x01,
433+
(byte) 0x00,
434+
(byte) 0x00,
435+
(byte) 0x00
436+
}));
436437
}
437438

438439
private void scanFunctions() {

bundles/org.connectorio.addons.binding.canopen.ta/src/main/java/org/connectorio/addons/binding/canopen/ta/tapi/dev/publisher/PublishingRunnable.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.plc4x.java.canopen.readwrite.CANOpenService;
2323
import org.apache.plc4x.java.spi.generation.WriteBuffer;
2424
import org.apache.plc4x.java.spi.generation.WriteBufferByteBased;
25+
import org.apache.plc4x.java.spi.values.PlcRawByteArray;
2526
import org.apache.plc4x.java.spi.values.PlcSINT;
2627
import org.apache.plc4x.java.spi.values.PlcValues;
2728
import org.connectorio.addons.binding.canopen.api.CoNode;
@@ -40,10 +41,7 @@ protected PublishingRunnable(CoNode node) {
4041
protected final void send(int nodeId, CANOpenService service, WriteBufferByteBased buffer) {
4142
byte[] data = buffer.getBytes();
4243
logger.trace("Send to node {} {} (cob {}) data: {}", nodeId, service, Integer.toHexString(service.getMin() + nodeId), Hex.encodeHexString(data));
43-
node.getConnection().send(nodeId, service, PlcValues.of(
44-
new PlcSINT(data[0]), new PlcSINT(data[1]), new PlcSINT(data[2]), new PlcSINT(data[3]),
45-
new PlcSINT(data[4]), new PlcSINT(data[5]), new PlcSINT(data[6]), new PlcSINT(data[7])
46-
));
44+
node.getConnection().send(nodeId, service, new PlcRawByteArray(data));
4745
}
4846

4947
protected boolean hasKeyInRange(Map<Integer, ?> elements, int start, int limit) {

bundles/org.connectorio.addons.binding.canopen/src/main/java/org/connectorio/addons/binding/canopen/internal/handler/CoPdoTransmitHandler.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.apache.plc4x.java.spi.generation.SerializationException;
3030
import org.apache.plc4x.java.spi.generation.WriteBuffer;
3131
import org.apache.plc4x.java.spi.generation.WriteBufferByteBased;
32+
import org.apache.plc4x.java.spi.values.PlcRawByteArray;
3233
import org.apache.plc4x.java.spi.values.PlcSINT;
3334
import org.apache.plc4x.java.spi.values.PlcValues;
3435
import org.connectorio.addons.binding.canopen.api.CoNode;
@@ -104,16 +105,7 @@ private void publish() {
104105
}
105106

106107
private PlcValue toPlcValue(byte[] data) {
107-
return PlcValues.of(
108-
new PlcSINT(data.length > 0 ? data[0] : 0),
109-
new PlcSINT(data.length > 1 ? data[1] : 0),
110-
new PlcSINT(data.length > 2 ? data[2] : 0),
111-
new PlcSINT(data.length > 3 ? data[3] : 0),
112-
new PlcSINT(data.length > 4 ? data[4] : 0),
113-
new PlcSINT(data.length > 5 ? data[5] : 0),
114-
new PlcSINT(data.length > 6 ? data[6] : 0),
115-
new PlcSINT(data.length > 7 ? data[7] : 0)
116-
);
108+
return new PlcRawByteArray(data);
117109
}
118110

119111
private void writeState(WriteBuffer buffer, CANOpenDataType type, Command command) throws SerializationException {

bundles/org.connectorio.addons.binding.canopen/src/main/java/org/connectorio/addons/binding/canopen/internal/handler/CoSocketCANBridgeHandler.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,24 @@ public void run() {
7676
config = getBridgeConfig().get();
7777
String nodeId = "nodeId=" + config.nodeId;
7878
String heartbeat = "&heartbeat=" + config.heartbeat;
79-
AbstractPlcConnection connection = (AbstractPlcConnection) driverManager
80-
.getConnectionManager().getConnection("canopen:socketcan://" + config.name + "?" + nodeId + heartbeat);
8179

82-
if (connection.isConnected()) {
80+
AbstractPlcConnection connection = null;
81+
ClassLoader tccl = Thread.currentThread().getContextClassLoader();
82+
try {
83+
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
84+
// List<Provider<Transport>> transports = ServiceLoader.load(Transport.class, getClass().getClassLoader()).stream().collect(Collectors.toList());
85+
// transports = ServiceLoader.load(Transport.class, SocketCANTransport.class.getClassLoader()).stream().collect(Collectors.toList());
86+
connection = (AbstractPlcConnection) driverManager.getConnectionManager().getConnection("canopen:socketcan://" + config.name + "?" + nodeId + heartbeat);
87+
} finally {
88+
Thread.currentThread().setContextClassLoader(tccl);
89+
}
90+
91+
if (connection != null && !connection.isConnected()) {
92+
// force connection attempt
93+
connection.connect();
94+
}
95+
96+
if (connection != null && connection.isConnected()) {
8397
updateStatus(ThingStatus.ONLINE);
8498
initializer.complete(connection);
8599
} else {
@@ -105,6 +119,15 @@ public void dispose() {
105119
if (statisticPoller != null) {
106120
statisticPoller.cancel(true);
107121
}
122+
initializer.thenAccept(connection -> {
123+
if (connection.isConnected()) {
124+
try {
125+
connection.close();
126+
} catch (Exception e) {
127+
logger.warn("Failure while closing connection", e);
128+
}
129+
}
130+
});
108131
}
109132

110133
@Override

bundles/org.connectorio.addons.binding.canopen/src/main/java/org/connectorio/addons/binding/canopen/internal/plc4x/CoRecordReader.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,11 @@ public CoRecordReader(String field) {
3333

3434
@Override
3535
protected byte[] extract(PlcReadResponse response, String field) {
36-
List<Byte> bytes = new ArrayList<>(response.getAllBytes(field));
37-
byte[] value = new byte[bytes.size()];
38-
for (int index = 0; index < bytes.size(); index++) {
39-
value[index] = bytes.get(index);
36+
Object bytes = response.getObject(field);
37+
if (bytes instanceof byte[]) {
38+
return (byte[]) bytes;
4039
}
41-
42-
return value;
40+
throw new IllegalArgumentException("Unexpected value, expected byte[] got " + (bytes != null ? bytes.getClass().getName() : "NULL"));
4341
}
4442

4543
}

bundles/org.connectorio.addons.network.can.dbus/src/main/java/org/connectorio/addons/network/can/dbus/internal/DBusCanNetworkInterfaceProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
import org.slf4j.Logger;
5656
import org.slf4j.LoggerFactory;
5757

58-
@Component(service = NetworkInterfaceProvider.class)
58+
@Component(service = NetworkInterfaceProvider.class, immediate = true)
5959
public class DBusCanNetworkInterfaceProvider implements NetworkInterfaceProvider, DBusSigHandler<PropertiesChanged> {
6060

6161
public static final String PROPERTIES_TYPE = "org.freedesktop.DBus.Properties";

parent/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
<compiler.source>11</compiler.source>
3838
<compiler.target>11</compiler.target>
3939

40-
<plc4x-extras.version>0.12.0</plc4x-extras.version>
41-
<bacnet4j-wrapper.version>1.3.0-beta1</bacnet4j-wrapper.version>
40+
<plc4x-extras.version>0.13.0-connectorio-1</plc4x-extras.version>
41+
<bacnet4j-wrapper.version>1.3.0-beta3</bacnet4j-wrapper.version>
4242

4343
<openhab.version>3.0.4</openhab.version>
4444
<thing4.version>3.0.0-alpha-4</thing4.version>

0 commit comments

Comments
 (0)