]> git.basschouten.com Git - openhab-addons.git/blob
40c7af8c7bcee4813dcbd32b782840ff002f51fc
[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.Units;
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, Units.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), Units.PERCENT);
64                             updateState(HUMIDITY_CHANNEL, humidityCache);
65                         } else if (v.getValueType().equals(PRESSURE)) {
66                             pressureCache = QuantityType.valueOf(NumberUtils.round(v.getValue(), 1), SIUnits.PASCAL);
67                             updateState(PRESSURE_CHANNEL, pressureCache);
68                         } else if (v.getValueType().equals(PRESSURE_SEALEVEL)) {
69                             pressureSeaCache = QuantityType.valueOf(NumberUtils.round(v.getValue(), 1), SIUnits.PASCAL);
70                             updateState(PRESSURE_SEA_CHANNEL, pressureSeaCache);
71                         }
72                     });
73                     return UpdateStatus.OK;
74                 } else {
75                     return UpdateStatus.VALUE_ERROR;
76                 }
77             } else {
78                 return UpdateStatus.VALUE_EMPTY;
79             }
80         } else {
81             return UpdateStatus.CONNECTION_ERROR;
82         }
83     }
84
85     @Override
86     protected void updateFromCache() {
87         updateState(TEMPERATURE_CHANNEL, temperatureCache);
88         updateState(HUMIDITY_CHANNEL, humidityCache);
89         updateState(PRESSURE_CHANNEL, pressureCache);
90         updateState(PRESSURE_SEA_CHANNEL, pressureSeaCache);
91     }
92 }