use of org.apache.plc4x.java.spi.values.PlcBOOL 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.spi.values.PlcBOOL in project plc4x by apache.
the class ModbusProtocolLogic method readBooleanList.
private PlcValue readBooleanList(int count, byte[] data) throws ParseException {
ReadBuffer io = new ReadBufferByteBased(data);
if (count == 1) {
return DataItem.staticParse(io, ModbusDataType.BOOL, 1);
}
// Make sure we read in all the bytes. Unfortunately when requesting 9 bytes
// they are ordered like this: 8 7 6 5 4 3 2 1 | 0 0 0 0 0 0 0 9
// Luckily it turns out that this is exactly how BitSet parses byte[]
BitSet bits = BitSet.valueOf(data);
List<PlcValue> result = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
result.add(new PlcBOOL(bits.get(i)));
}
return new PlcList(result);
}
use of org.apache.plc4x.java.spi.values.PlcBOOL in project plc4x by apache.
the class FirmataProtocolLogic method publishDigitalEvents.
protected void publishDigitalEvents(BitSet changedBits, BitSet bitValues) {
// If nothing changed, no need to do anything.
if (changedBits.cardinality() == 0) {
return;
}
// Try sending the subscription event to all listeners.
for (Map.Entry<DefaultPlcConsumerRegistration, Consumer<PlcSubscriptionEvent>> entry : consumers.entrySet()) {
final DefaultPlcConsumerRegistration registration = entry.getKey();
final Consumer<PlcSubscriptionEvent> consumer = entry.getValue();
// Only if the current data point matches the subscription, publish the event to it.
for (PlcSubscriptionHandle handle : registration.getSubscriptionHandles()) {
if (handle instanceof FirmataSubscriptionHandle) {
FirmataSubscriptionHandle subscriptionHandle = (FirmataSubscriptionHandle) handle;
// (The bit subscribed to in this field actually changed).
if (subscriptionHandle.getField() instanceof FirmataFieldDigital) {
FirmataFieldDigital digitalField = (FirmataFieldDigital) subscriptionHandle.getField();
// send out an update event with all of its current values.
if (digitalField.getBitSet().intersects(changedBits)) {
List<PlcValue> values = new ArrayList<>(digitalField.getBitSet().cardinality());
for (int i = 0; i < digitalField.getBitSet().length(); i++) {
values.add(new PlcBOOL(bitValues.get(i)));
}
sendUpdateEvents(consumer, subscriptionHandle.getName(), values);
}
}
}
}
}
}
Aggregations