Search in sources :

Example 11 with PlcDriverManager

use of org.apache.plc4x.java.PlcDriverManager in project plc4x by apache.

the class ScraperTest method scraper_schedulesJob.

@Test
void scraper_schedulesJob() throws InterruptedException, PlcConnectionException {
    PlcDriverManager driverManager = new PlcDriverManager();
    MockConnection connection = (MockConnection) driverManager.getConnection("mock:m1");
    connection.setDevice(mockDevice);
    when(mockDevice.read(any())).thenReturn(new ResponseItem<>(PlcResponseCode.OK, new PlcDINT(1)));
    ScraperImpl scraper = new ScraperImpl((j, a, m) -> {
    }, driverManager, Collections.singletonList(new ScrapeJobImpl("job1", 10, Collections.singletonMap("m1", "mock:m1"), Collections.singletonMap("field1", "qry1"))));
    scraper.start();
    Thread.sleep(1_000);
    // Assert that tasks got done.
    assertThat(scraper.getScheduler()).isInstanceOf(ScheduledThreadPoolExecutor.class);
    assertThat(scraper.getNumberOfActiveTasks()).isEqualTo(1);
    assertThat(((ScheduledThreadPoolExecutor) scraper.getScheduler()).getCompletedTaskCount()).isGreaterThan(10);
}
Also used : PlcDINT(org.apache.plc4x.java.spi.values.PlcDINT) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) PlcDriverManager(org.apache.plc4x.java.PlcDriverManager) PooledPlcDriverManager(org.apache.plc4x.java.utils.connectionpool.PooledPlcDriverManager) MockConnection(org.apache.plc4x.java.mock.connection.MockConnection) Test(org.junit.jupiter.api.Test)

Example 12 with PlcDriverManager

use of org.apache.plc4x.java.PlcDriverManager in project plc4x by apache.

the class ScraperTest method stop_stopsAllJobs.

@Test
void stop_stopsAllJobs() {
    PlcDriverManager driverManager = new PlcDriverManager();
    Scraper scraper = new ScraperImpl((j, a, m) -> {
    }, driverManager, Collections.singletonList(new ScrapeJobImpl("job1", 1, Collections.singletonMap("m1", "mock:m1"), Collections.singletonMap("field1", "qry1"))));
    scraper.start();
    assertThat(scraper.getNumberOfActiveTasks()).isEqualTo(1);
    scraper.stop();
    assertThat(scraper.getNumberOfActiveTasks()).isZero();
}
Also used : PlcDriverManager(org.apache.plc4x.java.PlcDriverManager) PooledPlcDriverManager(org.apache.plc4x.java.utils.connectionpool.PooledPlcDriverManager) Test(org.junit.jupiter.api.Test)

Example 13 with PlcDriverManager

use of org.apache.plc4x.java.PlcDriverManager in project plc4x by apache.

the class TriggeredScraperRunnerModbus method main.

/**
 * testing of TriggeredScraper vs real device (Modbus)
 */
public static void main(String[] args) throws IOException, ScraperException {
    ScraperConfiguration configuration = ScraperConfiguration.fromFile("plc4j/utils/scraper/src/test/resources/example_triggered_scraper_modbus.yml", ScraperConfigurationTriggeredImpl.class);
    PlcDriverManager plcDriverManager = new PooledPlcDriverManager();
    TriggeredScraperImpl scraper = new TriggeredScraperImpl(configuration, plcDriverManager, (j, a, m) -> {
        LOGGER.info("Results from {}/{}: {}", j, a, m);
        for (Map.Entry<String, Object> entry : m.entrySet()) {
            for (Object object : (List<Object>) entry.getValue()) {
                LOGGER.info("{}", object);
            }
        }
    }, new TriggerCollectorImpl(plcDriverManager));
    scraper.start();
}
Also used : ScraperConfiguration(org.apache.plc4x.java.scraper.config.ScraperConfiguration) TriggeredScraperImpl(org.apache.plc4x.java.scraper.triggeredscraper.TriggeredScraperImpl) List(java.util.List) TriggerCollectorImpl(org.apache.plc4x.java.scraper.triggeredscraper.triggerhandler.collector.TriggerCollectorImpl) PlcDriverManager(org.apache.plc4x.java.PlcDriverManager) PooledPlcDriverManager(org.apache.plc4x.java.utils.connectionpool.PooledPlcDriverManager) Map(java.util.Map) PooledPlcDriverManager(org.apache.plc4x.java.utils.connectionpool.PooledPlcDriverManager)

