/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package remotelrcontrol;
import java.awt.Point;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Properties;
import java.util.Scanner;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author victorn
*/
public class RCProperties {
public static Properties config;
RCProperties(){
config = new Properties();
loadConfig();
}
private void loadConfig(){
try {
File fConf = new File(".rconf");
if(fConf.exists()){
FileInputStream confInput = new FileInputStream(fConf);
if(confInput != null){
config.load(confInput);
confInput.close();
}
}else{
RemoteLRControlApp.writeToErr("Configuration file not found!");
RemoteLRControlApp.writeToLog("Default preferences loaded.");
fConf.createNewFile();
defaultConfig();
saveConfig();
return;
}
RemoteLRControlApp.mover.setCompressionCoeff(Double.parseDouble(config.getProperty("coordinatesCompressionParameter".intern())));
RemoteLRControlApp.mover.setDiscretisation(Integer.parseInt(config.getProperty("discretisationValue".intern())));
} catch (FileNotFoundException ex) {
Logger.getLogger(RCProperties.class.getName()).log(Level.SEVERE, null, ex);
}catch (IOException ex) {
RemoteLRControlApp.writeToLog("Unable to create configure file.");
Logger.getLogger(RCProperties.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void saveConfig(){
try {
File fConf = new File(".rconf");
if (!fConf.exists()) {
fConf.createNewFile();
}
FileOutputStream confOut = new FileOutputStream(fConf);
config.store(confOut, "RLRC Configuration file");
confOut.flush();
confOut.close();
} catch (IOException ex) {
Logger.getLogger(RCProperties.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void loadTrajectories(List<File> fnames){
Point first = null, second = null;
Scanner scan;
try {
for(int id = 0; id < MoveController.robotsCount; id++){
scan = new Scanner(fnames.get(id));
if(scan.hasNext()){
first = new Point(scan.nextInt(), scan.nextInt());
MoveController.trajectoryPoints.elementAt(id).add(first);
}
else{
RemoteLRControlApp.writeToErr("ERROR: Trajectory file '" + fnames.get(id) + "' is empty.");
continue;
}
MoveController.trajectoryPoints.add(new Vector<Point>());
while(scan.hasNext()){
second = first;
first = new Point(scan.nextInt(), scan.nextInt());
while((first.distance(second) < MoveController.discretisation) && scan.hasNext())
first.setLocation(scan.nextInt(), scan.nextInt());
MoveController.trajectoryPoints.elementAt(id).add(first);
}
scan.close();
}
} catch (FileNotFoundException ex) {
Logger.getLogger(RemoteLRControlView.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex){
Logger.getLogger(RemoteLRControlView.class.getName()).log(Level.SEVERE, null, ex);
}
scan = null;
first = second = null;
System.gc();
}
public String getLEJOSBinPath(){
try {
Process reqProc = Runtime.getRuntime().exec("echo $NXJ_HOME");
BufferedReader readOutput = new BufferedReader(new InputStreamReader(reqProc.getInputStream()));
String path = readOutput.readLine();
if(path == null || path.isEmpty()){
RemoteLRControlApp.writeToErr("Unable to locate LEJOS! Configure it first!");
return "Not found";
} else {
return path + "/bin";
}
} catch (IOException ex) {
Logger.getLogger(RCProperties.class.getName()).log(Level.SEVERE, null, ex);
return "Not found";
}
}
private static void defaultConfig(){
config.put("btModulePath".intern(), "");
config.put("btModuleBinaryName".intern(), "");
config.put("lejosBinPath".intern(),"");
config.put("useExternalCoordinatesSource".intern(), "");
config.put("useVideoCamDetection".intern(), "false");
config.put("videoDetectionPath".intern(), "");
config.put("videoDetectionBinary".intern(), "");
config.put("videoCamID".intern(), "");
config.put("videoDetectionServerPort".intern(), "");
config.put("imgSizeX".intern(), "640");
config.put("imgSizeY".intern(), "480");
config.put("discretisationValue".intern(), "18");
config.put("coordinatesCompressionParameter".intern(), "3.4");
}
}