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.max.internal.message;
15 import java.nio.charset.StandardCharsets;
16 import java.util.Base64;
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;
26 * The {@link NMessage} contains information about a newly discovered Device
27 * This is the response to a n: command
29 * @author Marcel Verpaalen - Initial contribution
32 public final class NMessage extends Message {
33 private final Logger logger = LoggerFactory.getLogger(NMessage.class);
35 private String decodedPayload = "";
36 private @Nullable DeviceType deviceType;
37 private String rfAddress = "";
38 private String serialnr = "";
41 * The {@link NMessage} contains information about a newly discovered Device
43 * @param raw String with raw message
45 public NMessage(String raw) {
47 String msgPayload = this.getPayload();
49 if (msgPayload.length() > 0) {
51 byte[] bytes = Base64.getDecoder().decode(msgPayload.trim());
52 decodedPayload = new String(bytes, StandardCharsets.UTF_8);
54 deviceType = DeviceType.create(bytes[0] & 0xFF);
55 rfAddress = Utils.toHex(bytes[1] & 0xFF, bytes[2] & 0xFF, bytes[3] & 0xFF);
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);
64 logger.debug("No device found during inclusion");
68 public @Nullable DeviceType getDeviceType() {
72 public String getRfAddress() {
76 public String getSerialNumber() {
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);
89 logger.trace("=== N Message === ");
90 logger.trace("\tRAW : {}", this.decodedPayload);
95 public MessageType getType() {