Rev 1563 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1532 | - | 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.OutputStreamWriter; |
||
9 | import java.io.PrintStream; |
||
10 | import java.io.Writer; |
||
11 | import java.util.ArrayList; |
||
12 | import java.util.Date; |
||
13 | import java.util.LinkedList; |
||
14 | import java.util.List; |
||
15 | |||
1563 | - | 16 | import dongfang.mkt.comm.FrameQueue; |
17 | import dongfang.mkt.comm.MKConnection; |
||
18 | import dongfang.mkt.comm.serial.RXTXSerialPort; |
||
19 | import dongfang.mkt.comm.tcp.MKTCPConnection; |
||
1532 | - | 20 | import dongfang.mkt.frames.AnalogDebugLabelRequestFrame; |
21 | import dongfang.mkt.frames.AnalogDebugLabelResponseFrame; |
||
22 | import dongfang.mkt.frames.DebugRequestFrame; |
||
23 | import dongfang.mkt.frames.DebugResponseFrame; |
||
24 | import dongfang.mkt.frames.Frame; |
||
25 | |||
26 | public class MKDebugLogger { |
||
27 | private static final PrintStream STDERR = System.out; |
||
1545 | - | 28 | // private static final FrameFactory ff = MKVersion.getFrameFactory(null); |
1532 | - | 29 | |
30 | interface DebugLogger { |
||
31 | void setLabel(int index, String label); |
||
32 | void start(String timestamp) throws IOException; |
||
33 | void log(DebugResponseFrame f, long timestamp) throws IOException; |
||
34 | void end() throws IOException; |
||
35 | } |
||
36 | |||
37 | static class ConsoleDebugLogger implements DebugLogger { |
||
38 | String[] labels = new String[32]; |
||
39 | |||
40 | public void setLabel(int channel, String label) { |
||
41 | labels[channel] = label; |
||
42 | } |
||
43 | |||
44 | public void start(String logId) {} |
||
45 | |||
46 | public void log(DebugResponseFrame f, long timestamp) { |
||
1562 | - | 47 | if (f == null) { |
48 | System.out.println("Oops, null response frame."); |
||
49 | return; |
||
50 | } |
||
51 | int l = f.getDigital()==null ? 0 : f.getDigital().length; |
||
52 | for (int i = 0; i < l; i++) { |
||
1532 | - | 53 | System.out.println("Digital " + i + ":\t" + f.getDigital()[i]); |
54 | } |
||
55 | |||
1562 | - | 56 | l = f.getAnalog()==null ? 0 : f.getAnalog().length; |
57 | for (int i = 0; i < l; i++) { |
||
1532 | - | 58 | String label = labels[i] == null ? ("Analog " + i) : labels[i]; |
59 | System.out.println(label + ":\t" + f.getAnalog()[i]); |
||
60 | } |
||
61 | } |
||
62 | |||
63 | public void end() {} |
||
64 | } |
||
65 | |||
66 | static class XMLDebugLogger implements DebugLogger { |
||
67 | String[] labels = new String[32]; |
||
68 | Writer w; |
||
69 | String encoding; |
||
70 | |||
71 | XMLDebugLogger(OutputStream out, String encoding) throws IOException { |
||
72 | w = new OutputStreamWriter(out, encoding); |
||
73 | this.encoding = encoding; |
||
74 | } |
||
75 | |||
76 | public void setLabel(int channel, String label) { |
||
77 | labels[channel] = label.trim(); |
||
78 | } |
||
79 | |||
80 | public void start(String logId) throws IOException { |
||
81 | w.write("<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>\n"); |
||
82 | w.write("<mk-debugdata time=\"" + logId + "\">\n"); |
||
83 | } |
||
84 | |||
85 | public void log(DebugResponseFrame f, long timestamp) |
||
86 | throws IOException { |
||
1564 | - | 87 | if (f==null) return; |
1532 | - | 88 | w.write(" <dataset timestamp=\"" + timestamp + "\">\n"); |
89 | for (int i = 0; i < 32; i++) { |
||
90 | w.write(" <data offset=\"" + i + "\" label=\"" + labels[i] |
||
91 | + "\" value=\"" + f.getAnalog()[i] + "\"/>\n"); |
||
92 | } |
||
93 | w.write(" </dataset>\n"); |
||
94 | } |
||
95 | |||
96 | public void end() throws IOException { |
||
97 | w.write("</mk-debugdata>\n"); |
||
98 | w.flush(); |
||
99 | w.close(); |
||
100 | } |
||
101 | } |
||
102 | |||
103 | static class CompositeDebugLogger implements DebugLogger { |
||
104 | List<DebugLogger> loggers = new ArrayList<DebugLogger>(2); |
||
105 | |||
106 | public void setLabel(int index, String label) { |
||
107 | for (DebugLogger l : loggers) |
||
108 | l.setLabel(index, label); |
||
109 | } |
||
110 | |||
111 | public void log(DebugResponseFrame f, long timestamp) throws IOException { |
||
112 | for (DebugLogger l : loggers) |
||
113 | l.log(f, timestamp); |
||
114 | } |
||
115 | |||
116 | public void start(String logId) throws IOException { |
||
117 | for (DebugLogger l : loggers) |
||
118 | l.start(logId); |
||
119 | } |
||
120 | |||
121 | public void end() throws IOException { |
||
122 | for (DebugLogger l : loggers) |
||
123 | l.end(); |
||
124 | } |
||
125 | |||
126 | |||
127 | void add(DebugLogger l) { |
||
128 | loggers.add(l); |
||
129 | } |
||
130 | } |
||
131 | |||
132 | private static DebugLogger createLogger(OutputStream out, String encoding) |
||
133 | throws IOException { |
||
134 | DebugLogger consoleLogger = new ConsoleDebugLogger(); |
||
135 | XMLDebugLogger xmlLogger = new XMLDebugLogger(out, encoding); |
||
136 | CompositeDebugLogger logger = new CompositeDebugLogger(); |
||
137 | logger.add(consoleLogger); |
||
138 | logger.add(xmlLogger); |
||
139 | return logger; |
||
140 | } |
||
141 | |||
142 | private void prepareLoggers(final FrameQueue q, final DebugLogger logger) |
||
143 | throws IOException { |
||
144 | new Thread() { |
||
145 | public void run() { |
||
146 | } |
||
147 | }.start(); |
||
148 | } |
||
149 | |||
150 | private void debug(final FrameQueue q, final DebugLogger logger, final int interval) throws IOException { |
||
151 | LinkedList<Integer> missing = new LinkedList<Integer>(); |
||
152 | |||
153 | for (int i = 0; i < 32; i++) { |
||
154 | missing.add(i); |
||
155 | } |
||
156 | |||
157 | int tries = 0; |
||
1562 | - | 158 | while (!missing.isEmpty() && tries < 300) { |
1532 | - | 159 | int i = missing.get(0); |
160 | tries++; |
||
1545 | - | 161 | AnalogDebugLabelRequestFrame f2 = //ff.createAnalogDebugLabelRequestFrame(Frame.FC_ADDRESS, i); |
162 | new AnalogDebugLabelRequestFrame(Frame.FC_ADDRESS, i); |
||
1532 | - | 163 | try { |
164 | q.sendRequest(f2); |
||
1562 | - | 165 | AnalogDebugLabelResponseFrame rf = (AnalogDebugLabelResponseFrame) q.getResponseFor(f2, 5000); |
1532 | - | 166 | if (rf != null) { |
167 | logger.setLabel(i, rf.getLabelAsString()); |
||
1562 | - | 168 | missing.remove(0); |
1532 | - | 169 | } else { |
170 | String unknown = "analog " + i; |
||
171 | logger.setLabel(i, unknown); |
||
172 | missing.add(i); |
||
173 | } |
||
174 | } catch (IOException ex) { |
||
175 | } |
||
176 | } |
||
177 | |||
1545 | - | 178 | DebugRequestFrame f = // ff.createDebugRequestFrame(Frame.FC_ADDRESS); |
179 | new DebugRequestFrame(Frame.FC_ADDRESS); |
||
1532 | - | 180 | f.setAutoSendInterval(interval); |
181 | BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); |
||
182 | q.sendRequest(f); |
||
183 | Long firsttimestamp = null; |
||
184 | while (!in.ready()) |
||
185 | try { |
||
186 | // q.output(f); |
||
187 | DebugResponseFrame rf = (DebugResponseFrame) q.getResponseFor(f, 1000); |
||
188 | // System.out.println(rf); |
||
189 | long timestamp = System.currentTimeMillis(); |
||
190 | |||
191 | if (firsttimestamp == null) { |
||
192 | firsttimestamp = timestamp; |
||
193 | timestamp = 0; |
||
194 | } else { |
||
195 | timestamp -= firsttimestamp; |
||
196 | } |
||
197 | logger.log(rf, timestamp); |
||
198 | } catch (IOException ex) { |
||
199 | STDERR.println(ex); |
||
200 | } |
||
201 | } |
||
202 | |||
203 | public static void main(String[] args) throws IOException { |
||
204 | MKDebugLogger test = new MKDebugLogger(); |
||
205 | |||
1564 | - | 206 | MKConnection port = new RXTXSerialPort(); |
207 | port.init(null); |
||
1532 | - | 208 | |
209 | FrameQueue q = new FrameQueue(port); |
||
210 | String encoding = "iso-8859-1"; |
||
211 | |||
212 | OutputStream fout = new FileOutputStream("debug.xml"); |
||
213 | |||
214 | DebugLogger logger = createLogger(fout, encoding); |
||
215 | logger.start(new Date().toGMTString()); |
||
216 | test.debug(q, logger, 10); |
||
217 | logger.end(); |
||
218 | fout.close(); |
||
219 | } |
||
220 | } |