Search in sources :

Example 1 with PlcIncompatibleDatatypeException

use of org.apache.plc4x.java.api.exceptions.PlcIncompatibleDatatypeException in project plc4x by apache.

the class PlcValues method of.

public static PlcValue of(Object o) {
    if (o == null) {
        return new PlcNull();
    }
    try {
        String simpleName = o.getClass().getSimpleName();
        Class<?> clazz = o.getClass();
        if (o instanceof List) {
            simpleName = "List";
            clazz = List.class;
        } else if (clazz.isArray()) {
            simpleName = "List";
            clazz = List.class;
            Object[] objectArray = (Object[]) o;
            o = Arrays.asList(objectArray);
        }
        // If it's one of the LocalDate, LocalTime or LocalDateTime, cut off the "Local".
        if (simpleName.startsWith("Local")) {
            simpleName = simpleName.substring(5);
        }
        Constructor<?> constructor = Class.forName(PlcValues.class.getPackage().getName() + ".Plc" + simpleName).getDeclaredConstructor(clazz);
        return ((PlcValue) constructor.newInstance(o));
    } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException | ClassNotFoundException e) {
        LOGGER.warn("Cannot wrap", e);
        throw new PlcIncompatibleDatatypeException(o.getClass());
    }
}
Also used : InvocationTargetException(java.lang.reflect.InvocationTargetException) PlcIncompatibleDatatypeException(org.apache.plc4x.java.api.exceptions.PlcIncompatibleDatatypeException) List(java.util.List)

Example 2 with PlcIncompatibleDatatypeException

use of org.apache.plc4x.java.api.exceptions.PlcIncompatibleDatatypeException in project plc4x by apache.

the class Plc4XConsumer method startUnTriggered.

private void startUnTriggered() {
    PlcReadRequest.Builder builder = plcConnection.readRequestBuilder();
    for (Map.Entry<String, Object> tag : tags.entrySet()) {
        try {
            builder.addItem(tag.getKey(), (String) tag.getValue());
        } catch (PlcIncompatibleDatatypeException e) {
            LOGGER.error("For consumer, please use Map<String,String>, currently using {}", tags.getClass().getSimpleName());
        }
    }
    PlcReadRequest request = builder.build();
    future = executorService.schedule(() -> request.execute().thenAccept(response -> {
        try {
            Exchange exchange = plc4XEndpoint.createExchange();
            Map<String, Object> rsp = new HashMap<>();
            for (String field : response.getFieldNames()) {
                rsp.put(field, response.getObject(field));
            }
            exchange.getIn().setBody(rsp);
            getProcessor().process(exchange);
        } catch (Exception e) {
            getExceptionHandler().handleException(e);
        }
    }), 500, TimeUnit.MILLISECONDS);
}
Also used : Exchange(org.apache.camel.Exchange) PlcIncompatibleDatatypeException(org.apache.plc4x.java.api.exceptions.PlcIncompatibleDatatypeException) HashMap(java.util.HashMap) PlcReadRequest(org.apache.plc4x.java.api.messages.PlcReadRequest) HashMap(java.util.HashMap) Map(java.util.Map) ScraperException(org.apache.plc4x.java.scraper.exception.ScraperException) PlcIncompatibleDatatypeException(org.apache.plc4x.java.api.exceptions.PlcIncompatibleDatatypeException)

Aggregations

PlcIncompatibleDatatypeException (org.apache.plc4x.java.api.exceptions.PlcIncompatibleDatatypeException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Exchange (org.apache.camel.Exchange)1 PlcReadRequest (org.apache.plc4x.java.api.messages.PlcReadRequest)1 ScraperException (org.apache.plc4x.java.scraper.exception.ScraperException)1