Example 14 with PlcDriverManager

use of org.apache.plc4x.java.PlcDriverManager in project plc4x by apache.

the class ManualPlc4XAdsTest method main.

public static void main(String... args) throws Exception {
    String connectionUrl;
    if (args.length > 0 && "serial".equalsIgnoreCase(args[0])) {
        System.out.println("Using serial");
        connectionUrl = "ads:serial:///dev/ttys003/10.10.64.40.1.1:851/10.10.56.23.1.1:30000";
    } else {
        System.out.println("Using tcp");
        connectionUrl = "ads:tcp://10.10.64.40/10.10.64.40.1.1:851/10.10.56.23.1.1:30000";
    }
    // TODO: temporary workaround
    Thread.currentThread().setUncaughtExceptionHandler((t, e) -> {
        System.err.println(t + " - " + e.getMessage());
        e.printStackTrace();
        System.exit(1);
    });
    try (PlcConnection plcConnection = new PlcDriverManager().getConnection(connectionUrl)) {
        System.out.println("PlcConnection " + plcConnection);
        PlcReadRequest.Builder readRequestBuilder = plcConnection.readRequestBuilder();
        PlcReadRequest readRequest = readRequestBuilder.addItem("station", "Allgemein_S2.Station:BYTE").build();
        CompletableFuture<? extends PlcReadResponse> response = readRequest.execute();
        PlcReadResponse readResponse = response.get();
        System.out.println("Response " + readResponse);
        Collection<Integer> stations = readResponse.getAllIntegers("station");
        stations.forEach(System.out::println);
        // 2. We build a subscription
        PlcSubscriptionRequest.Builder subscriptionRequestBuilder = plcConnection.subscriptionRequestBuilder();
        PlcSubscriptionRequest subscriptionRequest = subscriptionRequestBuilder.addChangeOfStateField("stationChange", "Allgemein_S2.Station:BYTE").addCyclicField("stationChange2", "Allgemein_S2.Station:BYTE", Duration.ofSeconds(3)).build();
        PlcSubscriptionResponse plcSubscriptionResponse = subscriptionRequest.execute().get();
        List<PlcConsumerRegistration> plcConsumerRegistrations = plcSubscriptionResponse.getSubscriptionHandles().stream().map(plcSubscriptionHandle -> plcSubscriptionHandle.register(plcSubscriptionEvent -> {
            int stationChange = plcSubscriptionEvent.getNumberOfValues("stationChange");
            int stationChange2 = plcSubscriptionEvent.getNumberOfValues("stationChange2");
            System.out.println(String.format("%s: [%d]- StationsNummer: {%d}", plcSubscriptionEvent.getTimestamp(), stationChange + stationChange2, plcSubscriptionEvent.getInteger("stationChange2")));
        })).collect(Collectors.toList());
        // Now we wait a bit
        TimeUnit.SECONDS.sleep(10);
        // we unregister the listener
        plcConsumerRegistrations.forEach(PlcConsumerRegistration::unregister);
        // we unsubscribe at the plc
        PlcUnsubscriptionRequest.Builder unsubscriptionRequestBuilder = plcConnection.unsubscriptionRequestBuilder();
        PlcUnsubscriptionRequest unsubscriptionRequest = unsubscriptionRequestBuilder.addHandles(plcSubscriptionResponse.getSubscriptionHandles()).build();
        CompletableFuture<PlcUnsubscriptionResponse> unsubscriptionResponse = unsubscriptionRequest.execute();
        unsubscriptionResponse.get(5, TimeUnit.SECONDS);
        System.out.println(unsubscriptionResponse);
    }
    System.exit(0);
}
Also used : PlcDriverManager(org.apache.plc4x.java.PlcDriverManager) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) PlcConnection(org.apache.plc4x.java.api.PlcConnection) Duration(java.time.Duration) Collection(java.util.Collection) CompletableFuture(java.util.concurrent.CompletableFuture) PlcConsumerRegistration(org.apache.plc4x.java.api.model.PlcConsumerRegistration) Collectors(java.util.stream.Collectors) org.apache.plc4x.java.api.messages(org.apache.plc4x.java.api.messages) PlcConnection(org.apache.plc4x.java.api.PlcConnection) PlcConsumerRegistration(org.apache.plc4x.java.api.model.PlcConsumerRegistration) PlcDriverManager(org.apache.plc4x.java.PlcDriverManager)

