use of io.openems.api.channel.thingstate.ThingStateEnum in project openems by OpenEMS.
the class FaultsAndWarningsTranspiler method getJson.
private static JsonObject getJson() throws ReflectionException, OpenemsException {
JsonObject j = new JsonObject();
for (Class<ThingStateEnum> clazz : getEnums()) {
ThingStateInfo annotation = clazz.getAnnotation(ThingStateInfo.class);
if (annotation == null) {
System.err.println("@ThingStateInfo is missing for Enum [" + clazz.getName() + "]");
continue;
}
// Find existing Thing definition or create new one
for (Class<?> thingClazz : annotation.reference()) {
String thingClassName = thingClazz.getName();
JsonObject jThing = JsonUtils.getAsOptionalJsonObject(j, thingClassName).orElse(new JsonObject());
// Parse enum constants
ThingStateEnum[] enoms = clazz.getEnumConstants();
JsonObject jState = new JsonObject();
for (ThingStateEnum enom : enoms) {
String name = splitCamelCase(enom.toString());
jState.addProperty(String.valueOf(enom.getValue()), name);
}
// Is it Fault or Warning?
if (FaultEnum.class.isAssignableFrom(clazz)) {
jThing.add("faults", jState);
} else if (WarningEnum.class.isAssignableFrom(clazz)) {
jThing.add("warnings", jState);
} else {
throw new OpenemsException("Neither Fault nor Warning in Enum [" + clazz.getName() + "]");
}
j.add(thingClassName, jThing);
}
}
return j;
}
use of io.openems.api.channel.thingstate.ThingStateEnum in project openems by OpenEMS.
the class FaultsAndWarningsTranspiler method getEnums.
@SuppressWarnings("unchecked")
private static Set<Class<ThingStateEnum>> getEnums() throws ReflectionException {
String topLevelPackage = "io.openems.impl";
Class<ThingStateEnum> searchClazz = ThingStateEnum.class;
Set<Class<ThingStateEnum>> clazzes = new HashSet<>();
try {
ClassPath classpath = ClassPath.from(ClassLoader.getSystemClassLoader());
for (ClassPath.ClassInfo classInfo : classpath.getTopLevelClassesRecursive(topLevelPackage)) {
Class<?> thisClazz = classInfo.load();
if (searchClazz.isAssignableFrom(thisClazz)) {
clazzes.add((Class<ThingStateEnum>) thisClazz);
}
}
} catch (IllegalArgumentException | IOException e) {
throw new ReflectionException(e.getMessage());
}
return clazzes;
}
Aggregations