]> git.basschouten.com Git - openhab-addons.git/blob
ab72c4fea58a0b86e7afdfc3e17869414329694c
[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.BROADCAST_GROUP_SWITCH_RESPONSE;
16
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
19
20 import org.openhab.binding.plugwise.internal.protocol.field.MACAddress;
21
22 /**
23  * A sleeping end device (SED: Scan, Sense, Switch) sends this message to switch groups on/off when the configured
24  * switching conditions have been met.
25  *
26  * @author Wouter Born - Initial contribution
27  */
28 public class BroadcastGroupSwitchResponseMessage extends Message {
29
30     private static final Pattern PAYLOAD_PATTERN = Pattern.compile("(\\w{16})(\\w{2})(\\w{2})");
31
32     private int portMask;
33     private boolean powerState;
34
35     public BroadcastGroupSwitchResponseMessage(int sequenceNumber, String payload) {
36         super(BROADCAST_GROUP_SWITCH_RESPONSE, sequenceNumber, payload);
37     }
38
39     public int getPortMask() {
40         return portMask;
41     }
42
43     public boolean getPowerState() {
44         return powerState;
45     }
46
47     @Override
48     protected void parsePayload() {
49         Matcher matcher = PAYLOAD_PATTERN.matcher(payload);
50         if (matcher.matches()) {
51             macAddress = new MACAddress(matcher.group(1));
52             portMask = Integer.parseInt(matcher.group(2));
53             powerState = ("01".equals(matcher.group(3)));
54         } else {
55             throw new PlugwisePayloadMismatchException(BROADCAST_GROUP_SWITCH_RESPONSE, PAYLOAD_PATTERN, payload);
56         }
57     }
58 }