Search in sources :

Example 26 with PlcSubscriptionResponse

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...");
    }
}
Also used : S7ModeEvent(org.apache.plc4x.java.s7.events.S7ModeEvent) PlcSubscriptionRequest(org.apache.plc4x.java.api.messages.PlcSubscriptionRequest) PlcSubscriptionResponse(org.apache.plc4x.java.api.messages.PlcSubscriptionResponse) S7UserEvent(org.apache.plc4x.java.s7.events.S7UserEvent) S7SysEvent(org.apache.plc4x.java.s7.events.S7SysEvent) S7AlarmEvent(org.apache.plc4x.java.s7.events.S7AlarmEvent) PlcDriverManager(org.apache.plc4x.java.PlcDriverManager) PlcConnection(org.apache.plc4x.java.api.PlcConnection)

Example 27 with PlcSubscriptionResponse

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);
}
Also used : PlcSubscriptionField(org.apache.plc4x.java.api.model.PlcSubscriptionField) PlcSubscriptionResponse(org.apache.plc4x.java.api.messages.PlcSubscriptionResponse) DefaultPlcSubscriptionResponse(org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionResponse) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DefaultPlcSubscriptionResponse(org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionResponse) PlcSubscriptionHandle(org.apache.plc4x.java.api.model.PlcSubscriptionHandle) DefaultPlcSubscriptionHandle(org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionHandle) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem) DefaultPlcSubscriptionHandle(org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionHandle)

Aggregations

PlcSubscriptionResponse (org.apache.plc4x.java.api.messages.PlcSubscriptionResponse)27 PlcSubscriptionRequest (org.apache.plc4x.java.api.messages.PlcSubscriptionRequest)23 OpcuaPlcDriverTest (org.apache.plc4x.java.opcua.OpcuaPlcDriverTest)16 PlcConnection (org.apache.plc4x.java.api.PlcConnection)8 PlcSubscriptionHandle (org.apache.plc4x.java.api.model.PlcSubscriptionHandle)6 PlcDriverManager (org.apache.plc4x.java.PlcDriverManager)5 PlcRuntimeException (org.apache.plc4x.java.api.exceptions.PlcRuntimeException)4 ResponseItem (org.apache.plc4x.java.spi.messages.utils.ResponseItem)3 PlcStruct (org.apache.plc4x.java.spi.values.PlcStruct)3 File (java.io.File)2 URL (java.net.URL)2 Scanner (java.util.Scanner)2 PlcResponseCode (org.apache.plc4x.java.api.types.PlcResponseCode)2 DefaultPlcSubscriptionResponse (org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionResponse)2 InfluxDBClient (com.influxdb.client.InfluxDBClient)1 WriteApi (com.influxdb.client.WriteApi)1 Point (com.influxdb.client.write.Point)1 ByteBuf (io.netty.buffer.ByteBuf)1 Unpooled (io.netty.buffer.Unpooled)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1