Search in sources :

Example 6 with ChannelDoc

use of io.openems.api.doc.ChannelDoc 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)

Example 7 with ChannelDoc

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

the class ModbusTcpApiController method updateChannelMapping.

protected void updateChannelMapping(Optional<JsonObject> jMappingOpt) {
    processImage.clearMapping();
    ThingRepository thingRepository = ThingRepository.getInstance();
    if (jMappingOpt.isPresent()) {
        JsonObject jMapping = jMappingOpt.get();
        for (Entry<String, JsonElement> entry : jMapping.entrySet()) {
            try {
                int ref = Integer.parseInt(entry.getKey());
                ChannelAddress channelAddress = ChannelAddress.fromString(JsonUtils.getAsString(entry.getValue()));
                Optional<ChannelDoc> channelDocOpt = thingRepository.getChannelDoc(channelAddress);
                if (channelDocOpt.isPresent()) {
                    processImage.addMapping(ref, channelAddress, channelDocOpt.get());
                } else {
                    Optional<Channel> channelOpt = thingRepository.getChannel(channelAddress);
                    if (channelOpt.isPresent()) {
                        throw new OpenemsException("ChannelDoc for channel [" + channelAddress + "] is not available.");
                    } else {
                        throw new OpenemsException("Channel [" + channelAddress + "] does not exist.");
                    }
                }
            } catch (Exception e) {
                log.error("Unable to add channel mapping: " + e.getMessage());
            }
        }
    }
}
Also used : ConfigChannel(io.openems.api.channel.ConfigChannel) Channel(io.openems.api.channel.Channel) JsonObject(com.google.gson.JsonObject) ChannelAddress(io.openems.common.types.ChannelAddress) OpenemsException(io.openems.common.exceptions.OpenemsException) ChannelDoc(io.openems.api.doc.ChannelDoc) OpenemsException(io.openems.common.exceptions.OpenemsException) ModbusException(com.ghgande.j2mod.modbus.ModbusException) ThingRepository(io.openems.core.ThingRepository) JsonElement(com.google.gson.JsonElement)

Aggregations

ChannelDoc (io.openems.api.doc.ChannelDoc)7 ThingDoc (io.openems.api.doc.ThingDoc)6 Channel (io.openems.api.channel.Channel)5 ConfigChannel (io.openems.api.channel.ConfigChannel)4 ReadChannel (io.openems.api.channel.ReadChannel)4 OpenemsException (io.openems.common.exceptions.OpenemsException)4 Field (java.lang.reflect.Field)4 Method (java.lang.reflect.Method)4 ThingStateChannel (io.openems.api.channel.ThingStateChannel)3 WriteChannel (io.openems.api.channel.WriteChannel)3 Thing (io.openems.api.thing.Thing)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 ChannelAddress (io.openems.common.types.ChannelAddress)2 Entry (java.util.Map.Entry)2 ModbusException (com.ghgande.j2mod.modbus.ModbusException)1 BiMap (com.google.common.collect.BiMap)1