2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.plugwise.internal.protocol;
15 import static org.openhab.binding.plugwise.internal.protocol.field.MessageType.NETWORK_STATUS_RESPONSE;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
20 import org.openhab.binding.plugwise.internal.protocol.field.MACAddress;
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}.
26 * @author Wouter Born, Karel Goderis - Initial contribution
28 public class NetworkStatusResponseMessage extends Message {
30 private static final Pattern PAYLOAD_PATTERN = Pattern
31 .compile("(\\w{16})(\\w{2})(\\w{2})(\\w{16})(\\w{4})(\\w{2})");
33 private boolean online;
34 private String networkID;
35 private String unknown1;
36 private String unknown2;
37 private String shortNetworkID;
39 private MACAddress circlePlusMAC;
41 public NetworkStatusResponseMessage(int sequenceNumber, String payload) {
42 super(NETWORK_STATUS_RESPONSE, sequenceNumber, payload);
45 public NetworkStatusResponseMessage(String payload) {
46 super(NETWORK_STATUS_RESPONSE, payload);
49 public MACAddress getCirclePlusMAC() {
53 public String getNetworkID() {
57 public String getShortNetworkID() {
58 return shortNetworkID;
61 public String getUnknown1() {
65 public String getUnknown2() {
69 public boolean isOnline() {
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);
84 // now some serious protocol reverse-engineering assumption. Circle+ MAC = networkID with first two bytes
86 circlePlusMAC = new MACAddress("00" + networkID.substring(2));
88 throw new PlugwisePayloadMismatchException(NETWORK_STATUS_RESPONSE, PAYLOAD_PATTERN, payload);
93 protected String payloadToHexString() {
94 return unknown1 + String.format("%02X", online ? 1 : 0) + networkID + shortNetworkID + unknown2;