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.ArrayList;
17 import java.util.Base64;
18 import java.util.Collection;
19 import java.util.List;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.max.internal.Utils;
23 import org.openhab.binding.max.internal.device.Device;
24 import org.openhab.binding.max.internal.device.DeviceConfiguration;
25 import org.slf4j.Logger;
28 * The L message contains real time information about all MAX! devices.
30 * @author Andreas Heil - Initial contribution
31 * @author Marcel Verpaalen - OH2 update
35 public final class LMessage extends Message {
37 public LMessage(String raw) {
41 public Collection<? extends Device> getDevices(List<DeviceConfiguration> configurations) {
42 final List<Device> devices = new ArrayList<>();
44 final byte[] decodedRawMessage = Base64.getDecoder()
45 .decode(getPayload().trim().getBytes(StandardCharsets.UTF_8));
47 final MaxTokenizer tokenizer = new MaxTokenizer(decodedRawMessage);
49 while (tokenizer.hasMoreElements()) {
50 byte[] token = tokenizer.nextElement();
51 final Device tempDevice = Device.create(token, configurations);
52 if (tempDevice != null) {
53 devices.add(tempDevice);
60 public Collection<? extends Device> updateDevices(List<Device> devices, List<DeviceConfiguration> configurations) {
61 byte[] decodedRawMessage = Base64.getDecoder().decode(getPayload().trim().getBytes(StandardCharsets.UTF_8));
63 MaxTokenizer tokenizer = new MaxTokenizer(decodedRawMessage);
65 while (tokenizer.hasMoreElements()) {
66 byte[] token = tokenizer.nextElement();
67 String rfAddress = Utils.toHex(token[0] & 0xFF, token[1] & 0xFF, token[2] & 0xFF);
68 // logger.debug("token: "+token+" rfaddress: "+rfAddress);
70 Device foundDevice = null;
71 for (Device device : devices) {
72 // logger.debug(device.getRFAddress().toUpperCase()+ " vs "+rfAddress);
73 if (device.getRFAddress().toUpperCase().equals(rfAddress)) {
74 // logger.debug("Updating device..."+rfAddress);
78 if (foundDevice != null) {
79 foundDevice = Device.update(token, configurations, foundDevice);
80 // devices.remove(token);
81 // devices.add(foundDevice);
83 Device tempDevice = Device.create(token, configurations);
84 if (tempDevice != null) {
85 devices.add(tempDevice);
94 public void debug(Logger logger) {
95 logger.trace("=== L Message === ");
96 logger.trace("\tRAW: {}", this.getPayload());
100 public MessageType getType() {
101 return MessageType.L;