2 * Copyright (c) 2010-2020 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.apache.commons.lang.StringUtils;
20 import org.openhab.binding.digitalstrom.internal.DigitalSTROMBindingConstants;
21 import org.openhab.binding.digitalstrom.internal.lib.listener.DeviceStatusListener;
22 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.Circuit;
23 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.GeneralDeviceInformation;
24 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.CachedMeteringValue;
25 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.DeviceStateUpdate;
26 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.ChangeableDeviceConfigEnum;
27 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.MeteringTypeEnum;
28 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.MeteringUnitsEnum;
29 import org.openhab.binding.digitalstrom.internal.providers.DsChannelTypeProvider;
30 import org.openhab.core.library.types.DecimalType;
31 import org.openhab.core.thing.Bridge;
32 import org.openhab.core.thing.ChannelUID;
33 import org.openhab.core.thing.Thing;
34 import org.openhab.core.thing.ThingStatus;
35 import org.openhab.core.thing.ThingStatusDetail;
36 import org.openhab.core.thing.ThingStatusInfo;
37 import org.openhab.core.thing.ThingTypeUID;
38 import org.openhab.core.thing.binding.BaseThingHandler;
39 import org.openhab.core.thing.binding.ThingHandler;
40 import org.openhab.core.types.Command;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * The {@link CircuitHandler} is responsible for handling the configuration and updating the metering channels of a
46 * digitalStrom circuit. <br>
48 * For that it uses the {@link BridgeHandler} to register this class as a {@link DeviceStatusListener} to get informed
49 * about changes from the accompanying {@link Circuit}.
51 * @author Michael Ochel
52 * @author Matthias Siegele
54 public class CircuitHandler extends BaseThingHandler implements DeviceStatusListener {
56 private final Logger logger = LoggerFactory.getLogger(CircuitHandler.class);
59 * Contains all supported thing types of this handler, will be filled by DsDeviceThingTypeProvider.
61 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = new HashSet<>();
64 private Circuit circuit;
66 private BridgeHandler dssBridgeHandler;
69 * Creates a new {@link CircuitHandler}.
71 * @param thing must not be null
73 public CircuitHandler(Thing thing) {
78 public void initialize() {
79 logger.debug("Initializing CircuitHandler.");
80 if (StringUtils.isNotBlank((String) getConfig().get(DigitalSTROMBindingConstants.DEVICE_DSID))) {
81 dSID = getConfig().get(DigitalSTROMBindingConstants.DEVICE_DSID).toString();
82 final Bridge bridge = getBridge();
84 bridgeStatusChanged(bridge.getStatusInfo());
86 // Set status to OFFLINE if no bridge is available e.g. because the bridge has been removed and the
87 // Thing was reinitialized.
88 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, "Bridge is missing!");
91 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "dSID is missing");
96 public void dispose() {
97 logger.debug("Handler disposed... unregister DeviceStatusListener");
99 if (dssBridgeHandler != null) {
100 dssBridgeHandler.unregisterDeviceStatusListener(this);
106 private synchronized BridgeHandler getDssBridgeHandler() {
107 if (this.dssBridgeHandler == null) {
108 Bridge bridge = getBridge();
109 if (bridge == null) {
110 logger.debug("Bride cannot be found");
113 ThingHandler handler = bridge.getHandler();
115 if (handler instanceof BridgeHandler) {
116 dssBridgeHandler = (BridgeHandler) handler;
121 return dssBridgeHandler;
125 public void thingUpdated(Thing thing) {
127 if (circuit == null) {
133 public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
134 if (bridgeStatusInfo.getStatus().equals(ThingStatus.ONLINE)) {
136 if (getDssBridgeHandler() != null) {
137 if (circuit == null) {
138 updateStatus(ThingStatus.ONLINE, ThingStatusDetail.CONFIGURATION_PENDING,
139 "waiting for listener registration");
140 dssBridgeHandler.registerDeviceStatusListener(this);
142 updateStatus(ThingStatus.ONLINE);
145 updateStatus(ThingStatus.OFFLINE);
148 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No dSID is set!");
151 if (bridgeStatusInfo.getStatus().equals(ThingStatus.OFFLINE)) {
152 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
154 if (bridgeStatusInfo.getStatus().equals(ThingStatus.REMOVED)) {
155 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, "Bridge has been removed.");
157 logger.debug("Set status to {}", getThing().getStatusInfo());
161 public void handleCommand(ChannelUID channelUID, Command command) {
162 // the same handling like total metering values
163 if (dssBridgeHandler != null) {
164 dssBridgeHandler.handleCommand(channelUID, command);
169 public void onDeviceStateChanged(DeviceStateUpdate deviceStateUpdate) {
170 if (deviceStateUpdate != null && DeviceStateUpdate.UPDATE_CIRCUIT_METER.equals(deviceStateUpdate.getType())) {
171 if (deviceStateUpdate.getValue() instanceof CachedMeteringValue) {
172 CachedMeteringValue cachedVal = (CachedMeteringValue) deviceStateUpdate.getValue();
173 if (MeteringUnitsEnum.WH.equals(cachedVal.getMeteringUnit())) {
174 if (cachedVal.getMeteringType().equals(MeteringTypeEnum.ENERGY)) {
175 updateState(getChannelID(cachedVal), new DecimalType(cachedVal.getValue() * 0.001));
177 updateState(getChannelID(cachedVal), new DecimalType(cachedVal.getValue()));
185 public void onDeviceRemoved(GeneralDeviceInformation device) {
186 if (device instanceof Circuit) {
187 this.circuit = (Circuit) device;
188 if (getThing().getStatus().equals(ThingStatus.ONLINE)) {
189 if (!circuit.isPresent()) {
190 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE,
191 "Circuit is not present in the digitalSTROM-System.");
193 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE,
194 "Circuit is not available in the digitalSTROM-System.");
198 logger.debug("Set status to {}", getThing().getStatus());
203 public void onDeviceAdded(GeneralDeviceInformation device) {
204 if (device instanceof Circuit) {
205 this.circuit = (Circuit) device;
206 if (this.circuit.isPresent()) {
207 ThingStatusInfo statusInfo = this.dssBridgeHandler.getThing().getStatusInfo();
208 updateStatus(statusInfo.getStatus(), statusInfo.getStatusDetail(), statusInfo.getDescription());
209 logger.debug("Set status to {}", getThing().getStatus());
211 checkCircuitInfoProperties(this.circuit);
213 // load first channel values
214 onCircuitStateInitial(this.circuit);
218 onDeviceRemoved(device);
221 private void checkCircuitInfoProperties(Circuit device) {
222 boolean propertiesChanged = false;
223 Map<String, String> properties = editProperties();
225 if (device.getName() != null) {
226 properties.put(DigitalSTROMBindingConstants.DEVICE_NAME, device.getName());
227 propertiesChanged = true;
229 if (device.getDSUID() != null) {
230 properties.put(DigitalSTROMBindingConstants.DEVICE_UID, device.getDSUID());
231 propertiesChanged = true;
233 if (device.getHwName() != null) {
234 properties.put(DigitalSTROMBindingConstants.HW_NAME, device.getHwName());
235 propertiesChanged = true;
237 if (device.getHwVersionString() != null) {
238 properties.put(DigitalSTROMBindingConstants.HW_VERSION, device.getHwVersionString());
239 propertiesChanged = true;
241 if (device.getSwVersion() != null) {
242 properties.put(DigitalSTROMBindingConstants.SW_VERSION, device.getSwVersion());
243 propertiesChanged = true;
245 if (device.getApiVersion() != null) {
246 properties.put(DigitalSTROMBindingConstants.API_VERSION, device.getApiVersion().toString());
247 propertiesChanged = true;
249 if (device.getDspSwVersion() != null) {
250 properties.put(DigitalSTROMBindingConstants.DSP_SW_VERSION, device.getDspSwVersion().toString());
251 propertiesChanged = true;
253 if (device.getArmSwVersion() != null) {
254 properties.put(DigitalSTROMBindingConstants.ARM_SW_VERSION, device.getArmSwVersion().toString());
255 propertiesChanged = true;
257 if (propertiesChanged) {
258 super.updateProperties(properties);
259 propertiesChanged = false;
263 private void onCircuitStateInitial(Circuit circuit) {
264 if (circuit != null) {
265 for (CachedMeteringValue cachedMeterValue : circuit.getAllCachedMeteringValues()) {
266 if (cachedMeterValue != null && MeteringUnitsEnum.WH.equals(cachedMeterValue.getMeteringUnit())) {
267 String channelID = getChannelID(cachedMeterValue);
268 if (isLinked(channelID)) {
269 channelLinked(new ChannelUID(getThing().getUID(), channelID));
276 private String getChannelID(CachedMeteringValue cachedMeterValue) {
277 return DsChannelTypeProvider.getMeteringChannelID(cachedMeterValue.getMeteringType(),
278 cachedMeterValue.getMeteringUnit(), false);
282 public void channelLinked(ChannelUID channelUID) {
283 if (circuit != null) {
284 MeteringTypeEnum meteringType = DsChannelTypeProvider.getMeteringType(channelUID.getId());
285 double val = circuit.getMeteringValue(meteringType, MeteringUnitsEnum.WH);
287 if (meteringType.equals(MeteringTypeEnum.ENERGY)) {
288 updateState(channelUID, new DecimalType(val * 0.001));
290 updateState(channelUID, new DecimalType(val));
297 public void onDeviceConfigChanged(ChangeableDeviceConfigEnum whatConfig) {
298 // nothing to do, will be registered again
302 public void onSceneConfigAdded(short sceneID) {
307 public String getDeviceStatusListenerID() {