Files
MXSC/src/main/java/ch/psi/mxsc/PuckState.java
2025-08-21 11:27:10 +02:00

105 lines
3.3 KiB
Java

/*
* Copyright (c) 2014 Paul Scherrer Institute. All rights reserved.
*/
package ch.psi.mxsc;
/**
*
*/
public class PuckState {
public final int id;
public boolean online;
public SwitchState mecSwitch;
public SwitchState indSwitch;
public enum SwitchState{
On,
Off,
Error;
static SwitchState fromInt(int value){
switch (value){
case 1: return SwitchState.On;
case 0: return SwitchState.Off;
default: return SwitchState.Error;
}
}
}
PuckState(int id) {
this.id = id;
}
BasePlate getBasePlate() {
if (Controller.getInstance() == null) {
return null;
}
return Controller.getInstance().basePlate;
}
void clear() {
online = false;
mecSwitch = SwitchState.Off;
indSwitch = SwitchState.Off;
BasePlate basePlate = getBasePlate();
if (basePlate != null) {
basePlate.getPucks()[id - 1].detection = Puck.Detection.Empty;
}
}
boolean set(int mecSwitch, int indSwitch) {
online = true;
//TODO: Handle -1 value: error
SwitchState mec = SwitchState.fromInt(mecSwitch);
SwitchState ind = SwitchState.fromInt(indSwitch);
boolean changed = (mec != this.mecSwitch) || (ind != this.indSwitch);
this.mecSwitch = mec;
this.indSwitch = ind;
BasePlate basePlate = getBasePlate();
if (basePlate != null) {
Puck puck = basePlate.getPucks()[id - 1];
Puck.Detection detection = Puck.Detection.Offline;
switch(puck.getDetectionMode()){
case Mechanical:
if (this.mecSwitch ==SwitchState.On ) {
detection = Puck.Detection.Present;
} else if (this.mecSwitch ==SwitchState.Off ) {
detection = Puck.Detection.Empty;
} else {
detection = Puck.Detection.Error;
}
break;
case Inductive:
if (this.indSwitch ==SwitchState.On ) {
detection = Puck.Detection.Present;
} else if (this.indSwitch ==SwitchState.Off ) {
detection = Puck.Detection.Empty;
} else {
detection = Puck.Detection.Error;
}
break;
case Both:
if ((this.mecSwitch != this.indSwitch) ||(this.mecSwitch == SwitchState.Error) ||(this.mecSwitch == SwitchState.Error)) {
detection = Puck.Detection.Error;
} else if (this.mecSwitch ==SwitchState.On ) {
detection = Puck.Detection.Present;
} else {
detection = Puck.Detection.Empty;
}
break;
}
puck.detection = detection;
}
return changed;
}
@Override
public String toString() {
return "Online = " + online + "\ns1 = " + mecSwitch + "\ns2 = " + indSwitch;
}
}