Hi Wolfgang,
i added a small example to the ManualPLC4XOpcua file which will be included into the up comming
OPC docu.
It is stripped down to the functions which are required to create a subscription and does
not include a further embedding into an application.
I hope it helps you!
String fieldAddress = "ns=2;s=HelloWorld/ScalarTypes/String"; // String in opc ua format to
describe target value
String fieldName = "field1"; // local unique key for the requested variable
String connectionString = "opcua:tcp://127.0.0.1:12686/milo?discovery=false"; // Address of
the opc ua server
PlcConnection opcuaSubConnection = new PlcDriverManager().getConnection(connectionString);
// build connection
PlcSubscriptionRequest.Builder subBuilder = opcuaSubConnection.subscriptionRequestBuilder();
// get builder
subBuilder.addChangeOfStateField(fieldName, fieldAddress); // add the tuple of fieldName and
Address to the request
PlcSubscriptionRequest subReq = subBuilder.build(); // build the request
PlcSubscriptionResponse subResp = subReq.execute().get(); // execute the build up of a subscription
Consumer<PlcSubscriptionEvent> consumer = plcSubscriptionEvent -> System.out.println("Your
Information"); // create a consumer function for the subscription
PlcConsumerRegistration registration = subResp.getSubscriptionHandle(fieldName).register(consumer);
// add the consumer to the created subscription of the request and access it over the SubscriptionHandler
registration.unregister(); // Unsubscribe
// You done :)
Beste regards
Matthias
________________________________
Von: Wolfgang Huse <wolfgang.huse@nutanix.com>
Gesendet: Sonntag, 3. Mai 2020 15:02:51
An: dev@plc4x.apache.org
Betreff: Java Example for Subscription
Hi,
could anybody provide me a small example how to use Subscription?
I am able to subscribe as example to a opcua field but I have difficulties to create the callback-function
which is triggered when new values arrive.
I had a look at different examples but most of them use synchronous communication or async
read but not Subscriptions.
Mit freundlichen Grüßen – With kind regards
Wolfgang Huse
|