use of org.apache.plc4x.java.firmata.readwrite.model.FirmataSubscriptionHandle 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.firmata.readwrite.model.FirmataSubscriptionHandle in project plc4x by apache.
the class FirmataProtocolLogic method subscribe.
@Override
public CompletableFuture<PlcSubscriptionResponse> subscribe(PlcSubscriptionRequest subscriptionRequest) {
CompletableFuture<PlcSubscriptionResponse> future = new CompletableFuture<>();
try {
final List<FirmataMessage> firmataMessages = ((FirmataDriverContext) getDriverContext()).processSubscriptionRequest(subscriptionRequest);
for (FirmataMessage firmataMessage : firmataMessages) {
context.sendToWire(firmataMessage);
}
Map<String, ResponseItem<PlcSubscriptionHandle>> result = new HashMap<>();
for (String fieldName : subscriptionRequest.getFieldNames()) {
DefaultPlcSubscriptionField subscriptionField = (DefaultPlcSubscriptionField) subscriptionRequest.getField(fieldName);
FirmataField field = (FirmataField) subscriptionField.getPlcField();
result.put(fieldName, new ResponseItem<>(PlcResponseCode.OK, new FirmataSubscriptionHandle(this, fieldName, field)));
}
future.complete(new DefaultPlcSubscriptionResponse(subscriptionRequest, result));
} catch (PlcRuntimeException e) {
future.completeExceptionally(e);
}
return future;
}
use of org.apache.plc4x.java.firmata.readwrite.model.FirmataSubscriptionHandle in project plc4x by apache.
the class FirmataProtocolLogic method publishDigitalEvents.
protected void publishDigitalEvents(BitSet changedBits, BitSet bitValues) {
// If nothing changed, no need to do anything.
if (changedBits.cardinality() == 0) {
return;
}
// 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 FirmataFieldDigital) {
FirmataFieldDigital digitalField = (FirmataFieldDigital) subscriptionHandle.getField();
// send out an update event with all of its current values.
if (digitalField.getBitSet().intersects(changedBits)) {
List<PlcValue> values = new ArrayList<>(digitalField.getBitSet().cardinality());
for (int i = 0; i < digitalField.getBitSet().length(); i++) {
values.add(new PlcBOOL(bitValues.get(i)));
}
sendUpdateEvents(consumer, subscriptionHandle.getName(), values);
}
}
}
}
}
}
Aggregations