Commit | Line | Data |
---|---|---|
cd28f68c TW |
1 | package kaka.cakelight; |
2 | ||
c9edf58d TW |
3 | import kaka.cakelight.mode.Mode; |
4 | ||
cd28f68c TW |
5 | import java.io.BufferedReader; |
6 | import java.io.IOException; | |
7 | import java.io.InputStreamReader; | |
2b49e4e2 TW |
8 | import java.util.ArrayList; |
9 | import java.util.HashMap; | |
10 | import java.util.List; | |
11 | import java.util.Map; | |
cd28f68c TW |
12 | |
13 | public class Console extends Thread { | |
e4738966 | 14 | private boolean running; |
cd28f68c TW |
15 | private CakeLight cakelight; |
16 | private Configuration config; | |
35990bbd TW |
17 | private Map<String, Command> commands = new HashMap<>(); |
18 | private List<Command> commandList = new ArrayList<>(); | |
cd28f68c | 19 | |
f1a6a6a5 TW |
20 | public static Console start(CakeLight cakelight, Configuration config) { |
21 | Console console = new Console(cakelight, config); | |
22 | console.start(); | |
23 | return console; | |
cd28f68c TW |
24 | } |
25 | ||
26 | private Console(CakeLight cakelight, Configuration config) { | |
27 | this.cakelight = cakelight; | |
28 | this.config = config; | |
e4738966 | 29 | register(Commands.help()); |
2b49e4e2 | 30 | register(Commands.quit()); |
c9edf58d TW |
31 | register(Commands.push()); |
32 | register(Commands.pop()); | |
2b49e4e2 TW |
33 | register(Commands.video()); |
34 | register(Commands.color()); | |
35 | register(Commands.brightness()); | |
36 | register(Commands.gamma()); | |
37 | register(Commands.saturation()); | |
38 | register(Commands.ambientMode()); | |
6a3d0eda | 39 | register(Commands.noiseMode()); |
e147b561 | 40 | register(Commands.fireMode()); |
40a06a9b | 41 | register(Commands.sunriseMode()); |
be3f2496 | 42 | } |
fa013e4b TW |
43 | |
44 | public CakeLight getCakelight() { | |
45 | return cakelight; | |
46 | } | |
47 | ||
48 | public Configuration getConfig() { | |
49 | return config; | |
50 | } | |
35990bbd | 51 | |
e4738966 TW |
52 | List<Command> getCommands() { |
53 | return commandList; | |
54 | } | |
be3f2496 | 55 | |
e4738966 TW |
56 | void quit() { |
57 | cakelight.turnOff(); | |
58 | running = false; | |
be3f2496 TW |
59 | } |
60 | ||
35990bbd TW |
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); | |
cd28f68c TW |
70 | } |
71 | ||
72 | @Override | |
73 | public void run() { | |
e4738966 | 74 | running = true; |
9f2bc172 | 75 | try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) { |
e4738966 | 76 | while (running) { |
9f2bc172 | 77 | System.out.print("> "); |
f1a6a6a5 | 78 | String input = reader.readLine(); |
fe55a044 | 79 | handleInput(input); |
f1a6a6a5 | 80 | } |
9f2bc172 TW |
81 | } catch (IOException e) { |
82 | System.out.println("Error reading from command line"); | |
83 | } | |
cd28f68c | 84 | } |
eca6fd31 | 85 | |
fe55a044 TW |
86 | void handleInput(String input) { |
87 | Object obj = internalHandleInput(input); | |
c9edf58d TW |
88 | if (obj instanceof Mode) { |
89 | cakelight.setMode((Mode) obj); | |
90 | } | |
91 | } | |
92 | ||
fe55a044 | 93 | Object internalHandleInput(String input) { |
f1a6a6a5 TW |
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) { | |
c9edf58d | 102 | return cmd.activate(this, args); |
f1a6a6a5 TW |
103 | } else { |
104 | out("no command named '" + name + "'"); | |
105 | } | |
c9edf58d | 106 | return null; |
f1a6a6a5 TW |
107 | } |
108 | ||
2b49e4e2 TW |
109 | void out(String text) { |
110 | System.out.println("(" + text + ")"); | |
111 | } | |
baaaa10b | 112 | |
2b49e4e2 TW |
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 | } | |
baaaa10b | 135 | } |
2b49e4e2 TW |
136 | System.out.println("Failed to parse color '" + s + "'. Using black instead."); |
137 | return Color.BLACK; | |
138 | } | |
35990bbd | 139 | |
2b49e4e2 TW |
140 | public interface Command { |
141 | String[] getNames(); | |
c9edf58d | 142 | Object activate(Console console, String[] args); |
eca6fd31 | 143 | } |
cd28f68c | 144 | } |