Subversion Repositories Projects

Rev

Rev 1545 | Rev 1563 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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