0,0 → 1,212 |
package dongfang.mkt.main; |
|
import java.io.BufferedReader; |
import java.io.FileOutputStream; |
import java.io.IOException; |
import java.io.InputStreamReader; |
import java.io.OutputStream; |
import java.io.OutputStreamWriter; |
import java.io.PrintStream; |
import java.io.Writer; |
import java.util.ArrayList; |
import java.util.Date; |
import java.util.LinkedList; |
import java.util.List; |
|
import dongfang.mkt.frames.AnalogDebugLabelRequestFrame; |
import dongfang.mkt.frames.AnalogDebugLabelResponseFrame; |
import dongfang.mkt.frames.DebugRequestFrame; |
import dongfang.mkt.frames.DebugResponseFrame; |
import dongfang.mkt.frames.Frame; |
import dongfang.mkt.frames.FrameFactory; |
import dongfang.mkt.io.MKCommPort; |
import dongfang.mkt.io.RXTXSerialPort; |
import dongfang.mkt.serial.FrameQueue; |
import dongfang.mkt.version.MKVersion; |
|
public class MKDebugLogger { |
private static final PrintStream STDERR = System.out; |
private static final FrameFactory ff = MKVersion.getFrameFactory(null); |
|
interface DebugLogger { |
void setLabel(int index, String label); |
void start(String timestamp) throws IOException; |
void log(DebugResponseFrame f, long timestamp) throws IOException; |
void end() throws IOException; |
} |
|
static class ConsoleDebugLogger implements DebugLogger { |
String[] labels = new String[32]; |
|
public void setLabel(int channel, String label) { |
labels[channel] = label; |
} |
|
public void start(String logId) {} |
|
public void log(DebugResponseFrame f, long timestamp) { |
for (int i = 0; i < f.getDigital().length; i++) { |
System.out.println("Digital " + i + ":\t" + f.getDigital()[i]); |
} |
|
for (int i = 0; i < f.getAnalog().length; i++) { |
String label = labels[i] == null ? ("Analog " + i) : labels[i]; |
System.out.println(label + ":\t" + f.getAnalog()[i]); |
} |
} |
|
public void end() {} |
} |
|
static class XMLDebugLogger implements DebugLogger { |
String[] labels = new String[32]; |
Writer w; |
String encoding; |
|
XMLDebugLogger(OutputStream out, String encoding) throws IOException { |
w = new OutputStreamWriter(out, encoding); |
this.encoding = encoding; |
} |
|
public void setLabel(int channel, String label) { |
labels[channel] = label.trim(); |
} |
|
public void start(String logId) throws IOException { |
w.write("<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>\n"); |
w.write("<mk-debugdata time=\"" + logId + "\">\n"); |
} |
|
public void log(DebugResponseFrame f, long timestamp) |
throws IOException { |
w.write(" <dataset timestamp=\"" + timestamp + "\">\n"); |
for (int i = 0; i < 32; i++) { |
w.write(" <data offset=\"" + i + "\" label=\"" + labels[i] |
+ "\" value=\"" + f.getAnalog()[i] + "\"/>\n"); |
} |
w.write(" </dataset>\n"); |
} |
|
public void end() throws IOException { |
w.write("</mk-debugdata>\n"); |
w.flush(); |
w.close(); |
} |
} |
|
static class CompositeDebugLogger implements DebugLogger { |
List<DebugLogger> loggers = new ArrayList<DebugLogger>(2); |
|
public void setLabel(int index, String label) { |
for (DebugLogger l : loggers) |
l.setLabel(index, label); |
} |
|
public void log(DebugResponseFrame f, long timestamp) throws IOException { |
for (DebugLogger l : loggers) |
l.log(f, timestamp); |
} |
|
public void start(String logId) throws IOException { |
for (DebugLogger l : loggers) |
l.start(logId); |
} |
|
public void end() throws IOException { |
for (DebugLogger l : loggers) |
l.end(); |
} |
|
|
void add(DebugLogger l) { |
loggers.add(l); |
} |
} |
|
private static DebugLogger createLogger(OutputStream out, String encoding) |
throws IOException { |
DebugLogger consoleLogger = new ConsoleDebugLogger(); |
XMLDebugLogger xmlLogger = new XMLDebugLogger(out, encoding); |
CompositeDebugLogger logger = new CompositeDebugLogger(); |
logger.add(consoleLogger); |
logger.add(xmlLogger); |
return logger; |
} |
|
private void prepareLoggers(final FrameQueue q, final DebugLogger logger) |
throws IOException { |
new Thread() { |
public void run() { |
} |
}.start(); |
} |
|
private void debug(final FrameQueue q, final DebugLogger logger, final int interval) throws IOException { |
LinkedList<Integer> missing = new LinkedList<Integer>(); |
|
for (int i = 0; i < 32; i++) { |
missing.add(i); |
} |
|
int tries = 0; |
while (!missing.isEmpty() && tries < 100) { |
int i = missing.get(0); |
tries++; |
AnalogDebugLabelRequestFrame f2 = ff |
.createAnalogDebugLabelRequestFrame(Frame.FC_ADDRESS, i); |
try { |
q.sendRequest(f2); |
AnalogDebugLabelResponseFrame rf = (AnalogDebugLabelResponseFrame) q.getResponseFor(f2, 1000); |
if (rf != null) { |
logger.setLabel(i, rf.getLabelAsString()); |
} else { |
String unknown = "analog " + i; |
logger.setLabel(i, unknown); |
missing.add(i); |
} |
} catch (IOException ex) { |
} |
} |
|
DebugRequestFrame f = ff.createDebugRequestFrame(Frame.FC_ADDRESS); |
f.setAutoSendInterval(interval); |
BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); |
q.sendRequest(f); |
Long firsttimestamp = null; |
while (!in.ready()) |
try { |
// q.output(f); |
DebugResponseFrame rf = (DebugResponseFrame) q.getResponseFor(f, 1000); |
// System.out.println(rf); |
long timestamp = System.currentTimeMillis(); |
|
if (firsttimestamp == null) { |
firsttimestamp = timestamp; |
timestamp = 0; |
} else { |
timestamp -= firsttimestamp; |
} |
logger.log(rf, timestamp); |
} catch (IOException ex) { |
STDERR.println(ex); |
} |
} |
|
public static void main(String[] args) throws IOException { |
MKDebugLogger test = new MKDebugLogger(); |
|
MKCommPort port = new RXTXSerialPort(); |
port.init(null); |
|
FrameQueue q = new FrameQueue(port); |
String encoding = "iso-8859-1"; |
|
OutputStream fout = new FileOutputStream("debug.xml"); |
|
DebugLogger logger = createLogger(fout, encoding); |
logger.start(new Date().toGMTString()); |
test.debug(q, logger, 10); |
logger.end(); |
fout.close(); |
} |
} |