use of io.openems.api.controller.Controller in project openems by OpenEMS.
the class ChannelThresholdScheduler method loadThresholds.
private void loadThresholds() throws InvalidValueException {
List<Threshold> thresholdCollection = new ArrayList<>();
JsonArray thresholds = this.thresholds.value();
for (JsonElement e : thresholds) {
if (e.isJsonObject()) {
JsonObject thresholdJson = e.getAsJsonObject();
Threshold t = new Threshold();
t.threshold = thresholdJson.get("threshold").getAsLong();
t.hysteresis = thresholdJson.get("hysteresis").getAsLong();
JsonArray controllers = thresholdJson.get("controller").getAsJsonArray();
for (JsonElement ctr : controllers) {
Controller c = getController(ctr.getAsString());
if (c != null) {
t.controllers.add(c);
} else {
log.error("can't find Controller '" + ctr.getAsString() + "'!");
}
}
if (t.threshold != null) {
if (t.hysteresis != null) {
thresholdCollection.add(t);
} else {
log.error("no hysteresis defined for threshold [" + t.threshold + "]!");
}
} else {
log.error("threshold of element [" + e + "] is not defined.");
}
} else {
log.error(e + " is no jsonobject!");
}
}
Collections.sort(thresholdCollection, (c1, c2) -> c1.threshold.compareTo(c2.threshold));
ControllerHysteresis lastHysteresis = new ControllerHysteresis();
lastHysteresis.min = Long.MIN_VALUE;
for (Threshold t : thresholdCollection) {
ControllerHysteresis ch = new ControllerHysteresis();
ch.min = t.threshold;
ch.below = lastHysteresis;
ch.controllers.addAll(t.controllers);
lastHysteresis.max = t.threshold + t.hysteresis;
lastHysteresis.above = ch;
lastHysteresis = ch;
}
lastHysteresis.max = Long.MAX_VALUE;
if (thresholdChannel.valueOptional().isPresent() && lastHysteresis.max > thresholdChannel.value()) {
while (lastHysteresis.below != null) {
if (lastHysteresis.isBetween(thresholdChannel.value())) {
break;
}
lastHysteresis = lastHysteresis.below;
}
}
activeHysteresis = lastHysteresis;
}
use of io.openems.api.controller.Controller in project openems by OpenEMS.
the class ChannelThresholdScheduler method execute.
@Override
protected void execute() {
// kick the watchdog
SDNotify.sendWatchdog();
List<Controller> controllers = getActiveControllers();
controllers.addAll(getAlwaysController());
Collections.sort(controllers, (c1, c2) -> c2.priority.valueOptional().orElse(Integer.MIN_VALUE) - c1.priority.valueOptional().orElse(Integer.MIN_VALUE));
for (Controller controller : controllers) {
controller.executeRun();
}
}
use of io.openems.api.controller.Controller in project openems by OpenEMS.
the class WeekTimeScheduler method floorController.
private List<Controller> floorController(JsonArray jHours, LocalTime time) throws OpenemsException {
// fill times map; sorted by hour
TreeMap<LocalTime, JsonArray> times = new TreeMap<>();
for (JsonElement jHourElement : jHours) {
JsonObject jHour = JsonUtils.getAsJsonObject(jHourElement);
String hourTime = JsonUtils.getAsString(jHour, "time");
JsonArray jControllers = JsonUtils.getAsJsonArray(jHourElement, "controllers");
times.put(LocalTime.parse(hourTime), jControllers);
}
// return matching controllers
if (times.floorEntry(time) != null) {
List<Controller> controllers = new ArrayList<>();
for (JsonElement jControllerElement : times.floorEntry(time).getValue()) {
String controllerId = JsonUtils.getAsString(jControllerElement);
Controller controller = this.controllers.get(controllerId);
if (controller != null) {
controllers.add(controller);
} else {
throw new ConfigException("Controller [" + controllerId + "] not found.");
}
}
return controllers;
} else {
throw new IndexOutOfBoundsException("No smaller time found");
}
}
use of io.openems.api.controller.Controller in project openems by OpenEMS.
the class WebsocketLogAppender method append.
@Override
protected void append(ILoggingEvent event) {
long timestamp = event.getTimeStamp();
String level = event.getLevel().toString();
String source = event.getLoggerName();
String message = event.getFormattedMessage();
ThingRepository thingRepository = ThingRepository.getInstance();
for (Scheduler scheduler : thingRepository.getSchedulers()) {
for (Controller controller : scheduler.getControllers()) {
if (controller instanceof WebsocketApiController) {
WebsocketApiController websocketApiController = (WebsocketApiController) controller;
websocketApiController.sendLog(timestamp, level, source, message);
}
}
}
// send to fenecon persistence
ThingRepository.getInstance().getPersistences().forEach((persistence) -> {
if (persistence instanceof FeneconPersistence) {
FeneconPersistence p = (FeneconPersistence) persistence;
p.sendLog(timestamp, level, source, message);
}
});
}
use of io.openems.api.controller.Controller in project openems by OpenEMS.
the class Config method getSchedulerJson.
public JsonObject getSchedulerJson(ConfigFormat format, Role role) throws NotImplementedException {
JsonObject jScheduler = null;
for (Scheduler scheduler : thingRepository.getSchedulers()) {
jScheduler = (JsonObject) ConfigUtils.getAsJsonElement(scheduler, format, role);
/*
* Controller
*/
JsonArray jControllers = new JsonArray();
for (Controller controller : scheduler.getControllers()) {
jControllers.add(ConfigUtils.getAsJsonElement(controller, format, role));
}
jScheduler.add("controllers", jControllers);
break;
}
return jScheduler;
}
Aggregations