use of org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration 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.DefaultPlcConsumerRegistration in project plc4x by apache.
the class FirmataProtocolLogic method publishAnalogEvents.
protected void publishAnalogEvents(int pin, int value) {
// Try sending the subscription event to all listeners.
for (Map.Entry<DefaultPlcConsumerRegistration, Consumer<PlcSubscriptionEvent>> entry : consumers.entrySet()) {
final DefaultPlcConsumerRegistration registration = entry.getKey();
final Consumer<PlcSubscriptionEvent> consumer = entry.getValue();
// Only if the current data point matches the subscription, publish the event to it.
for (PlcSubscriptionHandle handle : registration.getSubscriptionHandles()) {
if (handle instanceof FirmataSubscriptionHandle) {
FirmataSubscriptionHandle subscriptionHandle = (FirmataSubscriptionHandle) handle;
// (The bit subscribed to in this field actually changed).
if (subscriptionHandle.getField() instanceof FirmataFieldAnalog) {
FirmataFieldAnalog analogField = (FirmataFieldAnalog) subscriptionHandle.getField();
// Check if this field would include the current pin.
if ((analogField.getAddress() <= pin) && (analogField.getAddress() + analogField.getNumberOfElements() >= pin)) {
// Build an update event containing the current values for all subscribed fields.
List<PlcValue> values = new ArrayList<>(analogField.getNumberOfElements());
for (int i = analogField.getAddress(); i < analogField.getAddress() + analogField.getNumberOfElements(); i++) {
if (analogValues.containsKey(i)) {
values.add(new PlcDINT(analogValues.get(i).intValue()));
} else // This could be the case if only some of the requested array values are available
{
values.add(new PlcDINT(-1));
}
}
sendUpdateEvents(consumer, subscriptionHandle.getName(), values);
}
}
}
}
}
}
use of org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration in project plc4x by apache.
the class KnxNetIpProtocolLogic method publishEvent.
protected void publishEvent(GroupAddress groupAddress, PlcValue plcValue) {
// Create a subscription event from the input.
// TODO: Check this ... this is sort of not really right ...
final PlcSubscriptionEvent event = new DefaultPlcSubscriptionEvent(Instant.now(), Collections.singletonMap("knxData", new ResponseItem<>(PlcResponseCode.OK, plcValue)));
// Try sending the subscription event to all listeners.
for (Map.Entry<DefaultPlcConsumerRegistration, Consumer<PlcSubscriptionEvent>> entry : consumers.entrySet()) {
final DefaultPlcConsumerRegistration registration = entry.getKey();
final Consumer<PlcSubscriptionEvent> consumer = entry.getValue();
// Only if the current data point matches the subscription, publish the event to it.
for (PlcSubscriptionHandle handle : registration.getSubscriptionHandles()) {
if (handle instanceof KnxNetIpSubscriptionHandle) {
KnxNetIpSubscriptionHandle subscriptionHandle = (KnxNetIpSubscriptionHandle) handle;
// Check if the subscription matches this current event.
if (subscriptionHandle.getField().matchesGroupAddress(groupAddress)) {
consumer.accept(event);
}
}
}
}
}
use of org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration in project plc4x by apache.
the class KnxNetIpProtocolLogic method unregister.
@Override
public void unregister(PlcConsumerRegistration plcConsumerRegistration) {
DefaultPlcConsumerRegistration consumerRegistration = (DefaultPlcConsumerRegistration) plcConsumerRegistration;
consumers.remove(consumerRegistration);
}
use of org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration in project plc4x by apache.
the class KnxNetIpProtocolLogic method register.
@Override
public PlcConsumerRegistration register(Consumer<PlcSubscriptionEvent> consumer, Collection<PlcSubscriptionHandle> collection) {
final DefaultPlcConsumerRegistration consumerRegistration = new DefaultPlcConsumerRegistration(this, consumer, collection.toArray(new PlcSubscriptionHandle[0]));
consumers.put(consumerRegistration, consumer);
return consumerRegistration;
}
Aggregations