use of org.apache.plc4x.java.api.messages.PlcSubscriptionRequest 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.PlcSubscriptionRequest 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.PlcSubscriptionRequest 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);
}
}
use of org.apache.plc4x.java.api.messages.PlcSubscriptionRequest 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.PlcSubscriptionRequest in project plc4x by apache.
the class OpcuaSubscriptionHandleTest method subscribeMultiple.
@Test
public void subscribeMultiple() throws Exception {
String field1 = "UInteger";
String identifier1 = UINTEGER_IDENTIFIER_READ_WRITE;
String field2 = "Integer";
String identifier2 = INTEGER_IDENTIFIER_READ_WRITE;
LOGGER.info("Starting subscription {} and {} test", field1, field2);
// Create Subscription
PlcSubscriptionRequest.Builder builder = opcuaConnection.subscriptionRequestBuilder();
builder.addChangeOfStateField(field1, identifier1);
builder.addChangeOfStateField(field2, identifier2);
PlcSubscriptionRequest request = builder.build();
// Get result of creating subscription
PlcSubscriptionResponse response = request.execute().get();
final OpcuaSubscriptionHandle subscriptionHandle = (OpcuaSubscriptionHandle) response.getSubscriptionHandle(field1);
// Create handler for returned value
subscriptionHandle.register(plcSubscriptionEvent -> {
assert plcSubscriptionEvent.getResponseCode(field1).equals(PlcResponseCode.OK);
assert plcSubscriptionEvent.getResponseCode(field2).equals(PlcResponseCode.OK);
});
// Wait for value to be returned from server
Thread.sleep(1200);
subscriptionHandle.stopSubscriber();
}
Aggregations