]> git.basschouten.com Git - openhab-addons.git/blob
3bc804980e69c44f0fd045fd86c2afff29daf9e0
[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
27             .compile("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     @Override
31     public RevoltReading createReading(String inputLine) {
32         if (inputLine != null) {
33             Matcher matcher = LINE_P.matcher(inputLine);
34             if (matcher.matches()) {
35                 /*
36                  * r4F1BE400513206875B312F25
37                  */
38                 String id = matcher.group(1); // 4F1B
39
40                 int voltage = toInt(matcher.group(2)); // 0xE4 = 228 => 228 V
41                 float current = toInt(matcher.group(3)) / 100f; // 0x0051 = 81 => 0,81 A
42                 int frequency = toInt(matcher.group(4)); // 0x32 = 50 => 50 Hz
43                 float power = toInt(matcher.group(5)) / 10f; // 0x0687 = 1671 => 167,1 W
44                 float powerFact = toInt(matcher.group(6)) / 100f; // 0x5B = 91 => 0,91 VA
45                 float consumption = toInt(matcher.group(7)) / 100f; // 0x312F = 12591 => 125,91 Wh
46
47                 return new RevoltReading(id, voltage, current, frequency, power, powerFact, consumption);
48             }
49         }
50
51         return null;
52     }
53
54     private int toInt(String hex) {
55         Integer i = Integer.parseInt(hex, 16);
56         return i.intValue();
57     }
58 }