|
| 1 | +package de.doubleslash.usb_led_matrix; |
| 2 | + |
| 3 | +import javafx.scene.Scene; |
| 4 | +import org.apache.commons.cli.CommandLine; |
| 5 | +import org.apache.commons.cli.CommandLineParser; |
| 6 | +import org.apache.commons.cli.DefaultParser; |
| 7 | +import org.apache.commons.cli.HelpFormatter; |
| 8 | +import org.apache.commons.cli.Option; |
| 9 | +import org.apache.commons.cli.Options; |
| 10 | +import org.apache.commons.cli.ParseException; |
| 11 | +import org.apache.logging.log4j.Level; |
| 12 | +import org.apache.logging.log4j.LogManager; |
| 13 | +import org.apache.logging.log4j.core.LoggerContext; |
| 14 | +import org.apache.logging.log4j.core.config.Configuration; |
| 15 | +import org.apache.logging.log4j.core.config.LoggerConfig; |
| 16 | +import org.slf4j.Logger; |
| 17 | +import org.slf4j.LoggerFactory; |
| 18 | + |
| 19 | +import java.lang.invoke.MethodHandles; |
| 20 | +import java.util.regex.Matcher; |
| 21 | +import java.util.regex.Pattern; |
| 22 | + |
| 23 | +public class CommandLineOptions { |
| 24 | + private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); |
| 25 | + private static final String COM_PORT_OPTION = "port"; |
| 26 | + private static final String MODE_OPTION = "mode"; |
| 27 | + private static final String BRIGHTNESS_OPTION = "brightness"; |
| 28 | + private static final String HELP_OPTION = "help"; |
| 29 | + private static final String COLOR_MODE_OPTION = "colorMode"; |
| 30 | + private static final String IDLE_TIMEOUT_OPTION = "timeout"; |
| 31 | + private static final String LOG4J_OPTION = "log-level"; |
| 32 | + private static final String NUMBER_OF_LEDS_OPTION = "numberOfLeds"; |
| 33 | + |
| 34 | + private static final String defaultCom = ""; |
| 35 | + private static final String defaultMode = "teams"; |
| 36 | + private static final double defaultBrightness = 50.0; |
| 37 | + private static final String defaultColorMode = "dark"; |
| 38 | + private static final int defaultTimeoutInMinutes = 120; |
| 39 | + private static final int defaultNumberOfLeds = 10; |
| 40 | + |
| 41 | + private static String com = defaultCom; |
| 42 | + private static String mode = defaultMode; |
| 43 | + private static double brightness = defaultBrightness; |
| 44 | + private static String colorMode = defaultColorMode; |
| 45 | + private static String cssFile = "/CSS/light_Mode.css"; |
| 46 | + private static int timeoutInMinutes = defaultTimeoutInMinutes; |
| 47 | + private static int numberOfLeds = defaultNumberOfLeds; |
| 48 | + |
| 49 | + public static double getBrightness() { |
| 50 | + return brightness; |
| 51 | + } |
| 52 | + |
| 53 | + public static String getMode() { |
| 54 | + return mode; |
| 55 | + } |
| 56 | + |
| 57 | + public static String getCom() { |
| 58 | + return com; |
| 59 | + } |
| 60 | + |
| 61 | + public static String getColorMode() { |
| 62 | + return colorMode; |
| 63 | + } |
| 64 | + |
| 65 | + public static void setColorMode(final String inputColorBackgroundMode) { |
| 66 | + colorMode = inputColorBackgroundMode; |
| 67 | + } |
| 68 | + |
| 69 | + public static int getTimeout() { |
| 70 | + return timeoutInMinutes; |
| 71 | + } |
| 72 | + |
| 73 | + public static void setTimeoutInMinutes(final int timeoutInMinutes) { |
| 74 | + CommandLineOptions.timeoutInMinutes = timeoutInMinutes; |
| 75 | + } |
| 76 | + |
| 77 | + public static int getNumberOfLeds() { |
| 78 | + return numberOfLeds; |
| 79 | + } |
| 80 | + |
| 81 | + public static void handleParameters(final String[] args) { |
| 82 | + final CommandLineParser parser = new DefaultParser(); |
| 83 | + final Options options = createOptions(); |
| 84 | + |
| 85 | + try { |
| 86 | + final CommandLine commandLine = parser.parse(options, args); |
| 87 | + |
| 88 | + handlePortOption(commandLine); |
| 89 | + handleModeOption(commandLine); |
| 90 | + handleBrightnessOption(commandLine); |
| 91 | + handleHelpOption(options, commandLine); |
| 92 | + handleIdleTimeoutOption(commandLine); |
| 93 | + handleColorModeOption(commandLine); |
| 94 | + handleLog4jOption(commandLine); |
| 95 | + handleNumberOfLeds(commandLine); |
| 96 | + } catch (final ParseException e) { |
| 97 | + LOG.error("Error while processing parameters.", e); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private static Options createOptions() { |
| 102 | + final Options options = new Options(); |
| 103 | + |
| 104 | + final Option idleTimeoutOption = new Option("t", IDLE_TIMEOUT_OPTION, true, |
| 105 | + "Sets the time when the controller turns off if it's in the same state for too long. Enter the duration in minutes, the default timeout is 120 min = 2 hours."); |
| 106 | + idleTimeoutOption.setArgName("Idle Timeout"); |
| 107 | + options.addOption(idleTimeoutOption); |
| 108 | + |
| 109 | + final Option portOption = new Option("p", COM_PORT_OPTION, true, |
| 110 | + "Sets com port e.g. COM42. The default value is the first port found in the list of serial ports."); |
| 111 | + portOption.setArgName("COM port"); |
| 112 | + options.addOption(portOption); |
| 113 | + |
| 114 | + final Option colorModeOption = new Option("c", COLOR_MODE_OPTION, true, |
| 115 | + "Sets color mode dark or light. The default value is 'dark'."); |
| 116 | + colorModeOption.setArgName("dark | light "); |
| 117 | + options.addOption(colorModeOption); |
| 118 | + |
| 119 | + final Option modeOption = new Option("m", MODE_OPTION, true, |
| 120 | + "Sets the operation mode. The default value is 'teams'."); |
| 121 | + modeOption.setArgName("manual | teams"); |
| 122 | + options.addOption(modeOption); |
| 123 | + |
| 124 | + final Option brightnessOption = new Option("b", BRIGHTNESS_OPTION, true, |
| 125 | + "Sets brightness between 0% and 100%. When you set brightness to 0, the LEDs are turned off. The default value is '50'."); |
| 126 | + brightnessOption.setArgName("Number in [0,100]"); |
| 127 | + options.addOption(brightnessOption); |
| 128 | + |
| 129 | + final Option helpOption = new Option("h", HELP_OPTION, false, "Shows the help page."); |
| 130 | + options.addOption(helpOption); |
| 131 | + |
| 132 | + final Option log4jOption = new Option("l", LOG4J_OPTION, true, |
| 133 | + "Sets the log level (fatal; error; warn; info; debug; trace). 'info' is the default value."); |
| 134 | + log4jOption.setArgName("fatal | error | warn | info | debug | trace"); |
| 135 | + |
| 136 | + options.addOption(log4jOption); |
| 137 | + |
| 138 | + final Option numberOFLedsOption = new Option("nr", NUMBER_OF_LEDS_OPTION, true, |
| 139 | + "Sets the number of Leds. The default value is 10."); |
| 140 | + numberOFLedsOption.setArgName("Number of LEDs"); |
| 141 | + options.addOption(numberOFLedsOption); |
| 142 | + |
| 143 | + return options; |
| 144 | + } |
| 145 | + |
| 146 | + private static void handleHelpOption(final Options options, final CommandLine commandLine) { |
| 147 | + if (commandLine.hasOption(HELP_OPTION)) { |
| 148 | + final HelpFormatter formatter = new HelpFormatter(); |
| 149 | + formatter.printHelp("java -jar busylight_client.jar [OPTIONS]", "Busylight", options, ""); |
| 150 | + System.exit(0); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + private static void handlePortOption(final CommandLine commandLine) { |
| 155 | + if (commandLine.hasOption(COM_PORT_OPTION)) { |
| 156 | + final String port = commandLine.getOptionValue(COM_PORT_OPTION); |
| 157 | + final Pattern pattern = Pattern.compile("^com\\d+$", Pattern.CASE_INSENSITIVE); |
| 158 | + final Matcher matcher = pattern.matcher(port); |
| 159 | + if (matcher.find()) { |
| 160 | + com = port.toUpperCase(); |
| 161 | + } else { |
| 162 | + LOG.warn("'{}' is not a valid value for the COM port.", port); |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + private static void handleModeOption(final CommandLine commandLine) { |
| 168 | + if (commandLine.hasOption(MODE_OPTION)) { |
| 169 | + String modeInput = commandLine.getOptionValue(MODE_OPTION); |
| 170 | + modeInput = modeInput.toLowerCase(); |
| 171 | + if (modeInput.equals("manual") || modeInput.equals("teams")) { |
| 172 | + mode = modeInput; |
| 173 | + LOG.debug("The mode is: '{}'", mode); |
| 174 | + } else { |
| 175 | + LOG.warn("'{}' is not a valid mode.", modeInput); |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + private static void handleBrightnessOption(final CommandLine commandLine) { |
| 181 | + final String invalidInputMessage = "'{}' is not a valid value for brightness."; |
| 182 | + if (commandLine.hasOption(BRIGHTNESS_OPTION)) { |
| 183 | + final String brightnessValue = commandLine.getOptionValue(BRIGHTNESS_OPTION); |
| 184 | + try { |
| 185 | + final double brightnessInput = Double.parseDouble(brightnessValue); |
| 186 | + if (brightnessInput >= 0 && brightnessInput <= 100) { |
| 187 | + brightness = brightnessInput; |
| 188 | + } else { |
| 189 | + LOG.warn(invalidInputMessage, brightnessValue); |
| 190 | + } |
| 191 | + } catch (final NumberFormatException nfe) { |
| 192 | + LOG.warn(invalidInputMessage, brightnessValue); |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + private static void handleIdleTimeoutOption(final CommandLine commandLine) { |
| 198 | + if (commandLine.hasOption(IDLE_TIMEOUT_OPTION)) { |
| 199 | + final String timeoutValueString = commandLine.getOptionValue(IDLE_TIMEOUT_OPTION); |
| 200 | + final int timeoutValue = Integer.parseInt(timeoutValueString); |
| 201 | + timeoutInMinutes = timeoutValue; |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + private static void handleColorModeOption(final CommandLine commandLine) { |
| 206 | + if (commandLine.hasOption(COLOR_MODE_OPTION)) { |
| 207 | + String modeInput = commandLine.getOptionValue(COLOR_MODE_OPTION); |
| 208 | + modeInput = modeInput.toLowerCase(); |
| 209 | + if (modeInput.equals("dark") || modeInput.equals("light")) { |
| 210 | + colorMode = modeInput; |
| 211 | + LOG.debug("The colorMode is: '{}'", mode); |
| 212 | + } else { |
| 213 | + LOG.warn("'{}' is not a valid mode.", modeInput); |
| 214 | + } |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + private static void handleLog4jOption(final CommandLine commandLine) { |
| 219 | + if (commandLine.hasOption(LOG4J_OPTION)) { |
| 220 | + final String log4jOptionValue = commandLine.getOptionValue(LOG4J_OPTION); |
| 221 | + switch (log4jOptionValue.toLowerCase()) { |
| 222 | + case "fatal": |
| 223 | + setLogLevel(Level.FATAL); |
| 224 | + break; |
| 225 | + case "error": |
| 226 | + setLogLevel(Level.ERROR); |
| 227 | + break; |
| 228 | + case "warn": |
| 229 | + setLogLevel(Level.WARN); |
| 230 | + break; |
| 231 | + case "info": |
| 232 | + setLogLevel(Level.INFO); |
| 233 | + break; |
| 234 | + case "debug": |
| 235 | + setLogLevel(Level.DEBUG); |
| 236 | + break; |
| 237 | + case "trace": |
| 238 | + setLogLevel(Level.TRACE); |
| 239 | + break; |
| 240 | + default: |
| 241 | + LOG.warn( |
| 242 | + "The only valid log levels are 'fatal', 'error', 'warn', 'info' and 'debug', 'trace'. You have entered '{}'.", |
| 243 | + log4jOptionValue); |
| 244 | + break; |
| 245 | + } |
| 246 | + } |
| 247 | + } |
| 248 | + |
| 249 | + private static void setLogLevel(final Level logLevel) { |
| 250 | + final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); |
| 251 | + final Configuration config = ctx.getConfiguration(); |
| 252 | + final LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME); |
| 253 | + loggerConfig.setLevel(logLevel); |
| 254 | + ctx.updateLoggers(); |
| 255 | + } |
| 256 | + |
| 257 | + private static void handleNumberOfLeds(final CommandLine commandLine) { |
| 258 | + if (commandLine.hasOption(NUMBER_OF_LEDS_OPTION)) { |
| 259 | + final String numberOfLedsValueString = commandLine.getOptionValue(NUMBER_OF_LEDS_OPTION); |
| 260 | + final int numberOfLedsValue = Integer.parseInt(numberOfLedsValueString); |
| 261 | + numberOfLeds = numberOfLedsValue; |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + public static void Dark(final Scene scene) { |
| 266 | + setColorMode("dark"); |
| 267 | + if (scene.getStylesheets().contains(cssFile)) { |
| 268 | + scene.getStylesheets().remove(cssFile); |
| 269 | + } |
| 270 | + } |
| 271 | + |
| 272 | + public static void Light(final Scene scene) { |
| 273 | + scene.getStylesheets().add(cssFile); |
| 274 | + setColorMode("light"); |
| 275 | + } |
| 276 | + |
| 277 | + public static void reset() { |
| 278 | + com = defaultCom; |
| 279 | + mode = defaultMode; |
| 280 | + brightness = defaultBrightness; |
| 281 | + colorMode = defaultColorMode; |
| 282 | + timeoutInMinutes = defaultTimeoutInMinutes; |
| 283 | + numberOfLeds = defaultNumberOfLeds; |
| 284 | + } |
| 285 | +} |
0 commit comments