Search in sources :

Example 1 with ResponseItem

use of org.apache.plc4x.java.spi.messages.utils.ResponseItem in project plc4x by apache.

the class FirmataProtocolLogic method sendUpdateEvents.

protected void sendUpdateEvents(Consumer<PlcSubscriptionEvent> consumer, String fieldName, List<PlcValue> values) {
    // If it's just one element, return this as a direct PlcValue
    if (values.size() == 1) {
        final PlcSubscriptionEvent event = new DefaultPlcSubscriptionEvent(Instant.now(), Collections.singletonMap(fieldName, new ResponseItem<>(PlcResponseCode.OK, values.get(0))));
        consumer.accept(event);
    } else // If it's more, return a PlcList instead.
    {
        final PlcSubscriptionEvent event = new DefaultPlcSubscriptionEvent(Instant.now(), Collections.singletonMap(fieldName, new ResponseItem<>(PlcResponseCode.OK, new PlcList(values))));
        consumer.accept(event);
    }
}
Also used : PlcList(org.apache.plc4x.java.spi.values.PlcList) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem)

Example 2 with ResponseItem

use of org.apache.plc4x.java.spi.messages.utils.ResponseItem 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);
                }
            }
        }
    }
}
Also used : DefaultPlcConsumerRegistration(org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration) KnxNetIpSubscriptionHandle(org.apache.plc4x.java.knxnetip.model.KnxNetIpSubscriptionHandle) Consumer(java.util.function.Consumer) PlcSubscriptionHandle(org.apache.plc4x.java.api.model.PlcSubscriptionHandle) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 3 with ResponseItem

use of org.apache.plc4x.java.spi.messages.utils.ResponseItem in project plc4x by apache.

the class OpcuaProtocolLogic method subscribe.

@Override
public CompletableFuture<PlcSubscriptionResponse> subscribe(PlcSubscriptionRequest subscriptionRequest) {
    return CompletableFuture.supplyAsync(() -> {
        Map<String, ResponseItem<PlcSubscriptionHandle>> values = new HashMap<>();
        long subscriptionId;
        ArrayList<String> fields = new ArrayList<>(subscriptionRequest.getFieldNames());
        long cycleTime = (subscriptionRequest.getField(fields.get(0))).getDuration().orElse(Duration.ofMillis(1000)).toMillis();
        try {
            CompletableFuture<CreateSubscriptionResponse> subscription = onSubscribeCreateSubscription(cycleTime);
            CreateSubscriptionResponse response = subscription.get(SecureChannel.REQUEST_TIMEOUT_LONG, TimeUnit.MILLISECONDS);
            subscriptionId = response.getSubscriptionId();
            subscriptions.put(subscriptionId, new OpcuaSubscriptionHandle(context, this, channel, subscriptionRequest, subscriptionId, cycleTime));
        } catch (Exception e) {
            throw new PlcRuntimeException("Unable to subscribe because of: " + e.getMessage());
        }
        for (String fieldName : subscriptionRequest.getFieldNames()) {
            final DefaultPlcSubscriptionField fieldDefaultPlcSubscription = (DefaultPlcSubscriptionField) subscriptionRequest.getField(fieldName);
            if (!(fieldDefaultPlcSubscription.getPlcField() instanceof OpcuaField)) {
                values.put(fieldName, new ResponseItem<>(PlcResponseCode.INVALID_ADDRESS, null));
            } else {
                values.put(fieldName, new ResponseItem<>(PlcResponseCode.OK, subscriptions.get(subscriptionId)));
            }
        }
        return new DefaultPlcSubscriptionResponse(subscriptionRequest, values);
    });
}
Also used : PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) TimeoutException(java.util.concurrent.TimeoutException) PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) DefaultPlcSubscriptionField(org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionField) OpcuaField(org.apache.plc4x.java.opcua.field.OpcuaField) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem)

Example 4 with ResponseItem

use of org.apache.plc4x.java.spi.messages.utils.ResponseItem in project plc4x by apache.

the class SimulatedConnection method read.

