]> git.basschouten.com Git - openhab-addons.git/blob
dff632e463841f12db3a1be2f6ce11809bc6735d
[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 Tx22Reading.
24  *
25  * @author Volker Bier - Initial contribution
26  */
27 public class Tx22ReadingConverter implements JeeLinkReadingConverter<Tx22Reading> {
28     private static final Pattern LINE_P = Pattern.compile(
29             "OK\\s+WS\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\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(Tx22ReadingConverter.class);
32
33     @Override
34     public Tx22Reading 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                 /**
40                  * Format
41                  * 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
42                  * -------------------------------------------------------------------
43                  * OK WS 14 1 4 208 53 0 0 7 8 0 29 0 31 1 4 1 I D=0E 23.2°C 52%rH 0mm Dir.: 180.0° Wind:2.9m/s
44                  * Gust:3.1m/s new Batt. 1025 hPa
45                  * OK WS ID XXX TTT TTT HHH RRR RRR DDD DDD SSS SSS GGG GGG FFF PPP PPP
46                  * | | | | | | | | | | | | | | | | | |-- Pressure LSB (optional, FF/FF = none)
47                  * | | | | | | | | | | | | | | | | |------ Pressure MSB (optional)
48                  * | | | | | | | | | | | | | | | |---------- Flags *
49                  * | | | | | | | | | | | | | | |-------------- WindGust * 10 LSB (0.0 ... 50.0 m/s) FF/FF = none
50                  * | | | | | | | | | | | | | |------------------ WindGust * 10 MSB
51                  * | | | | | | | | | | | | |---------------------- WindSpeed * 10 LSB(0.0 ... 50.0 m/s) FF/FF = none
52                  * | | | | | | | | | | | |-------------------------- WindSpeed * 10 MSB
53                  * | | | | | | | | | | |------------------------------ WindDirection * 10 LSB (0.0 ... 365.0 Degrees)
54                  * FF/FF = none
55                  * | | | | | | | | | |---------------------------------- WindDirection * 10 MSB
56                  * | | | | | | | | |-------------------------------------- Rain * 0.5mm LSB (0 ... 9999 mm) FF/FF = none
57                  * | | | | | | | |------------------------------------------ Rain * 0.5mm MSB
58                  * | | | | | | |---------------------------------------------- Humidity (1 ... 99 %rH) FF = none
59                  * | | | | | |-------------------------------------------------- Temp * 10 + 1000 LSB (-40 ... +60 °C)
60                  * FF/FF = none
61                  * | | | | |------------------------------------------------------ Temp * 10 + 1000 MSB
62                  * | | | |---------------------------------------------------------- Sensor type (1=TX22, 2=NodeSensor)
63                  * | | |------------------------------------------------------------- Sensor ID (0 ... 63)
64                  * | |---------------------------------------------------------------- fix "WS"
65                  * |------------------------------------------------------------------- fix "OK"
66                  *
67                  * Flags: 128 64 32 16 8 4 2 1
68                  * | | |
69                  * | | |-- New battery
70                  * | |------ ERROR
71                  * |---------- Low battery
72                  */
73
74                 logger.trace("Creating reading from: {}", inputLine);
75
76                 int sensorId = Integer.parseInt(matcher.group(1));
77                 int type = Integer.parseInt(matcher.group(2));
78
79                 Float temperature = "255".equals(matcher.group(3)) ? null
80                         : (float) (Integer.parseInt(matcher.group(3)) * 256 + Integer.parseInt(matcher.group(4)) - 1000)
81                                 / 10;
82
83                 Integer humidity = "255".equals(matcher.group(5)) ? null : Integer.parseInt(matcher.group(5));
84
85                 Integer rain = "255".equals(matcher.group(6)) ? null
86                         : (int) ((Integer.parseInt(matcher.group(6)) * 256 + Integer.parseInt(matcher.group(7))) * 0.5);
87
88                 Float windDirection = "255".equals(matcher.group(8)) ? null
89                         : (Integer.parseInt(matcher.group(8)) * 256 + Integer.parseInt(matcher.group(9))) / 10f;
90                 Float windSpeed = "255".equals(matcher.group(10)) ? null
91                         : (Integer.parseInt(matcher.group(10)) * 256 + Integer.parseInt(matcher.group(11))) / 10f;
92                 Float windGust = "255".equals(matcher.group(12)) ? null
93                         : (Integer.parseInt(matcher.group(12)) * 256 + Integer.parseInt(matcher.group(13))) / 10f;
94
95                 int flags = Integer.parseInt(matcher.group(14));
96                 boolean batteryNew = (flags & (byte) 1) > 0;
97                 boolean batteryLow = (flags & (byte) 4) > 0;
98
99                 Integer pressure = null;
100                 if (matcher.groupCount() > 14 && matcher.group(15) != null && !"255".equals(matcher.group(15))) {
101                     pressure = Integer.parseInt(matcher.group(15)) * 256 + Integer.parseInt(matcher.group(16));
102                 }
103
104                 return new Tx22Reading(sensorId, type, 0, temperature, humidity, batteryNew, batteryLow, rain,
105                         windDirection, windSpeed, windGust, pressure);
106             }
107         }
108
109         return null;
110     }
111 }