]> git.basschouten.com Git - openhab-addons.git/blob
3154f693cc54d26333682e89acbe64fcc882afd9
[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.jeelink.internal.lacrosse;
14
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
17
18 import org.openhab.binding.jeelink.internal.JeeLinkReadingConverter;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * Converter for converting a line read from a LaCrosseITPlusReader sketch to a LaCrosseTemperatureReading.
24  *
25  * @author Volker Bier - Initial contribution
26  */
27 public class LaCrosseTemperatureReadingConverter implements JeeLinkReadingConverter<LaCrosseTemperatureReading> {
28     private static final Pattern LINE_P = Pattern
29             .compile("OK\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)");
30
31     private final Logger logger = LoggerFactory.getLogger(LaCrosseTemperatureReadingConverter.class);
32
33     @Override
34     public LaCrosseTemperatureReading createReading(String inputLine) {
35         // parse lines only if we have registered listeners
36         if (inputLine != null) {
37             Matcher matcher = LINE_P.matcher(inputLine);
38             if (matcher.matches()) {
39                 // Format
40                 //
41                 // OK 9 56 1 4 156 37 (ID = 56 T: 18.0 H: 37 no NewBatt)
42                 // OK 9 49 1 4 182 54 (ID = 49 T: 20.6 H: 54 no NewBatt)
43                 // OK 9 55 129 4 192 56 (ID = 55 T: 21.6 H: 56 WITH NewBatt)
44                 // OK 9 ID XXX XXX XXX XXX
45                 // | | | | | | |
46                 // | | | | | | --- Humidity incl. WeakBatteryFlag
47                 // | | | | | |------ Temp * 10 + 1000 LSB
48                 // | | | | |---------- Temp * 10 + 1000 MSB
49                 // | | | |-------------- Sensor type (1 or 2) +128 if NewBatteryFlag
50                 // | | |----------------- Sensor ID
51                 // | |------------------- fix "9"
52                 // |---------------------- fix "OK"
53                 logger.trace("Creating reading from: {}", inputLine);
54
55                 int sensorId = Integer.parseInt(matcher.group(2));
56                 int int3 = Integer.parseInt(matcher.group(3));
57
58                 int batteryNewInt = (int3 & 0x80) >> 7;
59                 int type = (int3 & 0x70) >> 4;
60                 int channel = int3 & 0x0F;
61
62                 float temperature = (float) (Integer.parseInt(matcher.group(4)) * 256
63                         + Integer.parseInt(matcher.group(5)) - 1000) / 10;
64                 int humidity = Integer.parseInt(matcher.group(6)) & 0x7f;
65                 int batteryLowInt = (Integer.parseInt(matcher.group(6)) & 0x80) >> 7;
66
67                 boolean batteryLow = batteryLowInt == 1;
68                 boolean batteryNew = batteryNewInt == 1;
69
70                 return new LaCrosseTemperatureReading(sensorId, type, channel, temperature, humidity, batteryNew,
71                         batteryLow);
72             }
73         }
74
75         return null;
76     }
77 }