Search in sources :

Example 1 with ThingStateChannel

use of io.openems.api.channel.ThingStateChannel in project openems by OpenEMS.

the class ThingRepository method addThing.

/**
 * Add a Thing to the Repository and cache its Channels and other information for later usage.
 *
 * @param thing
 */
public synchronized void addThing(Thing thing) {
    if (thingIds.containsValue(thing)) {
        // Thing was already added
        return;
    }
    // Add to thingIds
    thingIds.forcePut(thing.id(), thing);
    // Add to thingClasses
    thingClasses.put(thing.getClass(), thing);
    // Add to bridges
    if (thing instanceof Bridge) {
        bridges.add((Bridge) thing);
    }
    // Add to schedulers
    if (thing instanceof Scheduler) {
        schedulers.add((Scheduler) thing);
    }
    // Add to persistences
    if (thing instanceof Persistence) {
        persistences.add((Persistence) thing);
    }
    // Add to queryablePersistences
    if (thing instanceof QueryablePersistence) {
        queryablePersistences.add((QueryablePersistence) thing);
    }
    // Add to device natures
    if (thing instanceof DeviceNature) {
        deviceNatures.add((DeviceNature) thing);
    }
    // Add Listener
    thing.addListener(this);
    // Apply channel annotation (this happens now and again after initializing the thing via init()
    this.applyChannelAnnotation(thing);
    // Add Channels thingConfigChannels
    ThingDoc thingDoc = classRepository.getThingDoc(thing.getClass());
    for (ChannelDoc channelDoc : thingDoc.getChannelDocs()) {
        Member member = channelDoc.getMember();
        try {
            List<Channel> channels = new ArrayList<>();
            java.util.function.Consumer<Channel> addToChannels = (c) -> {
                if (c == null) {
                // TODO this error is not handled properly
                // log.error(
                // "Channel is returning null! Thing [" + thing.id() + "], Member [" + member.getName() + "]");
                } else {
                    channels.add(c);
                }
            };
            if (member instanceof Method) {
                if (((Method) member).getReturnType().isArray()) {
                    Channel[] ch = (Channel[]) ((Method) member).invoke(thing);
                    for (Channel c : ch) {
                        addToChannels.accept(c);
                    }
                } else {
                    // It's a Method with ReturnType Channel
                    Channel c = (Channel) ((Method) member).invoke(thing);
                    addToChannels.accept(c);
                    if (c instanceof ThingStateChannels) {
                        ThingStateChannels tsc = (ThingStateChannels) c;
                        for (ThingStateChannel fc : tsc.getFaultChannels()) {
                            addToChannels.accept(fc);
                        }
                        for (ThingStateChannel wc : tsc.getWarningChannels()) {
                            addToChannels.accept(wc);
                        }
                    }
                }
            } else if (member instanceof Field) {
                // It's a Field with Type Channel
                Channel c = (Channel) ((Field) member).get(thing);
                addToChannels.accept(c);
            } else {
                continue;
            }
            if (channels.isEmpty()) {
                continue;
            }
            for (Channel channel : channels) {
                // Add Channel to thingChannels
                thingChannels.put(thing, channel.id(), channel);
                if (channel instanceof ConfigChannel) {
                    // Add Channel to configChannels
                    thingConfigChannels.put(thing, (ConfigChannel<?>) channel);
                }
            }
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            log.warn("Unable to add Channel. Member [" + member.getName() + "]", e);
        }
    }
    for (ThingsChangedListener listener : thingListeners) {
        listener.thingChanged(thing, Action.ADD);
    }
}
Also used : ReadChannel(io.openems.api.channel.ReadChannel) JsonObject(com.google.gson.JsonObject) Controller(io.openems.api.controller.Controller) OpenemsException(io.openems.common.exceptions.OpenemsException) LoggerFactory(org.slf4j.LoggerFactory) WriteChannel(io.openems.api.channel.WriteChannel) HashBasedTable(com.google.common.collect.HashBasedTable) ConfigChannel(io.openems.api.channel.ConfigChannel) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) HashMultimap(com.google.common.collect.HashMultimap) Bridge(io.openems.api.bridge.Bridge) Map(java.util.Map) DeviceNature(io.openems.api.device.nature.DeviceNature) Scheduler(io.openems.api.scheduler.Scheduler) LinkedList(java.util.LinkedList) Method(java.lang.reflect.Method) BiMap(com.google.common.collect.BiMap) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) Member(java.lang.reflect.Member) Collection(java.util.Collection) JsonUtils(io.openems.common.utils.JsonUtils) Set(java.util.Set) Thing(io.openems.api.thing.Thing) ThingChannelsUpdatedListener(io.openems.api.thing.ThingChannelsUpdatedListener) Field(java.lang.reflect.Field) QueryablePersistence(io.openems.api.persistence.QueryablePersistence) InvocationTargetException(java.lang.reflect.InvocationTargetException) Device(io.openems.api.device.Device) Action(io.openems.core.ThingsChangedListener.Action) List(java.util.List) HashBiMap(com.google.common.collect.HashBiMap) ThingStateChannels(io.openems.api.channel.thingstate.ThingStateChannels) InjectionUtils(io.openems.core.utilities.InjectionUtils) Entry(java.util.Map.Entry) ConfigUtils(io.openems.core.utilities.ConfigUtils) Optional(java.util.Optional) Persistence(io.openems.api.persistence.Persistence) ChannelAddress(io.openems.common.types.ChannelAddress) ChannelDoc(io.openems.api.doc.ChannelDoc) ThingDoc(io.openems.api.doc.ThingDoc) Collections(java.util.Collections) Table(com.google.common.collect.Table) ThingStateChannel(io.openems.api.channel.ThingStateChannel) Channel(io.openems.api.channel.Channel) Scheduler(io.openems.api.scheduler.Scheduler) ConfigChannel(io.openems.api.channel.ConfigChannel) ArrayList(java.util.ArrayList) ThingStateChannels(io.openems.api.channel.thingstate.ThingStateChannels) ThingStateChannel(io.openems.api.channel.ThingStateChannel) Field(java.lang.reflect.Field) QueryablePersistence(io.openems.api.persistence.QueryablePersistence) DeviceNature(io.openems.api.device.nature.DeviceNature) Member(java.lang.reflect.Member) ThingDoc(io.openems.api.doc.ThingDoc) 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) Method(java.lang.reflect.Method) ChannelDoc(io.openems.api.doc.ChannelDoc) InvocationTargetException(java.lang.reflect.InvocationTargetException) QueryablePersistence(io.openems.api.persistence.QueryablePersistence) Persistence(io.openems.api.persistence.Persistence) Bridge(io.openems.api.bridge.Bridge)

