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);
}
}
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);
}
}
}
}
}
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);
});
}
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);
}
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);
}
}
Aggregations