Preliminary support to RT mode

This commit is contained in:
gac-S_Changer
2023-04-26 10:58:52 +02:00
parent a904eac8eb
commit 423a563488
5 changed files with 3136 additions and 3113 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -9,6 +9,7 @@ import ch.psi.pshell.device.DeviceAdapter;
import ch.psi.pshell.device.DeviceListener;
import ch.psi.pshell.device.GenericDevice;
import ch.psi.pshell.ui.Panel;
import ch.psi.utils.State;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -20,9 +21,24 @@ public class ControllerRT extends Controller{
//basePlate = new BasePlate();
basePlate.addListener(basePlateListener);
setPuckLoading(false);
for (PuckState ps : getPuckStates()){
ps.set(1, 0);
}
}
}
/*
@Override
public void onStateChange(State state, State former) {
if ((state == State.Ready)&&(former==State.Initializing)) {
PuckDetection puckDetection = this.getContext().getDevicePool().getByName("puckDetection", PuckDetection.class);
if (puckDetection!=null){
getMainFrame().removeDevice(puckDetection, true);
}
}
super.onStateChange(state, former);
}
*/
final DeviceListener basePlateListener = new DeviceAdapter() {
@Override
public void onValueChanged(Device device, Object value, Object former) {
@@ -43,5 +59,5 @@ public class ControllerRT extends Controller{
} catch (Exception ex) {
Logger.getLogger(Controller.class.getName()).log(Level.WARNING, null, ex);
}
}
}
}

View File

@@ -31,7 +31,7 @@
<Group type="102" alignment="1" attributes="0">
<Component id="panelTop" max="32767" attributes="0"/>
<EmptySpace min="-2" pref="0" max="-2" attributes="0"/>
<Component id="panelBottom" pref="79" max="32767" attributes="0"/>
<Component id="panelBottom" pref="80" max="32767" attributes="0"/>
<EmptySpace min="-2" pref="1" max="-2" attributes="0"/>
</Group>
</Group>
@@ -278,12 +278,12 @@
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<EmptySpace pref="14" max="32767" attributes="0"/>
<EmptySpace pref="20" max="32767" attributes="0"/>
<Group type="103" groupAlignment="0" max="-2" attributes="0">
<Component id="buttonClearDet" linkSize="4" max="32767" attributes="0"/>
<Component id="buttonPuckDet" max="32767" attributes="0"/>
</Group>
<EmptySpace pref="14" max="32767" attributes="0"/>
<EmptySpace pref="20" max="32767" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
@@ -346,9 +346,9 @@
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<EmptySpace pref="17" max="32767" attributes="0"/>
<EmptySpace pref="18" max="32767" attributes="0"/>
<Component id="hexiposiPanel" min="-2" max="-2" attributes="0"/>
<EmptySpace pref="12" max="32767" attributes="0"/>
<EmptySpace pref="13" max="32767" attributes="0"/>
<Component id="buttonDetectCover" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
</Group>
@@ -896,7 +896,7 @@
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<EmptySpace min="-2" pref="0" max="-2" attributes="0"/>
<Component id="panelDetail" pref="111" max="32767" attributes="0"/>
<Component id="panelDetail" pref="112" max="32767" attributes="0"/>
<EmptySpace min="0" pref="0" max="-2" attributes="0"/>
<Component id="panelDevices" min="-2" pref="280" max="-2" attributes="0"/>
<EmptySpace min="0" pref="0" max="-2" attributes="0"/>
@@ -1004,16 +1004,16 @@
<Group type="102" alignment="0" attributes="0">
<EmptySpace min="0" pref="0" max="-2" attributes="0"/>
<Component id="devicesPanel" min="-2" max="-2" attributes="0"/>
<EmptySpace pref="22" max="32767" attributes="0"/>
<EmptySpace pref="26" max="32767" attributes="0"/>
<Component id="panelExpert" min="-2" pref="93" max="-2" attributes="0"/>
<EmptySpace pref="22" max="32767" attributes="0"/>
<EmptySpace pref="27" max="32767" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<EmptySpace pref="9" max="32767" attributes="0"/>
<EmptySpace pref="10" max="32767" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="panelExpert" max="32767" attributes="0"/>
<Component id="devicesPanel" pref="237" max="32767" attributes="0"/>

