Add command control via named pipe
[kaka/cakelight.git] / src / kaka / cakelight / Console.java
index 3b66959..f32ba98 100644 (file)
 package kaka.cakelight;
 
-import kaka.cakelight.mode.AmbientMode;
-import kaka.cakelight.mode.SingleColorMode;
-import kaka.cakelight.mode.TwoColorNoiseMode;
-import kaka.cakelight.mode.VideoMode;
-
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
 public class Console extends Thread {
     private CakeLight cakelight;
     private Configuration config;
-    private BufferedReader reader;
+    private Map<String, Command> commands = new HashMap<>();
+    private List<Command> commandList = new ArrayList<>();
 
-    public static void start(CakeLight cakelight, Configuration config) {
-        new Console(cakelight, config).start();
+    public static Console start(CakeLight cakelight, Configuration config) {
+       Console console = new Console(cakelight, config);
+       console.start();
+       return console;
     }
 
     private Console(CakeLight cakelight, Configuration config) {
         this.cakelight = cakelight;
        this.config = config;
-       reader = new BufferedReader(new InputStreamReader(System.in));
+       register(new HelpCommand());
+       register(Commands.quit());
+       register(Commands.video());
+       register(Commands.color());
+       register(Commands.brightness());
+       register(Commands.gamma());
+       register(Commands.saturation());
+       register(Commands.ambientMode());
+       register(Commands.twoColorNoiseMode());
+    }
+
+    public CakeLight getCakelight() {
+       return cakelight;
+    }
+
+    public Configuration getConfig() {
+       return config;
+    }
+
+    private class HelpCommand implements Command {
+       @Override
+       public String[] getNames() {
+           return new String[] {"?", "h", "help"};
+       }
+
+       @Override
+       public void activate(Console console, String[] args) {
+           for (Command c : commandList) {
+               System.out.println(String.join("|", c.getNames()));
+           }
+       }
+    }
+
+    private void register(Command cmd) {
+       for (String name : cmd.getNames()){
+           if (commands.containsKey(name)) {
+               System.out.println("Command name '" + name + "' is already registered by '" + commands.get(name).getClass() + "'!");
+               System.exit(1);
+           }
+           commands.put(name, cmd);
+       }
+       commandList.add(cmd);
     }
 
     @Override
     public void run() {
         while (true) {
-            System.out.print("> ");
-            try {
-                String input = reader.readLine();
-                if (input.matches("[0-5]")) {
-                   cakelight.setMode(new AmbientMode(new String[] {input}));
-                   System.out.println("setting ambient mode to " + input);
-               } else if (input.matches("v|video")) {
-                    cakelight.setMode(new VideoMode());
-               } else if (input.matches("(b|brightness)\\s+[0-9]+")) {
-                   String[] split = input.split("\\s+");
-                   config.leds.brightness = Integer.parseInt(split[1]);
-                   System.out.println("setting brightness to " + config.leds.brightness);
-               } else if (input.matches("q|quit")) {
-                   cakelight.turnOff();
-                   System.out.println("stopping cakelight");
-                   break;
-               } else if (input.matches("(c|col|color)(\\s+[0-9]+){3}")) {
-                   String[] split = input.split("\\s+");
-                   Color c = Color.rgb(
-                           Integer.parseInt(split[1]),
-                           Integer.parseInt(split[2]),
-                           Integer.parseInt(split[3])
+           System.out.print("> ");
+           try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
+               String input = reader.readLine();
+               handleInput(input);
+           } catch (IOException e) {
+               System.out.println("Error reading from command line");
+               break;
+           }
+        }
+    }
+
+    void handleInput(String input) {
+       String[] splitInput = input.split("\\s+", 2);
+       String name = splitInput[0];
+       String[] args = splitInput.length == 2
+               ? splitInput[1].split("\\s+")
+               : new String[]{};
+
+       Command cmd = commands.get(name);
+       if (cmd != null) {
+           cmd.activate(this, args);
+       } else {
+           out("no command named '" + name + "'");
+       }
+    }
+
+    void out(String text) {
+       System.out.println("(" + text + ")");
+    }
+
+    Color parseColor(String s) {
+       switch (s.toLowerCase()) {
+           case "r": return Color.rgb(255, 0, 0);
+           case "g": return Color.rgb(0, 255, 0);
+           case "b": return Color.rgb(0, 0, 255);
+           default: // assume hexadecimal
+               if (s.startsWith("#")) {
+                   s = s.substring(1);
+               }
+               if (s.length() == 3) {
+                   return Color.rgb(
+                           Integer.parseInt(s.substring(0, 1), 16) * 16 + Integer.parseInt(s.substring(0, 1), 16),
+                           Integer.parseInt(s.substring(1, 2), 16) * 16 + Integer.parseInt(s.substring(1, 2), 16),
+                           Integer.parseInt(s.substring(2, 3), 16) * 16 + Integer.parseInt(s.substring(2, 3), 16)
+                   );
+               } else if (s.length() == 6) {
+                   return Color.rgb(
+                           Integer.parseInt(s.substring(0, 2), 16),
+                           Integer.parseInt(s.substring(2, 4), 16),
+                           Integer.parseInt(s.substring(4, 6), 16)
                    );
-                   cakelight.setMode(new SingleColorMode(c));
-                   System.out.println("setting color to " + c);
-               } else if (input.matches("(g|gamma)\\s+[0-9.]+")) {
-                   String[] split = input.split("\\s+");
-                   config.gamma = Double.parseDouble(split[1]);
-                   Color.calculateGammaCorrection(config.gamma);
-                   System.out.println("setting gamma to " + config.gamma);
-               } else if (input.matches("(s|saturation)\\s+[0-9.]+")) {
-                   String[] split = input.split("\\s+");
-                   config.video.saturation = Double.parseDouble(split[1]);
-                   System.out.println("setting saturation to " + config.video.saturation);
-               } else if (input.matches("(n|noise)")) {
-                   TwoColorNoiseMode.getCommand().activate(cakelight, config, input.split("\\s+"));
                }
-            } catch (IOException e) {
-                System.out.println("Error reading from command line");
-                break;
-            }
-        }
+       }
+       System.out.println("Failed to parse color '" + s + "'. Using black instead.");
+       return Color.BLACK;
     }
 
     public interface Command {
         String[] getNames();
-        void activate(CakeLight cakelight, Configuration config, String[] args);
+        void activate(Console console, String[] args);
     }
 }