Search in sources :

Example 6 with ThingDoc

use of io.openems.api.doc.ThingDoc in project openems by OpenEMS.

the class ThingRepository method getChannelDoc.

/**
 * Returns the ChannelDoc for a given Channel
 *
 * @param channelAddress
 * @return
 */
public synchronized Optional<ChannelDoc> getChannelDoc(ChannelAddress channelAddress) {
    Thing thing = getThing(channelAddress.getThingId());
    if (thing == null) {
        return Optional.empty();
    }
    ThingDoc thingDoc = ClassRepository.getInstance().getThingDoc(thing.getClass());
    Optional<ChannelDoc> channelDoc = thingDoc.getChannelDoc(channelAddress.getChannelId());
    return channelDoc;
}
Also used : Thing(io.openems.api.thing.Thing) ChannelDoc(io.openems.api.doc.ChannelDoc) ThingDoc(io.openems.api.doc.ThingDoc)

Example 7 with ThingDoc

use of io.openems.api.doc.ThingDoc in project openems by OpenEMS.

the class ThingRepository method thingChannelsUpdated.

@Override
public void thingChannelsUpdated(Thing thing) {
    // remove Channels from thingChannels, thingWriteChannels
    Databus databus = Databus.getInstance();
    Set<Entry<String, Channel>> thingRow = thingChannels.row(thing).entrySet();
    Iterator<Entry<String, Channel>> i = thingRow.iterator();
    while (i.hasNext()) {
        Entry<String, Channel> thingChannel = i.next();
        if (!(thingChannel.getValue() instanceof ConfigChannel)) {
            thingChannel.getValue().removeChangeListener(databus);
            thingChannel.getValue().removeUpdateListener(databus);
            i.remove();
        }
    }
    thingWriteChannels.removeAll(thing);
    // Add Channels to thingChannels, thingConfigChannels and thingWriteChannels
    ThingDoc thingDoc = classRepository.getThingDoc(thing.getClass());
    for (ChannelDoc channelDoc : thingDoc.getChannelDocs()) {
        Member member = channelDoc.getMember();
        try {
            List<Channel> channels = new ArrayList<>();
            boolean ignoreEmpty = false;
            if (member instanceof Method) {
                if (((Method) member).getReturnType().isArray()) {
                    // ignore e.g. if getFaultChannels is returning an empty array
                    ignoreEmpty = true;
                    Channel[] ch = (Channel[]) ((Method) member).invoke(thing);
                    for (Channel c : ch) {
                        channels.add(c);
                    }
                } else {
                    // It's a Method with ReturnType Channel
                    channels.add((Channel) ((Method) member).invoke(thing));
                }
            } else if (member instanceof Field) {
                // It's a Field with Type Channel
                channels.add((Channel) ((Field) member).get(thing));
            } else {
                continue;
            }
            if (!ignoreEmpty && channels.isEmpty()) {
                log.warn("Channel is returning null! Thing [" + thing.id() + "], Member [" + member.getName() + "]");
                continue;
            }
            for (Channel channel : channels) {
                if (channel != null) {
                    // Add Channel to thingChannels
                    thingChannels.put(thing, channel.id(), channel);
                    // Add Channel to writeChannels
                    if (channel instanceof WriteChannel) {
                        thingWriteChannels.put(thing, (WriteChannel<?>) channel);
                    }
                    // Register Databus as listener
                    if (channel instanceof ReadChannel) {
                        ((ReadChannel<?>) channel).addUpdateListener(databus);
                        ((ReadChannel<?>) channel).addChangeListener(databus);
                    }
                }
            }
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            log.warn("Unable to add Channel. Member [" + member.getName() + "]", e);
        }
    }
}
Also used : ConfigChannel(io.openems.api.channel.ConfigChannel) 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) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) ChannelDoc(io.openems.api.doc.ChannelDoc) InvocationTargetException(java.lang.reflect.InvocationTargetException) ReadChannel(io.openems.api.channel.ReadChannel) Field(java.lang.reflect.Field) Entry(java.util.Map.Entry) WriteChannel(io.openems.api.channel.WriteChannel) Member(java.lang.reflect.Member) ThingDoc(io.openems.api.doc.ThingDoc)

Aggregations

ThingDoc (io.openems.api.doc.ThingDoc)7 ChannelDoc (io.openems.api.doc.ChannelDoc)6 Channel (io.openems.api.channel.Channel)4 ReadChannel (io.openems.api.channel.ReadChannel)4 Thing (io.openems.api.thing.Thing)4 Field (java.lang.reflect.Field)4 Method (java.lang.reflect.Method)4 ConfigChannel (io.openems.api.channel.ConfigChannel)3 ThingStateChannel (io.openems.api.channel.ThingStateChannel)3 WriteChannel (io.openems.api.channel.WriteChannel)3 OpenemsException (io.openems.common.exceptions.OpenemsException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Member (java.lang.reflect.Member)3 ArrayList (java.util.ArrayList)3 JsonObject (com.google.gson.JsonObject)2 Device (io.openems.api.device.Device)2 LinkedList (java.util.LinkedList)2 Entry (java.util.Map.Entry)2 BiMap (com.google.common.collect.BiMap)1 HashBasedTable (com.google.common.collect.HashBasedTable)1