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