Search in sources :

Example 6 with Thing

use of io.openems.api.thing.Thing in project openems by OpenEMS.

the class ThingRepository method removeThing.

/**
 * Remove a Thing from the Repository.
 *
 * @param thing
 */
public synchronized void removeThing(String thingId) {
    Thing thing = thingIds.get(thingId);
    removeThing(thing);
}
Also used : Thing(io.openems.api.thing.Thing)

Example 7 with Thing

use of io.openems.api.thing.Thing in project openems by OpenEMS.

the class ThingRepository method getChannel.

public Optional<Channel> getChannel(String thingId, String channelId) {
    Thing thing = thingIds.get(thingId);
    if (thing == null) {
        return Optional.empty();
    }
    Map<String, Channel> channels = thingChannels.row(thing);
    Channel channel = channels.get(channelId);
    return Optional.ofNullable(channel);
}
Also used : ReadChannel(io.openems.api.channel.ReadChannel) WriteChannel(io.openems.api.channel.WriteChannel) ConfigChannel(io.openems.api.channel.ConfigChannel) ThingStateChannel(io.openems.api.channel.ThingStateChannel) Channel(io.openems.api.channel.Channel) Thing(io.openems.api.thing.Thing)

Example 8 with Thing

use of io.openems.api.thing.Thing in project openems by OpenEMS.

the class ConfigUtils method getAsJsonElement.

/**
 * Converts an object to a JsonElement
 *
 * @param value
 * @return
 * @throws NotImplementedException
 */
public static JsonElement getAsJsonElement(Object value, ConfigFormat format, Role role) throws NotImplementedException {
    // null
    if (value == null) {
        return null;
    }
    // optional
    if (value instanceof Optional<?>) {
        if (!((Optional<?>) value).isPresent()) {
            return null;
        } else {
            value = ((Optional<?>) value).get();
        }
    }
    try {
        /*
			 * test for simple types
			 */
        return JsonUtils.getAsJsonElement(value);
    } catch (NotImplementedException e) {
        ;
    }
    if (value instanceof Thing) {
        /*
			 * type Thing
			 */
        Thing thing = (Thing) value;
        JsonObject j = new JsonObject();
        if (format == ConfigFormat.OPENEMS_UI || !thing.id().startsWith("_")) {
            // ignore generated id names starting with "_"
            j.addProperty("id", thing.id());
            j.addProperty("alias", thing.getAlias());
        }
        // for file-format class is not needed for DeviceNatures
        j.addProperty("class", thing.getClass().getCanonicalName());
        ThingRepository thingRepository = ThingRepository.getInstance();
        for (ConfigChannel<?> channel : thingRepository.getConfigChannels(thing)) {
            if (channel.isReadAllowed(role)) {
                // check read permissions
                JsonElement jChannel = null;
                jChannel = ConfigUtils.getAsJsonElement(channel, format, role);
                if (jChannel != null) {
                    j.add(channel.id(), jChannel);
                }
            }
        }
        // for Bridge: add 'devices' array of thingIds
        if (value instanceof Bridge) {
            Bridge bridge = (Bridge) value;
            JsonArray jDevices = new JsonArray();
            for (Device device : bridge.getDevices()) {
                jDevices.add(device.id());
            }
            j.add("devices", jDevices);
        }
        return j;
    } else if (value instanceof ConfigChannel<?>) {
        /*
			 * type ConfigChannel
			 */
        ConfigChannel<?> channel = (ConfigChannel<?>) value;
        if (!channel.valueOptional().isPresent()) {
            // no value set
            return null;
        } else if (format == ConfigFormat.FILE && channel.getDefaultValue().equals(channel.valueOptional())) {
            // default value not changed
            return null;
        } else {
            // recursive call
            return ConfigUtils.getAsJsonElement(channel.valueOptional().get(), format, role);
        }
    } else if (value instanceof ThingMap) {
        /*
			 * ThingMap (we need only id)
			 */
        return new JsonPrimitive(((ThingMap) value).id());
    } else if (value instanceof List<?>) {
        /*
			 * List
			 */
        JsonArray jArray = new JsonArray();
        for (Object v : (List<?>) value) {
            jArray.add(ConfigUtils.getAsJsonElement(v, format, role));
        }
        return jArray;
    } else if (value instanceof Set<?>) {
        /*
			 * Set
			 */
        JsonArray jArray = new JsonArray();
        for (Object v : (Set<?>) value) {
            jArray.add(ConfigUtils.getAsJsonElement(v, format, role));
        }
        return jArray;
    }
    throw new NotImplementedException(// 
    "Converter for [" + value + "]" + " of type [" + value.getClass().getSimpleName() + // 
    "]" + " to JSON is not implemented.");
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Optional(java.util.Optional) JsonPrimitive(com.google.gson.JsonPrimitive) Device(io.openems.api.device.Device) ConfigChannel(io.openems.api.channel.ConfigChannel) NotImplementedException(io.openems.common.exceptions.NotImplementedException) JsonObject(com.google.gson.JsonObject) ThingRepository(io.openems.core.ThingRepository) JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) ThingMap(io.openems.api.controller.ThingMap) Thing(io.openems.api.thing.Thing) Bridge(io.openems.api.bridge.Bridge)

