use of org.apache.plc4x.java.canopen.readwrite.CANOpenPDOPayload in project plc4x by apache.
the class CANOpenProtocolLogic method writeInternally.
private void writeInternally(DefaultPlcWriteRequest writeRequest, CANOpenPDOField field, CompletableFuture<PlcWriteResponse> response) {
PlcValue writeValue = writeRequest.getPlcValues().get(0);
try {
String fieldName = writeRequest.getFieldNames().iterator().next();
WriteBufferByteBased writeBuffer = new WriteBufferByteBased(DataItem.getLengthInBytes(writeValue, field.getCanOpenDataType(), writeValue.getLength()), ByteOrder.LITTLE_ENDIAN);
DataItem.staticSerialize(writeBuffer, writeValue, field.getCanOpenDataType(), writeValue.getLength(), ByteOrder.LITTLE_ENDIAN);
final CANOpenPDOPayload payload = new CANOpenPDOPayload(new CANOpenPDO(writeBuffer.getData()));
context.sendToWire(new CANOpenFrame((short) field.getNodeId(), field.getService(), payload));
response.complete(new DefaultPlcWriteResponse(writeRequest, Collections.singletonMap(fieldName, PlcResponseCode.OK)));
} catch (Exception e) {
response.completeExceptionally(e);
}
}
use of org.apache.plc4x.java.canopen.readwrite.CANOpenPDOPayload in project plc4x by apache.
the class CANOpenProtocolLogic method decode.
@Override
public void decode(ConversationContext<CANOpenFrame> context, CANOpenFrame msg) throws Exception {
int nodeId = msg.getNodeId();
CANOpenService service = msg.getService();
CANOpenPayload payload = msg.getPayload();
if (service != null && nodeId != this.configuration.getNodeId()) {
if (service.getPdo() && payload instanceof CANOpenPDOPayload) {
publishEvent(service, nodeId, payload);
} else if (service == CANOpenService.HEARTBEAT && payload instanceof CANOpenHeartbeatPayload) {
publishEvent(service, nodeId, payload);
} else {
logger.debug("Decoded CANOpen {} from {}, message {}", service, nodeId, payload);
}
}
// int identifier = msg.getIdentifier();
// CANOpenService service = CANOpenService.valueOf((byte) (identifier >> 7));
// if (service != null) {
// ReadBuffer buffer = new ReadBuffer(msg.getData());
// CANOpenPayload payload = CANOpenPayloadIO.staticParse(buffer, service);
//
//
// }
}
use of org.apache.plc4x.java.canopen.readwrite.CANOpenPDOPayload in project plc4x by apache.
the class CANOpenProtocolLogic method publishEvent.
private void publishEvent(CANOpenService service, int nodeId, CANOpenPayload payload) {
DefaultPlcSubscriptionHandle dispatchedHandle = null;
for (Map.Entry<DefaultPlcConsumerRegistration, Consumer<PlcSubscriptionEvent>> entry : consumers.entrySet()) {
DefaultPlcConsumerRegistration registration = entry.getKey();
Consumer<PlcSubscriptionEvent> consumer = entry.getValue();
for (PlcSubscriptionHandle handler : registration.getSubscriptionHandles()) {
CANOpenSubscriptionHandle handle = (CANOpenSubscriptionHandle) handler;
if (payload instanceof CANOpenPDOPayload) {
if (handle.matches(service, nodeId)) {
logger.trace("Dispatching notification {} for node {} to {}", service, nodeId, handle);
dispatchedHandle = handle;
CANOpenPDOField field = (CANOpenPDOField) handle.getField();
byte[] data = ((CANOpenPDOPayload) payload).getPdo().getData();
try {
PlcValue value = DataItem.staticParse(new ReadBufferByteBased(data, ByteOrder.LITTLE_ENDIAN), field.getCanOpenDataType(), data.length);
DefaultPlcSubscriptionEvent event = new DefaultPlcSubscriptionEvent(Instant.now(), Collections.singletonMap(handle.getName(), new ResponseItem<>(PlcResponseCode.OK, value)));
consumer.accept(event);
} catch (ParseException e) {
logger.warn("Could not parse data to desired type: {}", field.getCanOpenDataType(), e);
DefaultPlcSubscriptionEvent event = new DefaultPlcSubscriptionEvent(Instant.now(), Collections.singletonMap(handle.getName(), new ResponseItem<>(PlcResponseCode.INVALID_DATA, new PlcNull())));
consumer.accept(event);
}
}
} else if (payload instanceof CANOpenHeartbeatPayload) {
if (handle.matches(service, nodeId)) {
logger.trace("Dispatching notification {} for node {} to {}", service, nodeId, handle);
dispatchedHandle = handle;
final NMTState state = ((CANOpenHeartbeatPayload) payload).getState();
Map<String, PlcValue> fields = new HashMap<>();
fields.put("state", new PlcUSINT(state.getValue()));
fields.put("node", new PlcUSINT(nodeId));
PlcStruct struct = new PlcStruct(fields);
DefaultPlcSubscriptionEvent event = new DefaultPlcSubscriptionEvent(Instant.now(), Collections.singletonMap(handle.getName(), new ResponseItem<>(PlcResponseCode.OK, struct)));
consumer.accept(event);
}
} else if (payload instanceof CANOpenNetworkPayload) {
if (handle.matches(service, nodeId)) {
logger.trace("Dispatching notification {} for node {} to {}", service, nodeId, handle);
dispatchedHandle = handle;
final NMTStateRequest state = ((CANOpenNetworkPayload) payload).getRequest();
Map<String, PlcValue> fields = new HashMap<>();
fields.put("state", new PlcUSINT(state.getValue()));
fields.put("node", new PlcUSINT(nodeId));
PlcStruct struct = new PlcStruct(fields);
DefaultPlcSubscriptionEvent event = new DefaultPlcSubscriptionEvent(Instant.now(), Collections.singletonMap(handle.getName(), new ResponseItem<>(PlcResponseCode.OK, struct)));
consumer.accept(event);
}
}
}
}
if (dispatchedHandle == null) {
logger.trace("Could not find subscription matching {} and node {}", service, nodeId);
}
}
Aggregations