2 * Copyright (c) 2010-2021 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.homematic.internal.communicator.parser;
15 import static org.openhab.binding.homematic.internal.HomematicBindingConstants.CONFIGURATION_CHANNEL_NUMBER;
17 import java.io.IOException;
18 import java.util.Collection;
19 import java.util.HashMap;
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;
30 * Parses a list devices message and generates device and channel metadata.
32 * @author Gerhard Riegler - Initial contribution
34 public class ListDevicesParser extends CommonRpcParser<Object[], Collection<HmDevice>> {
35 private HmInterface hmInterface;
36 private HomematicConfig config;
38 public ListDevicesParser(HmInterface hmInterface, HomematicConfig config) {
39 this.hmInterface = hmInterface;
44 @SuppressWarnings("unchecked")
45 public Collection<HmDevice> parse(Object[] message) throws IOException {
46 message = (Object[]) message[0];
47 Map<String, HmDevice> devices = new HashMap<>();
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")), ":");
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"));
59 HmDevice device = new HmDevice(address, hmInterface, type, config.getGatewayInfo().getId(), id,
61 device.addChannel(new HmChannel(type, CONFIGURATION_CHANNEL_NUMBER));
62 devices.put(address, device);
65 String deviceAddress = getSanitizedAddress(data.get("PARENT"));
66 HmDevice device = devices.get(deviceAddress);
68 String type = toString(data.get("TYPE"));
69 Integer number = toInteger(data.get("INDEX"));
71 device.addChannel(new HmChannel(type, number));
74 return devices.values();