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.*;
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;
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;
31 * Class that aggregates all devices which are found in one location
32 * and are working together to produce power.
34 * This fields have been identified to exist:
35 * com.kiwigrid.devices.location.Location=[
36 * WorkBufferedFromProducers,
37 * WorkOutFromProducers,
38 * PowerBufferedFromGrid,
40 * WorkConsumedFromStorage,
42 * PowerConsumedFromProducers,
43 * PowerConsumedFromStorage,
44 * PowerOutFromProducers,
45 * DatePowerProductionForecastStart,
47 * PowerProductionForecastNow,
48 * PowerOutFromStorage,
51 * TimePowerProductionForecastGranularity,
54 * DatePowerConsumptionForecastStart,
55 * PowerConsumptionForecastNow,
58 * WorkConsumedFromProducers,
60 * WorkConsumedFromGrid,
61 * PowerConsumptionForecastValues,
64 * PowerConsumedFromGrid,
65 * TimePowerConsumptionForecastGranularity,
71 * WorkBufferedFromGrid,
73 * PowerProductionForecastValues,
74 * LocationGeographical,
75 * PowerBufferedFromProducers,
81 * @author Sven Carstens - Initial contribution
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";
88 private @Nullable IdDevicesMap devicesMap;
90 public Location(DeviceDTO deviceDTO) {
95 public void update(DeviceDTO deviceDTO) {
96 super.update(deviceDTO);
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);
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);
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
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);
144 public IdDevicesMap getDevicesMap() {
145 IdDevicesMap returnDevicesMap = this.devicesMap;
146 if (returnDevicesMap != null) {
147 return returnDevicesMap;
150 return new IdDevicesMap();
154 protected String getSolarWattLabel() {
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;
176 public @Nullable Set<String> getInnerBuffer() {
177 return this.innerBuffer;
180 public @Nullable Set<String> getInnerConsumer() {
181 return this.innerConsumer;
184 public @Nullable Set<String> getPowermeterConsumption() {
185 return this.powermeterConsumption;
188 public @Nullable Set<String> getOuterConsumer() {
189 return this.outerConsumer;
192 public @Nullable Set<String> getOuterBuffer() {
193 return this.outerBuffer;
196 public @Nullable Set<String> getPowermeterProduction() {
197 return this.powermeterProduction;
200 public @Nullable Set<String> getOuterProducer() {
201 return this.outerProducer;
204 public @Nullable Set<String> getInnerProducer() {
205 return this.innerProducer;