use of org.apache.plc4x.java.api.messages.PlcSubscriptionResponse 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.messages.PlcSubscriptionResponse 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");
}
}
}
use of org.apache.plc4x.java.api.messages.PlcSubscriptionResponse in project plc4x by apache.
the class HelloPlc4xSubscription 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());
}
// 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");
}
}
}
use of org.apache.plc4x.java.api.messages.PlcSubscriptionResponse in project plc4x by apache.
the class OpcuaSubscriptionHandleTest method subscribeString.
@Test
public void subscribeString() throws Exception {
String field = "String";
String identifier = STRING_IDENTIFIER_READ_WRITE;
LOGGER.info("Starting subscription {} test", field);
// Create Subscription
PlcSubscriptionRequest.Builder builder = opcuaConnection.subscriptionRequestBuilder();
builder.addChangeOfStateField(field, identifier);
PlcSubscriptionRequest request = builder.build();
// Get result of creating subscription
PlcSubscriptionResponse response = request.execute().get();
final OpcuaSubscriptionHandle subscriptionHandle = (OpcuaSubscriptionHandle) response.getSubscriptionHandle(field);
// Create handler for returned value
subscriptionHandle.register(plcSubscriptionEvent -> {
assert plcSubscriptionEvent.getResponseCode(field).equals(PlcResponseCode.OK);
LOGGER.info("Received a response from {} test {}", field, plcSubscriptionEvent.getPlcValue(field).toString());
});
// Wait for value to be returned from server
Thread.sleep(1200);
subscriptionHandle.stopSubscriber();
}
use of org.apache.plc4x.java.api.messages.PlcSubscriptionResponse in project plc4x by apache.
the class OpcuaSubscriptionHandleTest method subscribeUInt32.
@Test
public void subscribeUInt32() throws Exception {
String field = "UInt32";
String identifier = UINT32_IDENTIFIER_READ_WRITE;
LOGGER.info("Starting subscription {} test", field);
// Create Subscription
PlcSubscriptionRequest.Builder builder = opcuaConnection.subscriptionRequestBuilder();
builder.addChangeOfStateField(field, identifier);
PlcSubscriptionRequest request = builder.build();
// Get result of creating subscription
PlcSubscriptionResponse response = request.execute().get();
final OpcuaSubscriptionHandle subscriptionHandle = (OpcuaSubscriptionHandle) response.getSubscriptionHandle(field);
// Create handler for returned value
subscriptionHandle.register(plcSubscriptionEvent -> {
assert plcSubscriptionEvent.getResponseCode(field).equals(PlcResponseCode.OK);
LOGGER.info("Received a response from {} test {}", field, plcSubscriptionEvent.getPlcValue(field).toString());
});
// Wait for value to be returned from server
Thread.sleep(1200);
subscriptionHandle.stopSubscriber();
}
Aggregations