@Override
public CompletableFuture<PlcReadResponse> read(PlcReadRequest readRequest) {
    Map<String, ResponseItem<PlcValue>> fields = new HashMap<>();
    for (String fieldName : readRequest.getFieldNames()) {
        SimulatedField field = (SimulatedField) readRequest.getField(fieldName);
        Optional<PlcValue> valueOptional = device.get(field);
        ResponseItem<PlcValue> fieldPair;
        boolean present = valueOptional.isPresent();
        fieldPair = present ? new ResponseItem<>(PlcResponseCode.OK, valueOptional.get()) : new ResponseItem<>(PlcResponseCode.NOT_FOUND, null);
        fields.put(fieldName, fieldPair);
    }
    PlcReadResponse response = new DefaultPlcReadResponse(readRequest, fields);
    return CompletableFuture.completedFuture(response);
}
Also used : PlcReadResponse(org.apache.plc4x.java.api.messages.PlcReadResponse) DefaultPlcReadResponse(org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse) PlcValue(org.apache.plc4x.java.api.value.PlcValue) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DefaultPlcReadResponse(org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem) SimulatedField(org.apache.plc4x.java.simulated.field.SimulatedField)

Example 5 with ResponseItem

use of org.apache.plc4x.java.spi.messages.utils.ResponseItem in project plc4x by apache.

the class HelloInflux method run.

public void run() {
    InfluxDBClient dbConnection = connectToDb();
    WriteApi writeApi = dbConnection.getWriteApi();
    try {
        PlcConnection plcConnection = connectToPlc();
        final PlcSubscriptionRequest subscriptionRequest = plcConnection.subscriptionRequestBuilder().addChangeOfStateField("query", configuration.getString("plc.query")).build();
        final PlcSubscriptionResponse subscriptionResponse = subscriptionRequest.execute().get(10, TimeUnit.SECONDS);
        subscriptionResponse.getSubscriptionHandle("query").register(plcSubscriptionEvent -> {
            DefaultPlcSubscriptionEvent internalEvent = (DefaultPlcSubscriptionEvent) plcSubscriptionEvent;
            final Point point = Point.measurement(configuration.getString("influx.measurement")).time(plcSubscriptionEvent.getTimestamp().toEpochMilli(), WritePrecision.MS);
            final Map<String, ResponseItem<PlcValue>> values = internalEvent.getValues();
            values.forEach((fieldName, fieldResponsePair) -> {
                final PlcResponseCode responseCode = fieldResponsePair.getCode();
                final PlcValue plcValue = fieldResponsePair.getValue();
                if (responseCode == PlcResponseCode.OK) {
                    PlcStruct structValue = (PlcStruct) plcValue;
                    for (String key : structValue.getKeys()) {
                        PlcValue subValue = structValue.getValue(key);
                        registerFields(point, key, subValue);
                    }
                }
            });
            writeApi.writePoint(configuration.getString("influx.bucket"), configuration.getString("influx.org"), point);
        });
    } catch (PlcException e) {
        logger.error("PLC Error", e);
    } catch (Exception e) {
        logger.error("General Error", e);
    }
}
Also used : InfluxDBClient(com.influxdb.client.InfluxDBClient) PlcSubscriptionRequest(org.apache.plc4x.java.api.messages.PlcSubscriptionRequest) DefaultPlcSubscriptionEvent(org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionEvent) Point(com.influxdb.client.write.Point) PlcConnection(org.apache.plc4x.java.api.PlcConnection) ConfigurationException(org.apache.commons.configuration2.ex.ConfigurationException) PlcException(org.apache.plc4x.java.api.exceptions.PlcException) PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) PlcResponseCode(org.apache.plc4x.java.api.types.PlcResponseCode) PlcException(org.apache.plc4x.java.api.exceptions.PlcException) WriteApi(com.influxdb.client.WriteApi) PlcSubscriptionResponse(org.apache.plc4x.java.api.messages.PlcSubscriptionResponse) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem)

Aggregations

ResponseItem (org.apache.plc4x.java.spi.messages.utils.ResponseItem)27 PlcResponseCode (org.apache.plc4x.java.api.types.PlcResponseCode)12 PlcValue (org.apache.plc4x.java.api.value.PlcValue)10 DefaultPlcReadResponse (org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse)10 HashMap (java.util.HashMap)8 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)8 PlcRuntimeException (org.apache.plc4x.java.api.exceptions.PlcRuntimeException)8 CompletableFuture (java.util.concurrent.CompletableFuture)7 PlcSubscriptionHandle (org.apache.plc4x.java.api.model.PlcSubscriptionHandle)6 DefaultPlcSubscriptionField (org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionField)6 Consumer (java.util.function.Consumer)5 DefaultPlcConsumerRegistration (org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration)5 DefaultPlcSubscriptionHandle (org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionHandle)4 BigInteger (java.math.BigInteger)3 Duration (java.time.Duration)3 LocalDateTime (java.time.LocalDateTime)3 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 TimeoutException (java.util.concurrent.TimeoutException)3 org.apache.plc4x.java.api.messages (org.apache.plc4x.java.api.messages)3