Control update

This commit is contained in:
Alexander Nozik 2018-03-11 19:40:40 +03:00
parent 0fe0c1f1e1
commit d9fabb850a
7 changed files with 64 additions and 180 deletions

View File

@ -42,7 +42,7 @@ class BoardController() : Controller(), AutoCloseable {
val numassRun = meta.optMeta("numass").map { ClientUtils.getRunName(it) }.orElse("") val numassRun = meta.optMeta("numass").map { ClientUtils.getRunName(it) }.orElse("")
meta.useMeta("storage") { meta.useMeta("storage") {
pluginManager.load(StorageManager::class.java).configure(it); pluginManager.load(StorageManager::class.java,it)
} }
val rootStorage = pluginManager.load(StorageManager::class.java).defaultStorage val rootStorage = pluginManager.load(StorageManager::class.java).defaultStorage
@ -55,7 +55,7 @@ class BoardController() : Controller(), AutoCloseable {
} }
val connection = StorageConnection(storage) val connection = StorageConnection(storage)
val deviceManager = pluginManager.getOrLoad(DeviceManager::class.java) val deviceManager = pluginManager.load(DeviceManager::class.java)
meta.useMetaList("device") { meta.useMetaList("device") {
it.forEach { it.forEach {

View File

@ -20,9 +20,10 @@ import hep.dataforge.connections.RoleDefs
import hep.dataforge.context.Context import hep.dataforge.context.Context
import hep.dataforge.control.connections.Roles import hep.dataforge.control.connections.Roles
import hep.dataforge.control.devices.PortSensor import hep.dataforge.control.devices.PortSensor
import hep.dataforge.control.devices.Sensor
import hep.dataforge.control.devices.stringState import hep.dataforge.control.devices.stringState
import hep.dataforge.control.ports.GenericPortController
import hep.dataforge.control.ports.Port import hep.dataforge.control.ports.Port
import hep.dataforge.control.ports.PortFactory
import hep.dataforge.description.ValueDef import hep.dataforge.description.ValueDef
import hep.dataforge.exceptions.ControlException import hep.dataforge.exceptions.ControlException
import hep.dataforge.exceptions.StorageException import hep.dataforge.exceptions.StorageException
@ -52,7 +53,7 @@ import java.util.*
@ValueDef(name = "port", def = "virtual", info = "The name of the port for this PKT8") @ValueDef(name = "port", def = "virtual", info = "The name of the port for this PKT8")
@StateDef(ValueDef(name = "storing")) @StateDef(ValueDef(name = "storing"))
@DeviceView(PKT8Display::class) @DeviceView(PKT8Display::class)
class PKT8Device(context: Context, meta: Meta) : PortSensor<PKT8Result>(context, meta) { class PKT8Device(context: Context, meta: Meta) : PortSensor(context, meta) {
/** /**
* The key is the letter (a,b,c,d...) as in measurements * The key is the letter (a,b,c,d...) as in measurements
*/ */
@ -89,7 +90,7 @@ class PKT8Device(context: Context, meta: Meta) : PortSensor<PKT8Result>(context,
val suffix = DateTimeUtils.fileSuffix() val suffix = DateTimeUtils.fileSuffix()
try { try {
return LoaderFactory.buildPointLoader(storage, "cryotemp_" + suffix, "", "timestamp", tableFormat) return LoaderFactory.buildPointLoader(storage, "cryotemp_$suffix", "", "timestamp", tableFormat)
} catch (e: StorageException) { } catch (e: StorageException) {
throw RuntimeException("Failed to builder loader from storage", e) throw RuntimeException("Failed to builder loader from storage", e)
} }
@ -122,7 +123,7 @@ class PKT8Device(context: Context, meta: Meta) : PortSensor<PKT8Result>(context,
if (response.contains("=")) { if (response.contains("=")) {
updateLogicalState(PGA, Integer.parseInt(response.substring(4))) updateLogicalState(PGA, Integer.parseInt(response.substring(4)))
} else { } else {
logger.error("Setting pga failed with message: " + response) logger.error("Setting pga failed with message: $response")
} }
} }
@ -140,25 +141,24 @@ class PKT8Device(context: Context, meta: Meta) : PortSensor<PKT8Result>(context,
super.shutdown() super.shutdown()
} }
override fun connect(meta: Meta): GenericPortController {
val portName = meta.getString("name", "virtual")
override fun buildPort(meta: Meta): Port { val port: Port = if (portName == "virtual") {
//setup connection
val handler: Port = if ("virtual" == portName) {
logger.info("Starting {} using virtual debug port", name) logger.info("Starting {} using virtual debug port", name)
PKT8VirtualPort("PKT8", getMeta().getMetaOrEmpty("debug")) PKT8VirtualPort("PKT8", meta)
} else { } else {
super.buildPort(portName) logger.info("Connecting to port {}", portName)
PortFactory.build(meta)
} }
handler.setDelimiter("\n") return GenericPortController(context, port, "\n")
return handler
} }
private fun setBUF(buf: Int) { private fun setBUF(buf: Int) {
logger.info("Setting avaraging buffer size to " + buf) logger.info("Setting averaging buffer size to $buf")
var response: String var response: String
try { try {
response = sendAndWait("b" + buf).trim { it <= ' ' } response = sendAndWait("b$buf").trim { it <= ' ' }
} catch (ex: Exception) { } catch (ex: Exception) {
response = ex.message ?: "" response = ex.message ?: ""
} }
@ -167,13 +167,13 @@ class PKT8Device(context: Context, meta: Meta) : PortSensor<PKT8Result>(context,
updateLogicalState(ABUF, Integer.parseInt(response.substring(14))) updateLogicalState(ABUF, Integer.parseInt(response.substring(14)))
// getLogger().info("successfully set buffer size to {}", this.abuf); // getLogger().info("successfully set buffer size to {}", this.abuf);
} else { } else {
logger.error("Setting averaging buffer failed with message: " + response) logger.error("Setting averaging buffer failed with message: $response")
} }
} }
@Throws(ControlException::class) @Throws(ControlException::class)
fun changeParameters(sps: Int, abuf: Int) { fun changeParameters(sps: Int, abuf: Int) {
stopCurrentMeasurement() stopMeasurement()
//setting sps //setting sps
setSPS(sps) setSPS(sps)
//setting buffer //setting buffer
@ -223,22 +223,25 @@ class PKT8Device(context: Context, meta: Meta) : PortSensor<PKT8Result>(context,
} }
fun setMeasurement(oldMeta: Meta, newMeta: Meta) { override fun setMeasurement(oldMeta: Meta?, newMeta: Meta) {
if (!oldMeta.isEmpty) { if (oldMeta != null) {
logger.warn("Trying to start measurement which is already started") stopMeasurement()
} }
try { // if (!oldMeta.isEmpty) {
// logger.warn("Trying to start measurement which is already started")
// }
logger.info("Starting measurement") logger.info("Starting measurement")
//add weak stop listener connection?.also {
stopListener = controller.onPhrase("[Ss]topped\\s*") { it.onPhrase("[Ss]topped\\s*", this) {
afterPause() notifyMeasurementState(MeasurementState.STOPPED)
updateLogicalState(Sensor.MEASURING_STATE, false)
} }
//add weak measurement listener //add weak measurement listener
valueListener = controller.onPhrase("[a-f].*") { it.onPhrase("[a-f].*", this) {
val trimmed = it.trim() val trimmed = it.trim()
val designation = trimmed.substring(0, 1) val designation = trimmed.substring(0, 1)
val rawValue = java.lang.Double.parseDouble(trimmed.substring(1)) / 100 val rawValue = java.lang.Double.parseDouble(trimmed.substring(1)) / 100
@ -246,6 +249,7 @@ class PKT8Device(context: Context, meta: Meta) : PortSensor<PKT8Result>(context,
val channel = this@PKT8Device.channels[designation] val channel = this@PKT8Device.channels[designation]
if (channel != null) { if (channel != null) {
notifyResult()
result(channel.evaluate(rawValue)) result(channel.evaluate(rawValue))
collector.put(channel.name, channel.getTemperature(rawValue)) collector.put(channel.name, channel.getTemperature(rawValue))
} else { } else {
@ -254,20 +258,13 @@ class PKT8Device(context: Context, meta: Meta) : PortSensor<PKT8Result>(context,
} }
//send start signal //send start signal
controller.send("s") it.send("s")
notifyMeasurementState(MeasurementState.IN_PROGRESS)
} ?: notifyError("Not connected")
afterStart()
} catch (ex: ControlException) {
onError("Failed to start measurement", ex)
}
} }
override fun stopMeasurement(meta: Meta) { override fun stopMeasurement() {
if (isFinished) {
logger.warn("Trying to stop measurement which is already stopped")
return true
} else {
try { try {
logger.info("Stopping measurement") logger.info("Stopping measurement")
val response = sendAndWait("p").trim() val response = sendAndWait("p").trim()
@ -278,19 +275,17 @@ class PKT8Device(context: Context, meta: Meta) : PortSensor<PKT8Result>(context,
return false return false
} finally { } finally {
afterStop() afterStop()
errorListener?.let { controller.removeErrorListener(it) } connection?.removeErrorListener(this)
stopListener?.let { controller.removePhraseListener(it) } connection?.removePhraseListener(this)
valueListener?.let { controller.removePhraseListener(it) }
collector.stop() collector.stop()
logger.debug("Collector stopped") logger.debug("Collector stopped")
} }
} }
}
private fun setSPS(sps: Int) { private fun setSPS(sps: Int) {
logger.info("Setting sampling rate to " + spsToStr(sps)) logger.info("Setting sampling rate to " + spsToStr(sps))
val response: String = try { val response: String = try {
sendAndWait("v" + sps).trim { it <= ' ' } sendAndWait("v$sps").trim { it <= ' ' }
} catch (ex: Exception) { } catch (ex: Exception) {
ex.message ?: "" ex.message ?: ""
} }
@ -298,127 +293,16 @@ class PKT8Device(context: Context, meta: Meta) : PortSensor<PKT8Result>(context,
if (response.contains("=")) { if (response.contains("=")) {
updateLogicalState(SPS, Integer.parseInt(response.substring(4))) updateLogicalState(SPS, Integer.parseInt(response.substring(4)))
} else { } else {
logger.error("Setting sps failed with message: " + response) logger.error("Setting sps failed with message: $response")
} }
} }
// @Throws(MeasurementException::class)
// override fun createMeasurement(): Measurement<PKT8Result> {
// return if (this.measurement != null) {
// this.measurement
// } else {
// try {
// PKT8Measurement(connection)
// } catch (e: ControlException) {
// throw MeasurementException(e)
// }
//
// }
// }
//
// @Throws(MeasurementException::class)
// override fun setMeasurement(): Measurement<PKT8Result> {
// //clearing PKT queue
// try {
// send("p")
// sendAndWait("p")
// } catch (e: ControlException) {
// logger.error("Failed to clear PKT8 port")
// // throw new MeasurementException(e);
// }
//
// return super.setMeasurement()
// }
// inner class PKT8Measurement(private val controller: GenericPortController) : AbstractMeasurement<PKT8Result>() {
//
// override fun getDevice(): Device = this@PKT8Device
//
// private var collector: RegularPointCollector = RegularPointCollector(duration, channels.values.map { it.name }) { dp: Values ->
// logger.debug("Point measurement complete. Pushing...")
// storageHelper?.push(dp)
// }
//
// var errorListener: BiConsumer<String, Throwable>? = null
// var stopListener: GenericPortController.PhraseListener? = null;
// var valueListener: GenericPortController.PhraseListener? = null;
//
// override fun start() {
// if (isStarted) {
// logger.warn("Trying to start measurement which is already started")
// }
//
// try {
// logger.info("Starting measurement")
// //add weak error listener
// errorListener = controller.onError(this::onError)
//
// //add weak stop listener
// stopListener = controller.onPhrase("[Ss]topped\\s*") {
// afterPause()
// updateLogicalState(Sensor.MEASURING_STATE, false)
// }
//
// //add weak measurement listener
// valueListener = controller.onPhrase("[a-f].*") {
// val trimmed = it.trim()
// val designation = trimmed.substring(0, 1)
// val rawValue = java.lang.Double.parseDouble(trimmed.substring(1)) / 100
//
// val channel = this@PKT8Device.channels[designation]
//
// if (channel != null) {
// result(channel.evaluate(rawValue))
// collector.put(channel.name, channel.getTemperature(rawValue))
// } else {
// result(PKT8Result(designation, rawValue, -1.0))
// }
// }
//
// //send start signal
// controller.send("s")
//
// afterStart()
// } catch (ex: ControlException) {
// onError("Failed to start measurement", ex)
// }
//
// }
//
// @Throws(MeasurementException::class)
// override fun stop(force: Boolean): Boolean {
// if (isFinished) {
// logger.warn("Trying to stop measurement which is already stopped")
// return true
// } else {
//
// try {
// logger.info("Stopping measurement")
// val response = sendAndWait("p").trim()
// // Должно быть именно с большой буквы!!!
// return "Stopped" == response || "stopped" == response
// } catch (ex: Exception) {
// onError("Failed to stop measurement", ex)
// return false
// } finally {
// afterStop()
// errorListener?.let { controller.removeErrorListener(it) }
// stopListener?.let { controller.removePhraseListener(it) }
// valueListener?.let { controller.removePhraseListener(it) }
// collector.stop()
// logger.debug("Collector stopped")
// }
// }
// }
// }
//
companion object { companion object {
val PKT8_DEVICE_TYPE = "numass.pkt8" const val PKT8_DEVICE_TYPE = "numass.pkt8"
val PGA = "pga" const val PGA = "pga"
val SPS = "sps" const val SPS = "sps"
val ABUF = "abuf" const val ABUF = "abuf"
private val CHANNEL_DESIGNATIONS = arrayOf("a", "b", "c", "d", "e", "f", "g", "h") private val CHANNEL_DESIGNATIONS = arrayOf("a", "b", "c", "d", "e", "f", "g", "h")
} }

View File

@ -18,7 +18,7 @@ import inr.numass.control.DeviceView
* @author Alexander Nozik * @author Alexander Nozik
*/ */
@DeviceView(VacDisplay::class) @DeviceView(VacDisplay::class)
class CM32Device(context: Context, meta: Meta) : PortSensor<Double>(context, meta) { class CM32Device(context: Context, meta: Meta) : PortSensor(context, meta) {
override fun connect(meta: Meta): GenericPortController { override fun connect(meta: Meta): GenericPortController {
val portName = meta.getString("name") val portName = meta.getString("name")

View File

@ -23,7 +23,7 @@ import inr.numass.control.DeviceView
@ValueDef(name = "channel") @ValueDef(name = "channel")
@DeviceView(VacDisplay::class) @DeviceView(VacDisplay::class)
@StateDef(value = ValueDef(name = "channel", type = [ValueType.NUMBER], def = "2"), writable = true) @StateDef(value = ValueDef(name = "channel", type = [ValueType.NUMBER], def = "2"), writable = true)
class MKSBaratronDevice(context: Context, meta: Meta) : PortSensor<Double>(context, meta) { class MKSBaratronDevice(context: Context, meta: Meta) : PortSensor(context, meta) {
var channel by intState("channel") var channel by intState("channel")
@ -38,7 +38,7 @@ class MKSBaratronDevice(context: Context, meta: Meta) : PortSensor<Double>(conte
} }
override fun setMeasurement(oldMeta: Meta?, newMeta: Meta) { override fun setMeasurement(oldMeta: Meta?, newMeta: Meta) {
startMeasurement(newMeta) { startMeasurement {
doMeasure() doMeasure()
} }
} }

View File

@ -36,7 +36,7 @@ import java.util.regex.Pattern
// StateDef(value = ValueDef(name = "channel", info = "Measurement channel", type = arrayOf(NUMBER), def = "5"), writable = true) // StateDef(value = ValueDef(name = "channel", info = "Measurement channel", type = arrayOf(NUMBER), def = "5"), writable = true)
) )
@DeviceView(VacDisplay::class) @DeviceView(VacDisplay::class)
class MKSVacDevice(context: Context, meta: Meta) : PortSensor<Double>(context, meta) { class MKSVacDevice(context: Context, meta: Meta) : PortSensor(context, meta) {
private val deviceAddress: String = meta.getString("address", "253") private val deviceAddress: String = meta.getString("address", "253")
@ -111,7 +111,7 @@ class MKSVacDevice(context: Context, meta: Meta) : PortSensor<Double>(context, m
override fun getType(): String = meta.getString("type", "numass.vac.mks") override fun getType(): String = meta.getString("type", "numass.vac.mks")
override fun setMeasurement(oldMeta: Meta?, newMeta: Meta) { override fun setMeasurement(oldMeta: Meta?, newMeta: Meta) {
startMeasurement(newMeta) { startMeasurement {
doMeasure() doMeasure()
} }
} }

View File

@ -24,7 +24,7 @@ import java.util.regex.Pattern
* @author Alexander Nozik * @author Alexander Nozik
*/ */
@StateDef(value = ValueDef(name = "address", type = [NUMBER], def = "1", info = "A modbus address"), writable = true) @StateDef(value = ValueDef(name = "address", type = [NUMBER], def = "1", info = "A modbus address"), writable = true)
class MeradatVacDevice(context: Context, meta: Meta) : PortSensor<Double>(context, meta) { class MeradatVacDevice(context: Context, meta: Meta) : PortSensor(context, meta) {
var address by intState("address") var address by intState("address")
@ -40,7 +40,7 @@ class MeradatVacDevice(context: Context, meta: Meta) : PortSensor<Double>(contex
} }
override fun setMeasurement(oldMeta: Meta?, newMeta: Meta) { override fun setMeasurement(oldMeta: Meta?, newMeta: Meta) {
startMeasurement(newMeta) { startMeasurement{
doMeasure() doMeasure()
} }
} }

View File

@ -9,7 +9,7 @@ import hep.dataforge.storage.commons.StorageManager
fun main(args: Array<String>) { fun main(args: Array<String>) {
val meta = Meta.empty(); val meta = Meta.empty();
val context = Context.build("SERVER", Global.instance(), meta) val context = Context.build("SERVER", Global, meta)
val server = KodexServer(context, meta) val server = KodexServer(context, meta)
context.optFeature(StorageManager::class.java).ifPresent{ context.optFeature(StorageManager::class.java).ifPresent{