]> git.basschouten.com Git - openhab-addons.git/blob
bf2fc41c9599cbd96641bc6548a5365e33896587
[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.PING_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 network diagnostic information. This message is the response of a {@link PingRequestMessage}.
24  *
25  * @author Wouter Born - Initial contribution
26  */
27 public class PingResponseMessage extends Message {
28
29     private static final Pattern PAYLOAD_PATTERN = Pattern.compile("(\\w{16})(\\w{2})(\\w{2})(\\w{4})");
30
31     private int inRSSI;
32     private int outRSSI;
33     private int pingMillis;
34
35     public PingResponseMessage(int sequenceNumber, String payload) {
36         super(PING_RESPONSE, sequenceNumber, payload);
37     }
38
39     public int getInRSSI() {
40         return inRSSI;
41     }
42
43     public int getOutRSSI() {
44         return outRSSI;
45     }
46
47     public int getPingMillis() {
48         return pingMillis;
49     }
50
51     @Override
52     protected void parsePayload() {
53         Matcher matcher = PAYLOAD_PATTERN.matcher(payload);
54         if (matcher.matches()) {
55             macAddress = new MACAddress(matcher.group(1));
56             inRSSI = (Integer.parseInt(matcher.group(2), 16));
57             outRSSI = (Integer.parseInt(matcher.group(3), 16));
58             pingMillis = (Integer.parseInt(matcher.group(4), 16));
59         } else {
60             throw new PlugwisePayloadMismatchException(PING_RESPONSE, PAYLOAD_PATTERN, payload);
61         }
62     }
63 }