]> git.basschouten.com Git - openhab-addons.git/blob
d8681a5f0b5704f0bc695d67a5085bee876ab844
[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.enocean.internal.eep.A5_09;
14
15 import static org.openhab.binding.enocean.internal.EnOceanBindingConstants.*;
16
17 import java.util.function.Function;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.enocean.internal.messages.ERP1Message;
21 import org.openhab.core.config.core.Configuration;
22 import org.openhab.core.library.types.QuantityType;
23 import org.openhab.core.library.types.StringType;
24 import org.openhab.core.library.unit.Units;
25 import org.openhab.core.types.State;
26 import org.openhab.core.types.UnDefType;
27
28 /**
29  *
30  * @author Zhivka Dimova - Initial contribution
31  */
32 @NonNullByDefault
33 public class A5_09_05 extends A5_09 {
34
35     public A5_09_05(ERP1Message packet) {
36         super(packet);
37     }
38
39     protected double[] ScaleMultiplier = new double[] { 0.01, 0.1, 1, 10 };
40
41     protected String[] getVOCIdentifications() {
42         return new String[] { "VOCT", "Formaldehyde", "Benzene", "Styrene", "Toluene", "Tetrachloroethylene", "Xylene",
43                 "n-Hexane", "n-Octane", "Cyclopentane", "Methanol", "Ethanol", "1-Pentanol", "Acetone",
44                 "ethylene Oxide", "Acetaldehyde ue", "Acetic Acid", "Propionine Acid", "Valeric Acid", "Butyric Acid",
45                 "Ammoniac", "Hidrogen Sulfide", "Dimethylsulfide", "2-Butanol", "2-Methylpropanol", "Diethyl ether",
46                 "VOC-Index", "Ozone" };
47     }
48
49     protected long getUnscaledVOCValue() {
50         return getDBByOffsetSizeValue(0, 16);
51     }
52
53     protected double getScalingFactor() {
54         int smid = Long.valueOf(getDBByOffsetSizeValue(30, 2)).intValue();
55         if (smid < 0 || smid >= ScaleMultiplier.length) {
56             logger.debug("Invalid value according to enocean specification for A5_09 Scale Multiplier {}", smid);
57             return 1;
58         }
59
60         return ScaleMultiplier[smid];
61     }
62
63     protected String getVOCID() {
64         int vocId = getDB1Value();
65         String[] VOCIdentifications = getVOCIdentifications();
66         if (vocId == 255) {
67             return VOCIdentifications[VOCIdentifications.length - 1];
68         } else if (vocId < 0 || vocId >= VOCIdentifications.length - 1) {
69             logger.debug("Invalid value according to enocean specification for A5_09 VOC Identification {}", vocId);
70             return "";
71         }
72
73         return VOCIdentifications[vocId];
74     }
75
76     @Override
77     protected State convertToStateImpl(String channelId, String channelTypeId,
78             Function<String, State> getCurrentStateFunc, Configuration config) {
79         if (CHANNEL_VOC.equals(channelId)) {
80             double scaledVOC = getUnscaledVOCValue() * getScalingFactor();
81             return new QuantityType<>(scaledVOC, Units.PARTS_PER_BILLION);
82         } else if (CHANNEL_VOC_ID.equals(channelId)) {
83             return new StringType(getVOCID());
84         }
85
86         return UnDefType.UNDEF;
87     }
88 }