]> git.basschouten.com Git - openhab-addons.git/blob
5b3e4774c42d6b8ba9e916ed172d20597925d7da
[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.ArrayList;
17 import java.util.Base64;
18 import java.util.Collection;
19 import java.util.List;
20
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;
26
27 /**
28  * The L message contains real time information about all MAX! devices.
29  *
30  * @author Andreas Heil - Initial contribution
31  * @author Marcel Verpaalen - OH2 update
32  *
33  */
34 @NonNullByDefault
35 public final class LMessage extends Message {
36
37     public LMessage(String raw) {
38         super(raw);
39     }
40
41     public Collection<? extends Device> getDevices(List<DeviceConfiguration> configurations) {
42         final List<Device> devices = new ArrayList<>();
43
44         final byte[] decodedRawMessage = Base64.getDecoder()
45                 .decode(getPayload().trim().getBytes(StandardCharsets.UTF_8));
46
47         final MaxTokenizer tokenizer = new MaxTokenizer(decodedRawMessage);
48
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);
54             }
55         }
56
57         return devices;
58     }
59
60     public Collection<? extends Device> updateDevices(List<Device> devices, List<DeviceConfiguration> configurations) {
61         byte[] decodedRawMessage = Base64.getDecoder().decode(getPayload().trim().getBytes(StandardCharsets.UTF_8));
62
63         MaxTokenizer tokenizer = new MaxTokenizer(decodedRawMessage);
64
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);
69
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);
75                     foundDevice = device;
76                 }
77             }
78             if (foundDevice != null) {
79                 foundDevice = Device.update(token, configurations, foundDevice);
80                 // devices.remove(token);
81                 // devices.add(foundDevice);
82             } else {
83                 Device tempDevice = Device.create(token, configurations);
84                 if (tempDevice != null) {
85                     devices.add(tempDevice);
86                 }
87             }
88         }
89
90         return devices;
91     }
92
93     @Override
94     public void debug(Logger logger) {
95         logger.trace("=== L Message === ");
96         logger.trace("\tRAW: {}", this.getPayload());
97     }
98
99     @Override
100     public MessageType getType() {
101         return MessageType.L;
102     }
103 }