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.*;
18 import java.util.List;
20 import javax.measure.quantity.Dimensionless;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.sensorcommunity.internal.dto.SensorDataValue;
25 import org.openhab.binding.sensorcommunity.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;
31 * The {@link NoiseHandler} is responsible for handling commands, which are
32 * sent to one of the channels.
34 * @author Bernd Weymann - Initial contribution
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);
42 public NoiseHandler(Thing thing) {
47 public UpdateStatus updateChannels(@Nullable String json) {
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);
64 return UpdateStatus.OK;
66 return UpdateStatus.VALUE_ERROR;
69 return UpdateStatus.VALUE_EMPTY;
72 return UpdateStatus.CONNECTION_ERROR;
77 protected void updateFromCache() {
78 updateState(NOISE_EQ_CHANNEL, noiseEQCache);
79 updateState(NOISE_MIN_CHANNEL, noiseMinCache);
80 updateState(NOISE_MAX_CHANNEL, noiseMaxCache);