]> git.basschouten.com Git - openhab-addons.git/blob
94e9220ede2d6ed0259545ce69f917854495eb7f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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  * Rain sensor device.
25  *
26  * @author Pepijn de Geus - Initial contribution
27  */
28 public class RainDevice extends AbstractNumericValueDevice {
29
30     private static final String LINK_ACCUMULATION = "accum";
31
32     public RainDevice(Item item) {
33         super(DeviceType.RAIN, item, "mm/h");
34     }
35
36     @Override
37     public void updateParams() {
38         super.updateParams();
39
40         if (getLinks().containsKey(LINK_ACCUMULATION)) {
41             String deviceName = getLinks().get(LINK_ACCUMULATION);
42             String deviceId = ItemProcessor.getDeviceId(deviceName);
43             AbstractDevice accumDevice = getDeviceRegistry().getDevice(deviceId);
44             if (accumDevice == null) {
45                 logger.error("Couldn't resolve linked accumulation device '{}', make sure the Item has iss tags",
46                         deviceName);
47                 return;
48             }
49
50             NumericValueParam valueParam = (NumericValueParam) accumDevice.getParams().get(ParamType.GENERIC_VALUE);
51             if (valueParam == null) {
52                 logger.warn("Linked Accumulation device has no Value parameter: {}", accumDevice);
53                 return;
54             }
55
56             NumericValueParam accumParam = new NumericValueParam(ParamType.ACCUMULATION, valueParam.getUnit(), null);
57             if (StringUtils.isEmpty(accumParam.getUnit())) {
58                 accumParam.setUnit("mm");
59             }
60
61             accumParam.setValue(valueParam.getValue());
62             addParam(accumParam);
63         }
64     }
65
66     @Override
67     public void stateUpdated(Item item, State newState) {
68         super.stateUpdated(item, newState);
69
70         DecimalType value = (DecimalType) item.getStateAs(DecimalType.class);
71         addParam(new NumericValueParam(ParamType.RAIN_VALUE, getUnit(), value));
72     }
73 }