2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.sensorcommunity.internal.handler;
15 import static org.openhab.binding.sensorcommunity.internal.SensorCommunityBindingConstants.*;
16 import static org.openhab.binding.sensorcommunity.internal.utils.Constants.*;
17 import static org.openhab.core.library.unit.MetricPrefix.HECTO;
19 import java.util.List;
21 import javax.measure.quantity.Dimensionless;
22 import javax.measure.quantity.Pressure;
23 import javax.measure.quantity.Temperature;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.binding.sensorcommunity.internal.dto.SensorDataValue;
28 import org.openhab.binding.sensorcommunity.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;
35 * The {@link ConditionHandler} is responsible for handling commands, which are
36 * sent to one of the channels.
38 * @author Bernd Weymann - Initial contribution
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));
47 public ConditionHandler(Thing thing) {
52 public UpdateStatus updateChannels(@Nullable String json) {
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),
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);
77 return UpdateStatus.OK;
79 return UpdateStatus.VALUE_ERROR;
82 return UpdateStatus.VALUE_EMPTY;
85 return UpdateStatus.CONNECTION_ERROR;
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);