]> git.basschouten.com Git - openhab-addons.git/blob
2c9e376db9d1ed8bace75849497392225210df56
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.utils.Constants.*;
17 import static org.openhab.core.library.unit.MetricPrefix.HECTO;
18
19 import java.util.List;
20
21 import javax.measure.quantity.Dimensionless;
22 import javax.measure.quantity.Pressure;
23 import javax.measure.quantity.Temperature;
24
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.binding.luftdateninfo.internal.dto.SensorDataValue;
28 import org.openhab.binding.luftdateninfo.internal.utils.NumberUtils;
29 import org.openhab.core.library.types.QuantityType;
30 import org.openhab.core.library.unit.SIUnits;
31 import org.openhab.core.library.unit.Units;
32 import org.openhab.core.thing.Thing;
33
34 /**
35  * The {@link ConditionHandler} is responsible for handling commands, which are
36  * sent to one of the channels.
37  *
38  * @author Bernd Weymann - Initial contribution
39  */
40 @NonNullByDefault
41 public class ConditionHandler extends BaseSensorHandler {
42     protected QuantityType<Temperature> temperatureCache = QuantityType.valueOf(-1, SIUnits.CELSIUS);
43     protected QuantityType<Dimensionless> humidityCache = QuantityType.valueOf(-1, Units.PERCENT);
44     protected QuantityType<Pressure> pressureCache = QuantityType.valueOf(-1, HECTO(SIUnits.PASCAL));
45     protected QuantityType<Pressure> pressureSeaCache = QuantityType.valueOf(-1, HECTO(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().endsWith(TEMPERATURE)) {
59                             temperatureCache = QuantityType.valueOf(NumberUtils.round(v.getValue(), 1),
60                                     SIUnits.CELSIUS);
61                             updateState(TEMPERATURE_CHANNEL, temperatureCache);
62                         } else if (v.getValueType().endsWith(HUMIDITY)) {
63                             humidityCache = QuantityType.valueOf(NumberUtils.round(v.getValue(), 1), Units.PERCENT);
64                             updateState(HUMIDITY_CHANNEL, humidityCache);
65                         } else if (v.getValueType().endsWith(PRESSURE)) {
66                             pressureCache = QuantityType.valueOf(
67                                     NumberUtils.round(NumberUtils.convert(v.getValue()) / 100, 1),
68                                     HECTO(SIUnits.PASCAL));
69                             updateState(PRESSURE_CHANNEL, pressureCache);
70                         } else if (v.getValueType().endsWith(PRESSURE_SEALEVEL)) {
71                             pressureSeaCache = QuantityType.valueOf(
72                                     NumberUtils.round(NumberUtils.convert(v.getValue()) / 100, 1),
73                                     HECTO(SIUnits.PASCAL));
74                             updateState(PRESSURE_SEA_CHANNEL, pressureSeaCache);
75                         }
76                     });
77                     return UpdateStatus.OK;
78                 } else {
79                     return UpdateStatus.VALUE_ERROR;
80                 }
81             } else {
82                 return UpdateStatus.VALUE_EMPTY;
83             }
84         } else {
85             return UpdateStatus.CONNECTION_ERROR;
86         }
87     }
88
89     @Override
90     protected void updateFromCache() {
91         updateState(TEMPERATURE_CHANNEL, temperatureCache);
92         updateState(HUMIDITY_CHANNEL, humidityCache);
93         updateState(PRESSURE_CHANNEL, pressureCache);
94         updateState(PRESSURE_SEA_CHANNEL, pressureSeaCache);
95     }
96 }