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