Fixed port controller

This commit is contained in:
Alexander Nozik 2017-11-18 10:16:09 +03:00
parent 3965b04297
commit 885b87f87f
13 changed files with 68 additions and 66 deletions

View File

@ -26,7 +26,7 @@ import hep.dataforge.control.devices.Sensor
import hep.dataforge.control.devices.StateDef
import hep.dataforge.control.measurements.AbstractMeasurement
import hep.dataforge.control.measurements.Measurement
import hep.dataforge.control.ports.PortHandler
import hep.dataforge.control.ports.Port
import hep.dataforge.description.ValueDef
import hep.dataforge.exceptions.ControlException
import hep.dataforge.exceptions.MeasurementException
@ -155,9 +155,9 @@ class PKT8Device(context: Context, meta: Meta) : PortSensor<PKT8Result>(context,
}
@Throws(ControlException::class)
override fun buildHandler(portName: String): PortHandler {
override fun buildHandler(portName: String): Port {
//setup connection
val handler: PortHandler = if ("virtual" == portName) {
val handler: Port = if ("virtual" == portName) {
logger.info("Starting {} using virtual debug port", name)
PKT8VirtualPort("PKT8", meta().getMetaOrEmpty("debug"))
} else {
@ -257,11 +257,11 @@ class PKT8Device(context: Context, meta: Meta) : PortSensor<PKT8Result>(context,
this.measurement
} else {
try {
if (handler.isLocked) {
if (port.isLocked) {
logger.error("Breaking hold on handler because it is locked")
handler.breakHold()
port.breakHold()
}
PKT8Measurement(handler)
PKT8Measurement(port)
} catch (e: ControlException) {
throw MeasurementException(e)
}
@ -284,7 +284,7 @@ class PKT8Device(context: Context, meta: Meta) : PortSensor<PKT8Result>(context,
}
inner class PKT8Measurement(private val handler: PortHandler) : AbstractMeasurement<PKT8Result>(), PortHandler.PortController {
inner class PKT8Measurement(private val handler: Port) : AbstractMeasurement<PKT8Result>(), Port.PortController {
override fun getDevice(): Device = this@PKT8Device
@ -321,12 +321,12 @@ class PKT8Device(context: Context, meta: Meta) : PortSensor<PKT8Result>(context,
} finally {
collector?.clear()
logger.debug("Removing port lock")
handler.unholdBy(this)
handler.releaseBy(this)
}
}
override fun acceptPortPhrase(message: String) {
override fun acceptPhrase(message: String) {
val trimmed = message.trim { it <= ' ' }
if (isStarted) {

View File

@ -15,9 +15,9 @@
*/
package inr.numass.control.magnet;
import hep.dataforge.control.ports.PortHandler;
import hep.dataforge.control.ports.GenericPortController;
import hep.dataforge.control.ports.Port;
import hep.dataforge.control.ports.PortTimeoutException;
import hep.dataforge.control.ports.SyncPortController;
import hep.dataforge.exceptions.PortException;
import hep.dataforge.utils.DateTimeUtils;
import org.slf4j.LoggerFactory;
@ -34,7 +34,7 @@ import java.util.concurrent.TimeUnit;
/**
* @author Polina
*/
public class MagnetController implements PortHandler.PortController {
public class MagnetController implements Port.PortController {
private static final DecimalFormat LAMBDAformat = new DecimalFormat("###.##");
public static double CURRENT_PRECISION = 0.05;
@ -47,8 +47,8 @@ public class MagnetController implements PortHandler.PortController {
public static double MAX_SPEED = 5d; // 5 A per minute
private final String name;
private final PortHandler port;
private final SyncPortController controller = new SyncPortController(this);
private final Port port;
private final GenericPortController controller = new GenericPortController(this);
private final int address;
private final ScheduledExecutorService scheduler = new ScheduledThreadPoolExecutor(1);
@ -71,7 +71,7 @@ public class MagnetController implements PortHandler.PortController {
* @param address number of TDK - Lambda
* @param timeout waiting time for response
*/
public MagnetController(String name, PortHandler port, int address, int timeout) {
public MagnetController(String name, Port port, int address, int timeout) {
this.name = name;
this.port = port;
this.port.setDelimiter("\r");//PENDING меняем состояние внешнего объекта?
@ -79,15 +79,15 @@ public class MagnetController implements PortHandler.PortController {
this.timeout = Duration.ofMillis(timeout);
}
public MagnetController(PortHandler port, int address, int timeout) {
public MagnetController(Port port, int address, int timeout) {
this(null, port, address, timeout);
}
public MagnetController(PortHandler port, int address) {
public MagnetController(Port port, int address) {
this(null, port, address);
}
public MagnetController(String name, PortHandler port, int address) {
public MagnetController(String name, Port port, int address) {
this(name, port, address, 300);
}
@ -110,7 +110,7 @@ public class MagnetController implements PortHandler.PortController {
}
@Override
public void acceptPortPhrase(String message) {
public void acceptPhrase(String message) {
}
@ -224,7 +224,7 @@ public class MagnetController implements PortHandler.PortController {
}
return monitor;
} finally {
port.unholdBy(controller);
port.releaseBy(controller);
}
}
@ -281,7 +281,7 @@ public class MagnetController implements PortHandler.PortController {
portError("Error in update task", ex);
stopUpdateTask();
} finally {
port.unholdBy(controller);
port.releaseBy(controller);
}
};
@ -311,7 +311,7 @@ public class MagnetController implements PortHandler.PortController {
listener.outputModeChanged(getName(), out);
}
} finally {
port.unholdBy(controller);
port.releaseBy(controller);
}
}
@ -407,7 +407,7 @@ public class MagnetController implements PortHandler.PortController {
}
return talk(message);
} finally {
port.unholdBy(controller);
port.releaseBy(controller);
}
} catch (PortException ex) {
portError("Can not send message to the port", ex);

View File

@ -15,7 +15,7 @@
*/
package inr.numass.control.magnet;
import hep.dataforge.control.ports.PortHandler;
import hep.dataforge.control.ports.Port;
import hep.dataforge.exceptions.PortException;
import org.slf4j.LoggerFactory;
@ -32,12 +32,12 @@ public class SafeMagnetController extends MagnetController {
private final Set<SafeMagnetCondition> safeConditions = new HashSet<>();
public SafeMagnetController(String name, PortHandler port, int address, int timeout, SafeMagnetCondition... safeConditions) {
public SafeMagnetController(String name, Port port, int address, int timeout, SafeMagnetCondition... safeConditions) {
super(name, port, address, timeout);
this.safeConditions.addAll(Arrays.asList(safeConditions));
}
public SafeMagnetController(String name, PortHandler port, int address, SafeMagnetCondition... safeConditions) {
public SafeMagnetController(String name, Port port, int address, SafeMagnetCondition... safeConditions) {
super(name, port, address);
this.safeConditions.addAll(Arrays.asList(safeConditions));
}

View File

@ -15,8 +15,8 @@
*/
package inr.numass.control.magnet;
import hep.dataforge.control.ports.ComPortHandler;
import hep.dataforge.control.ports.PortHandler;
import hep.dataforge.control.ports.ComPort;
import hep.dataforge.control.ports.Port;
import jssc.SerialPortException;
/**
@ -36,7 +36,7 @@ public class SetCurrent {
int lambdaaddress = Integer.valueOf(args[1]);
double current = Double.valueOf(args[2]);
PortHandler handler = new ComPortHandler(comName);
Port handler = new ComPort(comName);
MagnetController controller = new MagnetController(handler, lambdaaddress);

View File

@ -16,9 +16,9 @@
package inr.numass.control.magnet;
import ch.qos.logback.classic.Level;
import hep.dataforge.control.ports.GenericPortController;
import hep.dataforge.control.ports.Port;
import hep.dataforge.control.ports.PortFactory;
import hep.dataforge.control.ports.PortHandler;
import hep.dataforge.control.ports.SyncPortController;
import hep.dataforge.exceptions.PortException;
import hep.dataforge.utils.DateTimeUtils;
import org.slf4j.LoggerFactory;
@ -47,7 +47,7 @@ public class Talk {
if (args.length > 0) {
portName = args[0];
}
PortHandler handler;
Port handler;
handler = PortFactory.getPort(portName);
handler.setPhraseCondition((String str) -> str.endsWith("\r"));
@ -60,7 +60,7 @@ public class Talk {
while (!"exit".equals(nextString)) {
try {
Instant start = DateTimeUtils.now();
String answer = SyncPortController.sendAndWait(handler, nextString + "\r", Duration.ofSeconds(1));
String answer = GenericPortController.sendAndWait(handler, nextString + "\r", Duration.ofSeconds(1));
//String answer = controller.request(nextString);
System.out.printf("ANSWER (latency = %s): %s;%n", Duration.between(start, DateTimeUtils.now()), answer.trim());
} catch (PortException ex) {

View File

@ -15,7 +15,8 @@
*/
package inr.numass.control.magnet;
import hep.dataforge.control.ports.PortHandler;
import hep.dataforge.control.ports.Port;
import java.util.Locale;
/**
@ -33,12 +34,12 @@ public class TestController {
// ch.qos.logback.classic.Logger rootLogger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);
// rootLogger.setLevel(Level.INFO);
PortHandler handler;
Port handler;
MagnetController firstController;
MagnetController secondController;
// String comName = "COM12";
// handler = new ComPortHandler(comName);
// handler = new ComPort(comName);
handler = new VirtualLambdaPort("COM12", 1, 2, 3, 4);
firstController = new MagnetController(handler, 1);

View File

@ -16,10 +16,11 @@
package inr.numass.control.magnet;
import ch.qos.logback.classic.Level;
import hep.dataforge.control.ports.PortHandler;
import java.util.Locale;
import hep.dataforge.control.ports.Port;
import org.slf4j.LoggerFactory;
import java.util.Locale;
/**
*
* @author Alexander Nozik
@ -37,12 +38,12 @@ public class TestSynch {
ch.qos.logback.classic.Logger rootLogger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);
rootLogger.setLevel(Level.INFO);
PortHandler handler;
Port handler;
MagnetController firstController;
MagnetController secondController;
// String comName = "COM12";
// handler = new ComPortHandler(comName);
// handler = new ComPort(comName);
handler = new VirtualLambdaPort("COM12", 1, 2, 3, 4);
firstController = new MagnetController(handler, 1);

View File

@ -18,8 +18,8 @@ package inr.numass.control.magnet.fx;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.FileAppender;
import hep.dataforge.control.ports.Port;
import hep.dataforge.control.ports.PortFactory;
import hep.dataforge.control.ports.PortHandler;
import hep.dataforge.exceptions.ControlException;
import inr.numass.control.magnet.MagnetController;
import inr.numass.control.magnet.SafeMagnetController;
@ -41,7 +41,7 @@ import java.util.Locale;
*/
public class MagnetControllerApp extends Application {
PortHandler handler;
Port handler;
SafeMagnetController sourceController;
SafeMagnetController pinchController;
SafeMagnetController conusController;

View File

@ -23,9 +23,9 @@ import hep.dataforge.control.collectors.RegularPointCollector
import hep.dataforge.control.connections.Roles
import hep.dataforge.control.devices.*
import hep.dataforge.control.measurements.AbstractMeasurement
import hep.dataforge.control.ports.PortHandler
import hep.dataforge.control.ports.SyncPortController
import hep.dataforge.control.ports.TcpPortHandler
import hep.dataforge.control.ports.GenericPortController
import hep.dataforge.control.ports.Port
import hep.dataforge.control.ports.TcpPort
import hep.dataforge.description.ValueDef
import hep.dataforge.events.EventBuilder
import hep.dataforge.exceptions.ControlException
@ -61,10 +61,10 @@ import java.util.function.Consumer
StateDef(ValueDef(name = "filamentStatus", info = "Filament status"))
)
@DeviceView(MspDisplay::class)
class MspDevice(context: Context, meta: Meta) : Sensor<Values>(context, meta), PortHandler.PortController {
class MspDevice(context: Context, meta: Meta) : Sensor<Values>(context, meta), Port.PortController {
private var handler: TcpPortHandler? = null
private val controller = SyncPortController(this)
private var handler: TcpPort? = null
private val controller = GenericPortController(this)
private var measurementDelegate: Consumer<MspResponse>? = null
val isConnected: Boolean
@ -91,7 +91,7 @@ class MspDevice(context: Context, meta: Meta) : Sensor<Values>(context, meta), P
val ip = meta().getString("connection.ip", "127.0.0.1")
val port = meta().getInt("connection.port", 10014)!!
logger.info("Connection to MKS mass-spectrometer on {}:{}...", ip, port)
handler = TcpPortHandler(ip, port)
handler = TcpPort(ip, port)
handler!!.setDelimiter("\r\r")
}
@ -194,7 +194,7 @@ class MspDevice(context: Context, meta: Meta) : Sensor<Values>(context, meta), P
updateState(PortSensor.CONNECTED_STATE, true)
return true
} else {
getHandler().unholdBy(controller)
getHandler().releaseBy(controller)
return !sendAndWait("Release").isOK
}
@ -299,7 +299,7 @@ class MspDevice(context: Context, meta: Meta) : Sensor<Values>(context, meta), P
}
override fun acceptPortPhrase(message: String) {
override fun acceptPhrase(message: String) {
dispatchEvent(
EventBuilder
.make("msp")
@ -324,7 +324,7 @@ class MspDevice(context: Context, meta: Meta) : Sensor<Values>(context, meta), P
notifyError(errorMessage, error)
}
private fun getHandler(): TcpPortHandler {
private fun getHandler(): TcpPort {
return handler ?: throw RuntimeException("Device not initialized")
}

View File

@ -10,9 +10,9 @@ import hep.dataforge.control.devices.Device
import hep.dataforge.control.devices.PortSensor
import hep.dataforge.control.measurements.Measurement
import hep.dataforge.control.measurements.SimpleMeasurement
import hep.dataforge.control.ports.ComPortHandler
import hep.dataforge.control.ports.ComPort
import hep.dataforge.control.ports.Port
import hep.dataforge.control.ports.PortFactory
import hep.dataforge.control.ports.PortHandler
import hep.dataforge.exceptions.ControlException
import hep.dataforge.meta.Meta
import inr.numass.control.DeviceView
@ -24,16 +24,16 @@ import inr.numass.control.DeviceView
class CM32Device(context: Context, meta: Meta) : PortSensor<Double>(context, meta) {
@Throws(ControlException::class)
override fun buildHandler(portName: String): PortHandler {
override fun buildHandler(portName: String): Port {
logger.info("Connecting to port {}", portName)
val newHandler: PortHandler
val new: Port
if (portName.startsWith("com")) {
newHandler = ComPortHandler(portName, 2400, 8, 1, 0)
new = ComPort(portName, 2400, 8, 1, 0)
} else {
newHandler = PortFactory.getPort(portName)
new = PortFactory.getPort(portName)
}
newHandler.setDelimiter("T--\r")
return newHandler
new.setDelimiter("T--\r")
return new
}
override fun createMeasurement(): Measurement<Double> {

View File

@ -10,7 +10,7 @@ import hep.dataforge.control.devices.Device
import hep.dataforge.control.devices.PortSensor
import hep.dataforge.control.measurements.Measurement
import hep.dataforge.control.measurements.SimpleMeasurement
import hep.dataforge.control.ports.PortHandler
import hep.dataforge.control.ports.Port
import hep.dataforge.description.ValueDef
import hep.dataforge.exceptions.ControlException
import hep.dataforge.meta.Meta
@ -35,7 +35,7 @@ class MKSBaratronDevice(context: Context, meta: Meta) : PortSensor<Double>(conte
}
@Throws(ControlException::class)
override fun buildHandler(portName: String): PortHandler {
override fun buildHandler(portName: String): Port {
val handler = super.buildHandler(portName)
handler.setDelimiter("\r")
return handler

View File

@ -11,7 +11,7 @@ import hep.dataforge.control.devices.PortSensor
import hep.dataforge.control.devices.StateDef
import hep.dataforge.control.measurements.Measurement
import hep.dataforge.control.measurements.SimpleMeasurement
import hep.dataforge.control.ports.PortHandler
import hep.dataforge.control.ports.Port
import hep.dataforge.description.ValueDef
import hep.dataforge.description.ValueDefs
import hep.dataforge.exceptions.ControlException
@ -77,7 +77,7 @@ class MKSVacDevice(context: Context, meta: Meta) : PortSensor<Double>(context, m
}
@Throws(ControlException::class)
override fun buildHandler(portName: String): PortHandler {
override fun buildHandler(portName: String): Port {
val handler = super.buildHandler(portName)
handler.setDelimiter(";FF")
return handler

View File

@ -10,7 +10,7 @@ import hep.dataforge.control.devices.Device
import hep.dataforge.control.devices.PortSensor
import hep.dataforge.control.measurements.Measurement
import hep.dataforge.control.measurements.SimpleMeasurement
import hep.dataforge.control.ports.PortHandler
import hep.dataforge.control.ports.Port
import hep.dataforge.description.ValueDef
import hep.dataforge.exceptions.ControlException
import hep.dataforge.meta.Meta
@ -27,7 +27,7 @@ import java.util.regex.Pattern
class MeradatVacDevice(context: Context, meta: Meta) : PortSensor<Double>(context, meta) {
@Throws(ControlException::class)
override fun buildHandler(portName: String): PortHandler {
override fun buildHandler(portName: String): Port {
val newHandler = super.buildHandler(portName)
newHandler.setDelimiter("\r\n")
return newHandler