Search in sources :

Example 1 with SimulatedField

use of org.apache.plc4x.java.simulated.field.SimulatedField in project plc4x by apache.

the class SimulatedConnection method read.

@Override
public CompletableFuture<PlcReadResponse> read(PlcReadRequest readRequest) {
    Map<String, ResponseItem<PlcValue>> fields = new HashMap<>();
    for (String fieldName : readRequest.getFieldNames()) {
        SimulatedField field = (SimulatedField) readRequest.getField(fieldName);
        Optional<PlcValue> valueOptional = device.get(field);
        ResponseItem<PlcValue> fieldPair;
        boolean present = valueOptional.isPresent();
        fieldPair = present ? new ResponseItem<>(PlcResponseCode.OK, valueOptional.get()) : new ResponseItem<>(PlcResponseCode.NOT_FOUND, null);
        fields.put(fieldName, fieldPair);
    }
    PlcReadResponse response = new DefaultPlcReadResponse(readRequest, fields);
    return CompletableFuture.completedFuture(response);
}
Also used : PlcReadResponse(org.apache.plc4x.java.api.messages.PlcReadResponse) DefaultPlcReadResponse(org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse) PlcValue(org.apache.plc4x.java.api.value.PlcValue) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DefaultPlcReadResponse(org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem) SimulatedField(org.apache.plc4x.java.simulated.field.SimulatedField)

Example 2 with SimulatedField

use of org.apache.plc4x.java.simulated.field.SimulatedField in project plc4x by apache.

the class SimulatedDevice method addEventSubscription.

public void addEventSubscription(Consumer<PlcValue> consumer, PlcSubscriptionHandle handle, PlcSubscriptionField plcField) {
    LOGGER.debug("Adding event subscription: {}, {}, {}", consumer, handle, plcField);
    assert plcField instanceof DefaultPlcSubscriptionField;
    Future<?> submit = pool.submit(() -> {
        LOGGER.debug("WORKER: starting for {}, {}, {}", consumer, handle, plcField);
        while (!Thread.currentThread().isInterrupted()) {
            LOGGER.debug("WORKER: running for {}, {}, {}", consumer, handle, plcField);
            PlcField innerPlcField = ((DefaultPlcSubscriptionField) plcField).getPlcField();
            assert innerPlcField instanceof SimulatedField;
            PlcValue baseDefaultPlcValue = state.get(innerPlcField);
            if (baseDefaultPlcValue == null) {
                LOGGER.debug("WORKER: no value for {}, {}, {}", consumer, handle, plcField);
                continue;
            }
            LOGGER.debug("WORKER: accepting {} for {}, {}, {}", baseDefaultPlcValue, consumer, handle, plcField);
            consumer.accept(baseDefaultPlcValue);
            try {
                long sleepTime = Math.min(random.nextInt((int) TimeUnit.SECONDS.toNanos(5)), TimeUnit.MILLISECONDS.toNanos(500));
                LOGGER.debug("WORKER: sleeping {} milliseconds for {}, {}, {}", TimeUnit.NANOSECONDS.toMillis(sleepTime), consumer, handle, plcField);
                TimeUnit.NANOSECONDS.sleep(sleepTime);
            } catch (InterruptedException ignore) {
                Thread.currentThread().interrupt();
                LOGGER.debug("WORKER: got interrupted for {}, {}, {}", consumer, handle, plcField);
                return;
            }
        }
    });
    eventSubscriptions.put(handle, submit);
}
Also used : DefaultPlcSubscriptionField(org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionField) PlcField(org.apache.plc4x.java.api.model.PlcField) SimulatedField(org.apache.plc4x.java.simulated.field.SimulatedField)

Example 3 with SimulatedField

use of org.apache.plc4x.java.simulated.field.SimulatedField in project plc4x by apache.

the class SimulatedDevice method addCyclicSubscription.

