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.pca301;
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 pcaSerial sketch to a Pca301Reading.
25 * @author Volker Bier - Initial contribution
27 public class Pca301ReadingConverter implements JeeLinkReadingConverter<Pca301Reading> {
28 private static final Pattern READING_P = Pattern.compile(
29 "OK\\s+24\\s+([0-9]+)\\s+4\\s+([0-9]+)\\s+([0-9]+)\\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(Pca301ReadingConverter.class);
34 public Pca301Reading createReading(String inputLine) {
35 // parse lines only if we have registered listeners
36 if (inputLine != null) {
37 Matcher matcher = READING_P.matcher(inputLine);
38 if (matcher.matches()) {
41 // OK 24 1 4 1 160 236 0 0 0 0 0
45 // 1 Byte: command (04=retrieve measure data, 05=switch device, 06=identify device by toggling device
47 // 3 Byte: device address (UID)
48 // 1 Byte: data -> 1 with command=4 resets device statistics
49 // -> 0/1 with command=5 switches device off/on
50 // 2 Byte: current consumption in watt (scale 1/10)
51 // 2 Byte: total consumption in kWh (scale 1/100)
52 logger.trace("Creating reading from: {}", inputLine);
54 int channelId = Integer.parseInt(matcher.group(1));
55 String sensorId = matcher.group(2) + "-" + matcher.group(3) + "-" + matcher.group(4);
56 int data = Integer.parseInt(matcher.group(5));
58 long con1 = Long.parseLong(matcher.group(6));
59 long con2 = Long.parseLong(matcher.group(7));
60 long consumptionCurrent = ((con1 << 8) + con2);
62 con1 = Long.parseLong(matcher.group(8));
63 con2 = Long.parseLong(matcher.group(9));
64 long consumptionTotal = ((con1 << 8) + con2);
66 return new Pca301Reading(sensorId, channelId, data == 1, consumptionCurrent / 10f,
67 consumptionTotal * 10);