2 * Copyright (c) 2010-2024 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.airgradient.internal.handler;
15 import static org.openhab.binding.airgradient.internal.AirGradientBindingConstants.*;
17 import java.util.HashMap;
20 import javax.measure.Unit;
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;
34 * Helper class to reduce code duplication across things.
36 * @author Jørgen Austvik - Initial contribution
39 public class MeasureHelper {
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);
48 String locationName = measure.locationName;
49 if (locationName != null) {
50 properties.put(PROPERTY_NAME, locationName);
53 String serialNo = measure.serialno;
54 if (serialNo != null) {
55 properties.put(Thing.PROPERTY_SERIAL_NUMBER, serialNo);
58 String model = measure.getModel();
60 properties.put(Thing.PROPERTY_MODEL_ID, model);
66 public static Map<String, State> createStates(Measure measure) {
67 Map<String, State> states = new HashMap<>(11);
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));
77 Double rco2 = measure.rco2;
79 states.put(CHANNEL_RCO2, toQuantityType(rco2.longValue(), Units.PARTS_PER_MILLION));
82 Double tvoc = measure.tvoc;
84 states.put(CHANNEL_TVOC, toQuantityType(tvoc.longValue(), Units.PARTS_PER_BILLION));
87 states.put(CHANNEL_WIFI, toQuantityType(measure.wifi, Units.DECIBEL_MILLIWATTS));
88 states.put(CHANNEL_LEDS_MODE, toStringType(measure.ledMode));
93 private static State toQuantityType(@Nullable Number value, Unit<?> unit) {
94 return value == null ? UnDefType.NULL : new QuantityType<>(value, unit);
97 private static State toStringType(@Nullable String value) {
98 return value == null ? UnDefType.NULL : StringType.valueOf(value);