use of org.apache.plc4x.java.canopen.field.CANOpenPDOField in project plc4x by apache.
the class CANOpenProtocolLogic method write.
public CompletableFuture<PlcWriteResponse> write(PlcWriteRequest writeRequest) {
CompletableFuture<PlcWriteResponse> response = new CompletableFuture<>();
if (writeRequest.getFieldNames().size() != 1) {
response.completeExceptionally(new IllegalArgumentException("You can write only one field at the time"));
return response;
}
PlcField field = writeRequest.getFields().get(0);
if (!(field instanceof CANOpenField)) {
response.completeExceptionally(new IllegalArgumentException("Only CANOpenField instances are supported"));
return response;
}
if (field instanceof CANOpenSDOField) {
writeInternally((DefaultPlcWriteRequest) writeRequest, (CANOpenSDOField) field, response);
return response;
}
if (field instanceof CANOpenPDOField) {
writeInternally((DefaultPlcWriteRequest) writeRequest, (CANOpenPDOField) field, response);
return response;
}
response.completeExceptionally(new IllegalArgumentException("Only CANOpenSDOField instances are supported"));
return response;
}
use of org.apache.plc4x.java.canopen.field.CANOpenPDOField 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);
}
}
use of org.apache.plc4x.java.canopen.field.CANOpenPDOField in project plc4x by apache.
the class CANOpenProtocolLogic method subscribe.
@Override
public CompletableFuture<PlcSubscriptionResponse> subscribe(PlcSubscriptionRequest request) {
DefaultPlcSubscriptionRequest rq = (DefaultPlcSubscriptionRequest) request;
Map<String, ResponseItem<PlcSubscriptionHandle>> answers = new LinkedHashMap<>();
DefaultPlcSubscriptionResponse response = new DefaultPlcSubscriptionResponse(rq, answers);
for (String key : rq.getFieldNames()) {
DefaultPlcSubscriptionField subscription = (DefaultPlcSubscriptionField) rq.getField(key);
if (subscription.getPlcSubscriptionType() != PlcSubscriptionType.EVENT) {
answers.put(key, new ResponseItem<>(PlcResponseCode.UNSUPPORTED, null));
} else if ((subscription.getPlcField() instanceof CANOpenPDOField)) {
answers.put(key, new ResponseItem<>(PlcResponseCode.OK, new CANOpenSubscriptionHandle(this, key, (CANOpenPDOField) subscription.getPlcField())));
} else if ((subscription.getPlcField() instanceof CANOpenNMTField)) {
answers.put(key, new ResponseItem<>(PlcResponseCode.OK, new CANOpenSubscriptionHandle(this, key, (CANOpenNMTField) subscription.getPlcField())));
} else if ((subscription.getPlcField() instanceof CANOpenHeartbeatField)) {
answers.put(key, new ResponseItem<>(PlcResponseCode.OK, new CANOpenSubscriptionHandle(this, key, (CANOpenHeartbeatField) subscription.getPlcField())));
} else {
answers.put(key, new ResponseItem<>(PlcResponseCode.INVALID_ADDRESS, null));
}
}
return CompletableFuture.completedFuture(response);
}
Aggregations