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