use of org.apache.plc4x.java.api.messages.PlcSubscriptionResponse 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...");
}
}
use of org.apache.plc4x.java.api.messages.PlcSubscriptionResponse in project plc4x by apache.
the class SimulatedConnection method subscribe.
/**
* Blocking subscribe call
*
* @param subscriptionRequest subscription request containing at least one subscription request item.
* @return the {@code PlcSubscriptionResponse}
*/
@Override
public CompletableFuture<PlcSubscriptionResponse> subscribe(PlcSubscriptionRequest subscriptionRequest) {
LOGGER.info("subscribing {}", subscriptionRequest);
Map<String, ResponseItem<PlcSubscriptionHandle>> values = new HashMap<>();
subscriptionRequest.getFieldNames().forEach(name -> {
LOGGER.info("creating handle for field name {}", name);
PlcSubscriptionHandle handle = new DefaultPlcSubscriptionHandle(this);
final PlcSubscriptionField subscriptionPlcField = subscriptionRequest.getField(name);
switch(subscriptionPlcField.getPlcSubscriptionType()) {
case CYCLIC:
LOGGER.info("Adding cyclic subscription for field name {}", name);
device.addCyclicSubscription(dispatchSubscriptionEvent(name, handle), handle, subscriptionPlcField, subscriptionPlcField.getDuration().orElseThrow(RuntimeException::new));
break;
case CHANGE_OF_STATE:
LOGGER.info("Adding change of state subscription for field name {}", name);
device.addChangeOfStateSubscription(dispatchSubscriptionEvent(name, handle), handle, subscriptionPlcField);
break;
case EVENT:
LOGGER.info("Adding event subscription for field name {}", name);
device.addEventSubscription(dispatchSubscriptionEvent(name, handle), handle, subscriptionPlcField);
break;
}
values.put(name, new ResponseItem<>(PlcResponseCode.OK, handle));
});
PlcSubscriptionResponse response = new DefaultPlcSubscriptionResponse(subscriptionRequest, values);
return CompletableFuture.completedFuture(response);
}
Aggregations