2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.solarwatt.internal.domain.model;
15 import static org.openhab.binding.solarwatt.internal.SolarwattBindingConstants.CHANNEL_CURRENT_LIMIT;
16 import static org.openhab.binding.solarwatt.internal.SolarwattBindingConstants.CHANNEL_FEED_IN_LIMIT;
18 import java.math.BigDecimal;
19 import java.math.BigInteger;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.solarwatt.internal.domain.dto.DeviceDTO;
24 import org.openhab.core.library.types.QuantityType;
25 import org.openhab.core.library.unit.Units;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 import com.google.gson.Gson;
30 import com.google.gson.GsonBuilder;
31 import com.google.gson.JsonObject;
34 * Class planning the energy flow out/into the grid.
36 * This fields have been identified to exist:
37 * com.kiwigrid.kiwiapp.gridflow.GridFlow=[
39 * ConfigEvStationControl,
40 * FractionPVTestLimit,
41 * ConfigInverterControl,
47 * ConfigBatteryControl,
48 * ConfigForecastBatteryControl,
49 * ConfigEvStationChargingControl,
54 * @author Sven Carstens - Initial contribution
57 public class GridFlow extends Device {
58 public static final String SOLAR_WATT_CLASSNAME = "com.kiwigrid.kiwiapp.gridflow.GridFlow";
59 private final Logger logger = LoggerFactory.getLogger(GridFlow.class);
61 private @Nullable ConfigInverterControl configInverterControl;
63 public GridFlow(DeviceDTO deviceDTO) {
68 public void update(DeviceDTO deviceDTO) {
69 super.update(deviceDTO);
71 this.addAmpereQuantity(CHANNEL_CURRENT_LIMIT, deviceDTO, true);
74 JsonObject rawConfigInverterControl = deviceDTO.getJsonObjectFromTag("ConfigInverterControl");
75 Gson gson = new GsonBuilder().create();
76 this.configInverterControl = gson.fromJson(rawConfigInverterControl, GridFlow.ConfigInverterControl.class);
77 } catch (Exception ex) {
78 this.configInverterControl = null;
79 this.logger.warn("Could not read ConfigInverterControl", ex);
82 GridFlow.ConfigInverterControl localConfigInverterControl = this.configInverterControl;
83 if (localConfigInverterControl != null) {
84 // the only interesting value is the derailing (i.e. limit power flowing into the grid)
85 BigDecimal feedInLimit = localConfigInverterControl.getFeedInLimit();
86 if (feedInLimit != null) {
87 QuantityType<?> state = new QuantityType<>(feedInLimit.multiply(BigDecimal.valueOf(100)),
89 this.stateValues.put(CHANNEL_FEED_IN_LIMIT.getChannelName(), state);
91 this.addChannel(CHANNEL_FEED_IN_LIMIT.getChannelName(), Units.PERCENT, "status", false);
97 protected String getSolarWattLabel() {
101 public static class ConfigInverterControl {
102 private @Nullable BigDecimal testModeFeedInLimit;
103 private @Nullable ControllerParameter controllerParameter;
104 private @Nullable Boolean dynamicControllingOn;
105 private @Nullable BigDecimal feedInLimit;
106 private @Nullable DynamicControllerParameter dynamicControllerParameter;
107 private @Nullable Boolean on;
108 private @Nullable Boolean isErrorPVLimiting;
109 private @Nullable Boolean activeTestMode;
110 private @Nullable Boolean isPVPlantConfigured;
111 private @Nullable String selectedRcr;
112 private @Nullable Boolean limitByRcr;
114 public @Nullable BigDecimal getFeedInLimit() {
115 return this.feedInLimit;
118 public @Nullable BigDecimal getTestModeFeedInLimit() {
119 return this.testModeFeedInLimit;
122 public @Nullable ControllerParameter getControllerParameter() {
123 return this.controllerParameter;
126 public @Nullable Boolean getDynamicControllingOn() {
127 return this.dynamicControllingOn;
130 public @Nullable DynamicControllerParameter getDynamicControllerParameter() {
131 return this.dynamicControllerParameter;
134 public @Nullable Boolean getOn() {
138 public @Nullable Boolean getErrorPVLimiting() {
139 return this.isErrorPVLimiting;
142 public @Nullable Boolean getActiveTestMode() {
143 return this.activeTestMode;
146 public @Nullable Boolean getPVPlantConfigured() {
147 return this.isPVPlantConfigured;
150 public @Nullable String getSelectedRcr() {
151 return this.selectedRcr;
154 public @Nullable Boolean getLimitByRcr() {
155 return this.limitByRcr;
158 public static class ControllerParameter {
159 private @Nullable BigDecimal integrationRate;
160 private @Nullable BigInteger outputRampRateLimit;
161 private @Nullable BigDecimal differentialRate;
162 private @Nullable BigDecimal proportionalRate;
163 private @Nullable BigInteger outputValueLimit;
164 private @Nullable BigInteger controlFaultSumLimit;
166 public @Nullable BigDecimal getIntegrationRate() {
167 return this.integrationRate;
170 public @Nullable BigInteger getOutputRampRateLimit() {
171 return this.outputRampRateLimit;
174 public @Nullable BigDecimal getDifferentialRate() {
175 return this.differentialRate;
178 public @Nullable BigDecimal getProportionalRate() {
179 return this.proportionalRate;
182 public @Nullable BigInteger getOutputValueLimit() {
183 return this.outputValueLimit;
186 public @Nullable BigInteger getControlFaultSumLimit() {
187 return this.controlFaultSumLimit;
191 public static class DynamicControllerParameter {
192 private @Nullable BigDecimal integrationRate;
193 private @Nullable BigInteger outputRampRateLimit;
194 private @Nullable BigDecimal differentialRate;
195 private @Nullable BigDecimal proportionalRate;
196 private @Nullable BigInteger outputValueLimit;
197 private @Nullable BigInteger controlFaultSumLimit;
199 public @Nullable BigDecimal getIntegrationRate() {
200 return this.integrationRate;
203 public @Nullable BigInteger getOutputRampRateLimit() {
204 return this.outputRampRateLimit;
207 public @Nullable BigDecimal getDifferentialRate() {
208 return this.differentialRate;
211 public @Nullable BigDecimal getProportionalRate() {
212 return this.proportionalRate;
215 public @Nullable BigInteger getOutputValueLimit() {
216 return this.outputValueLimit;
219 public @Nullable BigInteger getControlFaultSumLimit() {
220 return this.controlFaultSumLimit;