use of org.apache.plc4x.java.api.messages.PlcSubscriptionRequest in project plc4x by apache.
the class OpcuaSubscriptionHandleTest method subscribeInt64.
@Test
public void subscribeInt64() throws Exception {
String field = "Int64";
String identifier = INT64_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.PlcSubscriptionRequest in project plc4x by apache.
the class S7ProtocolLogic method subscribe.
@Override
public CompletableFuture<PlcSubscriptionResponse> subscribe(PlcSubscriptionRequest subscriptionRequest) {
CompletableFuture<PlcSubscriptionResponse> future = new CompletableFuture<>();
DefaultPlcSubscriptionRequest request = (DefaultPlcSubscriptionRequest) subscriptionRequest;
List<S7ParameterUserDataItem> parameterItems = new ArrayList<>(request.getNumberOfFields());
List<S7PayloadUserDataItem> payloadItems = new ArrayList<>(request.getNumberOfFields());
for (String fieldName : request.getFieldNames()) {
final DefaultPlcSubscriptionField sf = (DefaultPlcSubscriptionField) request.getField(fieldName);
final S7SubscriptionField field = (S7SubscriptionField) sf.getPlcField();
switch(field.getFieldType()) {
case EVENT_SUBSCRIPTION:
encodeEventSubscriptionRequest(request, parameterItems, payloadItems);
break;
case EVENT_UNSUBSCRIPTION:
// encodeEventUnSubscriptionRequest(msg, out);
break;
case ALARM_ACK:
// encodeAlarmAckRequest(msg, out);
break;
case ALARM_QUERY:
// encodeAlarmQueryRequest(msg, out);
break;
case CYCLIC_SUBSCRIPTION:
// encodeCycledSubscriptionRequest(msg, out);
break;
case CYCLIC_UNSUBSCRIPTION:
// encodeCycledUnSubscriptionRequest(msg, out);
break;
default:
}
// final PlcValue plcValue = request.getPlcValue(fieldName);
// parameterItems.add(new S7VarRequestParameterItemAddress(encodeS7Address(field)));
// payloadItems.add(serializePlcValue(field, plcValue));
}
final int tpduId = tpduGenerator.getAndIncrement();
// If we've reached the max value for a 16 bit transaction identifier, reset back to 1
if (tpduGenerator.get() == 0xFFFF) {
tpduGenerator.set(1);
}
TPKTPacket tpktPacket = new TPKTPacket(new COTPPacketData(null, new S7MessageUserData(tpduId, new S7ParameterUserData(parameterItems), new S7PayloadUserData(payloadItems, null)), true, (short) tpduId, null));
// Start a new request-transaction (Is ended in the response-handler)
RequestTransactionManager.RequestTransaction transaction = tm.startRequest();
transaction.submit(() -> context.sendRequest(tpktPacket).onTimeout(new TransactionErrorCallback<>(future, transaction)).onError(new TransactionErrorCallback<>(future, transaction)).expectResponse(TPKTPacket.class, REQUEST_TIMEOUT).check(p -> p.getPayload() instanceof COTPPacketData).unwrap(p -> ((COTPPacketData) p.getPayload())).unwrap(COTPPacket::getPayload).check(p -> p.getTpduReference() == tpduId).handle(p -> {
try {
future.complete(decodeEventSubscriptionRequest(p, subscriptionRequest));
} catch (PlcProtocolException e) {
logger.warn("Error sending 'write' message: '{}'", e.getMessage(), e);
}
// Finish the request-transaction.
transaction.endRequest();
}));
return future;
}
use of org.apache.plc4x.java.api.messages.PlcSubscriptionRequest in project plc4x by apache.
the class EventSubscription method main.
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
try (PlcConnection connection = new PlcDriverManager().getConnection("s7://192.168.1.51?remote-rack=0&remote-slot=3&controller-type=S7_400")) {
final PlcSubscriptionRequest.Builder subscription = connection.subscriptionRequestBuilder();
subscription.addEventField("myMODE", "MODE");
subscription.addEventField("mySYS", "SYS");
subscription.addEventField("myUSR", "USR");
subscription.addEventField("myALM", "ALM");
final PlcSubscriptionRequest sub = subscription.build();
final PlcSubscriptionResponse subresponse = sub.execute().get();
// Si todo va bien con la subscripciĆ³n puedo
subresponse.getSubscriptionHandle("myMODE").register(msg -> {
System.out.println("******** S7ModeEvent ********");
Map<String, Object> map = ((S7ModeEvent) msg).getMap();
map.forEach((x, y) -> {
System.out.println(x + " : " + y);
});
System.out.println("****************************");
});
subresponse.getSubscriptionHandle("mySYS").register(msg -> {
System.out.println("******** S7SysEvent ********");
Map<String, Object> map = ((S7SysEvent) msg).getMap();
map.forEach((x, y) -> {
System.out.println(x + " : " + y);
});
System.out.println("****************************");
});
subresponse.getSubscriptionHandle("myUSR").register(msg -> {
System.out.println("******** S7UserEvent *******");
Map<String, Object> map = ((S7UserEvent) msg).getMap();
map.forEach((x, y) -> {
System.out.println(x + " : " + y);
});
System.out.println("****************************");
});
subresponse.getSubscriptionHandle("myALM").register(msg -> {
System.out.println("******** S7AlmEvent *********");
Map<String, Object> map = ((S7AlarmEvent) msg).getMap();
map.forEach((x, y) -> {
System.out.println(x + " : " + y);
});
System.out.println("****************************");
});
System.out.println("Waiting for events");
Thread.sleep(120000);
System.out.println("Bye...");
}
}
Aggregations