Rev 1562 | 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 | |||
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 { |
||
87 | w.write(" <dataset timestamp=\"" + timestamp + "\">\n"); |
||
88 | for (int i = 0; i < 32; i++) { |
||
89 | w.write(" <data offset=\"" + i + "\" label=\"" + labels[i] |
||
90 | + "\" value=\"" + f.getAnalog()[i] + "\"/>\n"); |
||
91 | } |
||
92 | w.write(" </dataset>\n"); |
||
93 | } |
||
94 | |||
95 | public void end() throws IOException { |
||
96 | w.write("</mk-debugdata>\n"); |
||
97 | w.flush(); |
||
98 | w.close(); |
||
99 | } |
||
100 | } |
||
101 | |||
102 | static class CompositeDebugLogger implements DebugLogger { |
||
103 | List<DebugLogger> loggers = new ArrayList<DebugLogger>(2); |
||
104 | |||
105 | public void setLabel(int index, String label) { |
||
106 | for (DebugLogger l : loggers) |
||
107 | l.setLabel(index, label); |
||
108 | } |
||
109 | |||
110 | public void log(DebugResponseFrame f, long timestamp) throws IOException { |
||
111 | for (DebugLogger l : loggers) |
||
112 | l.log(f, timestamp); |
||
113 | } |
||
114 | |||
115 | public void start(String logId) throws IOException { |
||
116 | for (DebugLogger l : loggers) |
||
117 | l.start(logId); |
||
118 | } |
||
119 | |||
120 | public void end() throws IOException { |
||
121 | for (DebugLogger l : loggers) |
||
122 | l.end(); |
||
123 | } |
||
124 | |||
125 | |||
126 | void add(DebugLogger l) { |
||
127 | loggers.add(l); |
||
128 | } |
||
129 | } |
||
130 | |||
131 | private static DebugLogger createLogger(OutputStream out, String encoding) |
||
132 | throws IOException { |
||
133 | DebugLogger consoleLogger = new ConsoleDebugLogger(); |
||
134 | XMLDebugLogger xmlLogger = new XMLDebugLogger(out, encoding); |
||
135 | CompositeDebugLogger logger = new CompositeDebugLogger(); |
||
136 | logger.add(consoleLogger); |
||
137 | logger.add(xmlLogger); |
||
138 | return logger; |
||
139 | } |
||
140 | |||
141 | private void prepareLoggers(final FrameQueue q, final DebugLogger logger) |
||
142 | throws IOException { |
||
143 | new Thread() { |
||
144 | public void run() { |
||
145 | } |
||
146 | }.start(); |
||
147 | } |
||
148 | |||
149 | private void debug(final FrameQueue q, final DebugLogger logger, final int interval) throws IOException { |
||
150 | LinkedList<Integer> missing = new LinkedList<Integer>(); |
||
151 | |||
152 | for (int i = 0; i < 32; i++) { |
||
153 | missing.add(i); |
||
154 | } |
||
155 | |||
156 | int tries = 0; |
||
1562 | - | 157 | while (!missing.isEmpty() && tries < 300) { |
1532 | - | 158 | int i = missing.get(0); |
159 | tries++; |
||
1545 | - | 160 | AnalogDebugLabelRequestFrame f2 = //ff.createAnalogDebugLabelRequestFrame(Frame.FC_ADDRESS, i); |
161 | new AnalogDebugLabelRequestFrame(Frame.FC_ADDRESS, i); |
||
1532 | - | 162 | try { |
163 | q.sendRequest(f2); |
||
1562 | - | 164 | AnalogDebugLabelResponseFrame rf = (AnalogDebugLabelResponseFrame) q.getResponseFor(f2, 5000); |
1532 | - | 165 | if (rf != null) { |
166 | logger.setLabel(i, rf.getLabelAsString()); |
||
1562 | - | 167 | missing.remove(0); |
1532 | - | 168 | } else { |
169 | String unknown = "analog " + i; |
||
170 | logger.setLabel(i, unknown); |
||
171 | missing.add(i); |
||
172 | } |
||
173 | } catch (IOException ex) { |
||
174 | } |
||
175 | } |
||
176 | |||
1545 | - | 177 | DebugRequestFrame f = // ff.createDebugRequestFrame(Frame.FC_ADDRESS); |
178 | new DebugRequestFrame(Frame.FC_ADDRESS); |
||
1532 | - | 179 | f.setAutoSendInterval(interval); |
180 | BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); |
||
181 | q.sendRequest(f); |
||
182 | Long firsttimestamp = null; |
||
183 | while (!in.ready()) |
||
184 | try { |
||
185 | // q.output(f); |
||
186 | DebugResponseFrame rf = (DebugResponseFrame) q.getResponseFor(f, 1000); |
||
187 | // System.out.println(rf); |
||
188 | long timestamp = System.currentTimeMillis(); |
||
189 | |||
190 | if (firsttimestamp == null) { |
||
191 | firsttimestamp = timestamp; |
||
192 | timestamp = 0; |
||
193 | } else { |
||
194 | timestamp -= firsttimestamp; |
||
195 | } |
||
196 | logger.log(rf, timestamp); |
||
197 | } catch (IOException ex) { |
||
198 | STDERR.println(ex); |
||
199 | } |
||
200 | } |
||
201 | |||
202 | public static void main(String[] args) throws IOException { |
||
203 | MKDebugLogger test = new MKDebugLogger(); |
||
204 | |||
1563 | - | 205 | MKConnection port = new MKTCPConnection(); //RXTXSerialPort(); |
206 | port.init("8080"); |
||
1532 | - | 207 | |
208 | FrameQueue q = new FrameQueue(port); |
||
209 | String encoding = "iso-8859-1"; |
||
210 | |||
211 | OutputStream fout = new FileOutputStream("debug.xml"); |
||
212 | |||
213 | DebugLogger logger = createLogger(fout, encoding); |
||
214 | logger.start(new Date().toGMTString()); |
||
215 | test.debug(q, logger, 10); |
||
216 | logger.end(); |
||
217 | fout.close(); |
||
218 | } |
||
219 | } |