]> git.basschouten.com Git - openhab-addons.git/blob
bdffba5a5ce663eb1b89e31d2e39031dcd9f201b
[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.*;
16
17 import java.util.Set;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.solarwatt.internal.domain.dto.DeviceDTO;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import com.google.gson.Gson;
26 import com.google.gson.GsonBuilder;
27 import com.google.gson.JsonObject;
28 import com.google.gson.annotations.SerializedName;
29
30 /**
31  * Class that aggregates all devices which are found in one location
32  * and are working together to produce power.
33  *
34  * This fields have been identified to exist:
35  * com.kiwigrid.devices.location.Location=[
36  * WorkBufferedFromProducers,
37  * WorkOutFromProducers,
38  * PowerBufferedFromGrid,
39  * WorkProduced,
40  * WorkConsumedFromStorage,
41  * PowerSelfConsumed,
42  * PowerConsumedFromProducers,
43  * PowerConsumedFromStorage,
44  * PowerOutFromProducers,
45  * DatePowerProductionForecastStart,
46  * PowerOut,
47  * PowerProductionForecastNow,
48  * PowerOutFromStorage,
49  * IdDevicesMap,
50  * PowerBuffered,
51  * TimePowerProductionForecastGranularity,
52  * WorkOutFromStorage,
53  * CountPersons,
54  * DatePowerConsumptionForecastStart,
55  * PowerConsumptionForecastNow,
56  * PowerProduced,
57  * WorkBuffered,
58  * WorkConsumedFromProducers,
59  * PowerConsumed,
60  * WorkConsumedFromGrid,
61  * PowerConsumptionForecastValues,
62  * PowerSelfSupplied,
63  * WorkReleased,
64  * PowerConsumedFromGrid,
65  * TimePowerConsumptionForecastGranularity,
66  * WorkConsumed,
67  * AddressLocation,
68  * WorkIn,
69  * PriceWorkIn,
70  * PowerIn,
71  * WorkBufferedFromGrid,
72  * WorkSelfConsumed,
73  * PowerProductionForecastValues,
74  * LocationGeographical,
75  * PowerBufferedFromProducers,
76  * PowerReleased,
77  * WorkOut,
78  * WorkSelfSupplied
79  * ]
80  *
81  * @author Sven Carstens - Initial contribution
82  */
83 @NonNullByDefault
84 public class Location extends Device {
85     private final Logger logger = LoggerFactory.getLogger(Location.class);
86     public static final String SOLAR_WATT_CLASSNAME = "com.kiwigrid.devices.location.Location";
87
88     private @Nullable IdDevicesMap devicesMap;
89
90     public Location(DeviceDTO deviceDTO) {
91         super(deviceDTO);
92     }
93
94     @Override
95     public void update(DeviceDTO deviceDTO) {
96         super.update(deviceDTO);
97
98         // values to put on an overview dashboard
99         this.addWattQuantity(CHANNEL_POWER_BUFFERED, deviceDTO);
100         this.addWattQuantity(CHANNEL_POWER_SELF_CONSUMED, deviceDTO);
101         this.addWattQuantity(CHANNEL_POWER_SELF_SUPPLIED, deviceDTO);
102         this.addWattQuantity(CHANNEL_POWER_CONSUMED_FROM_GRID, deviceDTO);
103         this.addWattQuantity(CHANNEL_POWER_CONSUMED_FROM_STORAGE, deviceDTO);
104         this.addWattQuantity(CHANNEL_POWER_CONSUMED, deviceDTO);
105         this.addWattQuantity(CHANNEL_POWER_PRODUCED, deviceDTO);
106         this.addWattQuantity(CHANNEL_POWER_OUT, deviceDTO);
107         this.addWattHourQuantity(CHANNEL_WORK_BUFFERED, deviceDTO);
108         this.addWattHourQuantity(CHANNEL_WORK_SELF_CONSUMED, deviceDTO);
109         this.addWattHourQuantity(CHANNEL_WORK_SELF_SUPPLIED, deviceDTO);
110         this.addWattHourQuantity(CHANNEL_WORK_CONSUMED_FROM_GRID, deviceDTO);
111         this.addWattHourQuantity(CHANNEL_WORK_CONSUMED_FROM_STORAGE, deviceDTO);
112         this.addWattHourQuantity(CHANNEL_WORK_CONSUMED, deviceDTO);
113         this.addWattHourQuantity(CHANNEL_WORK_PRODUCED, deviceDTO);
114         this.addWattHourQuantity(CHANNEL_WORK_OUT, deviceDTO);
115
116         // not necessary for a dashboard, so marked as advanced
117         this.addWattQuantity(CHANNEL_POWER_BUFFERED_FROM_GRID, deviceDTO, true);
118         this.addWattQuantity(CHANNEL_POWER_BUFFERED_FROM_PRODUCERS, deviceDTO, true);
119         this.addWattQuantity(CHANNEL_POWER_CONSUMED_FROM_PRODUCERS, deviceDTO, true);
120         this.addWattQuantity(CHANNEL_POWER_IN, deviceDTO, true);
121         this.addWattQuantity(CHANNEL_POWER_OUT_FROM_PRODUCERS, deviceDTO, true);
122         this.addWattQuantity(CHANNEL_POWER_OUT_FROM_STORAGE, deviceDTO, true);
123         this.addWattQuantity(CHANNEL_POWER_RELEASED, deviceDTO, true);
124         this.addWattHourQuantity(CHANNEL_WORK_BUFFERED_FROM_GRID, deviceDTO, true);
125         this.addWattHourQuantity(CHANNEL_WORK_BUFFERED_FROM_PRODUCERS, deviceDTO, true);
126         this.addWattHourQuantity(CHANNEL_WORK_CONSUMED_FROM_PRODUCERS, deviceDTO, true);
127         this.addWattHourQuantity(CHANNEL_WORK_OUT_FROM_PRODUCERS, deviceDTO, true);
128         this.addWattHourQuantity(CHANNEL_WORK_OUT_FROM_STORAGE, deviceDTO, true);
129         this.addWattHourQuantity(CHANNEL_WORK_RELEASED, deviceDTO, true);
130
131         // read IdDevicesMap to find out which devices are located/metered where
132         // to get the unmetered consumption we need for {@link LocationHandler} to take Location.(Work|Power)Consumed
133         // and subtract the (Work|Power)(AC)In of the OUTER_CONSUMERs
134         try {
135             JsonObject rawDevicesMap = deviceDTO.getJsonObjectFromTag("IdDevicesMap");
136             Gson gson = new GsonBuilder().create();
137             this.devicesMap = gson.fromJson(rawDevicesMap, IdDevicesMap.class);
138         } catch (Exception ex) {
139             this.devicesMap = null;
140             this.logger.warn("Could not read IdDevicesMap", ex);
141         }
142     }
143
144     public IdDevicesMap getDevicesMap() {
145         IdDevicesMap returnDevicesMap = this.devicesMap;
146         if (returnDevicesMap != null) {
147             return returnDevicesMap;
148         }
149
150         return new IdDevicesMap();
151     }
152
153     @Override
154     protected String getSolarWattLabel() {
155         return "Location";
156     }
157
158     public static class IdDevicesMap {
159         @SerializedName("INNER_BUFFER")
160         private @Nullable Set<String> innerBuffer;
161         @SerializedName("INNER_CONSUMER")
162         private @Nullable Set<String> innerConsumer;
163         @SerializedName("POWERMETER_CONSUMPTION")
164         private @Nullable Set<String> powermeterConsumption;
165         @SerializedName("OUTER_CONSUMER")
166         private @Nullable Set<String> outerConsumer;
167         @SerializedName("OUTER_BUFFER")
168         private @Nullable Set<String> outerBuffer;
169         @SerializedName("POWERMETER_PRODUCTION")
170         private @Nullable Set<String> powermeterProduction;
171         @SerializedName("OUTER_PRODUCER")
172         private @Nullable Set<String> outerProducer;
173         @SerializedName("INNER_PRODUCER")
174         private @Nullable Set<String> innerProducer;
175
176         public @Nullable Set<String> getInnerBuffer() {
177             return this.innerBuffer;
178         }
179
180         public @Nullable Set<String> getInnerConsumer() {
181             return this.innerConsumer;
182         }
183
184         public @Nullable Set<String> getPowermeterConsumption() {
185             return this.powermeterConsumption;
186         }
187
188         public @Nullable Set<String> getOuterConsumer() {
189             return this.outerConsumer;
190         }
191
192         public @Nullable Set<String> getOuterBuffer() {
193             return this.outerBuffer;
194         }
195
196         public @Nullable Set<String> getPowermeterProduction() {
197             return this.powermeterProduction;
198         }
199
200         public @Nullable Set<String> getOuterProducer() {
201             return this.outerProducer;
202         }
203
204         public @Nullable Set<String> getInnerProducer() {
205             return this.innerProducer;
206         }
207     }
208 }