Search in sources :

Example 1 with ScraperConfiguration

use of org.apache.plc4x.java.scraper.config.ScraperConfiguration in project plc4x by apache.

the class Plc4XConsumer method startTriggered.

private void startTriggered() throws ScraperException {
    ScraperConfiguration configuration = getScraperConfig(validateTags());
    TriggerCollector collector = new TriggerCollectorImpl(plc4XEndpoint.getPlcDriverManager());
    TriggeredScraperImpl scraper = new TriggeredScraperImpl(configuration, (job, alias, response) -> {
        try {
            Exchange exchange = plc4XEndpoint.createExchange();
            exchange.getIn().setBody(response);
            getProcessor().process(exchange);
        } catch (Exception e) {
            getExceptionHandler().handleException(e);
        }
    }, collector);
    scraper.start();
    collector.start();
}
Also used : ScraperConfiguration(org.apache.plc4x.java.scraper.config.ScraperConfiguration) TriggeredScraperImpl(org.apache.plc4x.java.scraper.triggeredscraper.TriggeredScraperImpl) Exchange(org.apache.camel.Exchange) TriggerCollector(org.apache.plc4x.java.scraper.triggeredscraper.triggerhandler.collector.TriggerCollector) TriggerCollectorImpl(org.apache.plc4x.java.scraper.triggeredscraper.triggerhandler.collector.TriggerCollectorImpl) ScraperException(org.apache.plc4x.java.scraper.exception.ScraperException) PlcIncompatibleDatatypeException(org.apache.plc4x.java.api.exceptions.PlcIncompatibleDatatypeException)

Example 2 with ScraperConfiguration

use of org.apache.plc4x.java.scraper.config.ScraperConfiguration 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 3 with ScraperConfiguration

use of org.apache.plc4x.java.scraper.config.ScraperConfiguration in project plc4x by apache.

the class TriggeredScraperImplTest method scrapeMultipleTargets.

/**
 * Test is added because we assume some strange behavior.
 */
@Test
void scrapeMultipleTargets() throws ScraperException, IOException, InterruptedException {
    // Prepare the Mocking
    // Scrate Jobs 1 and 2
    when(mockDevice1.read("%DB810:DBB0:USINT")).thenReturn(new ResponseItem<>(PlcResponseCode.OK, new PlcLINT(1L)));
    when(mockDevice2.read("%DB810:DBB0:USINT")).thenReturn(new ResponseItem<>(PlcResponseCode.OK, new PlcLINT(2L)));
    // Trigger Jobs
    // Trigger var
    Random rand = new Random();
    when(mockDevice1.read(("%M0.3:BOOL"))).thenAnswer(invocationOnMock -> {
        boolean trigger = rand.nextBoolean();
        System.out.println(trigger);
        return new ResponseItem<>(PlcResponseCode.OK, new PlcBOOL(trigger));
    });
    when(mockDevice2.read(("%M0.3:BOOL"))).thenAnswer(invocationOnMock -> {
        boolean trigger = rand.nextBoolean();
        System.out.println("\t\t" + trigger);
        return new ResponseItem<>(PlcResponseCode.OK, new PlcBOOL(trigger));
    });
    // Read var
    when(mockDevice1.read("%DB810:DBW0:INT")).thenReturn(new ResponseItem<>(PlcResponseCode.OK, new PlcLINT(3L)));
    when(mockDevice2.read("%DB810:DBW0:INT")).thenReturn(new ResponseItem<>(PlcResponseCode.OK, new PlcLINT(4L)));
    ScraperConfiguration configuration = ScraperConfiguration.fromFile("src/test/resources/mock-scraper-config.yml", ScraperConfigurationClassicImpl.class);
    TriggerCollector triggerCollector = new TriggerCollectorImpl(driverManager);
    TriggeredScraperImpl scraper = new TriggeredScraperImpl((j, a, m) -> System.out.printf("Results from %s/%s: %s%n", j, a, m), driverManager, configuration.getJobs(), triggerCollector, 1000);
    scraper.start();
    new Timer().schedule(new TimerTask() {

        @Override
        public void run() {
            scraper.stop();
        }
    }, TimeUnit.SECONDS.toMillis(2));
}
Also used : PlcBOOL(org.apache.plc4x.java.spi.values.PlcBOOL) TriggerCollector(org.apache.plc4x.java.scraper.triggeredscraper.triggerhandler.collector.TriggerCollector) ScraperConfiguration(org.apache.plc4x.java.scraper.config.ScraperConfiguration) Random(java.util.Random) Timer(java.util.Timer) TimerTask(java.util.TimerTask) TriggerCollectorImpl(org.apache.plc4x.java.scraper.triggeredscraper.triggerhandler.collector.TriggerCollectorImpl) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem) PlcLINT(org.apache.plc4x.java.spi.values.PlcLINT) Test(org.junit.jupiter.api.Test)

