1 package fr.ifremer.tutti.ichtyometer.tool;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import com.google.common.collect.Lists;
26 import fr.ifremer.tutti.ichtyometer.IchtyometerClient;
27 import fr.ifremer.tutti.ichtyometer.RemoteDeviceChooser;
28 import fr.ifremer.tutti.ichtyometer.interactive.Command;
29 import fr.ifremer.tutti.ichtyometer.interactive.CommandEngine;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 import java.io.Console;
34 import java.io.IOException;
35 import java.io.PrintWriter;
36 import java.util.List;
37
38
39
40
41
42
43
44 public class SendCommandTool {
45
46
47 private static final Log log = LogFactory.getLog(SendCommandTool.class);
48
49 enum ConsoleCommand {
50 a, b, c, d, e, g, q
51 }
52
53 public static void main(String... args) throws IOException {
54
55 final Console console = System.console();
56 final PrintWriter writer = console.writer();
57 writer.println("CommandTool: to send command to the board and wait a result v1.0");
58
59 RemoteDeviceChooser remoteDeviceChooser = remoteDeviceNames -> {
60 List<String> remoteDeviceNameList = Lists.newArrayList(remoteDeviceNames);
61 writer.println("Choose you device");
62 int i = 0;
63 for (String remoteDeviceName : remoteDeviceNameList) {
64 writer.println(i++ + " for device " + remoteDeviceName);
65 }
66 writer.println("q (to quit)");
67 writer.print("Your choice: ");
68 writer.flush();
69
70 String command = console.readLine();
71
72 if ("q".equals(command)) {
73 System.exit(0);
74 }
75 return remoteDeviceNameList.get(Integer.valueOf(command));
76 };
77
78 IchtyometerClient client = new IchtyometerClient(2);
79
80 client.open(remoteDeviceChooser, true);
81
82 CommandEngine reader = new CommandEngine();
83
84 writer.println("client " + client + " is ready.open and wait for a command");
85
86 reader.start(client);
87
88 boolean quit = false;
89 while (!quit) {
90
91 writer.print("new command: [a, b, c, d, e, f, g or q to quit]:");
92 writer.flush();
93 String command = console.readLine();
94 ConsoleCommand checkCommand;
95 try {
96 checkCommand = ConsoleCommand.valueOf(command);
97 } catch (IllegalArgumentException e) {
98 writer.println("Command " + command + " not possible");
99 continue;
100 }
101 switch (checkCommand) {
102
103 case q:
104 reader.stop();
105
106 quit = true;
107 break;
108 default:
109 try {
110 Command sendCommand = reader.sendCommand(checkCommand.name());
111 writer.println(String.format(
112 "------------------------------------------------------------------------------\n%s" +
113 "\n------------------------------------------------------------------------------", sendCommand.getResponse()));
114 } catch (Exception e) {
115 if (log.isErrorEnabled()) {
116 log.error("Something wrong happen", e);
117 }
118 }
119
120
121 }
122 }
123 }
124 }