]> git.basschouten.com Git - openhab-addons.git/blob
c409d57384c0b00fee80de6650c0bd7779a6b682
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.io.imperihome.internal.model.device;
14
15 import org.apache.commons.lang.StringUtils;
16 import org.openhab.core.items.Item;
17 import org.openhab.core.library.types.DecimalType;
18 import org.openhab.core.types.State;
19 import org.openhab.io.imperihome.internal.model.param.NumericValueParam;
20 import org.openhab.io.imperihome.internal.model.param.ParamType;
21 import org.openhab.io.imperihome.internal.processor.ItemProcessor;
22
23 /**
24  * Electricity device, containing current (Watt) and total (KWh) consumption.
25  *
26  * @author Pepijn de Geus - Initial contribution
27  */
28 public class ElectricityDevice extends AbstractNumericValueDevice {
29
30     private static final String LINK_WATTS = "watt";
31     private static final String LINK_KWH = "kwh";
32
33     public ElectricityDevice(Item item) {
34         super(DeviceType.ELECTRICITY, item, "W");
35     }
36
37     @Override
38     public void addLink(String linkType, String deviceId) {
39         super.addLink(linkType, deviceId);
40
41         if (getLinks().containsKey(LINK_WATTS)) {
42             setUnit("KWh");
43         } else if (getLinks().containsKey(LINK_KWH)) {
44             setUnit("W");
45         }
46     }
47
48     @Override
49     public void stateUpdated(Item item, State newState) {
50         super.stateUpdated(item, newState);
51
52         DecimalType value = (DecimalType) item.getStateAs(DecimalType.class);
53
54         if (getLinks().containsKey(LINK_WATTS) || getUnit().equalsIgnoreCase(LINK_KWH)) {
55             addParam(new NumericValueParam(ParamType.KWH, getUnit(), value));
56         } else if (getLinks().isEmpty() || getLinks().containsKey(LINK_KWH)) {
57             addParam(new NumericValueParam(ParamType.WATTS, getUnit(), value));
58         }
59     }
60
61     @Override
62     public void updateParams() {
63         super.updateParams();
64
65         if (getLinks().containsKey(LINK_WATTS)) {
66             String deviceName = getLinks().get(LINK_WATTS);
67             String deviceId = ItemProcessor.getDeviceId(deviceName);
68             AbstractDevice wattsDevice = getDeviceRegistry().getDevice(deviceId);
69             if (wattsDevice == null) {
70                 logger.error("Couldn't resolve linked watts device '{}', make sure the Item has iss tags", deviceName);
71             } else {
72                 setWattsParam(wattsDevice);
73             }
74         }
75
76         if (getLinks().containsKey(LINK_KWH)) {
77             String deviceName = getLinks().get(LINK_KWH);
78             String deviceId = ItemProcessor.getDeviceId(deviceName);
79             AbstractDevice kwhDevice = getDeviceRegistry().getDevice(deviceId);
80             if (kwhDevice == null) {
81                 logger.error("Couldn't resolve linked KWh device '{}', make sure the Item has iss tags", deviceName);
82             } else {
83                 setKwhParam(kwhDevice);
84             }
85         }
86     }
87
88     private void setWattsParam(AbstractDevice device) {
89         NumericValueParam valueParam = (NumericValueParam) device.getParams().get(ParamType.WATTS);
90         if (valueParam == null) {
91             logger.warn("Linked Watts device has no Watt value parameter: {}", device);
92             return;
93         }
94
95         NumericValueParam wattsParam = new NumericValueParam(ParamType.WATTS, valueParam.getUnit(), null);
96         if (StringUtils.isEmpty(wattsParam.getUnit())) {
97             wattsParam.setUnit("W");
98         }
99         wattsParam.setValue(valueParam.getValue());
100         addParam(wattsParam);
101     }
102
103     private void setKwhParam(AbstractDevice device) {
104         NumericValueParam valueParam = (NumericValueParam) device.getParams().get(ParamType.KWH);
105         if (valueParam == null) {
106             logger.warn("Linked KWh device has no KWh value parameter: {}", device);
107             return;
108         }
109
110         NumericValueParam kwhParam = new NumericValueParam(ParamType.KWH, valueParam.getUnit(), null);
111         if (StringUtils.isEmpty(kwhParam.getUnit())) {
112             kwhParam.setUnit("KWh");
113         }
114         kwhParam.setValue(valueParam.getValue());
115         addParam(kwhParam);
116     }
117 }