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
27 .compile("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]+)");
32 public Ec3kReading createReading(String inputLine) {
33 if (inputLine != null) {
34 Matcher matcher = LINE_P.matcher(inputLine);
35 if (matcher.matches()) {
37 * OK 22 188 129 0 209 209 102 0 174 89 187 0 1 123 102 0 0 10 117 2 0 (ID = BC81)
39 long id1 = Long.parseLong(matcher.group(1));
40 long id2 = Long.parseLong(matcher.group(2));
41 String id = String.format("%02X%02X", id1, id2);
43 long secTot1 = Long.parseLong(matcher.group(3));
44 long secTot2 = Long.parseLong(matcher.group(4));
45 long secTot3 = Long.parseLong(matcher.group(5));
46 long secTot4 = Long.parseLong(matcher.group(6));
47 long secondsTotal = (secTot1 << 24) + (secTot2 << 16) + (secTot3 << 8) + secTot4;
49 long secOn1 = Long.parseLong(matcher.group(7));
50 long secOn2 = Long.parseLong(matcher.group(8));
51 long secOn3 = Long.parseLong(matcher.group(9));
52 long secOn4 = Long.parseLong(matcher.group(10));
53 long secondsOn = (secOn1 << 24) + (secOn2 << 16) + (secOn3 << 8) + secOn4;
55 long con1 = Long.parseLong(matcher.group(11));
56 long con2 = Long.parseLong(matcher.group(12));
57 long con3 = Long.parseLong(matcher.group(13));
58 long con4 = Long.parseLong(matcher.group(14));
59 long consumptionTotal = ((con1 << 24) + (con2 << 16) + (con3 << 8) + con4) / 1000;
61 long cur1 = Long.parseLong(matcher.group(15));
62 long cur2 = Long.parseLong(matcher.group(16));
63 float currentWatt = ((cur1 << 8) + cur2) / 10f;
65 long max1 = Long.parseLong(matcher.group(17));
66 long max2 = Long.parseLong(matcher.group(18));
67 float maxWatt = ((max1 << 8) + max2) / 10f;
69 int resets = Integer.parseInt(matcher.group(19));
70 return new Ec3kReading(id, currentWatt, maxWatt, consumptionTotal, secondsOn, secondsTotal, resets);