use of org.apache.plc4x.java.scraper.triggeredscraper.triggerhandler.collector.TriggerCollector 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.triggeredscraper.triggerhandler.collector.TriggerCollector 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.triggeredscraper.triggerhandler.collector.TriggerCollector in project plc4x by apache.
the class Plc4xSourceTask method start.
@Override
public void start(Map<String, String> props) {
AbstractConfig config = new AbstractConfig(CONFIG_DEF, props);
String connectionName = config.getString(Constants.CONNECTION_NAME_CONFIG);
String plc4xConnectionString = config.getString(Constants.CONNECTION_STRING_CONFIG);
pollReturnInterval = config.getInt(Constants.KAFKA_POLL_RETURN_CONFIG);
Integer bufferSize = config.getInt(Constants.BUFFER_SIZE_CONFIG);
Map<String, String> topics = new HashMap<>();
// Create a buffer with a capacity of BUFFER_SIZE_CONFIG elements which schedules access in a fair way.
buffer = new ArrayBlockingQueue<>(bufferSize, true);
ScraperConfigurationTriggeredImplBuilder builder = new ScraperConfigurationTriggeredImplBuilder();
builder.addSource(connectionName, plc4xConnectionString);
List<String> jobConfigs = config.getList(Constants.QUERIES_CONFIG);
for (String jobConfig : jobConfigs) {
String[] jobConfigSegments = jobConfig.split("\\|");
if (jobConfigSegments.length < 4) {
log.warn("Error in job configuration '{}'. " + "The configuration expects at least 4 segments: " + "{job-name}|{topic}|{rate}(|{field-alias}#{field-address})+", jobConfig);
continue;
}
String jobName = jobConfigSegments[0];
String topic = jobConfigSegments[1];
Integer rate = Integer.valueOf(jobConfigSegments[2]);
JobConfigurationTriggeredImplBuilder jobBuilder = builder.job(jobName, String.format("(SCHEDULED,%s)", rate)).source(connectionName);
for (int i = 3; i < jobConfigSegments.length; i++) {
String[] fieldSegments = jobConfigSegments[i].split("#");
if (fieldSegments.length != 2) {
log.warn("Error in job configuration '{}'. " + "The field segment expects a format {field-alias}#{field-address}, but got '%s'", jobName, jobConfigSegments[i]);
continue;
}
String fieldAlias = fieldSegments[0];
String fieldAddress = fieldSegments[1];
jobBuilder.field(fieldAlias, fieldAddress);
topics.put(jobName, topic);
}
jobBuilder.build();
}
ScraperConfigurationTriggeredImpl scraperConfig = builder.build();
try {
PlcDriverManager plcDriverManager = new PooledPlcDriverManager();
TriggerCollector triggerCollector = new TriggerCollectorImpl(plcDriverManager);
scraper = new TriggeredScraperImpl(scraperConfig, (jobName, sourceName, results) -> {
try {
Long timestamp = System.currentTimeMillis();
Map<String, String> sourcePartition = new HashMap<>();
sourcePartition.put("sourceName", sourceName);
sourcePartition.put("jobName", jobName);
Map<String, Long> sourceOffset = Collections.singletonMap("offset", timestamp);
String topic = topics.get(jobName);
// Prepare the key structure.
Struct key = new Struct(KEY_SCHEMA).put(Constants.SOURCE_NAME_FIELD, sourceName).put(Constants.JOB_NAME_FIELD, jobName);
// Build the Schema for the result struct.
SchemaBuilder fieldSchemaBuilder = SchemaBuilder.struct().name("org.apache.plc4x.kafka.schema.Field");
for (Map.Entry<String, Object> result : results.entrySet()) {
// Get field-name and -value from the results.
String fieldName = result.getKey();
Object fieldValue = result.getValue();
// Get the schema for the given value type.
Schema valueSchema = getSchema(fieldValue);
// Add the schema description for the current field.
fieldSchemaBuilder.field(fieldName, valueSchema);
}
Schema fieldSchema = fieldSchemaBuilder.build();
Schema recordSchema = SchemaBuilder.struct().name("org.apache.plc4x.kafka.schema.JobResult").doc("PLC Job result. This contains all of the received PLCValues as well as a recieved timestamp").field(Constants.FIELDS_CONFIG, fieldSchema).field(Constants.TIMESTAMP_CONFIG, Schema.INT64_SCHEMA).field(Constants.EXPIRES_CONFIG, Schema.OPTIONAL_INT64_SCHEMA).build();
// Build the struct itself.
Struct fieldStruct = new Struct(fieldSchema);
for (Map.Entry<String, Object> result : results.entrySet()) {
// Get field-name and -value from the results.
String fieldName = result.getKey();
Object fieldValue = result.getValue();
if (fieldSchema.field(fieldName).schema().type() == Schema.Type.ARRAY) {
fieldStruct.put(fieldName, ((List) fieldValue).stream().map(p -> ((PlcValue) p).getObject()).collect(Collectors.toList()));
} else {
fieldStruct.put(fieldName, fieldValue);
}
}
Struct recordStruct = new Struct(recordSchema).put(Constants.FIELDS_CONFIG, fieldStruct).put(Constants.TIMESTAMP_CONFIG, timestamp);
// Prepare the source-record element.
SourceRecord sourceRecord = new SourceRecord(sourcePartition, sourceOffset, topic, KEY_SCHEMA, key, recordSchema, recordStruct);
// Add the new source-record to the buffer.
buffer.add(sourceRecord);
} catch (Exception e) {
log.error("Error while parsing returned values", e);
}
}, triggerCollector);
scraper.start();
triggerCollector.start();
} catch (ScraperException e) {
log.error("Error starting the scraper", e);
}
}
use of org.apache.plc4x.java.scraper.triggeredscraper.triggerhandler.collector.TriggerCollector in project plc4x by apache.
the class TriggeredScraperRunner method main.
/**
* testing of TriggeredScraper vs real device
*/
public static void main(String[] args) throws IOException, ScraperException {
ScraperConfiguration configuration = ScraperConfiguration.fromFile("plc4j/utils/scraper/src/test/resources/example_triggered_scraper.yml", ScraperConfigurationTriggeredImpl.class);
PlcDriverManager plcDriverManager = new PooledPlcDriverManager();
TriggerCollector triggerCollector = new TriggerCollectorImpl(plcDriverManager);
TriggeredScraperImpl scraper = new TriggeredScraperImpl(configuration, (j, a, m) -> LOGGER.info("Results from {}/{}: {}", j, a, m), triggerCollector);
scraper.start();
triggerCollector.start();
}
Aggregations