Files
MXSC/src/main/java/ch/psi/mxsc/Controller.java
gac-S_Changer 8800ea11be
2018-07-06 09:55:46 +02:00

237 lines
7.3 KiB
Java

/*
* Copyright (c) 2014 Paul Scherrer Institute. All rights reserved.
*/
package ch.psi.mxsc;
import ch.psi.pshell.core.Context;
import ch.psi.pshell.core.DevicePool;
import ch.psi.pshell.core.DevicePoolListener;
import ch.psi.pshell.device.Device;
import ch.psi.pshell.device.DeviceAdapter;
import ch.psi.pshell.device.GenericDevice;
import ch.psi.pshell.device.ProcessVariableBase;
import ch.psi.pshell.device.ReadbackDevice;
import ch.psi.pshell.ui.Panel;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.script.ScriptException;
import javax.swing.JComponent;
/**
*
*/
public class Controller {
static Controller instance;
final BasePlate basePlate;
final /*Panel*/ MainPanel mainFrame;
Device hexiposi;
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 = (MainPanel)mainFrame;
instance = this;
}
//public Panel getMainFrame() {
public MainPanel getMainFrame() {
return mainFrame;
}
public void updateView() {
getMainFrame().repaint();
}
void onInitialize(int runCount) {
GenericDevice former = 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));
}
}
hexiposi = (Device) getMainFrame().getDevice("hexiposi");
if (hexiposi!=null){
hexiposi.addListener(new DeviceAdapter() {
@Override
public void onValueChanged(Device device, Object value, Object former) {
updateView();
}
});
} else {
Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, "No hexiposi detected.");
}
}
final PuckState[] puckState;
public PuckState[] getPuckStates() {
return puckState;
}
public Puck getPuck(String name){
return basePlate.getPuckByName(name);
}
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 int getPuckIndex(int address) throws Exception {
if ((address <= 0) || (address > NUMBER_OF_PUCKS)) {
throw new Exception("invalid puck address: " + address);
}
for (int i = 0; i < Puck.ADDRESSES.length; i++) {
if (Puck.ADDRESSES[i] == address) {
return i + 1;
}
}
return -1;
}
public void clearPuckStates() {
for (PuckState puck : getPuckStates()) {
puck.clear();
}
updateView();
}
public String getHexiposiPosition() {
return (String) ((ReadbackDevice) hexiposi).getReadback().take();
}
public Boolean isLedRoomTemp() {
try {
return getMainFrame().eval("is_led_room_temp()", true).equals(true);
} catch (Exception ex) {
return null;
}
}
public Boolean isRoomTemp() {
try {
return getMainFrame().eval("is_room_temp()", true).equals(true);
} catch (Exception ex) {
return null;
}
}
public void imageDetectPucks() throws Context.ContextStateException {
imageDetectPucks(null, null, null);
}
public void imageDetectPucks(JComponent plot, JComponent renderer, JComponent text) throws Context.ContextStateException {
Map args = new HashMap();
args.put("PLOT", plot);
args.put("RENDERER", renderer);
args.put("TEXT", text);
getMainFrame().runAsync("imgproc/LedDetectionProc", args).handle((ret, ex) -> {
if (ex == null) {
Map<String, List<String>> map = (Map<String, List<String>>) ret;
setImageDetection(map);
} else {
getMainFrame().showException((Exception)ex);
}
return ret;
});
updateView();
}
public void clearImageDetection() throws Context.ContextStateException, ScriptException, IOException, InterruptedException {
Map<String, List<String>> map = (Map<String, List<String>>) getMainFrame().eval("clear_detection(None)");
setImageDetection(map);
updateView();
}
void setImageDetection(Map<String, List<String>> map){
for (Puck.ImageDetection id : Puck.ImageDetection.values()) {
for (String name : map.get(id.toString())) {
Puck p = basePlate.getPuckByName(name);
if (p != null) {
p.setImageDetection(id);
}
}
}
}
void onPuckPressed(Puck puck){
//getMainFrame().textDetSensors.setText(String.valueOf(puck.getDetection()));
//getMainFrame().textDetImage.setText(String.valueOf(puck.getImageDetection()));
}
void onSamplePressed(Sample sample){
onPuckPressed(sample.getPuck());
}
GenericDevice getDevice(String name){
return getMainFrame().getDevice(name);
}
DevicePool getDevicePool(){
return getMainFrame().getContext().getDevicePool();
}
Context getContext(){
return getMainFrame().getContext();
}
}