File diff suppressed because it is too large Load Diff

View File

@@ -1,222 +1,227 @@
package ch.psi.mxsc;
import ch.psi.pshell.core.JsonSerializer;
import ch.psi.pshell.device.DeviceBase;
import ch.psi.pshell.device.ReadonlyAsyncRegisterBase;
import ch.psi.pshell.device.ReadonlyRegisterBase;
import ch.psi.utils.Chrono;
import ch.psi.utils.State;
import ch.psi.utils.Threading;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
public class PuckDetection extends DeviceBase {
final String server;
public volatile Chrono chrono;
boolean debug;
public static final int TIMEOUT = 10000;
public PuckDetection(String name, String server) {
super(name);
this.server = server.startsWith("tcp://") ? server : "tcp://" + server;
}
public boolean isDebug() {
return debug;
}
public void setDebug(boolean value) {
debug = value;
}
Thread thread;
Thread watchDog;
class UpdateTimeReadable extends ReadonlyAsyncRegisterBase<Integer>{
UpdateTimeReadable(){
super("Raspberry Update Time");
}
void update(int value){
setCache(value);
}
};
public UpdateTimeReadable updateTime = new UpdateTimeReadable();
@Override
protected void doInitialize() throws IOException, InterruptedException {
doClose();
super.doInitialize();
chrono = new Chrono();
thread = new Thread(new Runnable() {
@Override
public void run() {
subscriberTask();
}
});
thread.setDaemon(true);
thread.start();
watchDog = new Thread(new Runnable() {
@Override
public void run() {
try {
while (!Thread.currentThread().isInterrupted()) {
if (chrono.isTimeout(TIMEOUT)) {
if (!isSimulated()) {
setState(State.Offline);
if (Controller.getInstance() != null) {
Controller.getInstance().clearPuckStates();
}
}
}
Thread.sleep(1000);
}
} catch (InterruptedException ex) {
getLogger().fine("Watch-dog thread interrupted");
} catch (Exception ex) {
getLogger().log(Level.WARNING, null, ex);
}
}
});
watchDog.setDaemon(true);
watchDog.start();
}
void subscriberTask() {
try {
setState(State.Ready);
if (isSimulated()) {
while (!Thread.currentThread().isInterrupted()) {
boolean changed = false;
for (int address = 1; address <= Controller.NUMBER_OF_PUCKS; address++) {
Integer indDetector = ((address <= 6) || (address==30)) ? 1 : 0;
Integer mecDetector = ((address <= 6) || (address==29)) ? 1 : 0;
int index = Controller.getInstance().getPuckIndex(address);
PuckState puck = Controller.getInstance().getPuckState(index);
if (puck.set(mecDetector, indDetector)){
changed = true;
}
}
if (changed){
if (Controller.getInstance() != null) {
Controller.getInstance().updateView();
}
}
chrono = new Chrono();
Thread.sleep(2000);
}
} else {
org.zeromq.ZMQ.Context context = org.zeromq.ZMQ.context(1);
org.zeromq.ZMQ.Socket subscriber = context.socket(org.zeromq.ZMQ.SUB);
subscriber.connect(server);
//subscriber.subscribe("Status".getBytes());
subscriber.subscribe("".getBytes());
try {
if (debug) {
System.out.println("Start listening");
}
while (!Thread.currentThread().isInterrupted()) {
//String type = subscriber.recvStr();
//System.out.println(type);
String contents = subscriber.recvStr();
if (debug) {
System.out.println(contents);
}
processMessage(contents);
if (Controller.getInstance() != null) {
Controller.getInstance().updateView();
}
setState(State.Ready);
if (chrono != null){
updateTime.update(chrono.getEllapsed());
}
chrono = new Chrono();
}
} finally {
if (debug) {
System.out.println("Stop listening");
}
if (Controller.getInstance() != null) {
Controller.getInstance().clearPuckStates();
}
subscriber.close();
context.term();
}
}
} catch (Exception ex) {
getLogger().log(Level.INFO, null, ex);
}
setState(State.Offline);
}
void processMessage(String str) {
try {
//System.out.println(str);
List<List> detection = (List) JsonSerializer.decode(str, List.class);
int address = 1;
for (List<List> bus : detection) {
for (List<Integer> sensor : bus) {
Integer indDetector = sensor.get(0);
Integer mecDetector = sensor.get(1);
if (Controller.getInstance()!=null){
int index = Controller.getInstance().getPuckIndex(address);
PuckState puck = Controller.getInstance().getPuckState(index);
puck.set(mecDetector, indDetector);
}
address++;
}
}
setCache(str);
} catch (Exception ex) {
getLogger().log(Level.INFO, null, ex);
}
}
public PuckState getPuckState(int id) throws Exception {
if (Controller.getInstance() == null) {
return null;
}
return Controller.getInstance().getPuckState(id);
}
public Puck getPuck(String name) throws Exception {
if (Controller.getInstance() == null) {
return null;
}
return Controller.getInstance().getPuck(name);
}
@Override
protected void doClose() {
if (watchDog != null) {
watchDog.interrupt();
watchDog = null;
}
if (thread != null) {
try {
Threading.stop(thread, true, 2000);
} catch (InterruptedException ex) {
//TODO: Filtering InterruptedException. But stop() should not throw InterruptedException;
getLogger().log(Level.WARNING, null, ex);
}
thread = null;
}
}
public static void main(String[] args) throws IOException, InterruptedException {
//PuckDetection pd = new PuckDetection("PD", "129.129.110.99:5556");
//PuckDetection pd = new PuckDetection("PD","raspberrypi:5556");
PuckDetection pd = new PuckDetection("PD", "tell10s-raspberrypi:5556");
pd.setDebug(true);
pd.initialize();
Thread.sleep(100000);
}
}
package ch.psi.mxsc;
import ch.psi.pshell.core.JsonSerializer;
import ch.psi.pshell.device.DeviceBase;
import ch.psi.pshell.device.ReadonlyAsyncRegisterBase;
import ch.psi.pshell.device.ReadonlyRegisterBase;
import ch.psi.utils.Chrono;
import ch.psi.utils.State;
import ch.psi.utils.Threading;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
public class PuckDetection extends DeviceBase {
final String server;
public volatile Chrono chrono;
boolean debug;
public static final int TIMEOUT = 10000;
public PuckDetection(String name, String server) {
super(name);
this.server = server.startsWith("tcp://") ? server : "tcp://" + server;
if(MainPanel.isRt()){
setSimulated();
}
}
public boolean isDebug() {
return debug;
}
public void setDebug(boolean value) {
debug = value;
}
Thread thread;
Thread watchDog;
class UpdateTimeReadable extends ReadonlyAsyncRegisterBase<Integer>{
UpdateTimeReadable(){
super("Raspberry Update Time");
}
void update(int value){
setCache(value);
}
};
public UpdateTimeReadable updateTime = new UpdateTimeReadable();
@Override
protected void doInitialize() throws IOException, InterruptedException {
doClose();
super.doInitialize();
chrono = new Chrono();
thread = new Thread(new Runnable() {
@Override
public void run() {
subscriberTask();
}
});
thread.setDaemon(true);
thread.start();
watchDog = new Thread(new Runnable() {
@Override
public void run() {
try {
while (!Thread.currentThread().isInterrupted()) {
if (chrono.isTimeout(TIMEOUT)) {
if (!isSimulated()) {
setState(State.Offline);
if (Controller.getInstance() != null) {
Controller.getInstance().clearPuckStates();
}
}
}
Thread.sleep(1000);
}
} catch (InterruptedException ex) {
getLogger().fine("Watch-dog thread interrupted");
} catch (Exception ex) {
getLogger().log(Level.WARNING, null, ex);
}
}
});
watchDog.setDaemon(true);
watchDog.start();
}
void subscriberTask() {
try {
setState(State.Ready);
if (isSimulated()) {
while (!Thread.currentThread().isInterrupted()) {
boolean changed = false;
for (int address = 1; address <= Controller.NUMBER_OF_PUCKS; address++) {
Integer indDetector = ((address <= 6) || (address==30)) ? 1 : 0;
Integer mecDetector = ((address <= 6) || (address==29)) ? 1 : 0;
int index = Controller.getInstance().getPuckIndex(address);
PuckState puck = Controller.getInstance().getPuckState(index);
if (puck.set(mecDetector, indDetector)){
changed = true;
}
}
if (changed){
if (Controller.getInstance() != null) {
Controller.getInstance().updateView();
}
}
chrono = new Chrono();
Thread.sleep(2000);
}
} else {
org.zeromq.ZMQ.Context context = org.zeromq.ZMQ.context(1);
org.zeromq.ZMQ.Socket subscriber = context.socket(org.zeromq.ZMQ.SUB);
subscriber.connect(server);
//subscriber.subscribe("Status".getBytes());
subscriber.subscribe("".getBytes());
try {
if (debug) {
System.out.println("Start listening");
}
while (!Thread.currentThread().isInterrupted()) {
//String type = subscriber.recvStr();
//System.out.println(type);
String contents = subscriber.recvStr();
if (debug) {
System.out.println(contents);
}
processMessage(contents);
if (Controller.getInstance() != null) {
Controller.getInstance().updateView();
}
setState(State.Ready);
if (chrono != null){
updateTime.update(chrono.getEllapsed());
}
chrono = new Chrono();
}
} finally {
if (debug) {
System.out.println("Stop listening");
}
if (Controller.getInstance() != null) {
Controller.getInstance().clearPuckStates();
}
subscriber.close();
context.term();
}
}
} catch (Exception ex) {
getLogger().log(Level.INFO, null, ex);
}
if (!isSimulated()) {
setState(State.Offline);
}
}
void processMessage(String str) {
try {
//System.out.println(str);
List<List> detection = (List) JsonSerializer.decode(str, List.class);
int address = 1;
for (List<List> bus : detection) {
for (List<Integer> sensor : bus) {
Integer indDetector = sensor.get(0);
Integer mecDetector = sensor.get(1);
if (Controller.getInstance()!=null){
int index = Controller.getInstance().getPuckIndex(address);
PuckState puck = Controller.getInstance().getPuckState(index);
puck.set(mecDetector, indDetector);
}
address++;
}
}
setCache(str);
} catch (Exception ex) {
getLogger().log(Level.INFO, null, ex);
}
}
public PuckState getPuckState(int id) throws Exception {
if (Controller.getInstance() == null) {
return null;
}
return Controller.getInstance().getPuckState(id);
}
public Puck getPuck(String name) throws Exception {
if (Controller.getInstance() == null) {
return null;
}
return Controller.getInstance().getPuck(name);
}
@Override
protected void doClose() {
if (watchDog != null) {
watchDog.interrupt();
watchDog = null;
}
if (thread != null) {
try {
Threading.stop(thread, true, 2000);
} catch (InterruptedException ex) {
//TODO: Filtering InterruptedException. But stop() should not throw InterruptedException;
getLogger().log(Level.WARNING, null, ex);
}
thread = null;
}
}
public static void main(String[] args) throws IOException, InterruptedException {
//PuckDetection pd = new PuckDetection("PD", "129.129.110.99:5556");
//PuckDetection pd = new PuckDetection("PD","raspberrypi:5556");
PuckDetection pd = new PuckDetection("PD", "tell10s-raspberrypi:5556");
pd.setDebug(true);
pd.initialize();
Thread.sleep(100000);
}
}