2 * Copyright (c) 2010-2023 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.jeelink.internal.lacrosse;
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
18 import org.openhab.binding.jeelink.internal.JeeLinkReadingConverter;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 * Converter for converting a line read from a LaCrosseITPlusReader sketch to a LaCrosseTemperatureReading.
25 * @author Volker Bier - Initial contribution
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]+)");
31 private final Logger logger = LoggerFactory.getLogger(LaCrosseTemperatureReadingConverter.class);
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()) {
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
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);
55 int sensorId = Integer.parseInt(matcher.group(2));
56 int int3 = Integer.parseInt(matcher.group(3));
58 int batteryNewInt = (int3 & 0x80) >> 7;
59 int type = (int3 & 0x70) >> 4;
60 int channel = int3 & 0x0F;
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;
67 boolean batteryLow = batteryLowInt == 1;
68 boolean batteryNew = batteryNewInt == 1;
70 return new LaCrosseTemperatureReading(sensorId, type, channel, temperature, humidity, batteryNew,