Created application main panel and separated Controller class
This commit is contained in:
110
src/main/java/ch/psi/mxsc/Controller.java
Normal file
110
src/main/java/ch/psi/mxsc/Controller.java
Normal file
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* Copyright (c) 2014 Paul Scherrer Institute. All rights reserved.
|
||||
*/
|
||||
|
||||
package ch.psi.mxsc;
|
||||
|
||||
import ch.psi.pshell.core.DevicePoolListener;
|
||||
import ch.psi.pshell.device.Device;
|
||||
import ch.psi.pshell.device.GenericDevice;
|
||||
import ch.psi.pshell.ui.Panel;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class Controller {
|
||||
static Controller instance;
|
||||
final BasePlate basePlate;
|
||||
final Panel mainFrame;
|
||||
|
||||
public static Controller getInstance(){
|
||||
return instance;
|
||||
}
|
||||
|
||||
static void createInstance(Panel mainFrame){
|
||||
instance = new Controller(mainFrame);
|
||||
}
|
||||
|
||||
enum PuckSensorAccess{
|
||||
RaspberryPi,
|
||||
Esera;
|
||||
}
|
||||
|
||||
final PuckSensorAccess puckSensorAccess = PuckSensorAccess.RaspberryPi;
|
||||
|
||||
|
||||
static String PUCK_ESERA_DEVICE = "onewire";
|
||||
|
||||
public static final int NUMBER_OF_PUCKS = 30;
|
||||
|
||||
private Controller(Panel mainFrame){
|
||||
basePlate = new BasePlate();
|
||||
puckState = new PuckState[NUMBER_OF_PUCKS];
|
||||
for (int i=0; i<NUMBER_OF_PUCKS; i++ ){
|
||||
puckState[i] = new PuckState(i+1);
|
||||
}
|
||||
this.mainFrame =mainFrame;
|
||||
instance = this;
|
||||
}
|
||||
|
||||
public Panel getMainFrame(){
|
||||
return mainFrame;
|
||||
}
|
||||
|
||||
void onInitialize(int runCount) {
|
||||
GenericDevice former = getMainFrame().getDevice("BasePlate");
|
||||
if (former!=null){
|
||||
getMainFrame().removeDevice(former);
|
||||
}
|
||||
getMainFrame().addDevice(basePlate);
|
||||
clearPuckStates();
|
||||
|
||||
if (puckSensorAccess == PuckSensorAccess.Esera){
|
||||
getMainFrame().getContext().getDevicePool().addListener(new DevicePoolListener() {
|
||||
@Override
|
||||
public void onDeviceAdded(GenericDevice dev) {
|
||||
if (dev.getName().equals(PUCK_ESERA_DEVICE)){
|
||||
detection = new EseraDetection((Device) dev);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void onDeviceRemoved(GenericDevice dev) {
|
||||
if (dev.getName().equals(PUCK_ESERA_DEVICE)){
|
||||
detection.close();
|
||||
detection = null;
|
||||
}
|
||||
}
|
||||
});
|
||||
if (detection != null){
|
||||
detection.close();
|
||||
detection = null;
|
||||
}
|
||||
if ((Device) getMainFrame().getDevice(PUCK_ESERA_DEVICE) != null){
|
||||
detection = new EseraDetection((Device) getMainFrame().getDevice(PUCK_ESERA_DEVICE));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
final PuckState[] puckState;
|
||||
public PuckState[] getPuckStates(){
|
||||
return puckState;
|
||||
}
|
||||
EseraDetection detection;
|
||||
|
||||
//From 1 to PUCKS_NUMBER
|
||||
public PuckState getPuckState(int id) throws Exception{
|
||||
if ((id<=0) || (id>NUMBER_OF_PUCKS)){
|
||||
throw new Exception("invalid puck id: "+ id);
|
||||
}
|
||||
return getPuckStates()[id-1];
|
||||
}
|
||||
|
||||
public void clearPuckStates(){
|
||||
for (PuckState puck:getPuckStates()){
|
||||
puck.clear();
|
||||
}
|
||||
getMainFrame().repaint();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user