]> git.basschouten.com Git - openhab-addons.git/blob
a0944c7c6d62befe7787cd3c81feb718dcc4be88
[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.ANNOUNCE_AWAKE_REQUEST;
16
17 import java.util.Arrays;
18 import java.util.regex.Matcher;
19 import java.util.regex.Pattern;
20
21 import org.openhab.binding.plugwise.internal.protocol.field.MACAddress;
22
23 /**
24  * A sleeping end device (SED: Scan, Sense, Switch) sends this message to announce that is awake.
25  *
26  * @author Wouter Born - Initial contribution
27  */
28 public class AnnounceAwakeRequestMessage extends Message {
29
30     public enum AwakeReason {
31
32         /** The SED joins the network for maintenance */
33         MAINTENANCE(0),
34
35         /** The SED joins a network for the first time */
36         JOIN_NETWORK(1),
37
38         /** The SED joins a network it has already joined, e.g. after reinserting a battery */
39         REJOIN_NETWORK(2),
40
41         /** When a SED switches a device group or when reporting values such as temperature/humidity */
42         NORMAL(3),
43
44         /** A human pressed the button on a SED to wake it up */
45         WAKEUP_BUTTON(5);
46
47         public static AwakeReason forValue(int value) {
48             return Arrays.stream(values()).filter(awakeReason -> awakeReason.id == value).findFirst().orElse(null);
49         }
50
51         private final int id;
52
53         AwakeReason(int id) {
54             this.id = id;
55         }
56     }
57
58     private static final Pattern PAYLOAD_PATTERN = Pattern.compile("(\\w{16})(\\w{2})");
59
60     private AwakeReason awakeReason;
61
62     public AnnounceAwakeRequestMessage(int sequenceNumber, String payload) {
63         super(ANNOUNCE_AWAKE_REQUEST, sequenceNumber, payload);
64     }
65
66     public AwakeReason getAwakeReason() {
67         return awakeReason;
68     }
69
70     @Override
71     protected void parsePayload() {
72         Matcher matcher = PAYLOAD_PATTERN.matcher(payload);
73         if (matcher.matches()) {
74             macAddress = new MACAddress(matcher.group(1));
75             awakeReason = AwakeReason.forValue(Integer.parseInt(matcher.group(2)));
76         } else {
77             throw new PlugwisePayloadMismatchException(ANNOUNCE_AWAKE_REQUEST, PAYLOAD_PATTERN, payload);
78         }
79     }
80 }