]> git.basschouten.com Git - openhab-addons.git/blob
2801c5272eb03a641400445efcae3f386b09360c
[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.max.internal.message;
14
15 import java.nio.charset.StandardCharsets;
16 import java.util.Base64;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.max.internal.Utils;
21 import org.openhab.binding.max.internal.device.DeviceType;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * The {@link: NMessage} contains information about a newly discovered Device
27  * This is the response to a n: command
28  *
29  * @author Marcel Verpaalen - Initial contribution
30  */
31 @NonNullByDefault
32 public final class NMessage extends Message {
33     private final Logger logger = LoggerFactory.getLogger(NMessage.class);
34
35     private String decodedPayload = "";
36     private @Nullable DeviceType deviceType;
37     private String rfAddress = "";
38     private String serialnr = "";
39
40     /**
41      * The {@link: NMessage} contains information about a newly discovered Device
42      *
43      * @param raw String with raw message
44      */
45     public NMessage(String raw) {
46         super(raw);
47         String msgPayload = this.getPayload();
48
49         if (msgPayload.length() > 0) {
50             try {
51                 byte[] bytes = Base64.getDecoder().decode(msgPayload.trim());
52                 decodedPayload = new String(bytes, StandardCharsets.UTF_8);
53
54                 deviceType = DeviceType.create(bytes[0] & 0xFF);
55                 rfAddress = Utils.toHex(bytes[1] & 0xFF, bytes[2] & 0xFF, bytes[3] & 0xFF);
56
57                 byte[] data = new byte[10];
58                 System.arraycopy(bytes, 4, data, 0, 10);
59                 serialnr = new String(data, StandardCharsets.UTF_8);
60             } catch (Exception e) {
61                 logger.debug("Exception occurred during parsing of N message: {}", e.getMessage(), e);
62             }
63         } else {
64             logger.debug("No device found during inclusion");
65         }
66     }
67
68     public @Nullable DeviceType getDeviceType() {
69         return deviceType;
70     }
71
72     public String getRfAddress() {
73         return rfAddress;
74     }
75
76     public String getSerialNumber() {
77         return serialnr;
78     }
79
80     @Override
81     public void debug(Logger logger) {
82         if (!this.rfAddress.isEmpty()) {
83             logger.debug("=== N Message === ");
84             logger.trace("\tRAW : {}", this.decodedPayload);
85             logger.debug("\tDevice Type    : {}", this.deviceType);
86             logger.debug("\tRF Address     : {}", this.rfAddress);
87             logger.debug("\tSerial         : {}", this.serialnr);
88         } else {
89             logger.trace("=== N Message === ");
90             logger.trace("\tRAW : {}", this.decodedPayload);
91         }
92     }
93
94     @Override
95     public MessageType getType() {
96         return MessageType.N;
97     }
98 }