Search in sources :

Example 1 with ThingStateEnum

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;
}
Also used : ThingStateEnum(io.openems.api.channel.thingstate.ThingStateEnum) ThingStateInfo(io.openems.common.types.ThingStateInfo) JsonObject(com.google.gson.JsonObject) WarningEnum(io.openems.api.channel.thingstate.WarningEnum) OpenemsException(io.openems.common.exceptions.OpenemsException)

Example 2 with ThingStateEnum

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;
}
Also used : ClassPath(com.google.common.reflect.ClassPath) ReflectionException(io.openems.api.exception.ReflectionException) ThingStateEnum(io.openems.api.channel.thingstate.ThingStateEnum) IOException(java.io.IOException) HashSet(java.util.HashSet)

Aggregations

ThingStateEnum (io.openems.api.channel.thingstate.ThingStateEnum)2 ClassPath (com.google.common.reflect.ClassPath)1 JsonObject (com.google.gson.JsonObject)1 WarningEnum (io.openems.api.channel.thingstate.WarningEnum)1 ReflectionException (io.openems.api.exception.ReflectionException)1 OpenemsException (io.openems.common.exceptions.OpenemsException)1 ThingStateInfo (io.openems.common.types.ThingStateInfo)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1