use of io.openems.api.exception.ReflectionException in project openems by OpenEMS.
the class SupplyBusSwitchController method generateSupplybuses.
private List<Supplybus> generateSupplybuses() {
if (esss.valueOptional().isPresent() && supplyBusConfig.valueOptional().isPresent() && switchDelay.valueOptional().isPresent()) {
List<Supplybus> buses = new ArrayList<>();
for (JsonElement bus : supplyBusConfig.valueOptional().get()) {
try {
String name = JsonUtils.getAsString(bus, "bus");
String primaryEssName = JsonUtils.getAsString(bus, "primaryEss");
JsonElement supplybusOnIndicationElement = bus.getAsJsonObject().get("supplybusOnIndication");
JsonElement loads = bus.getAsJsonObject().get("loads");
Optional<Channel> supplybusOnIndication = Optional.empty();
if (supplybusOnIndicationElement != null) {
supplybusOnIndication = repo.getChannelByAddress(supplybusOnIndicationElement.getAsString());
}
List<WriteChannel<Long>> loadChannels = new ArrayList<>();
if (loads != null) {
for (JsonElement load : loads.getAsJsonArray()) {
Optional<Channel> loadChannel = repo.getChannelByAddress(load.getAsString());
if (loadChannel.isPresent() && loadChannel.get() instanceof WriteChannel<?>) {
@SuppressWarnings("unchecked") WriteChannel<Long> writeChannel = (WriteChannel<Long>) loadChannel.get();
loadChannels.add(writeChannel);
}
}
}
Ess primaryEss = getEss(primaryEssName);
HashMap<Ess, WriteChannel<Boolean>> switchEssMapping = new HashMap<>();
JsonArray switches = JsonUtils.getAsJsonArray(bus, "switches");
for (JsonElement e : switches) {
try {
String essName = JsonUtils.getAsString(e, "ess");
try {
Ess ess = getEss(essName);
String channelAddress = JsonUtils.getAsString(e, "switchAddress");
Optional<Channel> outputChannel = repo.getChannelByAddress(channelAddress);
if (ess != null) {
if (outputChannel.isPresent() && outputChannel.get() instanceof WriteChannel<?>) {
@SuppressWarnings("unchecked") WriteChannel<Boolean> channel = (WriteChannel<Boolean>) outputChannel.get();
channel.required();
switchEssMapping.put(ess, channel);
} else {
log.error(channelAddress + " not found!");
}
} else {
log.error(essName + "not found!");
}
} catch (InvalidValueException e1) {
log.error(essName + " is missing in the 'esss' config parameter", e1);
}
} catch (ReflectionException e2) {
log.error("can't find JsonElement 'ess' or 'switchAddress'!", e2);
}
}
WriteChannel<Long> supplybusOnIndicationChannel = null;
if (supplybusOnIndication.isPresent()) {
if (supplybusOnIndication.get() instanceof WriteChannel<?>) {
@SuppressWarnings("unchecked") WriteChannel<Long> writeChannel = (WriteChannel<Long>) supplybusOnIndication.get();
supplybusOnIndicationChannel = writeChannel;
}
}
Supplybus sb = new Supplybus(switchEssMapping, name, primaryEss, switchDelay.value(), supplybusOnIndicationChannel, loadChannels);
buses.add(sb);
} catch (ReflectionException e) {
log.error("can't find JsonElement 'bus' or 'switches' in config parameter 'supplyBuses'!", e);
} catch (InvalidValueException e3) {
log.error("primaryEss not found", e3);
} catch (OpenemsException e4) {
log.error(e4.getMessage());
}
}
return buses;
} else {
return null;
}
}
use of io.openems.api.exception.ReflectionException in project openems by OpenEMS.
the class Config method getJson.
/**
* Gets the Config as Json in the given format
*
* @param format
* @return
* @throws NotImplementedException
*/
public synchronized JsonObject getJson(ConfigFormat format, Role role, String language) throws NotImplementedException {
JsonObject jConfig = new JsonObject();
if (format == ConfigFormat.FILE) {
/*
* Prepare Json in format for config.json file
*/
// Bridge
jConfig.add("things", getBridgesJson(format, role));
// Scheduler
jConfig.add("scheduler", getSchedulerJson(format, role));
// Persistence
jConfig.add("persistence", getPersistenceJson(format, role));
// Users
jConfig.add("users", getUsersJson());
} else {
/*
* Prepare Json in format for OpenEMS UI
*/
// things...
JsonObject jThings = new JsonObject();
Set<Thing> things = ThingRepository.getInstance().getThings();
for (Thing thing : things) {
JsonObject jThing = (JsonObject) ConfigUtils.getAsJsonElement(thing, format, role);
jThings.add(thing.id(), jThing);
}
jConfig.add("things", jThings);
// meta...
JsonObject jMeta = new JsonObject();
try {
Iterable<ThingDoc> availableThings = ClassRepository.getInstance().getAvailableThings();
for (ThingDoc availableThing : availableThings) {
jMeta.add(availableThing.getClazz().getName(), availableThing.getAsJsonObject());
}
} catch (ReflectionException e) {
log.error(e.getMessage());
e.printStackTrace();
}
jConfig.add("meta", jMeta);
}
return jConfig;
}
use of io.openems.api.exception.ReflectionException in project openems by OpenEMS.
the class ConfigUtils method getLongArrayFromConfig.
private static Object getLongArrayFromConfig(ConfigChannel<?> channel, JsonElement j) throws ReflectionException {
/*
* Get "Field" in Channels parent class
*/
Field field;
try {
field = channel.parent().getClass().getField(channel.id());
} catch (NoSuchFieldException | SecurityException e) {
throw new ReflectionException("Field for ConfigChannel [" + channel.address() + "] is not named [" + channel.id() + "] in [" + channel.getClass().getSimpleName() + "]");
}
/*
* Get expected Object Type (List, Set, simple Object)
*/
Type expectedObjectType = ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];
if (expectedObjectType instanceof ParameterizedType) {
expectedObjectType = ((ParameterizedType) expectedObjectType).getRawType();
}
Class<?> expectedObjectClass = (Class<?>) expectedObjectType;
if (Collection.class.isAssignableFrom(expectedObjectClass)) {
if (j.isJsonArray()) {
Set<Long[]> erg = new HashSet<>();
for (JsonElement e : j.getAsJsonArray()) {
if (e.isJsonArray()) {
JsonArray arr = e.getAsJsonArray();
Long[] larr = new Long[arr.size()];
for (int i = 0; i < arr.size(); i++) {
larr[i] = arr.get(i).getAsLong();
}
erg.add(larr);
} else {
throw new ReflectionException("The Json object for ConfigChannel [" + channel.address() + "] is no twodimensional array!");
}
}
if (Set.class.isAssignableFrom(expectedObjectClass)) {
return erg;
} else if (List.class.isAssignableFrom(expectedObjectClass)) {
return new ArrayList<>(erg);
} else {
throw new ReflectionException("Only List and Set ConfigChannels are currently implemented, not [" + expectedObjectClass + "]. ConfigChannel [" + channel.address() + "]");
}
} else {
throw new ReflectionException("The Json object for ConfigChannel [" + channel.address() + "] is no array!");
}
} else {
if (j.isJsonArray()) {
JsonArray arr = j.getAsJsonArray();
Long[] larr = new Long[arr.size()];
for (int i = 0; i < arr.size(); i++) {
larr[i] = arr.get(i).getAsLong();
}
return larr;
} else {
throw new ReflectionException("The Json object for ConfigChannel [" + channel.address() + "] is no array!");
}
}
}
use of io.openems.api.exception.ReflectionException in project openems by OpenEMS.
the class ConfigUtils method getAvailableClasses.
public static Set<Class<? extends Thing>> getAvailableClasses(String topLevelPackage, Class<? extends Thing> clazz, String suffix) throws ReflectionException {
Set<Class<? extends Thing>> clazzes = new HashSet<>();
try {
ClassPath classpath = ClassPath.from(ClassLoader.getSystemClassLoader());
for (ClassPath.ClassInfo classInfo : classpath.getTopLevelClassesRecursive(topLevelPackage)) {
if (classInfo.getName().endsWith(suffix)) {
Class<?> thisClazz = classInfo.load();
if (clazz.isAssignableFrom(thisClazz)) {
@SuppressWarnings("unchecked") Class<? extends Thing> thisThingClazz = (Class<? extends Thing>) thisClazz;
clazzes.add(thisThingClazz);
}
}
}
} catch (IllegalArgumentException | IOException e) {
throw new ReflectionException(e.getMessage());
}
return clazzes;
}
use of io.openems.api.exception.ReflectionException in project openems by OpenEMS.
the class InjectionUtils method getInstance.
// private final static Logger log = LoggerFactory.getLogger(InjectionUtils.class);
/**
* Creates an instance of the given {@link Class}. {@link Object} arguments are optional.
*
* Restriction: this implementation tries only the first constructor of the Class.
*
* @param clazz
* @param args
* @return
* @throws ConfigException
*/
public static Object getInstance(Class<?> clazz, Object... args) throws ReflectionException {
try {
if (args.length == 0) {
return clazz.newInstance();
} else {
Constructor<?>[] constructors = clazz.getConstructors();
Constructor<?> constructor = null;
for (Constructor<?> ct : constructors) {
List<Class<?>> types = new ArrayList<>(Arrays.asList(ct.getParameterTypes()));
if (types.size() == args.length) {
boolean isType = true;
for (Object arg : args) {
boolean isAssignable = false;
for (Class<?> type : types) {
if (type.isAssignableFrom(arg.getClass())) {
isAssignable = true;
}
}
if (!isAssignable) {
isType = false;
}
}
if (isType) {
constructor = ct;
}
}
}
if (constructor != null) {
return constructor.newInstance(args);
} else {
throw new ReflectionException("Unable to instantiate class [" + clazz.getName() + "] no matching constructor found.");
}
}
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | IllegalArgumentException | NullPointerException e) {
e.printStackTrace();
throw new ReflectionException("Unable to instantiate class [" + clazz.getName() + "]: " + e.getMessage());
}
}
Aggregations