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.digitalstrom.internal.lib.structure.devices.impl;
15 import java.util.Arrays;
16 import java.util.List;
18 import org.openhab.binding.digitalstrom.internal.lib.serverconnection.constants.JSONApiResponseKeysEnum;
19 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.AbstractGeneralDeviceInformations;
20 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.Circuit;
21 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.CachedMeteringValue;
22 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.DeviceStateUpdate;
23 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.MeteringTypeEnum;
24 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.MeteringUnitsEnum;
25 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.impl.DeviceStateUpdateImpl;
27 import com.google.gson.JsonObject;
30 * The {@link CircuitImpl} is the implementation of the {@link Circuit} and represent a digitalSTROM circuit.
32 * @author Michael Ochel - Initial contribution
33 * @author Matthias Siegele - Initial contribution
35 public class CircuitImpl extends AbstractGeneralDeviceInformations implements Circuit {
38 private Integer hwVersion;
39 private String hwVersionString;
40 private String swVersion;
41 private Integer armSwVersion;
42 private Integer dspSwVersion;
43 private Integer apiVersion;
44 private String hwName;
45 private Integer busMemberType;
46 private Boolean hasDevices;
47 private Boolean hasMetering;
48 private String vdcConfigURL;
49 private String vdcModelUID;
50 private String vdcHardwareGuid;
51 private String vdcHardwareModelGuid;
52 private String vdcVendorGuid;
53 private String vdcOemGuid;
54 private Boolean ignoreActionsFromNewDevices;
57 private CachedMeteringValue consumption;
58 private CachedMeteringValue energyWh;
59 private CachedMeteringValue energyWs;
60 // currently energyDelta not exist
61 // private CachedMeteringValue energyDeltaWh;
62 // private CachedMeteringValue energyDeltaWs;
65 * Creates a new {@link CircuitImpl} through the digitalSTROM json response as {@link JsonObject}.
67 * @param jObject of the digitalSTROM json response, must not be null
69 public CircuitImpl(JsonObject jObject) {
71 if (jObject.get(JSONApiResponseKeysEnum.HW_VERSION.getKey()) != null) {
72 hwVersion = jObject.get(JSONApiResponseKeysEnum.HW_VERSION.getKey()).getAsInt();
74 if (jObject.get(JSONApiResponseKeysEnum.HW_VERSION_STRING.getKey()) != null) {
75 hwVersionString = jObject.get(JSONApiResponseKeysEnum.HW_VERSION_STRING.getKey()).getAsString();
77 if (jObject.get(JSONApiResponseKeysEnum.SW_VERSION.getKey()) != null) {
78 swVersion = jObject.get(JSONApiResponseKeysEnum.SW_VERSION.getKey()).getAsString();
80 if (jObject.get(JSONApiResponseKeysEnum.ARM_SW_VERSION.getKey()) != null) {
81 armSwVersion = jObject.get(JSONApiResponseKeysEnum.ARM_SW_VERSION.getKey()).getAsInt();
83 if (jObject.get(JSONApiResponseKeysEnum.DSP_SW_VERSION.getKey()) != null) {
84 dspSwVersion = jObject.get(JSONApiResponseKeysEnum.DSP_SW_VERSION.getKey()).getAsInt();
86 if (jObject.get(JSONApiResponseKeysEnum.API_VERSION.getKey()) != null) {
87 apiVersion = jObject.get(JSONApiResponseKeysEnum.API_VERSION.getKey()).getAsInt();
89 if (jObject.get(JSONApiResponseKeysEnum.HW_NAME.getKey()) != null) {
90 hwName = jObject.get(JSONApiResponseKeysEnum.HW_NAME.getKey()).getAsString();
92 if (jObject.get(JSONApiResponseKeysEnum.BUS_MEMBER_TYPE.getKey()) != null) {
93 busMemberType = jObject.get(JSONApiResponseKeysEnum.BUS_MEMBER_TYPE.getKey()).getAsInt();
95 if (jObject.get(JSONApiResponseKeysEnum.HAS_DEVICES.getKey()) != null) {
96 hasDevices = jObject.get(JSONApiResponseKeysEnum.HAS_DEVICES.getKey()).getAsBoolean();
98 if (jObject.get(JSONApiResponseKeysEnum.HAS_METERING.getKey()) != null) {
99 hasMetering = jObject.get(JSONApiResponseKeysEnum.HAS_METERING.getKey()).getAsBoolean();
101 if (jObject.get(JSONApiResponseKeysEnum.VDC_CONFIG_URL.getKey()) != null) {
102 vdcConfigURL = jObject.get(JSONApiResponseKeysEnum.VDC_CONFIG_URL.getKey()).getAsString();
104 if (jObject.get(JSONApiResponseKeysEnum.VDC_MODEL_UID.getKey()) != null) {
105 vdcModelUID = jObject.get(JSONApiResponseKeysEnum.VDC_MODEL_UID.getKey()).getAsString();
107 if (jObject.get(JSONApiResponseKeysEnum.VDC_HARDWARE_GUID.getKey()) != null) {
108 vdcHardwareGuid = jObject.get(JSONApiResponseKeysEnum.VDC_HARDWARE_GUID.getKey()).getAsString();
110 if (jObject.get(JSONApiResponseKeysEnum.VDC_HARDWARE_MODEL_GUID.getKey()) != null) {
111 vdcHardwareModelGuid = jObject.get(JSONApiResponseKeysEnum.VDC_HARDWARE_MODEL_GUID.getKey()).getAsString();
113 if (jObject.get(JSONApiResponseKeysEnum.VDC_VENDOR_GUID.getKey()) != null) {
114 vdcVendorGuid = jObject.get(JSONApiResponseKeysEnum.VDC_VENDOR_GUID.getKey()).getAsString();
116 if (jObject.get(JSONApiResponseKeysEnum.VDC_OEM_GUID.getKey()) != null) {
117 vdcOemGuid = jObject.get(JSONApiResponseKeysEnum.VDC_OEM_GUID.getKey()).getAsString();
119 if (jObject.get(JSONApiResponseKeysEnum.IGNORE_ACTIONS_FROM_NEW_DEVICES.getKey()) != null) {
120 ignoreActionsFromNewDevices = jObject.get(JSONApiResponseKeysEnum.IGNORE_ACTIONS_FROM_NEW_DEVICES.getKey())
126 public Integer getHwVersion() {
131 public void setHwVersion(Integer hwVersion) {
132 this.hwVersion = hwVersion;
136 public String getHwVersionString() {
137 return hwVersionString;
141 public void setHwVersionString(String hwVersionString) {
142 this.hwVersionString = hwVersionString;
146 public String getSwVersion() {
151 public void setSwVersion(String swVersion) {
152 this.swVersion = swVersion;
156 public Integer getArmSwVersion() {
161 public void setArmSwVersion(Integer armSwVersion) {
162 this.armSwVersion = armSwVersion;
166 public Integer getDspSwVersion() {
171 public void setDspSwVersion(Integer dspSwVersion) {
172 this.dspSwVersion = dspSwVersion;
176 public Integer getApiVersion() {
181 public void setApiVersion(Integer apiVersion) {
182 this.apiVersion = apiVersion;
186 public String getHwName() {
191 public void setHwName(String hwName) {
192 this.hwName = hwName;
196 public Integer getBusMemberType() {
197 return busMemberType;
201 public void setBusMemberType(Integer busMemberType) {
202 this.busMemberType = busMemberType;
206 public Boolean getHasDevices() {
211 public void setHasDevices(Boolean hasDevices) {
212 this.hasDevices = hasDevices;
216 public Boolean getHasMetering() {
221 public void setHasMetering(Boolean hasMetering) {
222 this.hasMetering = hasMetering;
226 public String getVdcConfigURL() {
231 public void setVdcConfigURL(String vdcConfigURL) {
232 this.vdcConfigURL = vdcConfigURL;
236 public String getVdcModelUID() {
241 public void setVdcModelUID(String vdcModelUID) {
242 this.vdcModelUID = vdcModelUID;
246 public String getVdcHardwareGuid() {
247 return vdcHardwareGuid;
251 public void setVdcHardwareGuid(String vdcHardwareGuid) {
252 this.vdcHardwareGuid = vdcHardwareGuid;
256 public String getVdcHardwareModelGuid() {
257 return vdcHardwareModelGuid;
261 public void setVdcHardwareModelGuid(String vdcHardwareModelGuid) {
262 this.vdcHardwareModelGuid = vdcHardwareModelGuid;
266 public String getVdcVendorGuid() {
267 return vdcVendorGuid;
271 public void setVdcVendorGuid(String vdcVendorGuid) {
272 this.vdcVendorGuid = vdcVendorGuid;
276 public String getVdcOemGuid() {
281 public void setVdcOemGuid(String vdcOemGuid) {
282 this.vdcOemGuid = vdcOemGuid;
286 public Boolean getIgnoreActionsFromNewDevices() {
287 return ignoreActionsFromNewDevices;
291 public void setIgnoreActionsFromNewDevices(Boolean ignoreActionsFromNewDevices) {
292 this.ignoreActionsFromNewDevices = ignoreActionsFromNewDevices;
296 public void addMeteringValue(CachedMeteringValue cachedMeteringValue) {
297 if (cachedMeteringValue != null) {
298 switch (cachedMeteringValue.getMeteringType()) {
300 if (checkNewer(consumption, cachedMeteringValue)) {
301 consumption = cachedMeteringValue;
302 informListener(cachedMeteringValue);
306 if (cachedMeteringValue.getMeteringUnit().equals(MeteringUnitsEnum.WH)) {
307 if (checkNewer(energyWh, cachedMeteringValue)) {
308 energyWh = cachedMeteringValue;
309 informListener(cachedMeteringValue);
312 if (checkNewer(energyWs, cachedMeteringValue)) {
313 energyWs = cachedMeteringValue;
314 informListener(cachedMeteringValue);
324 private void informListener(CachedMeteringValue newMeteringValue) {
325 if (isListenerRegisterd()) {
326 super.listener.onDeviceStateChanged(
327 new DeviceStateUpdateImpl(DeviceStateUpdate.UPDATE_CIRCUIT_METER, newMeteringValue));
331 private boolean checkNewer(CachedMeteringValue oldCachedMeteringValue, CachedMeteringValue newCachedMeteringValue) {
332 return oldCachedMeteringValue == null
333 || oldCachedMeteringValue.getDateAsDate().before(newCachedMeteringValue.getDateAsDate());
337 public double getMeteringValue(MeteringTypeEnum meteringType, MeteringUnitsEnum meteringUnit) {
338 switch (meteringType) {
340 return getValue(consumption);
342 if (MeteringUnitsEnum.WS.equals(meteringUnit)) {
343 return getValue(energyWs);
345 return getValue(energyWh);
353 private double getValue(CachedMeteringValue cachedMeteringValue) {
354 return cachedMeteringValue != null ? cachedMeteringValue.getValue() : -1;
358 public List<CachedMeteringValue> getAllCachedMeteringValues() {
359 return Arrays.asList(consumption, energyWh, energyWs);
365 * @see java.lang.Object#hashCode()
368 public int hashCode() {
369 final int prime = 31;
370 int result = super.hashCode();
371 result = prime * result + ((apiVersion == null) ? 0 : apiVersion.hashCode());
372 result = prime * result + ((armSwVersion == null) ? 0 : armSwVersion.hashCode());
373 result = prime * result + ((busMemberType == null) ? 0 : busMemberType.hashCode());
374 result = prime * result + ((dspSwVersion == null) ? 0 : dspSwVersion.hashCode());
375 result = prime * result + ((hasDevices == null) ? 0 : hasDevices.hashCode());
376 result = prime * result + ((hasMetering == null) ? 0 : hasMetering.hashCode());
377 result = prime * result + ((hwName == null) ? 0 : hwName.hashCode());
378 result = prime * result + ((hwVersion == null) ? 0 : hwVersion.hashCode());
379 result = prime * result + ((hwVersionString == null) ? 0 : hwVersionString.hashCode());
380 result = prime * result + ((ignoreActionsFromNewDevices == null) ? 0 : ignoreActionsFromNewDevices.hashCode());
381 result = prime * result + ((swVersion == null) ? 0 : swVersion.hashCode());
382 result = prime * result + ((vdcConfigURL == null) ? 0 : vdcConfigURL.hashCode());
383 result = prime * result + ((vdcHardwareGuid == null) ? 0 : vdcHardwareGuid.hashCode());
384 result = prime * result + ((vdcHardwareModelGuid == null) ? 0 : vdcHardwareModelGuid.hashCode());
385 result = prime * result + ((vdcModelUID == null) ? 0 : vdcModelUID.hashCode());
386 result = prime * result + ((vdcOemGuid == null) ? 0 : vdcOemGuid.hashCode());
387 result = prime * result + ((vdcVendorGuid == null) ? 0 : vdcVendorGuid.hashCode());
394 * @see java.lang.Object#equals(java.lang.Object)
397 public boolean equals(Object obj) {
401 if (!super.equals(obj)) {
404 if (!(obj instanceof CircuitImpl)) {
407 CircuitImpl other = (CircuitImpl) obj;
408 if (apiVersion == null) {
409 if (other.apiVersion != null) {
412 } else if (!apiVersion.equals(other.apiVersion)) {
415 if (armSwVersion == null) {
416 if (other.armSwVersion != null) {
419 } else if (!armSwVersion.equals(other.armSwVersion)) {
422 if (busMemberType == null) {
423 if (other.busMemberType != null) {
426 } else if (!busMemberType.equals(other.busMemberType)) {
429 if (dspSwVersion == null) {
430 if (other.dspSwVersion != null) {
433 } else if (!dspSwVersion.equals(other.dspSwVersion)) {
436 if (hasDevices == null) {
437 if (other.hasDevices != null) {
440 } else if (!hasDevices.equals(other.hasDevices)) {
443 if (hasMetering == null) {
444 if (other.hasMetering != null) {
447 } else if (!hasMetering.equals(other.hasMetering)) {
450 if (hwName == null) {
451 if (other.hwName != null) {
454 } else if (!hwName.equals(other.hwName)) {
457 if (hwVersion == null) {
458 if (other.hwVersion != null) {
461 } else if (!hwVersion.equals(other.hwVersion)) {
464 if (hwVersionString == null) {
465 if (other.hwVersionString != null) {
468 } else if (!hwVersionString.equals(other.hwVersionString)) {
471 if (ignoreActionsFromNewDevices == null) {
472 if (other.ignoreActionsFromNewDevices != null) {
475 } else if (!ignoreActionsFromNewDevices.equals(other.ignoreActionsFromNewDevices)) {
478 if (swVersion == null) {
479 if (other.swVersion != null) {
482 } else if (!swVersion.equals(other.swVersion)) {
485 if (vdcConfigURL == null) {
486 if (other.vdcConfigURL != null) {
489 } else if (!vdcConfigURL.equals(other.vdcConfigURL)) {
492 if (vdcHardwareGuid == null) {
493 if (other.vdcHardwareGuid != null) {
496 } else if (!vdcHardwareGuid.equals(other.vdcHardwareGuid)) {
499 if (vdcHardwareModelGuid == null) {
500 if (other.vdcHardwareModelGuid != null) {
503 } else if (!vdcHardwareModelGuid.equals(other.vdcHardwareModelGuid)) {
506 if (vdcModelUID == null) {
507 if (other.vdcModelUID != null) {
510 } else if (!vdcModelUID.equals(other.vdcModelUID)) {
513 if (vdcOemGuid == null) {
514 if (other.vdcOemGuid != null) {
517 } else if (!vdcOemGuid.equals(other.vdcOemGuid)) {
520 if (vdcVendorGuid == null) {
521 if (other.vdcVendorGuid != null) {
524 } else if (!vdcVendorGuid.equals(other.vdcVendorGuid)) {