Rev 1563 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1562 | - | 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.List; |
||
14 | |||
1563 | - | 15 | import dongfang.mkt.comm.FrameQueue; |
16 | import dongfang.mkt.comm.MKConnection; |
||
17 | import dongfang.mkt.comm.serial.RXTXSerialPort; |
||
1562 | - | 18 | import dongfang.mkt.frames.OSDDataRequestFrame; |
19 | import dongfang.mkt.frames.OSDDataResponseFrame; |
||
20 | |||
21 | public class OSDLogger { |
||
22 | private static final PrintStream STDERR = System.out; |
||
23 | // private static final FrameFactory ff = MKVersion.getFrameFactory(null); |
||
24 | |||
25 | interface Logger { |
||
26 | void start(String timestamp) throws IOException; |
||
27 | void log(OSDDataResponseFrame f, long timestamp) throws IOException; |
||
28 | void end() throws IOException; |
||
29 | } |
||
30 | |||
31 | static class ConsoleOSDLogger implements Logger { |
||
32 | public void start(String logId) {} |
||
33 | |||
34 | public void log(OSDDataResponseFrame f, long timestamp) { |
||
35 | if (f == null) { |
||
36 | System.out.println("Oops, null response frame."); |
||
37 | return; |
||
38 | } |
||
39 | System.out.println(f); |
||
40 | } |
||
41 | |||
42 | public void end() {} |
||
43 | } |
||
44 | |||
45 | static class XMLOSDLogger implements Logger { |
||
46 | Writer w; |
||
47 | String encoding; |
||
48 | |||
49 | XMLOSDLogger(OutputStream out, String encoding) throws IOException { |
||
50 | w = new OutputStreamWriter(out, encoding); |
||
51 | this.encoding = encoding; |
||
52 | } |
||
53 | |||
54 | public void start(String logId) throws IOException { |
||
55 | w.write("<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>\n"); |
||
56 | w.write("<mk-osddata time=\"" + logId + "\">\n"); |
||
57 | } |
||
58 | |||
59 | public void log(OSDDataResponseFrame f, long timestamp) |
||
60 | throws IOException { |
||
61 | if (f==null) |
||
62 | return; |
||
63 | w.write(" <dataset timestamp=\"" + timestamp + "\" version=\"" + f.getVersion() + "\">\n"); |
||
64 | w.write(" <currentPosition " + f.getCurrentPosition().toXML() + "/>\n"); |
||
65 | w.write(" <targetPosition " + f.getTargetPosition().toXML() + " " + f.getCurrentToTarget().toXML() + "/>\n"); |
||
66 | w.write(" <homePosition " + f.getHomePosition().toXML() + " " + f.getCurrentToHome().toXML() + "/>\n"); |
||
67 | w.write(" <heading compass=\"" + f.getCompassHeading() + "\" flight=\"" + f.getDirectionOfFlight() + "\" groundSpeed=\"" + ((double)f.getGroundSpeed())/100 + "\"/>\n"); |
||
68 | |||
69 | w.write(" <waypoints index=\"" + f.getWaypointIndex() + "\" count=\"" + f.getWaypointCount() + "\"/>\n"); |
||
70 | w.write(" <height barometric=\"" + f.getHeightByPressure() + "\" barometricVerticalVelocity=\"" + f.getVerticalVelocityByPressure() + "\" gpsVerticalVelocity=\"" + f.getVerticalVelocityByGPS() + "\"/>\n"); |
||
71 | |||
72 | w.write(" <status rcQuality=\"" + f.getRcQuality() + "\" fcflags=\"" + f.getFcFlags() + "\" fcflags2=\"" + f.getFcFlags2() + "\" ncflags=\"" + f.getNcFlags() + "\" errorCode=\"" + f.getErrorCode() + "\" numberOfSatellites=\"" + f.getNumberOfSatellites() + "\"/>\n"); |
||
73 | w.write(" <flight time=\"" + f.getFlightTime() + "\" batteryVoltage=\"" + f.getBatteryVoltage() + "\" throttle=\"" + f.getThrottle() + "\" current=\"" + f.getCurrent() + "\" capacityUsed=\"" + f.getCapacityUsed() + "\"/>\n"); |
||
74 | |||
75 | w.write(" <attitude pitch=\"" + f.getPitchAngle() + "\" roll=\"" + f.getRollAngle() + "\"/>\n"); |
||
76 | |||
77 | /* |
||
78 | private int operatingRadius; |
||
79 | private int targetLoiterTime; |
||
80 | private int setpointForAltitude; // 16 bit signed. |
||
81 | */ |
||
82 | |||
83 | w.write(" </dataset>\n"); |
||
84 | } |
||
85 | |||
86 | public void end() throws IOException { |
||
87 | w.write("</mk-osddata>\n"); |
||
88 | w.flush(); |
||
89 | w.close(); |
||
90 | } |
||
91 | } |
||
92 | |||
93 | static class CompositeOSDLogger implements Logger { |
||
94 | List<Logger> loggers = new ArrayList<Logger>(2); |
||
95 | |||
96 | public void log(OSDDataResponseFrame f, long timestamp) throws IOException { |
||
97 | for (Logger l : loggers) |
||
98 | l.log(f, timestamp); |
||
99 | } |
||
100 | |||
101 | public void start(String logId) throws IOException { |
||
102 | for (Logger l : loggers) |
||
103 | l.start(logId); |
||
104 | } |
||
105 | |||
106 | public void end() throws IOException { |
||
107 | for (Logger l : loggers) |
||
108 | l.end(); |
||
109 | } |
||
110 | |||
111 | void add(Logger l) { |
||
112 | loggers.add(l); |
||
113 | } |
||
114 | } |
||
115 | |||
116 | private static Logger createLogger(OutputStream out, String encoding) throws IOException { |
||
117 | Logger consoleLogger = new ConsoleOSDLogger(); |
||
118 | XMLOSDLogger xmlLogger = new XMLOSDLogger(out, encoding); |
||
119 | CompositeOSDLogger logger = new CompositeOSDLogger(); |
||
120 | logger.add(consoleLogger); |
||
121 | logger.add(xmlLogger); |
||
122 | return logger; |
||
123 | } |
||
124 | |||
125 | private void prepareLoggers(final FrameQueue q, final Logger logger) |
||
126 | throws IOException { |
||
127 | new Thread() { |
||
128 | public void run() { |
||
129 | } |
||
130 | }.start(); |
||
131 | } |
||
132 | |||
133 | private void osd(final FrameQueue q, final Logger logger, final int interval) throws IOException { |
||
134 | long timer = 0; |
||
135 | OSDDataRequestFrame f = new OSDDataRequestFrame(); |
||
136 | f.setAutoSendInterval(interval); |
||
137 | BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); |
||
138 | Long firsttimestamp = null; |
||
139 | while (!in.ready()) |
||
140 | try { |
||
141 | long now = System.currentTimeMillis(); |
||
142 | if (now - timer > 3000) { |
||
143 | timer = now; |
||
144 | q.sendRequest(f); |
||
145 | } |
||
146 | OSDDataResponseFrame rf = (OSDDataResponseFrame) q.getResponseFor(f, 5000); |
||
147 | long timestamp = System.currentTimeMillis(); |
||
148 | if (firsttimestamp == null) { |
||
149 | firsttimestamp = timestamp; |
||
150 | timestamp = 0; |
||
151 | } else { |
||
152 | timestamp -= firsttimestamp; |
||
153 | } |
||
154 | logger.log(rf, timestamp); |
||
155 | } catch (IOException ex) { |
||
156 | STDERR.println(ex); |
||
157 | } |
||
158 | } |
||
159 | |||
160 | public static void main(String[] args) throws IOException { |
||
161 | OSDLogger test = new OSDLogger(); |
||
162 | |||
1563 | - | 163 | MKConnection _port = new RXTXSerialPort(); |
164 | _port.init(null); |
||
1562 | - | 165 | |
1563 | - | 166 | FrameQueue q = new FrameQueue(_port); |
1562 | - | 167 | String encoding = "iso-8859-1"; |
168 | |||
169 | OutputStream fout = new FileOutputStream("osd.xml"); |
||
170 | |||
171 | Logger logger = createLogger(fout, encoding); |
||
172 | logger.start(new Date().toGMTString()); |
||
173 | test.osd(q, logger, 20); |
||
174 | logger.end(); |
||
175 | fout.close(); |
||
176 | } |
||
177 | } |