public void addCyclicSubscription(Consumer<PlcValue> consumer, PlcSubscriptionHandle handle, PlcSubscriptionField plcField, Duration duration) {
    LOGGER.debug("Adding cyclic subscription: {}, {}, {}, {}", consumer, handle, plcField, duration);
    assert plcField instanceof DefaultPlcSubscriptionField;
    ScheduledFuture<?> scheduledFuture = scheduler.scheduleAtFixedRate(() -> {
        PlcField innerPlcField = ((DefaultPlcSubscriptionField) plcField).getPlcField();
        assert innerPlcField instanceof SimulatedField;
        PlcValue baseDefaultPlcValue = state.get(innerPlcField);
        if (baseDefaultPlcValue == null) {
            return;
        }
        consumer.accept(baseDefaultPlcValue);
    }, duration.toMillis(), duration.toMillis(), TimeUnit.MILLISECONDS);
    cyclicSubscriptions.put(handle, scheduledFuture);
}
Also used : DefaultPlcSubscriptionField(org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionField) PlcField(org.apache.plc4x.java.api.model.PlcField) SimulatedField(org.apache.plc4x.java.simulated.field.SimulatedField)

Example 4 with SimulatedField

use of org.apache.plc4x.java.simulated.field.SimulatedField in project plc4x by apache.

the class SimulatedDeviceTest method read.

@Test
public void read() {
    SimulatedDevice device = new SimulatedDevice("foobar");
    SimulatedField field = SimulatedField.of("STATE/bar:Integer");
    Optional<PlcValue> value = device.get(field);
    assertFalse(value.isPresent());
    device.set(field, new PlcLINT(42));
    value = device.get(field);
    assertTrue(value.isPresent());
    PlcValue plcValue = value.get();
    assertEquals(42L, plcValue.getLong());
}
Also used : PlcValue(org.apache.plc4x.java.api.value.PlcValue) SimulatedField(org.apache.plc4x.java.simulated.field.SimulatedField) PlcLINT(org.apache.plc4x.java.spi.values.PlcLINT) Test(org.junit.jupiter.api.Test)

Example 5 with SimulatedField

use of org.apache.plc4x.java.simulated.field.SimulatedField in project plc4x by apache.

the class SimulatedConnection method write.

@Override
public CompletableFuture<PlcWriteResponse> write(PlcWriteRequest writeRequest) {
    Map<String, PlcResponseCode> fields = new HashMap<>();
    for (String fieldName : writeRequest.getFieldNames()) {
        SimulatedField field = (SimulatedField) writeRequest.getField(fieldName);
        PlcValue value = writeRequest.getPlcValue(fieldName);
        device.set(field, value);
        fields.put(fieldName, PlcResponseCode.OK);
    }
    PlcWriteResponse response = new DefaultPlcWriteResponse(writeRequest, fields);
    return CompletableFuture.completedFuture(response);
}
Also used : PlcValue(org.apache.plc4x.java.api.value.PlcValue) DefaultPlcWriteResponse(org.apache.plc4x.java.spi.messages.DefaultPlcWriteResponse) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PlcWriteResponse(org.apache.plc4x.java.api.messages.PlcWriteResponse) DefaultPlcWriteResponse(org.apache.plc4x.java.spi.messages.DefaultPlcWriteResponse) SimulatedField(org.apache.plc4x.java.simulated.field.SimulatedField) PlcResponseCode(org.apache.plc4x.java.api.types.PlcResponseCode)

Aggregations

SimulatedField (org.apache.plc4x.java.simulated.field.SimulatedField)6 PlcValue (org.apache.plc4x.java.api.value.PlcValue)4 HashMap (java.util.HashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 PlcField (org.apache.plc4x.java.api.model.PlcField)2 DefaultPlcSubscriptionField (org.apache.plc4x.java.spi.model.DefaultPlcSubscriptionField)2 Test (org.junit.jupiter.api.Test)2 PlcReadResponse (org.apache.plc4x.java.api.messages.PlcReadResponse)1 PlcWriteResponse (org.apache.plc4x.java.api.messages.PlcWriteResponse)1 PlcResponseCode (org.apache.plc4x.java.api.types.PlcResponseCode)1 DefaultPlcReadResponse (org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse)1 DefaultPlcWriteResponse (org.apache.plc4x.java.spi.messages.DefaultPlcWriteResponse)1 ResponseItem (org.apache.plc4x.java.spi.messages.utils.ResponseItem)1 PlcLINT (org.apache.plc4x.java.spi.values.PlcLINT)1