Search in sources :

Example 1 with PooledPlcDriverManager

use of org.apache.plc4x.java.utils.connectionpool.PooledPlcDriverManager in project plc4x by apache.

the class Plc4xNamespace method onDataItemsCreated.

@Override
public void onDataItemsCreated(List<DataItem> dataItems) {
    for (DataItem item : dataItems) {
        plc4xServer.addField(item);
        if (plc4xServer.getDriverManager() == null) {
            plc4xServer.removeField(item);
            plc4xServer.setDriverManager(new PooledPlcDriverManager());
        }
    }
    subscriptionModel.onDataItemsCreated(dataItems);
}
Also used : DataItem(org.eclipse.milo.opcua.sdk.server.api.DataItem) PooledPlcDriverManager(org.apache.plc4x.java.utils.connectionpool.PooledPlcDriverManager)

Example 2 with PooledPlcDriverManager

use of org.apache.plc4x.java.utils.connectionpool.PooledPlcDriverManager in project plc4x by apache.

the class ScraperTest method real_stuff.

@Test
@Disabled
void real_stuff() throws InterruptedException {
    PlcDriverManager driverManager = new PooledPlcDriverManager(pooledPlcConnectionFactory -> {
        GenericKeyedObjectPoolConfig<PlcConnection> config = new GenericKeyedObjectPoolConfig<>();
        config.setJmxEnabled(true);
        config.setMaxWaitMillis(-1);
        config.setMaxTotal(3);
        config.setMinIdlePerKey(0);
        config.setBlockWhenExhausted(true);
        config.setTestOnBorrow(true);
        config.setTestOnReturn(true);
        return new GenericKeyedObjectPool<>(pooledPlcConnectionFactory, config);
    });
    Scraper scraper = new ScraperImpl((j, a, m) -> {
    }, driverManager, Arrays.asList(new ScrapeJobImpl("job1", 10, Collections.singletonMap("tim", CONN_STRING_TIM), Collections.singletonMap("distance", FIELD_STRING_TIM)), new ScrapeJobImpl("job2", 10, Collections.singletonMap("chris", CONN_STRING_CH), Collections.singletonMap("counter", FIELD_STRING_CH))));
    Thread.sleep(30_000_000);
}
Also used : GenericKeyedObjectPoolConfig(org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig) GenericKeyedObjectPool(org.apache.commons.pool2.impl.GenericKeyedObjectPool) PlcDriverManager(org.apache.plc4x.java.PlcDriverManager) PooledPlcDriverManager(org.apache.plc4x.java.utils.connectionpool.PooledPlcDriverManager) PlcConnection(org.apache.plc4x.java.api.PlcConnection) PooledPlcDriverManager(org.apache.plc4x.java.utils.connectionpool.PooledPlcDriverManager) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 3 with PooledPlcDriverManager

use of org.apache.plc4x.java.utils.connectionpool.PooledPlcDriverManager 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 4 with PooledPlcDriverManager

use of org.apache.plc4x.java.utils.connectionpool.PooledPlcDriverManager in project plc4x by apache.

the class ManualS7PlcDriverMT method simpleLoop.

// public static final String CONN_STRING = "s7://10.10.64.20/0/1";
// public static final String FIELD_STRING = "%DB3:DBD32:DINT";
@Test
public void simpleLoop() {
    PlcDriverManager plcDriverManager = new PooledPlcDriverManager();
    DescriptiveStatistics statistics = new DescriptiveStatistics();
    for (int i = 1; i <= 1000; i++) {
        double timeNs = runSingleRequest(plcDriverManager);
        statistics.addValue(timeNs);
    }
    printStatistics(statistics);
}
Also used : DescriptiveStatistics(org.apache.commons.math3.stat.descriptive.DescriptiveStatistics) PlcDriverManager(org.apache.plc4x.java.PlcDriverManager) PooledPlcDriverManager(org.apache.plc4x.java.utils.connectionpool.PooledPlcDriverManager) PooledPlcDriverManager(org.apache.plc4x.java.utils.connectionpool.PooledPlcDriverManager) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with PooledPlcDriverManager

use of org.apache.plc4x.java.utils.connectionpool.PooledPlcDriverManager in project plc4x by apache.

the class ManualS7PlcDriverMT method scheduledCancellingLoop.

@ParameterizedTest
@MethodSource("periodAndRus")
public void scheduledCancellingLoop(int period, int numberOfRuns) throws InterruptedException, PlcConnectionException {
    System.out.println("Starting iteration with period " + period + " and " + numberOfRuns + " runs.");
    PlcDriverManager plcDriverManager = new PooledPlcDriverManager();
    ScheduledExecutorService executorService = Executors.newScheduledThreadPool(10);
    DescriptiveStatistics statistics = new DescriptiveStatistics();
    AtomicInteger counter = new AtomicInteger(0);
    // Warmup
    plcDriverManager.getConnection(CONN_STRING);
    Runnable iteration = new Runnable() {

        @Override
        public void run() {
            // System.out.println("Setting a request / guard...");
            CompletableFuture<Double> requestFuture = CompletableFuture.supplyAsync(() -> ManualS7PlcDriverMT.this.runSingleRequest(plcDriverManager));
            executorService.schedule(() -> {
                if (!requestFuture.isDone()) {
                    requestFuture.cancel(true);
                    System.out.print("!");
                } else {
                    System.out.print(".");
                    try {
                        statistics.addValue(requestFuture.get());
                    } catch (InterruptedException | ExecutionException e) {
                    // do nothing...
                    }
                }
                if (counter.getAndIncrement() >= numberOfRuns) {
                    executorService.shutdown();
                }
            }, period, TimeUnit.MILLISECONDS);
        }
    };
    executorService.scheduleAtFixedRate(iteration, 0, period, TimeUnit.MILLISECONDS);
    executorService.awaitTermination(100, TimeUnit.SECONDS);
    // Print statistics
    ManualS7PlcDriverMT.this.printStatistics(statistics);
}
Also used : DescriptiveStatistics(org.apache.commons.math3.stat.descriptive.DescriptiveStatistics) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PlcDriverManager(org.apache.plc4x.java.PlcDriverManager) PooledPlcDriverManager(org.apache.plc4x.java.utils.connectionpool.PooledPlcDriverManager) PooledPlcDriverManager(org.apache.plc4x.java.utils.connectionpool.PooledPlcDriverManager) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

PooledPlcDriverManager (org.apache.plc4x.java.utils.connectionpool.PooledPlcDriverManager)11 PlcDriverManager (org.apache.plc4x.java.PlcDriverManager)8 DescriptiveStatistics (org.apache.commons.math3.stat.descriptive.DescriptiveStatistics)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 TriggeredScraperImpl (org.apache.plc4x.java.scraper.triggeredscraper.TriggeredScraperImpl)3 TriggerCollectorImpl (org.apache.plc4x.java.scraper.triggeredscraper.triggerhandler.collector.TriggerCollectorImpl)3 Test (org.junit.jupiter.api.Test)3 AbstractConfig (org.apache.kafka.common.config.AbstractConfig)2 ScraperConfiguration (org.apache.plc4x.java.scraper.config.ScraperConfiguration)2 TriggerCollector (org.apache.plc4x.java.scraper.triggeredscraper.triggerhandler.collector.TriggerCollector)2 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 SecureRandom (java.security.SecureRandom)1 LocalDate (java.time.LocalDate)1 LocalDateTime (java.time.LocalDateTime)1 LocalTime (java.time.LocalTime)1 java.util (java.util)1 List (java.util.List)1 Map (java.util.Map)1