2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.plugwise.internal.protocol;
15 import static org.openhab.binding.plugwise.internal.protocol.field.MessageType.POWER_CALIBRATION_RESPONSE;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
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;
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.
29 * @author Wouter Born, Karel Goderis - Initial contribution
31 public class PowerCalibrationResponseMessage extends Message {
33 private static final Pattern PAYLOAD_PATTERN = Pattern.compile("(\\w{16})(\\w{8})(\\w{8})(\\w{8})(\\w{8})");
37 private double offsetTotal;
38 private double offsetNoise;
40 public PowerCalibrationResponseMessage(int sequenceNumber, String payload) {
41 super(POWER_CALIBRATION_RESPONSE, sequenceNumber, payload);
44 public double getGainA() {
48 public double getGainB() {
52 public double getOffsetNoise() {
56 public double getOffsetTotal() {
60 public PowerCalibration getCalibration() {
61 return new PowerCalibration(gainA, gainB, offsetNoise, offsetTotal);
65 protected void parsePayload() {
66 Matcher matcher = PAYLOAD_PATTERN.matcher(payload);
67 if (matcher.matches()) {
68 macAddress = new MACAddress(matcher.group(1));
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)));
75 throw new PlugwisePayloadMismatchException(POWER_CALIBRATION_RESPONSE, PAYLOAD_PATTERN, payload);