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