2 * Copyright (c) 2010-2023 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.paradoxalarm.internal;
15 import java.util.Arrays;
16 import java.util.HashMap;
17 import java.util.List;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * The {@link MainMethodArgParser} Helper class to parse the arguments of main method, which is used for direct
25 * communication testing.
27 * @author Konstantin Polihronov - Initial contribution
29 public class MainMethodArgParser {
31 private final Logger logger = LoggerFactory.getLogger(MainMethodArgParser.class);
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";
36 private static final String KEY_PREFIX = "--";
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,
45 private Map<String, String> argumentsMap;
47 public MainMethodArgParser(String[] args) {
48 this.argumentsMap = parseArguments(args);
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));
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 --");
65 if (args[i + 1] != null && args[i + 1].startsWith(KEY_PREFIX)) {
72 result.put(key, value);
78 private void validateArguments() {
79 for (String argKey : ARGUMENT_KEYS) {
80 String value = argumentsMap.get(argKey);
82 logger.error(USAGE_TEXT);
83 throw new IllegalArgumentException("Argument " + argKey + "is mandatory");
88 public String getPassword() {
89 return argumentsMap.get(PASSWORD_KEY);
92 public String getPcPassword() {
93 return argumentsMap.get(PC_PASSWORD_KEY);
96 public String getIpAddress() {
97 return argumentsMap.get(IP_ADDRESS_KEY);
100 public String getPort() {
101 return argumentsMap.get(PORT_KEY);