]> git.basschouten.com Git - openhab-addons.git/blob
7aecf743786060a20a15f313f0dd2eacc0e7a001
[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.SENSE_REPORT_REQUEST;
16
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
19
20 import org.openhab.binding.plugwise.internal.protocol.field.Humidity;
21 import org.openhab.binding.plugwise.internal.protocol.field.MACAddress;
22 import org.openhab.binding.plugwise.internal.protocol.field.Temperature;
23
24 /**
25  * A Sense periodically sends this message for updating the current temperature and humidity.
26  *
27  * @author Wouter Born - Initial contribution
28  */
29 public class SenseReportRequestMessage extends Message {
30
31     private static final Pattern PAYLOAD_PATTERN = Pattern.compile("(\\w{16})(\\w{4})(\\w{4})");
32
33     private Humidity humidity;
34     private Temperature temperature;
35
36     public SenseReportRequestMessage(int sequenceNumber, String payload) {
37         super(SENSE_REPORT_REQUEST, sequenceNumber, payload);
38     }
39
40     public Humidity getHumidity() {
41         return humidity;
42     }
43
44     public Temperature getTemperature() {
45         return temperature;
46     }
47
48     @Override
49     protected void parsePayload() {
50         Matcher matcher = PAYLOAD_PATTERN.matcher(payload);
51         if (matcher.matches()) {
52             macAddress = new MACAddress(matcher.group(1));
53             humidity = new Humidity(matcher.group(2));
54             temperature = new Temperature(matcher.group(3));
55         } else {
56             throw new PlugwisePayloadMismatchException(SENSE_REPORT_REQUEST, PAYLOAD_PATTERN, payload);
57         }
58     }
59 }