]> git.basschouten.com Git - openhab-addons.git/blob
6c6f0d8acab4cce98ba974e1c5e520e544792f0d
[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
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.SmartHomeUnits;
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, SmartHomeUnits.DECIBEL);
39     protected QuantityType<Dimensionless> noiseMinCache = QuantityType.valueOf(-1, SmartHomeUnits.DECIBEL);
40     protected QuantityType<Dimensionless> noiseMaxCache = QuantityType.valueOf(-1, SmartHomeUnits.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().equals(NOISE_EQ)) {
54                             noiseEQCache = QuantityType.valueOf(NumberUtils.round(v.getValue(), 1),
55                                     SmartHomeUnits.DECIBEL);
56                             updateState(NOISE_EQ_CHANNEL, noiseEQCache);
57                         } else if (v.getValueType().equals(NOISE_MIN)) {
58                             noiseMinCache = QuantityType.valueOf(NumberUtils.round(v.getValue(), 1),
59                                     SmartHomeUnits.DECIBEL);
60                             updateState(NOISE_MIN_CHANNEL, noiseMinCache);
61                         } else if (v.getValueType().equals(NOISE_MAX)) {
62                             noiseMaxCache = QuantityType.valueOf(NumberUtils.round(v.getValue(), 1),
63                                     SmartHomeUnits.DECIBEL);
64                             updateState(NOISE_MAX_CHANNEL, noiseMaxCache);
65                         }
66                     });
67                     return UpdateStatus.OK;
68                 } else {
69                     return UpdateStatus.VALUE_ERROR;
70                 }
71             } else {
72                 return UpdateStatus.VALUE_EMPTY;
73             }
74         } else {
75             return UpdateStatus.CONNECTION_ERROR;
76         }
77     }
78
79     @Override
80     protected void updateFromCache() {
81         updateState(NOISE_EQ_CHANNEL, noiseEQCache);
82         updateState(NOISE_MIN_CHANNEL, noiseMinCache);
83         updateState(NOISE_MAX_CHANNEL, noiseMaxCache);
84     }
85 }