]> git.basschouten.com Git - openhab-addons.git/blob
f6dcb1c93e4909d2745e920bb549096b0a492e49
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.solarwatt.internal.domain.model;
14
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;
17
18 import java.math.BigDecimal;
19 import java.math.BigInteger;
20
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;
28
29 import com.google.gson.Gson;
30 import com.google.gson.GsonBuilder;
31 import com.google.gson.JsonObject;
32
33 /**
34  * Class planning the energy flow out/into the grid.
35  *
36  * This fields have been identified to exist:
37  * com.kiwigrid.kiwiapp.gridflow.GridFlow=[
38  * ConfigTimeshifting,
39  * ConfigEvStationControl,
40  * FractionPVTestLimit,
41  * ConfigInverterControl,
42  * LogLevel,
43  * PowerSetpoint,
44  * ConfigPeakshaving,
45  * ToEMRequestTag,
46  * CurrentLimit,
47  * ConfigBatteryControl,
48  * ConfigForecastBatteryControl,
49  * ConfigEvStationChargingControl,
50  * ConfigSgReady,
51  * ToCloudDataTag
52  * ]
53  *
54  * @author Sven Carstens - Initial contribution
55  */
56 @NonNullByDefault
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);
60
61     private @Nullable ConfigInverterControl configInverterControl;
62
63     public GridFlow(DeviceDTO deviceDTO) {
64         super(deviceDTO);
65     }
66
67     @Override
68     public void update(DeviceDTO deviceDTO) {
69         super.update(deviceDTO);
70
71         this.addAmpereQuantity(CHANNEL_CURRENT_LIMIT, deviceDTO, true);
72
73         try {
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);
80         }
81
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)),
88                         Units.PERCENT);
89                 this.stateValues.put(CHANNEL_FEED_IN_LIMIT.getChannelName(), state);
90
91                 this.addChannel(CHANNEL_FEED_IN_LIMIT.getChannelName(), Units.PERCENT, "status", false);
92             }
93         }
94     }
95
96     @Override
97     protected String getSolarWattLabel() {
98         return "GridFlow";
99     }
100
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;
113
114         public @Nullable BigDecimal getFeedInLimit() {
115             return this.feedInLimit;
116         }
117
118         public @Nullable BigDecimal getTestModeFeedInLimit() {
119             return this.testModeFeedInLimit;
120         }
121
122         public @Nullable ControllerParameter getControllerParameter() {
123             return this.controllerParameter;
124         }
125
126         public @Nullable Boolean getDynamicControllingOn() {
127             return this.dynamicControllingOn;
128         }
129
130         public @Nullable DynamicControllerParameter getDynamicControllerParameter() {
131             return this.dynamicControllerParameter;
132         }
133
134         public @Nullable Boolean getOn() {
135             return this.on;
136         }
137
138         public @Nullable Boolean getErrorPVLimiting() {
139             return this.isErrorPVLimiting;
140         }
141
142         public @Nullable Boolean getActiveTestMode() {
143             return this.activeTestMode;
144         }
145
146         public @Nullable Boolean getPVPlantConfigured() {
147             return this.isPVPlantConfigured;
148         }
149
150         public @Nullable String getSelectedRcr() {
151             return this.selectedRcr;
152         }
153
154         public @Nullable Boolean getLimitByRcr() {
155             return this.limitByRcr;
156         }
157
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;
165
166             public @Nullable BigDecimal getIntegrationRate() {
167                 return this.integrationRate;
168             }
169
170             public @Nullable BigInteger getOutputRampRateLimit() {
171                 return this.outputRampRateLimit;
172             }
173
174             public @Nullable BigDecimal getDifferentialRate() {
175                 return this.differentialRate;
176             }
177
178             public @Nullable BigDecimal getProportionalRate() {
179                 return this.proportionalRate;
180             }
181
182             public @Nullable BigInteger getOutputValueLimit() {
183                 return this.outputValueLimit;
184             }
185
186             public @Nullable BigInteger getControlFaultSumLimit() {
187                 return this.controlFaultSumLimit;
188             }
189         }
190
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;
198
199             public @Nullable BigDecimal getIntegrationRate() {
200                 return this.integrationRate;
201             }
202
203             public @Nullable BigInteger getOutputRampRateLimit() {
204                 return this.outputRampRateLimit;
205             }
206
207             public @Nullable BigDecimal getDifferentialRate() {
208                 return this.differentialRate;
209             }
210
211             public @Nullable BigDecimal getProportionalRate() {
212                 return this.proportionalRate;
213             }
214
215             public @Nullable BigInteger getOutputValueLimit() {
216                 return this.outputValueLimit;
217             }
218
219             public @Nullable BigInteger getControlFaultSumLimit() {
220                 return this.controlFaultSumLimit;
221             }
222         }
223     }
224 }