Search in sources :

Example 1 with FirmataDriverContext

use of org.apache.plc4x.java.firmata.readwrite.context.FirmataDriverContext 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;
}
Also used : PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) FirmataField(org.apache.plc4x.java.firmata.readwrite.field.FirmataField) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CompletableFuture(java.util.concurrent.CompletableFuture) DefaultPlcSubscriptionField(org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionField) FirmataSubscriptionHandle(org.apache.plc4x.java.firmata.readwrite.model.FirmataSubscriptionHandle) FirmataDriverContext(org.apache.plc4x.java.firmata.readwrite.context.FirmataDriverContext) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem)

Aggregations

CompletableFuture (java.util.concurrent.CompletableFuture)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 PlcRuntimeException (org.apache.plc4x.java.api.exceptions.PlcRuntimeException)1 FirmataDriverContext (org.apache.plc4x.java.firmata.readwrite.context.FirmataDriverContext)1 FirmataField (org.apache.plc4x.java.firmata.readwrite.field.FirmataField)1 FirmataSubscriptionHandle (org.apache.plc4x.java.firmata.readwrite.model.FirmataSubscriptionHandle)1 ResponseItem (org.apache.plc4x.java.spi.messages.utils.ResponseItem)1 DefaultPlcSubscriptionField (org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionField)1