use of io.openems.api.persistence.QueryablePersistence in project openems by OpenEMS.
the class EdgeWebsocketHandler method historicData.
private void historicData(JsonObject jMessageId, JsonObject jHistoricData) {
// select first QueryablePersistence (by default the running InfluxdbPersistence)
TimedataService timedataSource = null;
for (QueryablePersistence queryablePersistence : ThingRepository.getInstance().getQueryablePersistences()) {
timedataSource = queryablePersistence;
break;
}
if (timedataSource == null) {
WebSocketUtils.sendNotificationOrLogError(this.websocket, new JsonObject(), LogBehaviour.WRITE_TO_LOG, Notification.NO_TIMEDATA_SOURCE_AVAILABLE);
return;
}
JsonArray jData;
try {
jData = timedataSource.queryHistoricData(jHistoricData);
WebSocketUtils.send(this.websocket, DefaultMessages.historicDataQueryReply(jMessageId, jData));
} catch (OpenemsException e) {
WebSocketUtils.sendNotificationOrLogError(this.websocket, jMessageId, LogBehaviour.WRITE_TO_LOG, Notification.UNABLE_TO_QUERY_HISTORIC_DATA, e.getMessage());
}
return;
}
use of io.openems.api.persistence.QueryablePersistence in project openems by OpenEMS.
the class ThingRepository method removeThing.
/**
* Remove a Thing from the Repository.
*
* @param thing
*/
public synchronized void removeThing(Thing thing) {
// Remove from thingIds
thingIds.remove(thing.id());
// Remove from thingClasses
thingClasses.remove(thing.getClass(), thing);
// Remove from bridges
if (thing instanceof Bridge) {
bridges.remove(thing);
}
// Remove from schedulers
if (thing instanceof Scheduler) {
schedulers.remove(thing);
}
// Remove from persistences
if (thing instanceof Persistence) {
persistences.remove(thing);
}
// Remove from queryablePersistences
if (thing instanceof QueryablePersistence) {
queryablePersistences.remove(thing);
}
// Remove from deviceNatures
if (thing instanceof DeviceNature) {
deviceNatures.remove(thing);
}
// Remove controller
if (thing instanceof Controller) {
Controller controller = (Controller) thing;
for (Scheduler scheduler : getSchedulers()) {
scheduler.removeController(controller);
}
}
// Remove device
if (thing instanceof Device) {
for (Bridge bridge : bridges) {
bridge.removeDevice((Device) thing);
}
}
// Remove Listener
thing.removeListener(this);
for (ThingsChangedListener listener : thingListeners) {
listener.thingChanged(thing, Action.REMOVE);
}
}
use of io.openems.api.persistence.QueryablePersistence 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);
}
}
Aggregations