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();
}
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();
}
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));
}
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;
}
}
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();
}
Aggregations