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