Example 4 with ScraperConfiguration

use of org.apache.plc4x.java.scraper.config.ScraperConfiguration in project plc4x by apache.

the class Plc4xSchemaFactory method create.

@Override
public Schema create(SchemaPlus parentSchema, String name, Map<String, Object> operand) {
    // Fetch config
    Object config = operand.get("config");
    Validate.notNull(config, "No configuration file given. Please specify operand 'config'...'");
    // Load configuration from file
    ScraperConfiguration configuration;
    try {
        configuration = ScraperConfiguration.fromFile(config.toString(), ScraperConfigurationTriggeredImpl.class);
    } catch (IOException e) {
        throw new IllegalArgumentException("Unable to load configuration file!", e);
    }
    // Fetch limit
    Object limit = operand.get("limit");
    Validate.notNull(limit, "No limit for the number of rows for a table. Please specify operand 'config'...'");
    long parsedLimit;
    try {
        parsedLimit = Long.parseLong(limit.toString());
    } catch (NumberFormatException e) {
        throw new IllegalArgumentException("Given limit '" + limit + "' cannot be parsed to valid long!", e);
    }
    // Pass the configuration to the Schema
    try {
        return new Plc4xSchema(configuration, parsedLimit);
    } catch (ScraperException e) {
        LOGGER.warn("Could not evaluate Plc4xSchema", e);
        // ToDo Exception, but interface does not accept ... null is fishy
        return null;
    }
}
Also used : ScraperConfiguration(org.apache.plc4x.java.scraper.config.ScraperConfiguration) ScraperConfigurationTriggeredImpl(org.apache.plc4x.java.scraper.config.triggeredscraper.ScraperConfigurationTriggeredImpl) ScraperException(org.apache.plc4x.java.scraper.exception.ScraperException) IOException(java.io.IOException)

Example 5 with ScraperConfiguration

use of org.apache.plc4x.java.scraper.config.ScraperConfiguration in project plc4x by apache.

the class ScraperRunner method main.

public static void main(String[] args) throws IOException, ScraperException {
    ScraperConfiguration configuration = ScraperConfiguration.fromFile("plc4j/utils/scraper/src/test/resources/example.yml", ScraperConfigurationClassicImpl.class);
    Scraper scraper = new ScraperImpl(configuration, (j, a, m) -> LOGGER.info("Results from {}/{}: {}", j, a, m));
    scraper.start();
}
Also used : ScraperConfiguration(org.apache.plc4x.java.scraper.config.ScraperConfiguration)

Aggregations

ScraperConfiguration (org.apache.plc4x.java.scraper.config.ScraperConfiguration)7 TriggerCollectorImpl (org.apache.plc4x.java.scraper.triggeredscraper.triggerhandler.collector.TriggerCollectorImpl)4 TriggeredScraperImpl (org.apache.plc4x.java.scraper.triggeredscraper.TriggeredScraperImpl)3 TriggerCollector (org.apache.plc4x.java.scraper.triggeredscraper.triggerhandler.collector.TriggerCollector)3 PlcDriverManager (org.apache.plc4x.java.PlcDriverManager)2 ScraperException (org.apache.plc4x.java.scraper.exception.ScraperException)2 PooledPlcDriverManager (org.apache.plc4x.java.utils.connectionpool.PooledPlcDriverManager)2 Test (org.junit.jupiter.api.Test)2 IOException (java.io.IOException)1 List (java.util.List)1 Map (java.util.Map)1 Random (java.util.Random)1 Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1 Exchange (org.apache.camel.Exchange)1 PlcIncompatibleDatatypeException (org.apache.plc4x.java.api.exceptions.PlcIncompatibleDatatypeException)1 JobConfiguration (org.apache.plc4x.java.scraper.config.JobConfiguration)1 ScraperConfigurationTriggeredImpl (org.apache.plc4x.java.scraper.config.triggeredscraper.ScraperConfigurationTriggeredImpl)1 ResponseItem (org.apache.plc4x.java.spi.messages.utils.ResponseItem)1 PlcBOOL (org.apache.plc4x.java.spi.values.PlcBOOL)1