]> git.basschouten.com Git - openhab-addons.git/blob
3950378d02772b005af8fcad636b36ea9b9b1366
[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.shelly.internal.config;
14
15 import java.util.Collections;
16 import java.util.Dictionary;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.function.Function;
20 import java.util.stream.Collectors;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24
25 /**
26  * The {@link ShellyBindingConfiguration} class contains fields mapping binding configuration parameters.
27  *
28  * @author Markus Michels - Initial contribution
29  */
30 @NonNullByDefault
31 public class ShellyBindingConfiguration {
32     // Binding Configuration Properties
33     public static final String CONFIG_DEF_HTTP_USER = "defaultUserId";
34     public static final String CONFIG_DEF_HTTP_PWD = "defaultPassword";
35     public static final String CONFIG_LOCAL_IP = "localIP";
36     public static final String CONFIG_AUTOCOIOT = "autoCoIoT";
37
38     public String defaultUserId = "admin"; // default for http basic user id
39     public String defaultPassword = "admin"; // default for http basic auth password
40     public String localIP = ""; // default:use OH network config
41     public int httpPort = -1;
42     public boolean autoCoIoT = true;
43
44     public void updateFromProperties(Map<String, Object> properties) {
45         for (Map.Entry<String, Object> e : properties.entrySet()) {
46             switch (e.getKey()) {
47                 case CONFIG_DEF_HTTP_USER:
48                     defaultUserId = (String) e.getValue();
49                     break;
50                 case CONFIG_DEF_HTTP_PWD:
51                     defaultPassword = (String) e.getValue();
52                     break;
53                 case CONFIG_LOCAL_IP:
54                     localIP = (String) e.getValue();
55                     break;
56                 case CONFIG_AUTOCOIOT:
57                     Object value = e.getValue();
58                     if (value instanceof String) {
59                         // support config through shelly.cfg
60                         autoCoIoT = ((String) value).equalsIgnoreCase("true");
61                     } else {
62                         autoCoIoT = (boolean) value;
63                     }
64                     break;
65             }
66
67         }
68     }
69
70     public void updateFromProperties(@Nullable Dictionary<String, Object> properties) {
71         if (properties == null) { // saw this once
72             return;
73         }
74         List<String> keys = Collections.list(properties.keys());
75         Map<String, Object> dictCopy = keys.stream().collect(Collectors.toMap(Function.identity(), properties::get));
76         updateFromProperties(dictCopy);
77     }
78 }