Subversion Repositories Projects

Rev

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