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