Example 15 with PlcDriverManager

use of org.apache.plc4x.java.PlcDriverManager in project plc4x by apache.

the class GenericCANDriverTest method testSubscribeAndWrite.

@Test
@Disabled("This test requires working virtual CAN transport to be truly platform independent")
void testSubscribeAndWrite() throws Exception {
    // PlcConnection connection1 = new PlcDriverManager().getConnection("genericcan:socketcan://vcan0");
    // PlcConnection connection2 = new PlcDriverManager().getConnection("genericcan:socketcan://vcan0");
    PlcConnection connection1 = new PlcDriverManager().getConnection("genericcan:virtualcan://");
    PlcConnection connection2 = connection1;
    CountDownLatch latch = new CountDownLatch(1);
    Byte field1 = 0x55;
    short field2 = 10;
    short field3 = 50;
    final AtomicReference<PlcSubscriptionEvent> plcEvent = new AtomicReference<>();
    connection1.subscriptionRequestBuilder().addEventField("field1", "200:BYTE").addEventField("field2", "200:UNSIGNED8").addEventField("field3", "200:UNSIGNED8").build().execute().whenComplete((reply, error) -> {
        if (error != null) {
            fail(error);
            return;
        }
        reply.getSubscriptionHandle("field1").register(new Consumer<PlcSubscriptionEvent>() {

            @Override
            public void accept(PlcSubscriptionEvent event) {
                plcEvent.set(event);
                latch.countDown();
            }
        });
    });
    connection2.writeRequestBuilder().addItem("f1", "200:BYTE", field1).addItem("f2", "200:UNSIGNED8", field2).addItem("f3", "200:UNSIGNED8", field3).build().execute().whenComplete((reply, error) -> {
        if (error != null) {
            fail(error);
        }
    }).get();
    latch.await();
    PlcSubscriptionEvent event = plcEvent.get();
    assertEquals(field1, event.getByte("field1"));
    assertEquals(field2, event.getShort("field2"));
    assertEquals(field3, event.getShort("field3"));
}
Also used : Consumer(java.util.function.Consumer) PlcDriverManager(org.apache.plc4x.java.PlcDriverManager) PlcSubscriptionEvent(org.apache.plc4x.java.api.messages.PlcSubscriptionEvent) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) PlcConnection(org.apache.plc4x.java.api.PlcConnection) PlcConnectionException(org.apache.plc4x.java.api.exceptions.PlcConnectionException) Assertions(org.junit.jupiter.api.Assertions) Disabled(org.junit.jupiter.api.Disabled) AtomicReference(java.util.concurrent.atomic.AtomicReference) PlcSubscriptionEvent(org.apache.plc4x.java.api.messages.PlcSubscriptionEvent) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) PlcDriverManager(org.apache.plc4x.java.PlcDriverManager) PlcConnection(org.apache.plc4x.java.api.PlcConnection) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Aggregations

PlcDriverManager (org.apache.plc4x.java.PlcDriverManager)54 PlcConnection (org.apache.plc4x.java.api.PlcConnection)35 PlcConnectionException (org.apache.plc4x.java.api.exceptions.PlcConnectionException)13 PlcReadRequest (org.apache.plc4x.java.api.messages.PlcReadRequest)13 PlcReadResponse (org.apache.plc4x.java.api.messages.PlcReadResponse)13 Test (org.junit.jupiter.api.Test)12 PooledPlcDriverManager (org.apache.plc4x.java.utils.connectionpool.PooledPlcDriverManager)10 BigInteger (java.math.BigInteger)8 ExecutionException (java.util.concurrent.ExecutionException)8 TimeUnit (java.util.concurrent.TimeUnit)5 Condition (org.assertj.core.api.Condition)5 BigDecimal (java.math.BigDecimal)4 LocalDate (java.time.LocalDate)4 Map (java.util.Map)4 PlcRuntimeException (org.apache.plc4x.java.api.exceptions.PlcRuntimeException)4 PlcSubscriptionRequest (org.apache.plc4x.java.api.messages.PlcSubscriptionRequest)4 PlcSubscriptionResponse (org.apache.plc4x.java.api.messages.PlcSubscriptionResponse)4 PlcSubscriptionHandle (org.apache.plc4x.java.api.model.PlcSubscriptionHandle)4 Disabled (org.junit.jupiter.api.Disabled)4 LocalDateTime (java.time.LocalDateTime)3