Search in sources :

Example 1 with PlcLINT

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

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));
}
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 3 with PlcLINT

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));
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) CANOpenAbortException(org.apache.plc4x.java.canopen.transport.CANOpenAbortException) RequestTransactionManager(org.apache.plc4x.java.spi.transaction.RequestTransactionManager) PlcValue(org.apache.plc4x.java.api.value.PlcValue) CompletableFuture(java.util.concurrent.CompletableFuture) SDOUploadConversation(org.apache.plc4x.java.canopen.api.conversation.canopen.SDOUploadConversation) IndexAddress(org.apache.plc4x.java.canopen.readwrite.IndexAddress) DefaultPlcReadResponse(org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem) PlcLINT(org.apache.plc4x.java.spi.values.PlcLINT)

Example 4 with PlcLINT

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());
}
Also used : PlcDINT(org.apache.plc4x.java.spi.values.PlcDINT) HashMap(java.util.HashMap) PlcLINT(org.apache.plc4x.java.spi.values.PlcLINT) Test(org.junit.jupiter.api.Test)

Aggregations

PlcLINT (org.apache.plc4x.java.spi.values.PlcLINT)4 Test (org.junit.jupiter.api.Test)3 HashMap (java.util.HashMap)2 PlcValue (org.apache.plc4x.java.api.value.PlcValue)2 ResponseItem (org.apache.plc4x.java.spi.messages.utils.ResponseItem)2 LinkedHashMap (java.util.LinkedHashMap)1 Random (java.util.Random)1 Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 SDOUploadConversation (org.apache.plc4x.java.canopen.api.conversation.canopen.SDOUploadConversation)1 IndexAddress (org.apache.plc4x.java.canopen.readwrite.IndexAddress)1 CANOpenAbortException (org.apache.plc4x.java.canopen.transport.CANOpenAbortException)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 SimulatedField (org.apache.plc4x.java.simulated.field.SimulatedField)1 DefaultPlcReadResponse (org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse)1 RequestTransactionManager (org.apache.plc4x.java.spi.transaction.RequestTransactionManager)1