Subversion Repositories Projects

Rev

Rev 1563 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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