]> git.basschouten.com Git - openhab-addons.git/blob
8119f28d957d737c53917123c5f92480add15316
[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.plugwise.internal.protocol;
14
15 import static org.openhab.binding.plugwise.internal.protocol.field.MessageType.POWER_CALIBRATION_RESPONSE;
16
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
19
20 import org.openhab.binding.plugwise.internal.protocol.field.Energy;
21 import org.openhab.binding.plugwise.internal.protocol.field.MACAddress;
22 import org.openhab.binding.plugwise.internal.protocol.field.PowerCalibration;
23
24 /**
25  * Contains the power calibration data of a relay device (Circle, Circle+, Stealth). This message is the response of a
26  * {@link PowerCalibrationRequestMessage}. The {@link PowerCalibration} data is used to calculate power (W) and energy
27  * (kWh) from pulses with the {@link Energy} class.
28  *
29  * @author Wouter Born, Karel Goderis - Initial contribution
30  */
31 public class PowerCalibrationResponseMessage extends Message {
32
33     private static final Pattern PAYLOAD_PATTERN = Pattern.compile("(\\w{16})(\\w{8})(\\w{8})(\\w{8})(\\w{8})");
34
35     private double gainA;
36     private double gainB;
37     private double offsetTotal;
38     private double offsetNoise;
39
40     public PowerCalibrationResponseMessage(int sequenceNumber, String payload) {
41         super(POWER_CALIBRATION_RESPONSE, sequenceNumber, payload);
42     }
43
44     public double getGainA() {
45         return gainA;
46     }
47
48     public double getGainB() {
49         return gainB;
50     }
51
52     public double getOffsetNoise() {
53         return offsetNoise;
54     }
55
56     public double getOffsetTotal() {
57         return offsetTotal;
58     }
59
60     public PowerCalibration getCalibration() {
61         return new PowerCalibration(gainA, gainB, offsetNoise, offsetTotal);
62     }
63
64     @Override
65     protected void parsePayload() {
66         Matcher matcher = PAYLOAD_PATTERN.matcher(payload);
67         if (matcher.matches()) {
68             macAddress = new MACAddress(matcher.group(1));
69
70             gainA = Float.intBitsToFloat((int) (Long.parseLong(matcher.group(2), 16)));
71             gainB = Float.intBitsToFloat((int) (Long.parseLong(matcher.group(3), 16)));
72             offsetTotal = Float.intBitsToFloat((int) (Long.parseLong(matcher.group(4), 16)));
73             offsetNoise = Float.intBitsToFloat((int) (Long.parseLong(matcher.group(5), 16)));
74         } else {
75             throw new PlugwisePayloadMismatchException(POWER_CALIBRATION_RESPONSE, PAYLOAD_PATTERN, payload);
76         }
77     }
78 }