2 * Copyright (c) 2010-2024 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.handler;
15 import java.util.HashSet;
19 import org.openhab.binding.digitalstrom.internal.DigitalSTROMBindingConstants;
20 import org.openhab.binding.digitalstrom.internal.lib.listener.DeviceStatusListener;
21 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.Circuit;
22 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.GeneralDeviceInformation;
23 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.CachedMeteringValue;
24 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.DeviceStateUpdate;
25 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.ChangeableDeviceConfigEnum;
26 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.MeteringTypeEnum;
27 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.MeteringUnitsEnum;
28 import org.openhab.binding.digitalstrom.internal.providers.DsChannelTypeProvider;
29 import org.openhab.core.library.types.DecimalType;
30 import org.openhab.core.thing.Bridge;
31 import org.openhab.core.thing.ChannelUID;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.thing.ThingStatus;
34 import org.openhab.core.thing.ThingStatusDetail;
35 import org.openhab.core.thing.ThingStatusInfo;
36 import org.openhab.core.thing.ThingTypeUID;
37 import org.openhab.core.thing.binding.BaseThingHandler;
38 import org.openhab.core.thing.binding.ThingHandler;
39 import org.openhab.core.types.Command;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * The {@link CircuitHandler} is responsible for handling the configuration and updating the metering channels of a
45 * digitalStrom circuit. <br>
47 * For that it uses the {@link BridgeHandler} to register this class as a {@link DeviceStatusListener} to get informed
48 * about changes from the accompanying {@link Circuit}.
50 * @author Michael Ochel
51 * @author Matthias Siegele
53 public class CircuitHandler extends BaseThingHandler implements DeviceStatusListener {
55 private final Logger logger = LoggerFactory.getLogger(CircuitHandler.class);
58 * Contains all supported thing types of this handler, will be filled by DsDeviceThingTypeProvider.
60 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = new HashSet<>();
63 private Circuit circuit;
65 private BridgeHandler dssBridgeHandler;
68 * Creates a new {@link CircuitHandler}.
70 * @param thing must not be null
72 public CircuitHandler(Thing thing) {
77 public void initialize() {
78 logger.debug("Initializing CircuitHandler.");
79 dSID = (String) getConfig().get(DigitalSTROMBindingConstants.DEVICE_DSID);
80 if (dSID != null && !dSID.isBlank()) {
81 final Bridge bridge = getBridge();
83 bridgeStatusChanged(bridge.getStatusInfo());
85 // Set status to OFFLINE if no bridge is available e.g. because the bridge has been removed and the
86 // Thing was reinitialized.
87 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, "Bridge is missing!");
90 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "dSID is missing");
95 public void dispose() {
96 logger.debug("Handler disposed... unregister DeviceStatusListener");
98 if (dssBridgeHandler != null) {
99 dssBridgeHandler.unregisterDeviceStatusListener(this);
105 private synchronized BridgeHandler getDssBridgeHandler() {
106 if (this.dssBridgeHandler == null) {
107 Bridge bridge = getBridge();
108 if (bridge == null) {
109 logger.debug("Bride cannot be found");
112 ThingHandler handler = bridge.getHandler();
114 if (handler instanceof BridgeHandler bridgeHandler) {
115 dssBridgeHandler = bridgeHandler;
120 return dssBridgeHandler;
124 public void thingUpdated(Thing thing) {
126 if (circuit == null) {
132 public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
133 if (bridgeStatusInfo.getStatus().equals(ThingStatus.ONLINE)) {
135 if (getDssBridgeHandler() != null) {
136 if (circuit == null) {
137 updateStatus(ThingStatus.ONLINE, ThingStatusDetail.CONFIGURATION_PENDING,
138 "waiting for listener registration");
139 dssBridgeHandler.registerDeviceStatusListener(this);
141 updateStatus(ThingStatus.ONLINE);
144 updateStatus(ThingStatus.OFFLINE);
147 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No dSID is set!");
150 if (bridgeStatusInfo.getStatus().equals(ThingStatus.OFFLINE)) {
151 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
153 logger.debug("Set status to {}", getThing().getStatusInfo());
157 public void handleCommand(ChannelUID channelUID, Command command) {
158 // the same handling like total metering values
159 if (dssBridgeHandler != null) {
160 dssBridgeHandler.handleCommand(channelUID, command);
165 public void onDeviceStateChanged(DeviceStateUpdate deviceStateUpdate) {
166 if (deviceStateUpdate != null && DeviceStateUpdate.UPDATE_CIRCUIT_METER.equals(deviceStateUpdate.getType())) {
167 if (deviceStateUpdate.getValue() instanceof CachedMeteringValue) {
168 CachedMeteringValue cachedVal = (CachedMeteringValue) deviceStateUpdate.getValue();
169 if (MeteringUnitsEnum.WH.equals(cachedVal.getMeteringUnit())) {
170 if (cachedVal.getMeteringType().equals(MeteringTypeEnum.ENERGY)) {
171 updateState(getChannelID(cachedVal), new DecimalType(cachedVal.getValue() * 0.001));
173 updateState(getChannelID(cachedVal), new DecimalType(cachedVal.getValue()));
181 public void onDeviceRemoved(GeneralDeviceInformation device) {
182 if (device instanceof Circuit circ) {
184 if (getThing().getStatus().equals(ThingStatus.ONLINE)) {
185 if (!circuit.isPresent()) {
186 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE,
187 "Circuit is not present in the digitalSTROM-System.");
189 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE,
190 "Circuit is not available in the digitalSTROM-System.");
194 logger.debug("Set status to {}", getThing().getStatus());
199 public void onDeviceAdded(GeneralDeviceInformation device) {
200 if (device instanceof Circuit circ) {
202 if (this.circuit.isPresent()) {
203 ThingStatusInfo statusInfo = this.dssBridgeHandler.getThing().getStatusInfo();
204 updateStatus(statusInfo.getStatus(), statusInfo.getStatusDetail(), statusInfo.getDescription());
205 logger.debug("Set status to {}", getThing().getStatus());
207 checkCircuitInfoProperties(this.circuit);
209 // load first channel values
210 onCircuitStateInitial(this.circuit);
214 onDeviceRemoved(device);
217 private void checkCircuitInfoProperties(Circuit device) {
218 boolean propertiesChanged = false;
219 Map<String, String> properties = editProperties();
221 if (device.getName() != null) {
222 properties.put(DigitalSTROMBindingConstants.DEVICE_NAME, device.getName());
223 propertiesChanged = true;
225 if (device.getDSUID() != null) {
226 properties.put(DigitalSTROMBindingConstants.DEVICE_UID, device.getDSUID());
227 propertiesChanged = true;
229 if (device.getHwName() != null) {
230 properties.put(DigitalSTROMBindingConstants.HW_NAME, device.getHwName());
231 propertiesChanged = true;
233 if (device.getHwVersionString() != null) {
234 properties.put(DigitalSTROMBindingConstants.HW_VERSION, device.getHwVersionString());
235 propertiesChanged = true;
237 if (device.getSwVersion() != null) {
238 properties.put(DigitalSTROMBindingConstants.SW_VERSION, device.getSwVersion());
239 propertiesChanged = true;
241 if (device.getApiVersion() != null) {
242 properties.put(DigitalSTROMBindingConstants.API_VERSION, device.getApiVersion().toString());
243 propertiesChanged = true;
245 if (device.getDspSwVersion() != null) {
246 properties.put(DigitalSTROMBindingConstants.DSP_SW_VERSION, device.getDspSwVersion().toString());
247 propertiesChanged = true;
249 if (device.getArmSwVersion() != null) {
250 properties.put(DigitalSTROMBindingConstants.ARM_SW_VERSION, device.getArmSwVersion().toString());
251 propertiesChanged = true;
253 if (propertiesChanged) {
254 super.updateProperties(properties);
255 propertiesChanged = false;
259 private void onCircuitStateInitial(Circuit circuit) {
260 if (circuit != null) {
261 for (CachedMeteringValue cachedMeterValue : circuit.getAllCachedMeteringValues()) {
262 if (cachedMeterValue != null && MeteringUnitsEnum.WH.equals(cachedMeterValue.getMeteringUnit())) {
263 String channelID = getChannelID(cachedMeterValue);
264 if (isLinked(channelID)) {
265 channelLinked(new ChannelUID(getThing().getUID(), channelID));
272 private String getChannelID(CachedMeteringValue cachedMeterValue) {
273 return DsChannelTypeProvider.getMeteringChannelID(cachedMeterValue.getMeteringType(),
274 cachedMeterValue.getMeteringUnit(), false);
278 public void channelLinked(ChannelUID channelUID) {
279 if (circuit != null) {
280 MeteringTypeEnum meteringType = DsChannelTypeProvider.getMeteringType(channelUID.getId());
281 double val = circuit.getMeteringValue(meteringType, MeteringUnitsEnum.WH);
283 if (meteringType.equals(MeteringTypeEnum.ENERGY)) {
284 updateState(channelUID, new DecimalType(val * 0.001));
286 updateState(channelUID, new DecimalType(val));
293 public void onDeviceConfigChanged(ChangeableDeviceConfigEnum whatConfig) {
294 // nothing to do, will be registered again
298 public void onSceneConfigAdded(short sceneID) {
303 public String getDeviceStatusListenerID() {