]> git.basschouten.com Git - openhab-addons.git/blob
7ee0aa383839e36eea9f1b074bc881dabfeebcce
[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 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;
28
29 /**
30  * The {@link PMHandler} is responsible for handling commands, which are
31  * sent to one of the channels.
32  *
33  * @author Bernd Weymann - Initial contribution
34  */
35 @NonNullByDefault
36 public class PMHandler extends BaseSensorHandler {
37
38     protected QuantityType<Density> pm25Cache = QuantityType.valueOf(-1, Units.MICROGRAM_PER_CUBICMETRE);
39     protected QuantityType<Density> pm100Cache = QuantityType.valueOf(-1, Units.MICROGRAM_PER_CUBICMETRE);
40
41     public PMHandler(Thing thing) {
42         super(thing);
43     }
44
45     @Override
46     public UpdateStatus updateChannels(@Nullable String json) {
47         if (json != null) {
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);
60                         }
61                     });
62                     return UpdateStatus.OK;
63                 } else {
64                     return UpdateStatus.VALUE_ERROR;
65                 }
66             } else {
67                 return UpdateStatus.VALUE_EMPTY;
68             }
69         } else {
70             return UpdateStatus.CONNECTION_ERROR;
71         }
72     }
73
74     @Override
75     protected void updateFromCache() {
76         updateState(PM25_CHANNEL, pm25Cache);
77         updateState(PM100_CHANNEL, pm100Cache);
78     }
79 }