190 lines
5.9 KiB
Java
190 lines
5.9 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.DevicePoolListener;
|
|
import ch.psi.pshell.device.Device;
|
|
import ch.psi.pshell.device.DeviceAdapter;
|
|
import ch.psi.pshell.device.DeviceListener;
|
|
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 ch.psi.utils.State;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import javax.swing.JComponent;
|
|
|
|
/**
|
|
*
|
|
*/
|
|
public class Controller {
|
|
|
|
static Controller instance;
|
|
final BasePlate basePlate;
|
|
final Panel mainFrame;
|
|
Device hexaposi;
|
|
|
|
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;
|
|
}
|
|
|
|
public void updateView() {
|
|
getMainFrame().repaint();
|
|
}
|
|
|
|
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));
|
|
}
|
|
}
|
|
hexaposi = (Device) getMainFrame().getDevice("hexiposi");
|
|
hexaposi.addListener(new DeviceAdapter() {
|
|
@Override
|
|
public void onValueChanged(Device device, Object value, Object former) {
|
|
updateView();
|
|
}
|
|
});
|
|
}
|
|
|
|
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 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) hexaposi).getReadback().take();
|
|
}
|
|
|
|
public Boolean isLedRoomTemp() {
|
|
try {
|
|
return ((ProcessVariableBase) getMainFrame().getDevice("led_ctrl_1")).getConfig().maxValue <= 0.50;
|
|
} 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;
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
getMainFrame().showException((Exception)ex);
|
|
}
|
|
return ret;
|
|
});
|
|
updateView();
|
|
}
|
|
|
|
public void clearImageDetection() throws Context.ContextStateException {
|
|
getMainFrame().evalAsync("clear_detection(None)");
|
|
updateView();
|
|
}
|
|
|
|
}
|