]> git.basschouten.com Git - openhab-addons.git/blob
501c22c08c8dad15d2635397bcb465be3a294122
[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.revolt;
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 a SlowRF CUL to a RevoltReading.
22  *
23  * @author Volker Bier - Initial contribution
24  */
25 public class RevoltReadingConverter implements JeeLinkReadingConverter<RevoltReading> {
26     private static final Pattern LINE_P = Pattern.compile("""
27             r([0-9A-Za-z]{4})([0-9A-Za-z]{2})([0-9A-Za-z]{4})([0-9A-Za-z]{2})([0-9A-Za-z]{4})\
28             ([0-9A-Za-z]{2})([0-9A-Za-z]{4})[0-9A-Za-z][0-9A-Za-z]\
29             """);
30
31     @Override
32     public RevoltReading createReading(String inputLine) {
33         if (inputLine != null) {
34             Matcher matcher = LINE_P.matcher(inputLine);
35             if (matcher.matches()) {
36                 /*
37                  * r4F1BE400513206875B312F25
38                  */
39                 String id = matcher.group(1); // 4F1B
40
41                 int voltage = toInt(matcher.group(2)); // 0xE4 = 228 => 228 V
42                 float current = toInt(matcher.group(3)) / 100f; // 0x0051 = 81 => 0,81 A
43                 int frequency = toInt(matcher.group(4)); // 0x32 = 50 => 50 Hz
44                 float power = toInt(matcher.group(5)) / 10f; // 0x0687 = 1671 => 167,1 W
45                 float powerFact = toInt(matcher.group(6)) / 100f; // 0x5B = 91 => 0,91 VA
46                 float consumption = toInt(matcher.group(7)) / 100f; // 0x312F = 12591 => 125,91 Wh
47
48                 return new RevoltReading(id, voltage, current, frequency, power, powerFact, consumption);
49             }
50         }
51
52         return null;
53     }
54
55     private int toInt(String hex) {
56         Integer i = Integer.parseInt(hex, 16);
57         return i.intValue();
58     }
59 }