| 1 | package kaka.cakelight; |
| 2 | |
| 3 | import kaka.cakelight.mode.Mode; |
| 4 | |
| 5 | import java.io.BufferedReader; |
| 6 | import java.io.IOException; |
| 7 | import java.io.InputStreamReader; |
| 8 | import java.util.ArrayList; |
| 9 | import java.util.HashMap; |
| 10 | import java.util.List; |
| 11 | import java.util.Map; |
| 12 | |
| 13 | public class Console extends Thread { |
| 14 | private boolean running; |
| 15 | private CakeLight cakelight; |
| 16 | private Configuration config; |
| 17 | private Map<String, Command> commands = new HashMap<>(); |
| 18 | private List<Command> commandList = new ArrayList<>(); |
| 19 | |
| 20 | public static Console start(CakeLight cakelight, Configuration config) { |
| 21 | Console console = new Console(cakelight, config); |
| 22 | console.start(); |
| 23 | return console; |
| 24 | } |
| 25 | |
| 26 | private Console(CakeLight cakelight, Configuration config) { |
| 27 | this.cakelight = cakelight; |
| 28 | this.config = config; |
| 29 | register(Commands.help()); |
| 30 | register(Commands.quit()); |
| 31 | register(Commands.push()); |
| 32 | register(Commands.pop()); |
| 33 | register(Commands.video()); |
| 34 | register(Commands.color()); |
| 35 | register(Commands.brightness()); |
| 36 | register(Commands.gamma()); |
| 37 | register(Commands.saturation()); |
| 38 | register(Commands.ambientMode()); |
| 39 | register(Commands.noiseMode()); |
| 40 | register(Commands.fireMode()); |
| 41 | register(Commands.sunriseMode()); |
| 42 | } |
| 43 | |
| 44 | public CakeLight getCakelight() { |
| 45 | return cakelight; |
| 46 | } |
| 47 | |
| 48 | public Configuration getConfig() { |
| 49 | return config; |
| 50 | } |
| 51 | |
| 52 | List<Command> getCommands() { |
| 53 | return commandList; |
| 54 | } |
| 55 | |
| 56 | void quit() { |
| 57 | cakelight.turnOff(); |
| 58 | running = false; |
| 59 | } |
| 60 | |
| 61 | private void register(Command cmd) { |
| 62 | for (String name : cmd.getNames()){ |
| 63 | if (commands.containsKey(name)) { |
| 64 | System.out.println("Command name '" + name + "' is already registered by '" + commands.get(name).getClass() + "'!"); |
| 65 | System.exit(1); |
| 66 | } |
| 67 | commands.put(name, cmd); |
| 68 | } |
| 69 | commandList.add(cmd); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void run() { |
| 74 | running = true; |
| 75 | try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) { |
| 76 | while (running) { |
| 77 | System.out.print("> "); |
| 78 | String input = reader.readLine(); |
| 79 | handleInput(input); |
| 80 | } |
| 81 | } catch (IOException e) { |
| 82 | System.out.println("Error reading from command line"); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | void handleInput(String input) { |
| 87 | Object obj = internalHandleInput(input); |
| 88 | if (obj instanceof Mode) { |
| 89 | cakelight.setMode((Mode) obj); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | Object internalHandleInput(String input) { |
| 94 | String[] splitInput = input.split("\\s+", 2); |
| 95 | String name = splitInput[0]; |
| 96 | String[] args = splitInput.length == 2 |
| 97 | ? splitInput[1].split("\\s+") |
| 98 | : new String[]{}; |
| 99 | |
| 100 | Command cmd = commands.get(name); |
| 101 | if (cmd != null) { |
| 102 | return cmd.activate(this, args); |
| 103 | } else { |
| 104 | out("no command named '" + name + "'"); |
| 105 | } |
| 106 | return null; |
| 107 | } |
| 108 | |
| 109 | void out(String text) { |
| 110 | System.out.println("(" + text + ")"); |
| 111 | } |
| 112 | |
| 113 | Color parseColor(String s) { |
| 114 | switch (s.toLowerCase()) { |
| 115 | case "r": return Color.rgb(255, 0, 0); |
| 116 | case "g": return Color.rgb(0, 255, 0); |
| 117 | case "b": return Color.rgb(0, 0, 255); |
| 118 | default: // assume hexadecimal |
| 119 | if (s.startsWith("#")) { |
| 120 | s = s.substring(1); |
| 121 | } |
| 122 | if (s.length() == 3) { |
| 123 | return Color.rgb( |
| 124 | Integer.parseInt(s.substring(0, 1), 16) * 16 + Integer.parseInt(s.substring(0, 1), 16), |
| 125 | Integer.parseInt(s.substring(1, 2), 16) * 16 + Integer.parseInt(s.substring(1, 2), 16), |
| 126 | Integer.parseInt(s.substring(2, 3), 16) * 16 + Integer.parseInt(s.substring(2, 3), 16) |
| 127 | ); |
| 128 | } else if (s.length() == 6) { |
| 129 | return Color.rgb( |
| 130 | Integer.parseInt(s.substring(0, 2), 16), |
| 131 | Integer.parseInt(s.substring(2, 4), 16), |
| 132 | Integer.parseInt(s.substring(4, 6), 16) |
| 133 | ); |
| 134 | } |
| 135 | } |
| 136 | System.out.println("Failed to parse color '" + s + "'. Using black instead."); |
| 137 | return Color.BLACK; |
| 138 | } |
| 139 | |
| 140 | public interface Command { |
| 141 | String[] getNames(); |
| 142 | Object activate(Console console, String[] args); |
| 143 | } |
| 144 | } |