]> git.basschouten.com Git - openhab-addons.git/blob
41e724e393752e86c872108fe30f88bbfe0cd6c9
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.airgradient.internal.handler;
14
15 import static org.openhab.binding.airgradient.internal.AirGradientBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.Map;
19
20 import javax.measure.Unit;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.airgradient.internal.model.Measure;
25 import org.openhab.core.library.types.QuantityType;
26 import org.openhab.core.library.types.StringType;
27 import org.openhab.core.library.unit.SIUnits;
28 import org.openhab.core.library.unit.Units;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.types.State;
31 import org.openhab.core.types.UnDefType;
32
33 /**
34  * Helper class to reduce code duplication across things.
35  *
36  * @author Jørgen Austvik - Initial contribution
37  */
38 @NonNullByDefault
39 public class MeasureHelper {
40
41     public static Map<String, String> createProperties(Measure measure) {
42         Map<String, String> properties = new HashMap<>(4);
43         String firmwareVersion = measure.firmwareVersion;
44         if (firmwareVersion != null) {
45             properties.put(Thing.PROPERTY_FIRMWARE_VERSION, firmwareVersion);
46         }
47
48         String locationName = measure.locationName;
49         if (locationName != null) {
50             properties.put(PROPERTY_NAME, locationName);
51         }
52
53         String serialNo = measure.serialno;
54         if (serialNo != null) {
55             properties.put(Thing.PROPERTY_SERIAL_NUMBER, serialNo);
56         }
57
58         String model = measure.getModel();
59         if (model != null) {
60             properties.put(Thing.PROPERTY_MODEL_ID, model);
61         }
62
63         return properties;
64     }
65
66     public static Map<String, State> createStates(Measure measure) {
67         Map<String, State> states = new HashMap<>(11);
68
69         states.put(CHANNEL_ATMP, toQuantityType(measure.atmp, SIUnits.CELSIUS));
70         states.put(CHANNEL_PM_003_COUNT, toQuantityType(measure.pm003Count, Units.ONE));
71         states.put(CHANNEL_PM_01, toQuantityType(measure.pm01, Units.MICROGRAM_PER_CUBICMETRE));
72         states.put(CHANNEL_PM_02, toQuantityType(measure.pm02, Units.MICROGRAM_PER_CUBICMETRE));
73         states.put(CHANNEL_PM_10, toQuantityType(measure.pm10, Units.MICROGRAM_PER_CUBICMETRE));
74         states.put(CHANNEL_RHUM, toQuantityType(measure.rhum, Units.PERCENT));
75         states.put(CHANNEL_UPLOADS_SINCE_BOOT, toQuantityType(measure.boot, Units.ONE));
76
77         Double rco2 = measure.rco2;
78         if (rco2 != null) {
79             states.put(CHANNEL_RCO2, toQuantityType(rco2.longValue(), Units.PARTS_PER_MILLION));
80         }
81
82         Double tvoc = measure.tvoc;
83         if (tvoc != null) {
84             states.put(CHANNEL_TVOC, toQuantityType(tvoc.longValue(), Units.PARTS_PER_BILLION));
85         }
86
87         states.put(CHANNEL_WIFI, toQuantityType(measure.wifi, Units.DECIBEL_MILLIWATTS));
88         states.put(CHANNEL_LEDS_MODE, toStringType(measure.ledMode));
89
90         return states;
91     }
92
93     private static State toQuantityType(@Nullable Number value, Unit<?> unit) {
94         return value == null ? UnDefType.NULL : new QuantityType<>(value, unit);
95     }
96
97     private static State toStringType(@Nullable String value) {
98         return value == null ? UnDefType.NULL : StringType.valueOf(value);
99     }
100 }