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.luftdateninfo.internal.handler;
15 import static org.openhab.binding.luftdateninfo.internal.LuftdatenInfoBindingConstants.*;
16 import static org.openhab.binding.luftdateninfo.internal.utils.Constants.*;
18 import java.util.List;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.luftdateninfo.internal.dto.SensorDataValue;
23 import org.openhab.binding.luftdateninfo.internal.utils.NumberUtils;
24 import org.openhab.core.library.dimension.Density;
25 import org.openhab.core.library.types.QuantityType;
26 import org.openhab.core.library.unit.Units;
27 import org.openhab.core.thing.Thing;
30 * The {@link PMHandler} is responsible for handling commands, which are
31 * sent to one of the channels.
33 * @author Bernd Weymann - Initial contribution
36 public class PMHandler extends BaseSensorHandler {
38 protected QuantityType<Density> pm25Cache = QuantityType.valueOf(-1, Units.MICROGRAM_PER_CUBICMETRE);
39 protected QuantityType<Density> pm100Cache = QuantityType.valueOf(-1, Units.MICROGRAM_PER_CUBICMETRE);
41 public PMHandler(Thing thing) {
46 public UpdateStatus updateChannels(@Nullable String json) {
48 List<SensorDataValue> valueList = HTTPHandler.getHandler().getLatestValues(json);
49 if (valueList != null) {
50 if (HTTPHandler.getHandler().isParticulate(valueList)) {
51 valueList.forEach(v -> {
52 if (v.getValueType().endsWith(P1)) {
53 pm100Cache = QuantityType.valueOf(NumberUtils.round(v.getValue(), 1),
54 Units.MICROGRAM_PER_CUBICMETRE);
55 updateState(PM100_CHANNEL, pm100Cache);
56 } else if (v.getValueType().endsWith(P2)) {
57 pm25Cache = QuantityType.valueOf(NumberUtils.round(v.getValue(), 1),
58 Units.MICROGRAM_PER_CUBICMETRE);
59 updateState(PM25_CHANNEL, pm25Cache);
62 return UpdateStatus.OK;
64 return UpdateStatus.VALUE_ERROR;
67 return UpdateStatus.VALUE_EMPTY;
70 return UpdateStatus.CONNECTION_ERROR;
75 protected void updateFromCache() {
76 updateState(PM25_CHANNEL, pm25Cache);
77 updateState(PM100_CHANNEL, pm100Cache);