]> git.basschouten.com Git - openhab-addons.git/blob
cf23de4e384d67244382393c74fe86eebe48d705
[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 java.io.IOException;
16 import java.util.Collection;
17 import java.util.HashMap;
18 import java.util.Map;
19
20 import org.openhab.binding.homematic.internal.model.HmDevice;
21
22 /**
23  * Parses a Homegear message containing names for devices.
24  *
25  * @author Gerhard Riegler - Initial contribution
26  */
27 public class HomegearLoadDeviceNamesParser extends CommonRpcParser<Object[], Void> {
28     private Collection<HmDevice> devices;
29
30     public HomegearLoadDeviceNamesParser(Collection<HmDevice> devices) {
31         this.devices = devices;
32     }
33
34     @Override
35     @SuppressWarnings("unchecked")
36     public Void parse(Object[] message) throws IOException {
37         Map<String, HmDevice> devicesById = new HashMap<>();
38         for (HmDevice device : devices) {
39             devicesById.put(device.getHomegearId(), device);
40         }
41
42         message = (Object[]) message[0];
43         for (int i = 0; i < message.length; i++) {
44             Map<String, ?> data = (Map<String, ?>) message[i];
45             String id = toString(data.get("ID"));
46             String name = toString(data.get("NAME"));
47
48             HmDevice device = devicesById.get(getSanitizedAddress(id));
49             if (device != null) {
50                 device.setName(name);
51             }
52
53         }
54
55         return null;
56     }
57 }