Migrate to PShell 2.0

This commit is contained in:
gac-S_Changer
2025-08-08 10:50:06 +02:00
parent 5f19b8ac42
commit 249e9e3855
16 changed files with 294 additions and 285 deletions
+61 -56
View File
@@ -5,22 +5,23 @@ package ch.psi.mxsc;
import ch.psi.mxsc.Puck.Detection;
import ch.psi.mxsc.Puck.PuckType;
import ch.psi.pshell.core.CommandInfo;
import ch.psi.pshell.core.Context;
import ch.psi.pshell.core.DevicePool;
import ch.psi.pshell.core.DevicePoolListener;
import ch.psi.pshell.core.JsonSerializer;
import ch.psi.pshell.sequencer.CommandInfo;
import ch.psi.pshell.framework.Context;
import ch.psi.pshell.devices.DevicePool;
import ch.psi.pshell.devices.DevicePoolListener;
import ch.psi.pshell.utils.EncoderJson;
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.ui.App;
import ch.psi.pshell.ui.Panel;
import ch.psi.utils.Arr;
import ch.psi.utils.Audio;
import ch.psi.utils.Chrono;
import ch.psi.utils.State;
import ch.psi.utils.swing.SwingUtils;
import ch.psi.pshell.app.App;
import ch.psi.pshell.framework.Panel;
import ch.psi.pshell.framework.Setup;
import ch.psi.pshell.sequencer.Interpreter;
import ch.psi.pshell.utils.Arr;
import ch.psi.pshell.utils.Audio;
import ch.psi.pshell.utils.Chrono;
import ch.psi.pshell.utils.State;
import ch.psi.pshell.swing.SwingUtils;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
@@ -76,7 +77,7 @@ public class Controller {
static void createInstance(Panel mainFrame) {
Controller.mainFrame = (MainPanel) mainFrame;
System.setProperty(GenericDevice.PROPERTY_CONFIG_PATH, Context.getInstance().getSetup().getDevicesPath());
//### System.setProperty(GenericDevice.PROPERTY_CONFIG_PATH, Setup.getDevicesPath());
if (isRt()){
instance = new ControllerRT(mainFrame);
} else {
@@ -89,7 +90,7 @@ public class Controller {
Dialog
}
final PuckMountMode puckMountMode = App.hasArgument("direct") ? PuckMountMode.Direct : PuckMountMode.Dialog;
final PuckMountMode puckMountMode = App.hasAdditionalArgument("direct") ? PuckMountMode.Direct : PuckMountMode.Dialog;
enum PuckSensorAccess {
RaspberryPi,
@@ -152,7 +153,7 @@ public class Controller {
}
final DeviceListener basePlateListener = new DeviceAdapter() {
final DeviceListener basePlateListener = new DeviceListener() {
@Override
public void onValueChanged(Device device, Object value, Object former) {
if (value != null) {
@@ -207,7 +208,7 @@ public class Controller {
basePlate.addListener(basePlateListener);
if (puckSensorAccess == PuckSensorAccess.Esera) {
getMainFrame().getContext().getDevicePool().addListener(new DevicePoolListener() {
Context.getDevicePool().addListener(new DevicePoolListener() {
@Override
public void onDeviceAdded(GenericDevice dev) {
if (dev.getName().equals(PUCK_ESERA_DEVICE)) {
@@ -250,7 +251,7 @@ public class Controller {
}
public Sample getMountedSample() throws Exception{
String mountedSample = (String) Context.getInstance().evalLineBackground("get_setting('mounted_sample_position')");
String mountedSample = (String) Context.getInterpreter().evalLineBackground("get_setting('mounted_sample_position')");
checkMountedSampleChange(currentMountedSample, mountedSample);
currentMountedSample = mountedSample;
Sample sample = basePlate.getSampleByName(currentMountedSample);
@@ -258,9 +259,9 @@ public class Controller {
}
public void resetMountedSample() throws Exception{
String mountedSample = (String) Context.getInstance().evalLineBackground("get_setting('mounted_sample_position')");
String mountedSample = (String) Context.getInterpreter().evalLineBackground("get_setting('mounted_sample_position')");
checkMountedSampleChange(currentMountedSample, null);
Context.getInstance().evalLineBackground("set_setting('mounted_sample_position', None)");
Context.getInterpreter().evalLineBackground("set_setting('mounted_sample_position', None)");
}
void checkMountedSampleChange(String former, String current){
@@ -307,7 +308,7 @@ public class Controller {
}
final DeviceListener barcodeReaderListener = new DeviceAdapter() {
final DeviceListener barcodeReaderListener = new DeviceListener() {
@Override
public void onValueChanged(Device device, Object value, Object former) {
if (value != null) { //Keep last value
@@ -321,7 +322,7 @@ public class Controller {
}
};
final DeviceListener barcodeReaderPuckListener = new DeviceAdapter() {
final DeviceListener barcodeReaderPuckListener = new DeviceListener() {
@Override
public void onValueChanged(Device device, Object value, Object former) {
if (value != null) { //Keep last value
@@ -331,7 +332,7 @@ public class Controller {
}
};
final DeviceListener puckDetectionListener = new DeviceAdapter() {
final DeviceListener puckDetectionListener = new DeviceListener() {
@Override
public void onValueChanged(Device device, Object value, Object former) {
if (value != null) { //Keep last value
@@ -431,35 +432,43 @@ public class Controller {
public Boolean isLedRoomTemp() {
try {
return getMainFrame().eval("is_led_room_temp()", true).equals(true);
} catch (Exception ex) {
return null;
if (getMainFrame().getState().isInitialized()){
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;
if (getMainFrame().getState().isInitialized()){
return getMainFrame().eval("is_room_temp()", true).equals(true);
}
} catch (Exception ex) {
}
return null;
}
public Boolean isServiceMode() {
try {
return getMainFrame().eval("is_service_mode()", true).equals(true);
} catch (Exception ex) {
return null;
if (getMainFrame().getState().isInitialized()){
return getMainFrame().eval("is_service_mode()", true).equals(true);
}
} catch (Exception ex) {
}
return null;
}
public Boolean isDoorClosed() {
try {
return getMainFrame().eval("is_door_closed()", true).equals(true);
} catch (Exception ex) {
return null;
if (getMainFrame().getState().isInitialized()){
return getMainFrame().eval("is_door_closed()", true).equals(true);
}
} catch (Exception ex) {
}
return null;
}
public void setServiceMode(boolean value){
@@ -528,11 +537,11 @@ public class Controller {
}
}
public void imageDetectPucks() throws Context.ContextStateException {
public void imageDetectPucks() throws Interpreter.InterpreterStateException {
imageDetectPucks(null, null, null);
}
public void imageDetectPucks(JComponent plot, JComponent renderer, JComponent text) throws Context.ContextStateException {
public void imageDetectPucks(JComponent plot, JComponent renderer, JComponent text) throws Interpreter.InterpreterStateException {
Map args = new HashMap();
args.put("PLOT", plot);
args.put("RENDERER", renderer);
@@ -549,7 +558,7 @@ public class Controller {
updateView();
}
public void clearImageDetection() throws Context.ContextStateException, ScriptException, IOException, InterruptedException {
public void clearImageDetection() throws Interpreter.InterpreterStateException, ScriptException, IOException, InterruptedException {
Map<String, List<String>> map = (Map<String, List<String>>) getMainFrame().eval("clear_detection(None)");
setImageDetection(map);
}
@@ -624,11 +633,7 @@ public class Controller {
}
DevicePool getDevicePool() {
return getMainFrame().getContext().getDevicePool();
}
Context getContext() {
return getMainFrame().getContext();
return Context.getDevicePool();
}
State getState() {
@@ -659,13 +664,13 @@ public class Controller {
//synchronized(samplesTableLock){
SwingUtilities.invokeLater(() -> {
try {
if (Context.getInstance().getState().isInitialized()) {
String json = (String) Context.getInstance().evalLineBackground("get_samples_info()");
if (Context.getInterpreter().getState().isInitialized()) {
String json = (String) Context.getInterpreter().evalLineBackground("get_samples_info()");
if (!json.equals(samplesTableJson)) {
samplesTableJson = json;
//SamplesInfo sampleInfo = (SamplesInfo) JsonSerializer.decode(json, SampleInfo.class);
SampleInfo[] samples = (SampleInfo[]) JsonSerializer.decode(json, SampleInfo[].class);
//SamplesInfo sampleInfo = (SamplesInfo) EncoderJson.decode(json, SampleInfo.class);
SampleInfo[] samples = (SampleInfo[]) EncoderJson.decode(json, SampleInfo[].class);
Object[][] sampleData = new Object[samples.length][];
for (int i = 0; i < samples.length; i++) {
@@ -755,7 +760,7 @@ public class Controller {
public void setLaserPos(String pos){
try {
getMainFrame().evalAsync("set_laser_pos(" + ((pos==null) ? "" : ("'" + pos + "'") ) + ")" ,true);
} catch (Context.ContextStateException ex) {
} catch (Interpreter.InterpreterStateException ex) {
Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
}
}
@@ -772,7 +777,7 @@ public class Controller {
void playSound(String name) {
try {
Audio.playFile(new File(Context.getInstance().getSetup().expandPath("{home}/sounds/" + name + ".wav")), false);
Audio.playFile(new File(Setup.expandPath("{home}/sounds/" + name + ".wav")), false);
} catch (Exception ex) {
Logger.getLogger(Controller.class.getName()).log(Level.FINE, null, ex);
}
@@ -848,7 +853,7 @@ public class Controller {
System.out.println("Setting datamatrix '" + datamatrix + "' to puck: " + puckName);
try {
Context.getInstance().evalLineBackground("set_puck_datamatrix('" + puckName + "','" + datamatrix + "')");
Context.getInterpreter().evalLineBackground("set_puck_datamatrix('" + puckName + "','" + datamatrix + "')");
if (puck != null) {
puck.setId(datamatrix);
} else {
@@ -871,7 +876,7 @@ public class Controller {
Map getPuckDatamatrix() {
try {
return (Map) Context.getInstance().evalLineBackground("get_puck_datamatrix()");
return (Map) Context.getInterpreter().evalLineBackground("get_puck_datamatrix()");
} catch (Exception ex) {
return null;
}
@@ -879,7 +884,7 @@ public class Controller {
Map getPuckDatamatrixInfo() {
try {
return (Map) Context.getInstance().evalLineBackground("get_puck_datamatrix_info()");
return (Map) Context.getInterpreter().evalLineBackground("get_puck_datamatrix_info()");
} catch (Exception ex) {
return null;
}
@@ -887,7 +892,7 @@ public class Controller {
String getPuckFromDatamatrix(String dm){
try {
return (String) Context.getInstance().evalLineBackground("get_puck_from_datamatrix('" + dm + "')");
return (String) Context.getInterpreter().evalLineBackground("get_puck_from_datamatrix('" + dm + "')");
} catch (Exception ex) {
return null;
}
@@ -984,7 +989,7 @@ public class Controller {
try {
List puckInfo = (List) getMainFrame().eval("get_puck_info()", true);
Context.getInstance().sendEvent("DewarContentUpdate", puckInfo);
Context.getInterpreter().sendEvent("DewarContentUpdate", puckInfo);
} catch (Exception ex) {
Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
}
@@ -1065,7 +1070,7 @@ public class Controller {
void logStateChange(State state){
String command = "";
if (state==State.Busy){
CommandInfo info = Context.getInstance().getCommandManager().getCurrentCommand(true);
CommandInfo info = Context.getInterpreter().getCommandManager().getCurrentCommand(true);
if (info != null){
command = (info.script != null) ? info.script : info.command;
}
@@ -1076,7 +1081,7 @@ public class Controller {
public void logEvent(String... event){
try{
long now = System.currentTimeMillis();
Path path = Paths.get(Context.getInstance().getSetup().getOutputPath(), "events", Chrono.getTimeStr(now,"YYYYMMdd") + ".txt");
Path path = Paths.get(Setup.getOutputPath(), "events", Chrono.getTimeStr(now,"YYYYMMdd") + ".txt");
path.toFile().getParentFile().mkdirs();
String[] data = new String[]{