]> git.basschouten.com Git - openhab-addons.git/blob
109dcb93a45d4bf708671d7ace69e68e0ae0b090
[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.paradoxalarm.internal;
14
15 import java.util.Arrays;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * The {@link MainMethodArgParser} Helper class to parse the arguments of main method, which is used for direct
25  * communication testing.
26  *
27  * @author Konstantin Polihronov - Initial contribution
28  */
29 public class MainMethodArgParser {
30
31     private final Logger logger = LoggerFactory.getLogger(MainMethodArgParser.class);
32
33     private static final int NUMBER_OF_ARGUMENTS = 8;
34     private static final String USAGE_TEXT = "Usage: application --password <YOUR_PASSWORD_FOR_IP150> --pc_password <your PC_password> --ip_address <address of IP150> --port <port of Paradox>\n (pc password default is 0000, can be obtained by checking section 3012), default port is 10000";
35
36     private static final String KEY_PREFIX = "--";
37
38     private static final String PASSWORD_KEY = "--password";
39     private static final String PC_PASSWORD_KEY = "--pc_password";
40     private static final String IP_ADDRESS_KEY = "--ip_address";
41     private static final String PORT_KEY = "--port";
42     private static final List<String> ARGUMENT_KEYS = Arrays.asList(PASSWORD_KEY, PC_PASSWORD_KEY, IP_ADDRESS_KEY,
43             PORT_KEY);
44
45     private Map<String, String> argumentsMap;
46
47     public MainMethodArgParser(String[] args) {
48         this.argumentsMap = parseArguments(args);
49         validateArguments();
50     }
51
52     private Map<String, String> parseArguments(String[] args) {
53         if (args == null || args.length < NUMBER_OF_ARGUMENTS) {
54             logger.error(USAGE_TEXT);
55             throw new IllegalArgumentException("Arguments= " + Arrays.asList(args));
56         }
57
58         Map<String, String> result = new HashMap<>();
59         for (int i = 0; i < args.length;) {
60             if (!args[i].startsWith(KEY_PREFIX)) {
61                 throw new IllegalArgumentException("Argument " + args[i] + " does not start with --");
62             }
63             String key = args[i];
64             String value;
65             if (args[i + 1] != null && args[i + 1].startsWith(KEY_PREFIX)) {
66                 value = null;
67                 i++;
68             } else {
69                 value = args[i + 1];
70                 i += 2;
71             }
72             result.put(key, value);
73
74         }
75         return result;
76     }
77
78     private void validateArguments() {
79         for (String argKey : ARGUMENT_KEYS) {
80             String value = argumentsMap.get(argKey);
81             if (value == null) {
82                 logger.error(USAGE_TEXT);
83                 throw new IllegalArgumentException("Argument " + argKey + "is mandatory");
84             }
85         }
86     }
87
88     public String getPassword() {
89         return argumentsMap.get(PASSWORD_KEY);
90     }
91
92     public String getPcPassword() {
93         return argumentsMap.get(PC_PASSWORD_KEY);
94     }
95
96     public String getIpAddress() {
97         return argumentsMap.get(IP_ADDRESS_KEY);
98     }
99
100     public String getPort() {
101         return argumentsMap.get(PORT_KEY);
102     }
103 }