From 885b87f87f060496ef69f91d366f979b6485e80a Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sat, 18 Nov 2017 10:16:09 +0300 Subject: [PATCH] Fixed port controller --- .../inr/numass/control/cryotemp/PKT8Device.kt | 18 ++++++------ .../control/magnet/MagnetController.java | 28 +++++++++---------- .../control/magnet/SafeMagnetController.java | 6 ++-- .../inr/numass/control/magnet/SetCurrent.java | 6 ++-- .../java/inr/numass/control/magnet/Talk.java | 8 +++--- .../numass/control/magnet/TestController.java | 7 +++-- .../inr/numass/control/magnet/TestSynch.java | 9 +++--- .../magnet/fx/MagnetControllerApp.java | 4 +-- .../inr/numass/control/msp/MspDevice.kt | 20 ++++++------- .../inr/numass/control/readvac/CM32Device.kt | 16 +++++------ .../control/readvac/MKSBaratronDevice.kt | 4 +-- .../numass/control/readvac/MKSVacDevice.kt | 4 +-- .../control/readvac/MeradatVacDevice.kt | 4 +-- 13 files changed, 68 insertions(+), 66 deletions(-) diff --git a/numass-control/cryotemp/src/main/kotlin/inr/numass/control/cryotemp/PKT8Device.kt b/numass-control/cryotemp/src/main/kotlin/inr/numass/control/cryotemp/PKT8Device.kt index a6c6e70b..eba1bff8 100644 --- a/numass-control/cryotemp/src/main/kotlin/inr/numass/control/cryotemp/PKT8Device.kt +++ b/numass-control/cryotemp/src/main/kotlin/inr/numass/control/cryotemp/PKT8Device.kt @@ -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(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(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(context, } - inner class PKT8Measurement(private val handler: PortHandler) : AbstractMeasurement(), PortHandler.PortController { + inner class PKT8Measurement(private val handler: Port) : AbstractMeasurement(), Port.PortController { override fun getDevice(): Device = this@PKT8Device @@ -321,12 +321,12 @@ class PKT8Device(context: Context, meta: Meta) : PortSensor(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) { diff --git a/numass-control/magnet/src/main/java/inr/numass/control/magnet/MagnetController.java b/numass-control/magnet/src/main/java/inr/numass/control/magnet/MagnetController.java index 92dc5a96..f7aa6162 100644 --- a/numass-control/magnet/src/main/java/inr/numass/control/magnet/MagnetController.java +++ b/numass-control/magnet/src/main/java/inr/numass/control/magnet/MagnetController.java @@ -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); diff --git a/numass-control/magnet/src/main/java/inr/numass/control/magnet/SafeMagnetController.java b/numass-control/magnet/src/main/java/inr/numass/control/magnet/SafeMagnetController.java index 9068c5b0..cc227f39 100644 --- a/numass-control/magnet/src/main/java/inr/numass/control/magnet/SafeMagnetController.java +++ b/numass-control/magnet/src/main/java/inr/numass/control/magnet/SafeMagnetController.java @@ -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 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)); } diff --git a/numass-control/magnet/src/main/java/inr/numass/control/magnet/SetCurrent.java b/numass-control/magnet/src/main/java/inr/numass/control/magnet/SetCurrent.java index dffd0ed6..4de7b4df 100644 --- a/numass-control/magnet/src/main/java/inr/numass/control/magnet/SetCurrent.java +++ b/numass-control/magnet/src/main/java/inr/numass/control/magnet/SetCurrent.java @@ -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); diff --git a/numass-control/magnet/src/main/java/inr/numass/control/magnet/Talk.java b/numass-control/magnet/src/main/java/inr/numass/control/magnet/Talk.java index e6fc9dd5..113c6e7a 100644 --- a/numass-control/magnet/src/main/java/inr/numass/control/magnet/Talk.java +++ b/numass-control/magnet/src/main/java/inr/numass/control/magnet/Talk.java @@ -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) { diff --git a/numass-control/magnet/src/main/java/inr/numass/control/magnet/TestController.java b/numass-control/magnet/src/main/java/inr/numass/control/magnet/TestController.java index 47c03648..22376809 100644 --- a/numass-control/magnet/src/main/java/inr/numass/control/magnet/TestController.java +++ b/numass-control/magnet/src/main/java/inr/numass/control/magnet/TestController.java @@ -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); diff --git a/numass-control/magnet/src/main/java/inr/numass/control/magnet/TestSynch.java b/numass-control/magnet/src/main/java/inr/numass/control/magnet/TestSynch.java index 7d2b37f9..63061e7e 100644 --- a/numass-control/magnet/src/main/java/inr/numass/control/magnet/TestSynch.java +++ b/numass-control/magnet/src/main/java/inr/numass/control/magnet/TestSynch.java @@ -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); diff --git a/numass-control/magnet/src/main/java/inr/numass/control/magnet/fx/MagnetControllerApp.java b/numass-control/magnet/src/main/java/inr/numass/control/magnet/fx/MagnetControllerApp.java index 227141e6..a8b0c6a6 100644 --- a/numass-control/magnet/src/main/java/inr/numass/control/magnet/fx/MagnetControllerApp.java +++ b/numass-control/magnet/src/main/java/inr/numass/control/magnet/fx/MagnetControllerApp.java @@ -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; diff --git a/numass-control/msp/src/main/kotlin/inr/numass/control/msp/MspDevice.kt b/numass-control/msp/src/main/kotlin/inr/numass/control/msp/MspDevice.kt index 71d55053..b2c53eb3 100644 --- a/numass-control/msp/src/main/kotlin/inr/numass/control/msp/MspDevice.kt +++ b/numass-control/msp/src/main/kotlin/inr/numass/control/msp/MspDevice.kt @@ -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(context, meta), PortHandler.PortController { +class MspDevice(context: Context, meta: Meta) : Sensor(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? = null val isConnected: Boolean @@ -91,7 +91,7 @@ class MspDevice(context: Context, meta: Meta) : Sensor(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(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(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(context, meta), P notifyError(errorMessage, error) } - private fun getHandler(): TcpPortHandler { + private fun getHandler(): TcpPort { return handler ?: throw RuntimeException("Device not initialized") } diff --git a/numass-control/vac/src/main/kotlin/inr/numass/control/readvac/CM32Device.kt b/numass-control/vac/src/main/kotlin/inr/numass/control/readvac/CM32Device.kt index 6be92b58..3f8fa266 100644 --- a/numass-control/vac/src/main/kotlin/inr/numass/control/readvac/CM32Device.kt +++ b/numass-control/vac/src/main/kotlin/inr/numass/control/readvac/CM32Device.kt @@ -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(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 { diff --git a/numass-control/vac/src/main/kotlin/inr/numass/control/readvac/MKSBaratronDevice.kt b/numass-control/vac/src/main/kotlin/inr/numass/control/readvac/MKSBaratronDevice.kt index 1da68bc5..08e24a3a 100644 --- a/numass-control/vac/src/main/kotlin/inr/numass/control/readvac/MKSBaratronDevice.kt +++ b/numass-control/vac/src/main/kotlin/inr/numass/control/readvac/MKSBaratronDevice.kt @@ -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(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 diff --git a/numass-control/vac/src/main/kotlin/inr/numass/control/readvac/MKSVacDevice.kt b/numass-control/vac/src/main/kotlin/inr/numass/control/readvac/MKSVacDevice.kt index 0da8d2a4..9468fec2 100644 --- a/numass-control/vac/src/main/kotlin/inr/numass/control/readvac/MKSVacDevice.kt +++ b/numass-control/vac/src/main/kotlin/inr/numass/control/readvac/MKSVacDevice.kt @@ -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(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 diff --git a/numass-control/vac/src/main/kotlin/inr/numass/control/readvac/MeradatVacDevice.kt b/numass-control/vac/src/main/kotlin/inr/numass/control/readvac/MeradatVacDevice.kt index 99dfab5d..2c5dc107 100644 --- a/numass-control/vac/src/main/kotlin/inr/numass/control/readvac/MeradatVacDevice.kt +++ b/numass-control/vac/src/main/kotlin/inr/numass/control/readvac/MeradatVacDevice.kt @@ -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(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