]> git.basschouten.com Git - openhab-addons.git/blob
dbcc269091b3beb8bd2b7d0070482bc71bde40f5
[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 LGW to a LgwReading.
24  *
25  * @author Volker Bier - Initial contribution
26  */
27 public class LgwReadingConverter implements JeeLinkReadingConverter<LgwReading> {
28     private static final Pattern LINE_P = Pattern.compile(
29             "OK\\s+WS\\s+([0-9]+)\\s+4\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)(?:\\s+255){8}?\\s+0\\s+([0-9]+)\\s+([0-9]+)(?:\\s+255){9}?");
30     private final Logger logger = LoggerFactory.getLogger(LgwReadingConverter.class);
31
32     @Override
33     public LgwReading createReading(String inputLine) {
34         // parse lines only if we have registered listeners
35         if (inputLine != null) {
36             Matcher matcher = LINE_P.matcher(inputLine);
37             if (matcher.matches()) {
38                 /*
39                  * Format
40                  * OK WS 71 4 4 203 53 255 255 255 255 255 255 255 255 0 3 219 255 255 255 255 255 255 255 255 255
41                  * OK WS 75 4 4 195 61 255 255 255 255 255 255 255 255 0 255 255 255 255 255 255 255 255 255 255 255
42                  * OK WS 213 4 5 126 40 255 255 255 255 255 255 255 255 0 40 53 0 48 57
43                  * OK WS ID XXX TTT TTT HHH RRR RRR DDD DDD SSS SSS GGG GGG FFF PPP PPP GAS GAS GAS DEB DEB DEB LUX LUX
44                  * LUX
45                  * | | | | | | | | | | | | | | | | | |-------------------------------------- Pressure LSB
46                  * | | | | | | | | | | | | | | | | |------------------------------------------ Pressure MSB
47                  * | | | | | | | | | | | | | | | |-- Fix 0
48                  * | | | | | | |-------------------------------------- Humidity (1 ... 99 %rH) FF = none
49                  * | | | | | |------------------------------------------ Temp * 10 + 1000 LSB (-40 ... +60 °C) FF/FF =
50                  * none
51                  * | | | | |---------------------------------------------- Temp * 10 + 1000 MSB
52                  * | | | |-------------------------------------------------- fix "4"
53                  * | | |------------------------------------------------------ Sensor ID (1 ... 63)
54                  * | |--------------------------------------------------------- fix "WS"
55                  * |------------------------------------------------------------ fix "OK"
56                  */
57                 logger.trace("Creating reading from: {}", inputLine);
58
59                 int sensorId = Integer.parseInt(matcher.group(1));
60
61                 Float temperature = "255".equals(matcher.group(2)) ? null
62                         : (float) (Integer.parseInt(matcher.group(2)) * 256 + Integer.parseInt(matcher.group(3)) - 1000)
63                                 / 10;
64
65                 Integer humidity = "255".equals(matcher.group(4)) ? null : Integer.parseInt(matcher.group(4));
66
67                 Integer pressure = null;
68                 if (!"255".equals(matcher.group(5))) {
69                     pressure = Integer.parseInt(matcher.group(5)) * 256 + Integer.parseInt(matcher.group(6));
70                 }
71
72                 return new LgwReading(sensorId, temperature, humidity, pressure);
73             }
74         }
75
76         return null;
77     }
78 }