]> git.basschouten.com Git - openhab-addons.git/blob
364629bdcd58aa5662309f357099ba2efe853ef2
[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.pca301;
14
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
17
18 import org.openhab.binding.jeelink.internal.JeeLinkReadingConverter;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * Converter for converting a line read from a pcaSerial sketch to a Pca301Reading.
24  *
25  * @author Volker Bier - Initial contribution
26  */
27 public class Pca301ReadingConverter implements JeeLinkReadingConverter<Pca301Reading> {
28     private static final Pattern READING_P = Pattern.compile(
29             "OK\\s+24\\s+([0-9]+)\\s+4\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)");
30
31     private final Logger logger = LoggerFactory.getLogger(Pca301ReadingConverter.class);
32
33     @Override
34     public Pca301Reading createReading(String inputLine) {
35         // parse lines only if we have registered listeners
36         if (inputLine != null) {
37             Matcher matcher = READING_P.matcher(inputLine);
38             if (matcher.matches()) {
39                 // Format
40                 //
41                 // OK 24 1 4 1 160 236 0 0 0 0 0
42                 // Interpretation:
43                 // OK 24: fixed
44                 // 1 Byte: channel
45                 // 1 Byte: command (04=retrieve measure data, 05=switch device, 06=identify device by toggling device
46                 // LED
47                 // 3 Byte: device address (UID)
48                 // 1 Byte: data -> 1 with command=4 resets device statistics
49                 // -> 0/1 with command=5 switches device off/on
50                 // 2 Byte: current consumption in watt (scale 1/10)
51                 // 2 Byte: total consumption in kWh (scale 1/100)
52                 logger.trace("Creating reading from: {}", inputLine);
53
54                 int channelId = Integer.parseInt(matcher.group(1));
55                 String sensorId = matcher.group(2) + "-" + matcher.group(3) + "-" + matcher.group(4);
56                 int data = Integer.parseInt(matcher.group(5));
57
58                 long con1 = Long.parseLong(matcher.group(6));
59                 long con2 = Long.parseLong(matcher.group(7));
60                 long consumptionCurrent = ((con1 << 8) + con2);
61
62                 con1 = Long.parseLong(matcher.group(8));
63                 con2 = Long.parseLong(matcher.group(9));
64                 long consumptionTotal = ((con1 << 8) + con2);
65
66                 return new Pca301Reading(sensorId, channelId, data == 1, consumptionCurrent / 10f,
67                         consumptionTotal * 10);
68             }
69         }
70
71         return null;
72     }
73 }