Search in sources :

Example 1 with PlcBOOL

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));
}
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 2 with PlcBOOL

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);
}
Also used : PlcList(org.apache.plc4x.java.spi.values.PlcList) PlcBOOL(org.apache.plc4x.java.spi.values.PlcBOOL) BitSet(java.util.BitSet) ArrayList(java.util.ArrayList)

Example 3 with PlcBOOL

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);
                    }
                }
            }
        }
    }
}
Also used : DefaultPlcConsumerRegistration(org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration) PlcBOOL(org.apache.plc4x.java.spi.values.PlcBOOL) PlcSubscriptionHandle(org.apache.plc4x.java.api.model.PlcSubscriptionHandle) FirmataSubscriptionHandle(org.apache.plc4x.java.firmata.readwrite.model.FirmataSubscriptionHandle) Consumer(java.util.function.Consumer) FirmataFieldDigital(org.apache.plc4x.java.firmata.readwrite.field.FirmataFieldDigital) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

PlcBOOL (org.apache.plc4x.java.spi.values.PlcBOOL)3 ArrayList (java.util.ArrayList)1 BitSet (java.util.BitSet)1 Random (java.util.Random)1 Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Consumer (java.util.function.Consumer)1 PlcSubscriptionHandle (org.apache.plc4x.java.api.model.PlcSubscriptionHandle)1 FirmataFieldDigital (org.apache.plc4x.java.firmata.readwrite.field.FirmataFieldDigital)1 FirmataSubscriptionHandle (org.apache.plc4x.java.firmata.readwrite.model.FirmataSubscriptionHandle)1 ScraperConfiguration (org.apache.plc4x.java.scraper.config.ScraperConfiguration)1 TriggerCollector (org.apache.plc4x.java.scraper.triggeredscraper.triggerhandler.collector.TriggerCollector)1 TriggerCollectorImpl (org.apache.plc4x.java.scraper.triggeredscraper.triggerhandler.collector.TriggerCollectorImpl)1 ResponseItem (org.apache.plc4x.java.spi.messages.utils.ResponseItem)1 DefaultPlcConsumerRegistration (org.apache.plc4x.java.spi.model.DefaultPlcConsumerRegistration)1 PlcLINT (org.apache.plc4x.java.spi.values.PlcLINT)1 PlcList (org.apache.plc4x.java.spi.values.PlcList)1 Test (org.junit.jupiter.api.Test)1