]> git.basschouten.com Git - openhab-addons.git/blob
bf017beb47136d5b47cc0f20a1763213d6a16f26
[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.binding.luftdateninfo.internal.handler;
14
15 import static org.openhab.binding.luftdateninfo.internal.LuftdatenInfoBindingConstants.*;
16 import static org.openhab.binding.luftdateninfo.internal.handler.HTTPHandler.*;
17
18 import java.util.List;
19
20 import javax.measure.quantity.Dimensionless;
21 import javax.measure.quantity.Pressure;
22 import javax.measure.quantity.Temperature;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.luftdateninfo.internal.dto.SensorDataValue;
27 import org.openhab.binding.luftdateninfo.internal.utils.NumberUtils;
28 import org.openhab.core.library.types.QuantityType;
29 import org.openhab.core.library.unit.SIUnits;
30 import org.openhab.core.library.unit.SmartHomeUnits;
31 import org.openhab.core.thing.Thing;
32
33 /**
34  * The {@link ConditionHandler} is responsible for handling commands, which are
35  * sent to one of the channels.
36  *
37  * @author Bernd Weymann - Initial contribution
38  */
39 @NonNullByDefault
40 public class ConditionHandler extends BaseSensorHandler {
41
42     protected QuantityType<Temperature> temperatureCache = QuantityType.valueOf(-1, SIUnits.CELSIUS);
43     protected QuantityType<Dimensionless> humidityCache = QuantityType.valueOf(-1, SmartHomeUnits.PERCENT);
44     protected QuantityType<Pressure> pressureCache = QuantityType.valueOf(-1, SIUnits.PASCAL);
45     protected QuantityType<Pressure> pressureSeaCache = QuantityType.valueOf(-1, SIUnits.PASCAL);
46
47     public ConditionHandler(Thing thing) {
48         super(thing);
49     }
50
51     @Override
52     public UpdateStatus updateChannels(@Nullable String json) {
53         if (json != null) {
54             List<SensorDataValue> valueList = HTTPHandler.getHandler().getLatestValues(json);
55             if (valueList != null) {
56                 if (HTTPHandler.getHandler().isCondition(valueList)) {
57                     valueList.forEach(v -> {
58                         if (v.getValueType().equals(TEMPERATURE)) {
59                             temperatureCache = QuantityType.valueOf(NumberUtils.round(v.getValue(), 1),
60                                     SIUnits.CELSIUS);
61                             updateState(TEMPERATURE_CHANNEL, temperatureCache);
62                         } else if (v.getValueType().equals(HUMIDITY)) {
63                             humidityCache = QuantityType.valueOf(NumberUtils.round(v.getValue(), 1),
64                                     SmartHomeUnits.PERCENT);
65                             updateState(HUMIDITY_CHANNEL, humidityCache);
66                         } else if (v.getValueType().equals(PRESSURE)) {
67                             pressureCache = QuantityType.valueOf(NumberUtils.round(v.getValue(), 1), SIUnits.PASCAL);
68                             updateState(PRESSURE_CHANNEL, pressureCache);
69                         } else if (v.getValueType().equals(PRESSURE_SEALEVEL)) {
70                             pressureSeaCache = QuantityType.valueOf(NumberUtils.round(v.getValue(), 1), SIUnits.PASCAL);
71                             updateState(PRESSURE_SEA_CHANNEL, pressureSeaCache);
72                         }
73                     });
74                     return UpdateStatus.OK;
75                 } else {
76                     return UpdateStatus.VALUE_ERROR;
77                 }
78             } else {
79                 return UpdateStatus.VALUE_EMPTY;
80             }
81         } else {
82             return UpdateStatus.CONNECTION_ERROR;
83         }
84     }
85
86     @Override
87     protected void updateFromCache() {
88         updateState(TEMPERATURE_CHANNEL, temperatureCache);
89         updateState(HUMIDITY_CHANNEL, humidityCache);
90         updateState(PRESSURE_CHANNEL, pressureCache);
91         updateState(PRESSURE_SEA_CHANNEL, pressureSeaCache);
92     }
93 }