]> git.basschouten.com Git - openhab-addons.git/blob
1cb6bafde8b684f3fb8a7098306858fd28fa9bf0
[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.homematic.internal.communicator.parser;
14
15 import static org.openhab.binding.homematic.internal.HomematicBindingConstants.CONFIGURATION_CHANNEL_NUMBER;
16
17 import java.io.IOException;
18 import java.util.Collection;
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import org.openhab.binding.homematic.internal.common.HomematicConfig;
23 import org.openhab.binding.homematic.internal.misc.MiscUtils;
24 import org.openhab.binding.homematic.internal.model.HmChannel;
25 import org.openhab.binding.homematic.internal.model.HmDevice;
26 import org.openhab.binding.homematic.internal.model.HmInterface;
27
28 /**
29  * Parses a list devices message and generates device and channel metadata.
30  *
31  * @author Gerhard Riegler - Initial contribution
32  */
33 public class ListDevicesParser extends CommonRpcParser<Object[], Collection<HmDevice>> {
34     private HmInterface hmInterface;
35     private HomematicConfig config;
36
37     public ListDevicesParser(HmInterface hmInterface, HomematicConfig config) {
38         this.hmInterface = hmInterface;
39         this.config = config;
40     }
41
42     @Override
43     @SuppressWarnings("unchecked")
44     public Collection<HmDevice> parse(Object[] message) throws IOException {
45         message = (Object[]) message[0];
46         Map<String, HmDevice> devices = new HashMap<>();
47
48         for (int i = 0; i < message.length; i++) {
49             Map<String, ?> data = (Map<String, ?>) message[i];
50             if (MiscUtils.isDevice(toString(data.get("ADDRESS")), true)) {
51                 String address = getSanitizedAddress(data.get("ADDRESS"));
52                 String type = MiscUtils.validateCharacters(toString(data.get("TYPE")), "Device type", "-");
53                 String id = toString(data.get("ID"));
54                 String firmware = toString(data.get("FIRMWARE"));
55
56                 HmDevice device = new HmDevice(address, hmInterface, type, config.getGatewayInfo().getId(), id,
57                         firmware);
58                 device.addChannel(new HmChannel(type, CONFIGURATION_CHANNEL_NUMBER));
59                 devices.put(address, device);
60             } else {
61                 // channel
62                 String deviceAddress = getSanitizedAddress(data.get("PARENT"));
63                 HmDevice device = devices.get(deviceAddress);
64
65                 String type = toString(data.get("TYPE"));
66                 Integer number = toInteger(data.get("INDEX"));
67
68                 device.addChannel(new HmChannel(type, number));
69             }
70         }
71         return devices.values();
72     }
73 }