2 * Copyright (c) 2010-2022 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.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;
29 * Parses a list devices message and generates device and channel metadata.
31 * @author Gerhard Riegler - Initial contribution
33 public class ListDevicesParser extends CommonRpcParser<Object[], Collection<HmDevice>> {
34 private HmInterface hmInterface;
35 private HomematicConfig config;
37 public ListDevicesParser(HmInterface hmInterface, HomematicConfig config) {
38 this.hmInterface = hmInterface;
43 @SuppressWarnings("unchecked")
44 public Collection<HmDevice> parse(Object[] message) throws IOException {
45 message = (Object[]) message[0];
46 Map<String, HmDevice> devices = new HashMap<>();
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"));
56 HmDevice device = new HmDevice(address, hmInterface, type, config.getGatewayInfo().getId(), id,
58 device.addChannel(new HmChannel(type, CONFIGURATION_CHANNEL_NUMBER));
59 devices.put(address, device);
62 String deviceAddress = getSanitizedAddress(data.get("PARENT"));
63 HmDevice device = devices.get(deviceAddress);
65 String type = toString(data.get("TYPE"));
66 Integer number = toInteger(data.get("INDEX"));
68 device.addChannel(new HmChannel(type, number));
71 return devices.values();