View Javadoc
1   package fr.ifremer.tutti.ichtyometer.tool;
2   
3   /*
4    * #%L
5    * Tutti :: Ichtyometer API
6    * %%
7    * Copyright (C) 2012 - 2014 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
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   * Created on 2/4/14.
40   *
41   * @author Tony Chemit - chemit@codelutin.com
42   * @since 3.1.2
43   */
44  public class SendCommandTool {
45  
46      /** Logger. */
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 }