Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1690 | - | 1 | package dongfang.mkt.main; |
2 | |||
3 | import java.io.BufferedReader; |
||
4 | import java.io.FileOutputStream; |
||
5 | import java.io.IOException; |
||
6 | import java.io.InputStreamReader; |
||
7 | import java.io.OutputStream; |
||
8 | import java.io.PrintStream; |
||
9 | import java.util.Date; |
||
10 | |||
11 | import dongfang.mkt.comm.FrameQueue; |
||
12 | import dongfang.mkt.comm.MKConnection; |
||
13 | import dongfang.mkt.comm.serial.RXTXSerialPort; |
||
14 | import dongfang.mkt.frames.ReadRCChannelsRequestFrame; |
||
15 | import dongfang.mkt.frames.ReadRCChannelsResponseFrame; |
||
16 | |||
17 | public class RCDump { |
||
18 | private static final PrintStream STDERR = System.out; |
||
19 | |||
20 | interface Logger { |
||
21 | void start(String timestamp) throws IOException; |
||
22 | void log(ReadRCChannelsResponseFrame f, long timestamp) throws IOException; |
||
23 | void end() throws IOException; |
||
24 | } |
||
25 | |||
26 | static class ConsoleRCLogger implements Logger { |
||
27 | public void start(String logId) {} |
||
28 | |||
29 | public void log(ReadRCChannelsResponseFrame f, long timestamp) { |
||
30 | if (f == null) { |
||
31 | System.out.println("Oops, null response frame."); |
||
32 | return; |
||
33 | } |
||
34 | System.out.println(f); |
||
35 | } |
||
36 | |||
37 | public void end() {} |
||
38 | } |
||
39 | |||
40 | private static Logger createLogger() throws IOException { |
||
41 | Logger consoleLogger = new ConsoleRCLogger(); |
||
42 | return consoleLogger; |
||
43 | } |
||
44 | |||
45 | private void rcDump(final FrameQueue q, final Logger logger) throws IOException { |
||
46 | long timer = 0; |
||
47 | ReadRCChannelsRequestFrame f = new ReadRCChannelsRequestFrame(); |
||
48 | BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); |
||
49 | Long firsttimestamp = null; |
||
50 | while (!in.ready()) |
||
51 | try { |
||
52 | long now = System.currentTimeMillis(); |
||
53 | if (now - timer > 500) { |
||
54 | timer = now; |
||
55 | q.sendRequest(f); |
||
56 | ReadRCChannelsResponseFrame rf = (ReadRCChannelsResponseFrame) q.getResponseFor(f, 500); |
||
57 | long timestamp = System.currentTimeMillis(); |
||
58 | if (firsttimestamp == null) { |
||
59 | firsttimestamp = timestamp; |
||
60 | timestamp = 0; |
||
61 | } else { |
||
62 | timestamp -= firsttimestamp; |
||
63 | } |
||
64 | logger.log(rf, timestamp); |
||
65 | } |
||
66 | } catch (IOException ex) { |
||
67 | STDERR.println(ex); |
||
68 | } |
||
69 | } |
||
70 | |||
71 | public static void main(String[] args) throws IOException { |
||
72 | RCDump test = new RCDump(); |
||
73 | RXTXSerialPort _port = new RXTXSerialPort(); |
||
74 | _port.init(null); |
||
75 | FrameQueue q = new FrameQueue(_port); |
||
76 | Logger logger = createLogger(); |
||
77 | logger.start(new Date().toGMTString()); |
||
78 | test.rcDump(q, logger); |
||
79 | logger.end(); |
||
80 | } |
||
81 | } |