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