Example 9 with Thing

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

Example 10 with Thing

use of io.openems.api.thing.Thing in project openems by OpenEMS.

the class ConfigUtils method getThingFromConfig.

private static Thing getThingFromConfig(Class<? extends Thing> type, JsonElement j, Object... objects) throws OpenemsException {
    String thingId = JsonUtils.getAsString(j, "id");
    ThingRepository thingRepository = ThingRepository.getInstance();
    Optional<Thing> existingThing = thingRepository.getThingById(thingId);
    Thing thing;
    if (existingThing.isPresent()) {
        // reuse existing Thing
        thing = existingThing.get();
    } else {
        // Thing is not existing. Create a new instance
        Object[] args = new Object[objects.length + 1];
        args[0] = thingId;
        for (int i = 1; i < objects.length + 1; i++) {
            args[i] = objects[i - 1];
        }
        thing = InjectionUtils.getThingInstance(type, args);
        log.debug("Add Thing[" + thing.id() + "], Implementation[" + thing.getClass().getSimpleName() + "]");
        thingRepository.addThing(thing);
    }
    // Recursive call to inject config parameters for the newly created Thing
    injectConfigChannels(thingRepository.getConfigChannels(thing), j.getAsJsonObject());
    // thing.init();
    return thing;
}
Also used : ThingRepository(io.openems.core.ThingRepository) JsonObject(com.google.gson.JsonObject) Thing(io.openems.api.thing.Thing)

Aggregations

Thing (io.openems.api.thing.Thing)17 JsonArray (com.google.gson.JsonArray)6 JsonElement (com.google.gson.JsonElement)5 JsonObject (com.google.gson.JsonObject)5 ConfigChannel (io.openems.api.channel.ConfigChannel)4 ThingDoc (io.openems.api.doc.ThingDoc)4 ReflectionException (io.openems.api.exception.ReflectionException)4 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 Channel (io.openems.api.channel.Channel)3 ReadChannel (io.openems.api.channel.ReadChannel)3 ThingMap (io.openems.api.controller.ThingMap)3 Device (io.openems.api.device.Device)3 ChannelDoc (io.openems.api.doc.ChannelDoc)3 Field (java.lang.reflect.Field)3 Bridge (io.openems.api.bridge.Bridge)2 ThingStateChannel (io.openems.api.channel.ThingStateChannel)2 WriteChannel (io.openems.api.channel.WriteChannel)2 DeviceNature (io.openems.api.device.nature.DeviceNature)2 ChargerNature (io.openems.api.device.nature.charger.ChargerNature)2