use of org.apache.plc4x.java.spi.values.PlcLINT 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.spi.values.PlcLINT 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.PlcLINT in project plc4x by apache.
the class CANOpenProtocolLogic method readInternally.
private void readInternally(PlcReadRequest readRequest, CANOpenSDOField field, CompletableFuture<PlcReadResponse> response) {
String fieldName = readRequest.getFieldNames().iterator().next();
final RequestTransactionManager.RequestTransaction transaction = tm.startRequest();
CompletableFuture<PlcValue> callback = new CompletableFuture<>();
callback.whenComplete((value, error) -> {
if (error != null) {
Map<String, ResponseItem<PlcValue>> fields = new HashMap<>();
if (error instanceof CANOpenAbortException) {
fields.put(fieldName, new ResponseItem<>(PlcResponseCode.REMOTE_ERROR, new PlcLINT(((CANOpenAbortException) error).getAbortCode())));
} else {
fields.put(fieldName, new ResponseItem<>(PlcResponseCode.REMOTE_ERROR, null));
}
response.complete(new DefaultPlcReadResponse(readRequest, fields));
transaction.endRequest();
return;
}
Map<String, ResponseItem<PlcValue>> fields = new HashMap<>();
fields.put(fieldName, new ResponseItem<>(PlcResponseCode.OK, value));
response.complete(new DefaultPlcReadResponse(readRequest, fields));
transaction.endRequest();
});
SDOUploadConversation upload = new SDOUploadConversation(conversation, field.getNodeId(), field.getAnswerNodeId(), new IndexAddress(field.getIndex(), field.getSubIndex()), field.getCanOpenDataType());
transaction.submit(() -> upload.execute(callback));
}
use of org.apache.plc4x.java.spi.values.PlcLINT in project plc4x by apache.
the class PlcEntityManagerComplexTest method read.
@Test
public void read() throws OPMException, PlcConnectionException {
Map<String, PlcValue> results = new HashMap<>();
String prefix = MyEntity.class.getName() + ".";
results.put(prefix + "counter", new PlcDINT(1));
results.put(prefix + "counter2", new PlcLINT(1L));
PlcEntityManager manager = getPlcEntityManager(results);
MyEntity myEntity = manager.read(MyEntity.class, "s7://localhost:5555/0/0");
assertEquals(1, (long) myEntity.getCounter());
assertEquals(1, myEntity.getCounter2());
}
Aggregations