use of org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionHandle in project plc4x by apache.
the class CANOpenProtocolLogic method register.
@Override
public PlcConsumerRegistration register(Consumer<PlcSubscriptionEvent> consumer, Collection<PlcSubscriptionHandle> handles) {
final DefaultPlcConsumerRegistration consumerRegistration = new DefaultPlcConsumerRegistration(this, consumer, handles.toArray(new DefaultPlcSubscriptionHandle[0]));
consumers.put(consumerRegistration, consumer);
return consumerRegistration;
}
use of org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionHandle in project plc4x by apache.
the class SimulatedConnection method subscribe.
/**
* Blocking subscribe call
*
* @param subscriptionRequest subscription request containing at least one subscription request item.
* @return the {@code PlcSubscriptionResponse}
*/
@Override
public CompletableFuture<PlcSubscriptionResponse> subscribe(PlcSubscriptionRequest subscriptionRequest) {
LOGGER.info("subscribing {}", subscriptionRequest);
Map<String, ResponseItem<PlcSubscriptionHandle>> values = new HashMap<>();
subscriptionRequest.getFieldNames().forEach(name -> {
LOGGER.info("creating handle for field name {}", name);
PlcSubscriptionHandle handle = new DefaultPlcSubscriptionHandle(this);
final PlcSubscriptionField subscriptionPlcField = subscriptionRequest.getField(name);
switch(subscriptionPlcField.getPlcSubscriptionType()) {
case CYCLIC:
LOGGER.info("Adding cyclic subscription for field name {}", name);
device.addCyclicSubscription(dispatchSubscriptionEvent(name, handle), handle, subscriptionPlcField, subscriptionPlcField.getDuration().orElseThrow(RuntimeException::new));
break;
case CHANGE_OF_STATE:
LOGGER.info("Adding change of state subscription for field name {}", name);
device.addChangeOfStateSubscription(dispatchSubscriptionEvent(name, handle), handle, subscriptionPlcField);
break;
case EVENT:
LOGGER.info("Adding event subscription for field name {}", name);
device.addEventSubscription(dispatchSubscriptionEvent(name, handle), handle, subscriptionPlcField);
break;
}
values.put(name, new ResponseItem<>(PlcResponseCode.OK, handle));
});
PlcSubscriptionResponse response = new DefaultPlcSubscriptionResponse(subscriptionRequest, values);
return CompletableFuture.completedFuture(response);
}
use of org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionHandle 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.spi.model.DefaultPlcSubscriptionHandle in project plc4x by apache.
the class GenericCANProtocolLogic method register.
@Override
public PlcConsumerRegistration register(Consumer<PlcSubscriptionEvent> consumer, Collection<PlcSubscriptionHandle> handles) {
final DefaultPlcConsumerRegistration consumerRegistration = new DefaultPlcConsumerRegistration(this, consumer, handles.toArray(new DefaultPlcSubscriptionHandle[0]));
consumers.put(consumerRegistration, consumer);
return consumerRegistration;
}
Aggregations