Search in sources :

Example 1 with ThingStateInfo

use of io.openems.common.types.ThingStateInfo 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)

Aggregations

JsonObject (com.google.gson.JsonObject)1 ThingStateEnum (io.openems.api.channel.thingstate.ThingStateEnum)1 WarningEnum (io.openems.api.channel.thingstate.WarningEnum)1 OpenemsException (io.openems.common.exceptions.OpenemsException)1 ThingStateInfo (io.openems.common.types.ThingStateInfo)1