]> git.basschouten.com Git - openhab-addons.git/blob
ec95df24498b55ae093d1189e4b15b990f069346
[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 import org.openhab.binding.homematic.internal.model.TclScriptDataEntry;
22 import org.openhab.binding.homematic.internal.model.TclScriptDataList;
23
24 /**
25  * Parses a TclRega script result containing names for devices.
26  *
27  * @author Gerhard Riegler - Initial contribution
28  */
29 public class CcuLoadDeviceNamesParser extends CommonRpcParser<TclScriptDataList, Void> {
30     private Collection<HmDevice> devices;
31
32     public CcuLoadDeviceNamesParser(Collection<HmDevice> devices) {
33         this.devices = devices;
34     }
35
36     @Override
37     public Void parse(TclScriptDataList resultList) throws IOException {
38         if (resultList.getEntries() != null) {
39             Map<String, HmDevice> devicesByAddress = new HashMap<>();
40             for (HmDevice device : devices) {
41                 devicesByAddress.put(device.getAddress(), device);
42             }
43
44             for (TclScriptDataEntry entry : resultList.getEntries()) {
45                 HmDevice device = devicesByAddress.get(getSanitizedAddress(entry.name));
46                 if (device != null) {
47                     device.setName(entry.value);
48                 }
49             }
50         }
51         return null;
52     }
53 }