]> git.basschouten.com Git - openhab-addons.git/blob
19d8c4fb660caf32abfee5b0f879fca9db384180
[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.NETWORK_STATUS_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  * Contains the current network status as well as the MAC address of the Circle+ that coordinates the network. The Stick
24  * sends this message as response of a {@link NetworkStatusRequestMessage}.
25  *
26  * @author Wouter Born, Karel Goderis - Initial contribution
27  */
28 public class NetworkStatusResponseMessage extends Message {
29
30     private static final Pattern PAYLOAD_PATTERN = Pattern
31             .compile("(\\w{16})(\\w{2})(\\w{2})(\\w{16})(\\w{4})(\\w{2})");
32
33     private boolean online;
34     private String networkID;
35     private String unknown1;
36     private String unknown2;
37     private String shortNetworkID;
38
39     private MACAddress circlePlusMAC;
40
41     public NetworkStatusResponseMessage(int sequenceNumber, String payload) {
42         super(NETWORK_STATUS_RESPONSE, sequenceNumber, payload);
43     }
44
45     public NetworkStatusResponseMessage(String payload) {
46         super(NETWORK_STATUS_RESPONSE, payload);
47     }
48
49     public MACAddress getCirclePlusMAC() {
50         return circlePlusMAC;
51     }
52
53     public String getNetworkID() {
54         return networkID;
55     }
56
57     public String getShortNetworkID() {
58         return shortNetworkID;
59     }
60
61     public String getUnknown1() {
62         return unknown1;
63     }
64
65     public String getUnknown2() {
66         return unknown2;
67     }
68
69     public boolean isOnline() {
70         return online;
71     }
72
73     @Override
74     protected void parsePayload() {
75         Matcher matcher = PAYLOAD_PATTERN.matcher(payload);
76         if (matcher.matches()) {
77             macAddress = new MACAddress(matcher.group(1));
78             unknown1 = matcher.group(2);
79             online = (Integer.parseInt(matcher.group(3), 16) == 1);
80             networkID = matcher.group(4);
81             shortNetworkID = matcher.group(5);
82             unknown2 = matcher.group(6);
83
84             // now some serious protocol reverse-engineering assumption. Circle+ MAC = networkID with first two bytes
85             // replaced by 00
86             circlePlusMAC = new MACAddress("00" + networkID.substring(2));
87         } else {
88             throw new PlugwisePayloadMismatchException(NETWORK_STATUS_RESPONSE, PAYLOAD_PATTERN, payload);
89         }
90     }
91
92     @Override
93     protected String payloadToHexString() {
94         return unknown1 + String.format("%02X", online ? 1 : 0) + networkID + shortNetworkID + unknown2;
95     }
96 }