Commit 9de55d50 authored by Ivan Dages's avatar Ivan Dages
Browse files

link with nomad command system

parent b5a41379
/**
*
*/
package fr.ill.ics.n3d.link;
import java.util.HashMap;
import java.util.Set;
import java.util.logging.Logger;
import org.omg.PortableInterceptor.ServerIdHelper;
import fr.ill.ics.bridge.Controller;
import fr.ill.ics.bridge.ControllerManager;
import fr.ill.ics.bridge.LoginManager;
import fr.ill.ics.bridge.LoginManager.ClientAlreadyLaunchedException;
import fr.ill.ics.bridge.LoginManager.ConnectionFailure;
import fr.ill.ics.bridge.LoginManager.LoginIncorrectException;
import fr.ill.ics.bridge.command.CommandZoneWrapper;
import fr.ill.ics.cameo.Application;
import fr.ill.ics.core.property.Property;
import fr.ill.ics.core.property.PropertyManager;
import fr.ill.ics.nscclient.servant.ControllerServant;
import fr.ill.ics.nscclient.serverconnection.ServerInstance;
import fr.ill.ics.util.ConfigManager;
/**
* @author dages
*/
public class NomadLinker {
public final static int LOGIN_OK = 0;
public final static int LOGIN_NOT_OK = 1;
public final static int LOGIN_REFUSED = 2;
private final static NomadLinker instance = new NomadLinker();
private HashMap<String, ControllerServant> controllers;
/**
* Default constructor.
*/
private NomadLinker() {
controllers = new HashMap<>();
}
public static NomadLinker getInstance() {
return instance;
}
public void init(String[] args) {
Application.This.init(args);
// Initialize the ConfigManager at the very beginning because it is in this class where all .nomad/settings.properties are initialized!
ConfigManager.initInstance();
ServerInstance.getInstance().init();
Logger.getLogger("nomad-3d").info("Nomad server endpoint : " + ConfigManager.getInstance().getNomadServerEndpoint());
int status = login("", "", true);
Logger.getLogger("nomad-3d").info("Nomad server login status : " + status);
PropertyManager.getInstance().init();
Set<String> controllersNames = ControllerManager.getInstance().getControllersOfType("axis", true);
// System.out.println("Controllers : \n " + controllers);
for (String controllerName : controllersNames) {
ControllerServant controller = ControllerManager.getInstance().getController(controllerName);
controllers.put(controllerName, controller);
// Property prop = PropertyManager.getInstance().getProperty(controller, "actual_position");
// System.out.println("server value : " + prop.getServerValue());
}
// System.out.println("Loaded controllers : \n" + controllers);
displayActualPositions();
}
public Set<String> getAxisControllersNames() {
return controllers.keySet();
}
public String getServerValue(String controllerName, String propertyName) {
ControllerServant controller = controllers.get(controllerName);
Property prop = PropertyManager.getInstance().getProperty(controller, propertyName);
return prop.getServerValue();
}
public String getValue(String controllerName, String propertyName) {
ControllerServant controller = controllers.get(controllerName);
Property prop = PropertyManager.getInstance().getProperty(controller, propertyName);
return prop.getValue();
}
// TODO TEST
private void displayActualPositions() {
System.out.println("Controllers positions : ");
for (String controllerName : getAxisControllersNames()) {
double pos = Double.parseDouble(getServerValue(controllerName, "actual_position"));
System.out.println("\t" + controllerName + " : " + pos);
}
}
public void terminate() {
// Close connection with Nomad Server
LoginManager.getInstance().logout(CommandZoneWrapper.SERVER_ID);
Application.This.terminate();
}
private int login(String login, String password, boolean standalone) {
int loginState = LOGIN_NOT_OK;
// Check login and password
try {
LoginManager.getInstance().login(login, password, standalone, CommandZoneWrapper.SERVER_ID);
loginState = LOGIN_OK;
} catch (LoginIncorrectException e) {
loginState = LOGIN_NOT_OK;
} catch (ClientAlreadyLaunchedException e) {
loginState = LOGIN_REFUSED;
} catch (ConnectionFailure e) {
System.exit(1);
}
return loginState;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment