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;
}
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);
}
}
}
Aggregations