Subversion Repositories Projects

Rev

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

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