]> git.basschouten.com Git - openhab-addons.git/blob
509631db63db4e92a9368aa7e30989aa8329a946
[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
18 import java.util.List;
19
20 import javax.measure.quantity.Dimensionless;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.luftdateninfo.internal.dto.SensorDataValue;
25 import org.openhab.binding.luftdateninfo.internal.utils.NumberUtils;
26 import org.openhab.core.library.types.QuantityType;
27 import org.openhab.core.library.unit.Units;
28 import org.openhab.core.thing.Thing;
29
30 /**
31  * The {@link NoiseHandler} is responsible for handling commands, which are
32  * sent to one of the channels.
33  *
34  * @author Bernd Weymann - Initial contribution
35  */
36 @NonNullByDefault
37 public class NoiseHandler extends BaseSensorHandler {
38     protected QuantityType<Dimensionless> noiseEQCache = QuantityType.valueOf(-1, Units.DECIBEL);
39     protected QuantityType<Dimensionless> noiseMinCache = QuantityType.valueOf(-1, Units.DECIBEL);
40     protected QuantityType<Dimensionless> noiseMaxCache = QuantityType.valueOf(-1, Units.DECIBEL);
41
42     public NoiseHandler(Thing thing) {
43         super(thing);
44     }
45
46     @Override
47     public UpdateStatus updateChannels(@Nullable String json) {
48         if (json != null) {
49             List<SensorDataValue> valueList = HTTPHandler.getHandler().getLatestValues(json);
50             if (valueList != null) {
51                 if (HTTPHandler.getHandler().isNoise(valueList)) {
52                     valueList.forEach(v -> {
53                         if (v.getValueType().endsWith(NOISE_EQ)) {
54                             noiseEQCache = QuantityType.valueOf(NumberUtils.round(v.getValue(), 1), Units.DECIBEL);
55                             updateState(NOISE_EQ_CHANNEL, noiseEQCache);
56                         } else if (v.getValueType().endsWith(NOISE_MIN)) {
57                             noiseMinCache = QuantityType.valueOf(NumberUtils.round(v.getValue(), 1), Units.DECIBEL);
58                             updateState(NOISE_MIN_CHANNEL, noiseMinCache);
59                         } else if (v.getValueType().endsWith(NOISE_MAX)) {
60                             noiseMaxCache = QuantityType.valueOf(NumberUtils.round(v.getValue(), 1), Units.DECIBEL);
61                             updateState(NOISE_MAX_CHANNEL, noiseMaxCache);
62                         }
63                     });
64                     return UpdateStatus.OK;
65                 } else {
66                     return UpdateStatus.VALUE_ERROR;
67                 }
68             } else {
69                 return UpdateStatus.VALUE_EMPTY;
70             }
71         } else {
72             return UpdateStatus.CONNECTION_ERROR;
73         }
74     }
75
76     @Override
77     protected void updateFromCache() {
78         updateState(NOISE_EQ_CHANNEL, noiseEQCache);
79         updateState(NOISE_MIN_CHANNEL, noiseMinCache);
80         updateState(NOISE_MAX_CHANNEL, noiseMaxCache);
81     }
82 }