]> git.basschouten.com Git - openhab-addons.git/blob
0910804fdd9265cc7fe8bb021582da0c7c5a8598
[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.ec3k;
14
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
17
18 import org.openhab.binding.jeelink.internal.JeeLinkReadingConverter;
19
20 /**
21  * Converter for converting a line read from an ec3kSerial sketch to an Ec3kReading.
22  *
23  * @author Volker Bier - Initial contribution
24  */
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]+)\
30             """);
31
32     @Override
33     public Ec3kReading createReading(String inputLine) {
34         if (inputLine != null) {
35             Matcher matcher = LINE_P.matcher(inputLine);
36             if (matcher.matches()) {
37                 /*
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)
39                  */
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);
43
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;
49
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;
55
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;
61
62                 long cur1 = Long.parseLong(matcher.group(15));
63                 long cur2 = Long.parseLong(matcher.group(16));
64                 float currentWatt = ((cur1 << 8) + cur2) / 10f;
65
66                 long max1 = Long.parseLong(matcher.group(17));
67                 long max2 = Long.parseLong(matcher.group(18));
68                 float maxWatt = ((max1 << 8) + max2) / 10f;
69
70                 int resets = Integer.parseInt(matcher.group(19));
71                 return new Ec3kReading(id, currentWatt, maxWatt, consumptionTotal, secondsOn, secondsTotal, resets);
72             }
73         }
74
75         return null;
76     }
77 }