Search in sources :

Example 1 with Controller

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;
}
Also used : JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) Controller(io.openems.api.controller.Controller)

Example 2 with Controller

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();
    }
}
Also used : Controller(io.openems.api.controller.Controller)

Example 3 with Controller

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");
    }
}
Also used : JsonArray(com.google.gson.JsonArray) LocalTime(java.time.LocalTime) JsonElement(com.google.gson.JsonElement) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) ConfigException(io.openems.api.exception.ConfigException) TreeMap(java.util.TreeMap) Controller(io.openems.api.controller.Controller)

Example 4 with Controller

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);
        }
    });
}
Also used : ThingRepository(io.openems.core.ThingRepository) WebsocketApiController(io.openems.impl.controller.api.websocket.WebsocketApiController) FeneconPersistence(io.openems.impl.persistence.fenecon.FeneconPersistence) Scheduler(io.openems.api.scheduler.Scheduler) Controller(io.openems.api.controller.Controller) WebsocketApiController(io.openems.impl.controller.api.websocket.WebsocketApiController)

Example 5 with Controller

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;
}
Also used : JsonArray(com.google.gson.JsonArray) Scheduler(io.openems.api.scheduler.Scheduler) JsonObject(com.google.gson.JsonObject) Controller(io.openems.api.controller.Controller)

Aggregations

Controller (io.openems.api.controller.Controller)11 JsonArray (com.google.gson.JsonArray)5 ArrayList (java.util.ArrayList)5 JsonObject (com.google.gson.JsonObject)4 Scheduler (io.openems.api.scheduler.Scheduler)4 JsonElement (com.google.gson.JsonElement)3 Bridge (io.openems.api.bridge.Bridge)2 Device (io.openems.api.device.Device)2 Persistence (io.openems.api.persistence.Persistence)2 OpenemsException (io.openems.common.exceptions.OpenemsException)2 LocalTime (java.time.LocalTime)2 DeviceNature (io.openems.api.device.nature.DeviceNature)1 ConfigException (io.openems.api.exception.ConfigException)1 ReflectionException (io.openems.api.exception.ReflectionException)1 QueryablePersistence (io.openems.api.persistence.QueryablePersistence)1 ThingRepository (io.openems.core.ThingRepository)1 WebsocketApiController (io.openems.impl.controller.api.websocket.WebsocketApiController)1 FeneconPersistence (io.openems.impl.persistence.fenecon.FeneconPersistence)1 DateTimeParseException (java.time.format.DateTimeParseException)1 TreeMap (java.util.TreeMap)1