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.ec3k;
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
18 import org.openhab.binding.jeelink.internal.JeeLinkReadingConverter;
21 * Converter for converting a line read from an ec3kSerial sketch to an Ec3kReading.
23 * @author Volker Bier - Initial contribution
25 public class Ec3kReadingConverter implements JeeLinkReadingConverter<Ec3kReading> {
26 private static final Pattern LINE_P = Pattern.compile("""
27 OK\\s+22\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\
28 \\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\
29 \\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\
33 public Ec3kReading createReading(String inputLine) {
34 if (inputLine != null) {
35 Matcher matcher = LINE_P.matcher(inputLine);
36 if (matcher.matches()) {
38 * OK 22 188 129 0 209 209 102 0 174 89 187 0 1 123 102 0 0 10 117 2 0 (ID = BC81)
40 long id1 = Long.parseLong(matcher.group(1));
41 long id2 = Long.parseLong(matcher.group(2));
42 String id = String.format("%02X%02X", id1, id2);
44 long secTot1 = Long.parseLong(matcher.group(3));
45 long secTot2 = Long.parseLong(matcher.group(4));
46 long secTot3 = Long.parseLong(matcher.group(5));
47 long secTot4 = Long.parseLong(matcher.group(6));
48 long secondsTotal = (secTot1 << 24) + (secTot2 << 16) + (secTot3 << 8) + secTot4;
50 long secOn1 = Long.parseLong(matcher.group(7));
51 long secOn2 = Long.parseLong(matcher.group(8));
52 long secOn3 = Long.parseLong(matcher.group(9));
53 long secOn4 = Long.parseLong(matcher.group(10));
54 long secondsOn = (secOn1 << 24) + (secOn2 << 16) + (secOn3 << 8) + secOn4;
56 long con1 = Long.parseLong(matcher.group(11));
57 long con2 = Long.parseLong(matcher.group(12));
58 long con3 = Long.parseLong(matcher.group(13));
59 long con4 = Long.parseLong(matcher.group(14));
60 long consumptionTotal = ((con1 << 24) + (con2 << 16) + (con3 << 8) + con4) / 1000;
62 long cur1 = Long.parseLong(matcher.group(15));
63 long cur2 = Long.parseLong(matcher.group(16));
64 float currentWatt = ((cur1 << 8) + cur2) / 10f;
66 long max1 = Long.parseLong(matcher.group(17));
67 long max2 = Long.parseLong(matcher.group(18));
68 float maxWatt = ((max1 << 8) + max2) / 10f;
70 int resets = Integer.parseInt(matcher.group(19));
71 return new Ec3kReading(id, currentWatt, maxWatt, consumptionTotal, secondsOn, secondsTotal, resets);