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