Example 2 with ThingStateChannel

use of io.openems.api.channel.ThingStateChannel in project openems by OpenEMS.

the class ThingStateChannels method updateState.

private void updateState() {
    ThingState currentState = ThingState.RUN;
    for (ThingStateChannel faultChannel : faultChannels) {
        if (faultChannel.isValuePresent() && faultChannel.getValue()) {
            currentState = ThingState.FAULT;
        }
    }
    for (ThingStateChannel warningChannel : warningChannels) {
        if (warningChannel.isValuePresent() && warningChannel.getValue() && currentState != ThingState.FAULT) {
            currentState = ThingState.WARNING;
        }
    }
    updateValue(currentState);
}
Also used : ThingStateChannel(io.openems.api.channel.ThingStateChannel)

Example 3 with ThingStateChannel

use of io.openems.api.channel.ThingStateChannel in project openems by OpenEMS.

the class Ess method toString.

@Override
public String toString() {
    StringBuilder b = new StringBuilder();
    b.append(// 
    ess.id() + " [" + "SOC:" + ess.soc().format() + "|");
    if (ess instanceof SymmetricEssNature) {
        SymmetricEssNature e = (SymmetricEssNature) ess;
        b.append("L:" + e.activePower().format() + ";" + e.reactivePower().format());
    }
    if (ess instanceof AsymmetricEssNature && ess instanceof SymmetricEssNature) {
        b.append("|");
    }
    if (ess instanceof AsymmetricEssNature) {
        AsymmetricEssNature e = (AsymmetricEssNature) ess;
        b.append(// 
        "L1:" + e.activePowerL1().format() + ";" + e.reactivePowerL1().format() + "|" + "L2:" + e.activePowerL2().format() + ";" + e.reactivePowerL2().format() + // 
        "|" + "L3:" + e.activePowerL3().format() + ";" + e.reactivePowerL3().format());
    }
    b.append(// 
    "|" + "Allowed:" + ess.allowedCharge().format() + ";" + ess.allowedDischarge().format());
    b.append(// 
    "|" + "GridMode:" + ess.gridMode().labelOptional().orElse("unknown"));
    List<ThingStateChannel> warningChannels = ess.getStateChannel().getWarningChannels().stream().filter(c -> c.isValuePresent() && c.getValue()).collect(Collectors.toList());
    List<ThingStateChannel> faultChannels = ess.getStateChannel().getFaultChannels().stream().filter(c -> c.isValuePresent() && c.getValue()).collect(Collectors.toList());
    if (warningChannels.size() > 0) {
        b.append("|Warn:");
        b.append(warningChannels.stream().map(c -> c.name()).collect(Collectors.joining(",")));
    }
    if (faultChannels.size() > 0) {
        b.append("|Fault:");
        b.append(faultChannels.stream().map(c -> c.name()).collect(Collectors.joining(",")));
    }
    b.append("]");
    return b.toString();
}
Also used : List(java.util.List) SymmetricEssNature(io.openems.api.device.nature.ess.SymmetricEssNature) AsymmetricEssNature(io.openems.api.device.nature.ess.AsymmetricEssNature) IsThingMap(io.openems.api.controller.IsThingMap) ThingMap(io.openems.api.controller.ThingMap) Collectors(java.util.stream.Collectors) ThingStateChannel(io.openems.api.channel.ThingStateChannel) EssNature(io.openems.api.device.nature.ess.EssNature) ThingStateChannel(io.openems.api.channel.ThingStateChannel) SymmetricEssNature(io.openems.api.device.nature.ess.SymmetricEssNature) AsymmetricEssNature(io.openems.api.device.nature.ess.AsymmetricEssNature)

Aggregations

ThingStateChannel (io.openems.api.channel.ThingStateChannel)3 List (java.util.List)2 BiMap (com.google.common.collect.BiMap)1 HashBasedTable (com.google.common.collect.HashBasedTable)1 HashBiMap (com.google.common.collect.HashBiMap)1 HashMultimap (com.google.common.collect.HashMultimap)1 Table (com.google.common.collect.Table)1 JsonObject (com.google.gson.JsonObject)1 Bridge (io.openems.api.bridge.Bridge)1 Channel (io.openems.api.channel.Channel)1 ConfigChannel (io.openems.api.channel.ConfigChannel)1 ReadChannel (io.openems.api.channel.ReadChannel)1 WriteChannel (io.openems.api.channel.WriteChannel)1 ThingStateChannels (io.openems.api.channel.thingstate.ThingStateChannels)1 Controller (io.openems.api.controller.Controller)1 IsThingMap (io.openems.api.controller.IsThingMap)1 ThingMap (io.openems.api.controller.ThingMap)1 Device (io.openems.api.device.Device)1 DeviceNature (io.openems.api.device.nature.DeviceNature)1 AsymmetricEssNature (io.openems.api.device.nature.ess.AsymmetricEssNature)1