use of org.apache.plc4x.java.api.model.PlcSubscriptionHandle 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.api.model.PlcSubscriptionHandle 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.api.model.PlcSubscriptionHandle 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;
}
use of org.apache.plc4x.java.api.model.PlcSubscriptionHandle in project plc4x by apache.
the class ManualKnxNetIp method main.
// Addresses:
// */*/10: Temperature
// */*/12: Heating
// */*/60: Primary Window
// */*/64: Second Window
// */*/101: Power Line 1
public static void main(String[] args) throws Exception {
// final PlcConnection connection = new PlcDriverManager().getConnection("knxnet-ip://192.168.42.11?knxproj-file-path=/Users/christofer.dutz/Projects/Apache/PLC4X-Documents/KNX/Stettiner%20Str.%2013/StettinerStr-Soll-Ist-Temperatur.knxproj");
final PlcConnection connection = new PlcDriverManager().getConnection("knxnet-ip:pcap:///Users/christofer.dutz/Projects/Apache/PLC4X-Documents/KNX/Recording-01.03.2020-2.pcapng?knxproj-file-path=/Users/christofer.dutz/Projects/Apache/PLC4X-Documents/KNX/Stettiner%20Str.%2013/StettinerStr-Soll-Ist-Temperatur.knxproj");
// Make sure we hang up correctly when terminating.
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
connection.close();
} catch (Exception e) {
throw new PlcRuntimeException("Error closing connection", e);
}
}));
// Create a new subscription request.
// The address and the name is just bogus as we're always returning everything.
// We will probably refactor the API in the near future.
final PlcSubscriptionRequest subscriptionRequest = connection.subscriptionRequestBuilder().addEventField("knxData", "*/*/*").build();
// Register the subscription
// The timeout is also just a bogus value as the data is coming in actively
// We will probably refactor the API in the near future.
final PlcSubscriptionResponse subscriptionResponse = subscriptionRequest.execute().get(1000, TimeUnit.MILLISECONDS);
// Register a callback which is called on new data being available.
final PlcSubscriptionHandle subscriptionHandle = subscriptionResponse.getSubscriptionHandle("knxData");
subscriptionHandle.register(knxData -> {
System.out.println(knxData.getTimestamp().toString() + " - " + ((DefaultPlcSubscriptionEvent) knxData).getValues().get("knxData"));
});
}
use of org.apache.plc4x.java.api.model.PlcSubscriptionHandle in project plc4x by apache.
the class HelloWebservice method run.
public void run() throws Exception {
// Establish a connection to the plc.
try (PlcConnection plcConnection = new PlcDriverManager().getConnection(options.getConnectionString())) {
// Check if this connection support subscriptions.
if (!plcConnection.getMetadata().canSubscribe()) {
logger.error("This connection doesn't support subscriptions.");
return;
}
// Create a new read request:
// - Give the single item requested the alias name "value"
final PlcSubscriptionRequest.Builder builder = plcConnection.subscriptionRequestBuilder();
for (int i = 0; i < options.getFieldAddress().length; i++) {
builder.addChangeOfStateField("value-" + i, options.getFieldAddress()[i]);
}
PlcSubscriptionRequest subscriptionRequest = builder.build();
// Execute the subscription response.
final PlcSubscriptionResponse subscriptionResponse = subscriptionRequest.execute().get();
// Attach handlers for the incoming data.
for (String subscriptionName : subscriptionResponse.getFieldNames()) {
final PlcSubscriptionHandle subscriptionHandle = subscriptionResponse.getSubscriptionHandle(subscriptionName);
subscriptionHandle.register(new ValueChangeHandler(options.getWebserviceUrl()));
}
// Wait for the user to press "Enter" to abort the program.
Scanner scanner = new Scanner(System.in);
try {
logger.info("Please press Enter to exit program.");
scanner.nextLine();
logger.info("Finishing");
} catch (IllegalStateException e) {
// System.in has been closed
logger.error("System.in was closed; exiting");
}
}
}
Aggregations