]> git.basschouten.com Git - openhab-addons.git/blob
d53379ebbfa6cc3a4e94d9c6e8ce1d8d2cef2186
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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 java.time.ZoneOffset.UTC;
16 import static org.openhab.binding.plugwise.internal.protocol.field.MessageType.POWER_INFORMATION_RESPONSE;
17
18 import java.time.Duration;
19 import java.time.ZonedDateTime;
20 import java.util.regex.Matcher;
21 import java.util.regex.Pattern;
22
23 import org.openhab.binding.plugwise.internal.protocol.field.Energy;
24 import org.openhab.binding.plugwise.internal.protocol.field.MACAddress;
25
26 /**
27  * Contains the real-time energy consumption of a relay device (Circle, Circle+, Stealth). This
28  * message is the response of a {@link PowerInformationRequestMessage}.
29  *
30  * @author Wouter Born, Karel Goderis - Initial contribution
31  */
32 public class PowerInformationResponseMessage extends Message {
33
34     private static final Pattern PAYLOAD_PATTERN = Pattern.compile("(\\w{16})(\\w{4})(\\w{4})(\\w{8})(\\w{8})(\\w{4})");
35     private static final double NANOSECONDS_CORRECTION_DIVISOR = 0.000046875; // 46875 divided by nanos per second
36
37     private Energy oneSecond;
38     private Energy eightSecond;
39     private Energy oneHourConsumed;
40     private Energy oneHourProduced;
41     private long nanosCorrection;
42
43     public PowerInformationResponseMessage(int sequenceNumber, String payload) {
44         super(POWER_INFORMATION_RESPONSE, sequenceNumber, payload);
45     }
46
47     public Energy getEightSecond() {
48         return eightSecond;
49     }
50
51     public Energy getOneHourConsumed() {
52         return oneHourConsumed;
53     }
54
55     public Energy getOneHourProduced() {
56         return oneHourProduced;
57     }
58
59     public Energy getOneSecond() {
60         return oneSecond;
61     }
62
63     @Override
64     protected void parsePayload() {
65         Matcher matcher = PAYLOAD_PATTERN.matcher(payload);
66         if (matcher.matches()) {
67             ZonedDateTime utcNow = ZonedDateTime.now(UTC);
68             macAddress = new MACAddress(matcher.group(1));
69             nanosCorrection = Math.round(Integer.parseInt(matcher.group(6), 16) / NANOSECONDS_CORRECTION_DIVISOR);
70             oneSecond = new Energy(utcNow, Integer.parseInt(matcher.group(2), 16),
71                     Duration.ofSeconds(1, nanosCorrection));
72             eightSecond = new Energy(utcNow, Integer.parseInt(matcher.group(3), 16),
73                     Duration.ofSeconds(8, nanosCorrection));
74             oneHourConsumed = new Energy(utcNow, Long.parseLong(matcher.group(4), 16), Duration.ofHours(1));
75             oneHourProduced = new Energy(utcNow, Long.parseLong(matcher.group(5), 16), Duration.ofHours(1));
76         } else {
77             throw new PlugwisePayloadMismatchException(POWER_INFORMATION_RESPONSE, PAYLOAD_PATTERN, payload);
78         }
79     }
80 }