/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.test.simplecarusorobot;
import ca.teamdave.caruso.CarusoException;
import ca.teamdave.caruso.logger.LogFrame;
import ca.teamdave.caruso.logger.Logger;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
/**
*
* @author leigh
*/
public class LiveGraphLogger implements Logger {
private String liveGraphJar;
private String fileName;
private ArrayList<String> parameterOrder;
int frameNum;
public LiveGraphLogger(String fileName, String liveGraphJar) {
this.fileName = fileName;
this.liveGraphJar = liveGraphJar;
frameNum = 0;
}
public void robotInit() throws CarusoException {
try {
File toDelete = new File(this.fileName + ".lgdfs");
if (toDelete.exists()){
toDelete.delete();
}
toDelete = new File(this.fileName + ".dat");
if (toDelete.exists()) {
toDelete.delete();
}
// write the data discription file
PrintWriter metaFile = new PrintWriter(new FileWriter(this.fileName + ".lgdfs"));
metaFile.write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"
+ "<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">\n"
+ "<properties>\n"
+ "<comment>LiveGraph version 1.1.3. DataFileSettings.</comment>\n"
+ "<entry key=\"DataFile\">"+this.fileName+".dat</entry>\n"
+ "<entry key=\"ShowOnlyTailData\">0</entry>\n"
+ "<entry key=\"UpdateFrequency\">-1</entry>\n"
+ "<entry key=\"DoNotCacheData\">1</entry>\n"
+ "</properties>");
metaFile.close();
// erase the current data file
PrintWriter dataFile = new PrintWriter(new FileWriter(fileName + ".dat"));
dataFile.close();
} catch (IOException ex) {
throw new CarusoException("Error, could not initialize the specified files: " + this.fileName + ".dat, .lgdfs");
}
}
public void addFrame(LogFrame frame) throws CarusoException {
try {
PrintWriter dataFile = new PrintWriter(new FileWriter(fileName + ".dat", true), true);
// if I need to initialize the data file and start LiveGraph
if (0 == this.frameNum) {
dataFile.println("##;##");
dataFile.println("@Automatically generated by Caruso Shades Robot Simulator");
dataFile.print("time");
for (String sensorName : frame.getSensorNames()) {
dataFile.print(";" + sensorName);
}
for (String actuatorName : frame.getActuatorNames()) {
dataFile.print(";" + actuatorName);
}
dataFile.println();
try {
Runtime.getRuntime().exec("java -jar " + this.liveGraphJar + " -dfs " + this.fileName + ".lgdfs");
} catch(Exception e) {
System.err.println("Caruso could not start LiveGraph, you can still view generated data in LiveGraph at: " + this.fileName + ".dat");
}
}
// write this frame's data to the file
dataFile.print("" + (frameNum * 0.05)); // TODO replace this number with a centrally defined constant
for (String sensorName : frame.getSensorNames()) {
dataFile.print(";" + frame.getSensorValue(sensorName).toString());
}
for (String actuatorName : frame.getActuatorNames()) {
dataFile.print(";" + frame.getActuatorValue(actuatorName).toString());
}
dataFile.println();
dataFile.close();
} catch (IOException ex) {
throw new CarusoException("Error, could not append file: " + this.fileName + ".dat");
}
frameNum++;
}
}