Another revision od devices. Now with the scheme

This commit is contained in:
Alexander Nozik 2017-11-24 22:15:06 +03:00
parent 8e32cb7897
commit a1187722da
3 changed files with 94 additions and 84 deletions

View File

@ -23,7 +23,6 @@ import hep.dataforge.control.ports.PortFactory
import hep.dataforge.control.ports.PortTimeoutException import hep.dataforge.control.ports.PortTimeoutException
import hep.dataforge.description.ValueDef import hep.dataforge.description.ValueDef
import hep.dataforge.exceptions.ControlException import hep.dataforge.exceptions.ControlException
import hep.dataforge.exceptions.NameNotFoundException
import hep.dataforge.exceptions.PortException import hep.dataforge.exceptions.PortException
import hep.dataforge.meta.Meta import hep.dataforge.meta.Meta
import hep.dataforge.utils.DateTimeUtils import hep.dataforge.utils.DateTimeUtils
@ -104,7 +103,7 @@ open class LambdaMagnet(context: Context, meta: Meta, private val controller: La
override fun computeState(stateName: String): Any { override fun computeState(stateName: String): Any {
when (stateName) { return when (stateName) {
"current" -> controller.talk(address, timeout) { s2d(getParameter("MC")) } "current" -> controller.talk(address, timeout) { s2d(getParameter("MC")) }
"voltage" -> controller.talk(address, timeout) { s2d(getParameter("MV")) } "voltage" -> controller.talk(address, timeout) { s2d(getParameter("MV")) }
"targetCurrent" -> controller.talk(address, timeout) { s2d(getParameter("PC")) } "targetCurrent" -> controller.talk(address, timeout) { s2d(getParameter("PC")) }
@ -112,42 +111,36 @@ open class LambdaMagnet(context: Context, meta: Meta, private val controller: La
"output" -> controller.talk(address, timeout) { talk("OUT?") == "OK" } "output" -> controller.talk(address, timeout) { talk("OUT?") == "OK" }
"monitoring" -> monitorTask != null "monitoring" -> monitorTask != null
"updating" -> updateTask != null "updating" -> updateTask != null
else -> getDefaultState(stateName) else -> getLogicalState(stateName)
} }
} }
override fun requestStateChange(stateName: String, value: Value): Any { override fun requestStateChange(stateName: String, value: Value) {
return when (stateName) { when (stateName) {
"targetCurrent" -> { "targetCurrent" -> {
if (!setParameter("PC", value.doubleValue())) { if (setParameter("PC", value.doubleValue())) {
reportError("Can't set the target current", null)
Value.NULL
} else {
lastUpdate = DateTimeUtils.now() lastUpdate = DateTimeUtils.now()
value } else {
reportError("Can't set the target current", null)
} }
} }
"targetVoltage" -> { "targetVoltage" -> {
if (!setParameter("PV", value.doubleValue())) { if (!setParameter("PV", value.doubleValue())) {
reportError("Can't set the target voltage", null) reportError("Can't set the target voltage", null)
Value.NULL
} else {
value
} }
} }
"speed" -> value
"lastUpdate" -> value
"updating" -> if (value.booleanValue()) { "updating" -> if (value.booleanValue()) {
startUpdateTask() startUpdateTask()
true
} else { } else {
stopUpdateTask() stopUpdateTask()
false
} }
"monitoring" -> i "monitoring" -> if (value.booleanValue()) {
"output" -> startMonitorTask()
else -> throw NameNotFoundException("State not found", stateName) } else {
stopMonitorTask()
}
"output" -> setOutputMode(value.booleanValue())
else -> setLogicalState(stateName, value);
} }
} }
@ -270,8 +263,8 @@ open class LambdaMagnet(context: Context, meta: Meta, private val controller: La
* @param targetI * @param targetI
* @param delay * @param delay
*/ */
@JvmOverloads
fun startUpdateTask(delay: Int = DEFAULT_DELAY) { private fun startUpdateTask(delay: Int = DEFAULT_DELAY) {
assert(delay > 0) assert(delay > 0)
stopUpdateTask() stopUpdateTask()
val call = { val call = {
@ -295,26 +288,22 @@ open class LambdaMagnet(context: Context, meta: Meta, private val controller: La
updateTask = scheduler.scheduleWithFixedDelay(call, 0, delay.toLong(), TimeUnit.MILLISECONDS) updateTask = scheduler.scheduleWithFixedDelay(call, 0, delay.toLong(), TimeUnit.MILLISECONDS)
listener?.updateTaskStateChanged(getName(), true) listener?.updateTaskStateChanged(getName(), true)
setLogicalState("updating", Value.of(true))
} }
@Throws(PortException::class) @Throws(PortException::class)
fun setOutputMode(out: Boolean) { private fun setOutputMode(out: Boolean) {
controller.talk(address, timeout) { val outState: Int = if (out) 1 else 0
val outState: Int = if (out) { if (!setParameter("OUT", outState)) {
1 listener?.error(getName(), "Can't set output mode", null)
} else { } else {
0 requestStateChange("output", Value.of(out))
} listener?.outputModeChanged(getName(), out)
if (!setParameter("OUT", outState)) {
listener?.error(getName(), "Can't set output mode", null)
} else {
listener?.outputModeChanged(getName(), out)
}
} }
} }
private fun nextI(measuredI: Double, targetI: Double): Double { private fun nextI(measuredI: Double, targetI: Double): Double {
assert(measuredI != targetI) // assert(measuredI != targetI)
var step = if (lastUpdate == null) { var step = if (lastUpdate == null) {
MIN_UP_STEP_SIZE MIN_UP_STEP_SIZE
@ -345,12 +334,13 @@ open class LambdaMagnet(context: Context, meta: Meta, private val controller: La
/** /**
* Cancel current monitoring task * Cancel current monitoring task
*/ */
fun stopMonitorTask() { private fun stopMonitorTask() {
monitorTask?.let { monitorTask?.let {
it.cancel(true) it.cancel(true)
listener?.monitorTaskStateChanged(getName(), false) listener?.monitorTaskStateChanged(getName(), false)
monitorTask = null monitorTask = null
} }
setLogicalState("output", Value.of(false))
} }
override fun getName(): String { override fun getName(): String {

View File

@ -9,6 +9,7 @@ import hep.dataforge.context.Context
import hep.dataforge.control.devices.Device import hep.dataforge.control.devices.Device
import hep.dataforge.control.devices.PortSensor import hep.dataforge.control.devices.PortSensor
import hep.dataforge.control.devices.StateDef import hep.dataforge.control.devices.StateDef
import hep.dataforge.control.devices.StateDefs
import hep.dataforge.control.measurements.Measurement import hep.dataforge.control.measurements.Measurement
import hep.dataforge.control.measurements.SimpleMeasurement import hep.dataforge.control.measurements.SimpleMeasurement
import hep.dataforge.control.ports.Port import hep.dataforge.control.ports.Port
@ -19,11 +20,7 @@ import hep.dataforge.meta.Meta
import hep.dataforge.values.Value import hep.dataforge.values.Value
import hep.dataforge.values.ValueType.BOOLEAN import hep.dataforge.values.ValueType.BOOLEAN
import inr.numass.control.DeviceView import inr.numass.control.DeviceView
import javafx.beans.property.BooleanProperty import java.lang.Double.parseDouble
import javafx.beans.property.SimpleBooleanProperty
import javafx.beans.property.SimpleIntegerProperty
import javafx.beans.property.adapter.JavaBeanBooleanPropertyBuilder
import tornadofx.*
import java.util.regex.Pattern import java.util.regex.Pattern
/** /**
@ -34,20 +31,22 @@ import java.util.regex.Pattern
ValueDef(name = "channel", def = "5"), ValueDef(name = "channel", def = "5"),
ValueDef(name = "powerButton", type = arrayOf(BOOLEAN), def = "true") ValueDef(name = "powerButton", type = arrayOf(BOOLEAN), def = "true")
) )
@StateDef(value = ValueDef(name = "power", info = "Device powered up"), writable = true) @StateDefs(
StateDef(value = ValueDef(name = "power", info = "Device powered up"), 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<Double>(context, meta) {
private val deviceAddress: String private val deviceAddress: String = meta.getString("address", "253")
get() = getMeta().getString("address", "253")
val isPowerOnProperty = SimpleBooleanProperty() // val isPowerOnProperty = SimpleBooleanProperty()
var isPowerOn by isPowerOnProperty // var isPowerOn by isPowerOnProperty
val channelProperty = SimpleIntegerProperty(meta.getInt("channel", 5)) // val channelProperty = SimpleIntegerProperty(meta.getInt("channel", 5))
var channel by channelProperty // var channel by channelProperty
@Throws(ControlException::class) @Throws(ControlException::class)
@ -78,9 +77,9 @@ class MKSVacDevice(context: Context, meta: Meta) : PortSensor<Double>(context, m
} }
@Throws(ControlException::class) @Throws(ControlException::class)
override fun requestStateChange(stateName: String, value: Value): Any { override fun requestStateChange(stateName: String, value: Value) {
when (stateName) { when (stateName) {
"power" -> isPowerOn = value.booleanValue() "power" -> setPower(value.booleanValue())
else -> super.requestStateChange(stateName, value) else -> super.requestStateChange(stateName, value)
} }
@ -89,24 +88,46 @@ class MKSVacDevice(context: Context, meta: Meta) : PortSensor<Double>(context, m
@Throws(ControlException::class) @Throws(ControlException::class)
override fun shutdown() { override fun shutdown() {
if (isConnected) { if (isConnected) {
isPowerOn = false setState("power",false)
} }
super.shutdown() super.shutdown()
} }
fun powerOnProperty(): BooleanProperty { // fun powerOnProperty(): BooleanProperty {
try { // try {
return JavaBeanBooleanPropertyBuilder().bean(this) // return JavaBeanBooleanPropertyBuilder().bean(this)
.name("powerOn").getter("isPowerOn").setter("setPowerOn").build() // .name("powerOn").getter("isPowerOn").setter("setPower").build()
} catch (ex: NoSuchMethodException) { // } catch (ex: NoSuchMethodException) {
throw Error(ex) // throw Error(ex)
// }
//
// }
private fun setPower(powerOn: Boolean) {
if (powerOn != getLogicalState("power").booleanValue()) {
if (powerOn) {
// String ans = talkMKS(p1Port, "@253ENC!OFF;FF");
// if (!ans.equals("OFF")) {
// LoggerFactory.getLogger(getClass()).warn("The @253ENC!OFF;FF command is not working");
// }
val ans = talk("FP!ON")
if (ans == "ON") {
setLogicalState("power", true)
} else {
this.notifyError("Failed to set power state", null)
}
} else {
val ans = talk("FP!OFF")
if (ans == "OFF") {
setLogicalState("power", false)
} else {
this.notifyError("Failed to set power state", null)
}
}
} }
} }
override fun getType(): String { override fun getType(): String = meta.getString("type", "MKS vacuumeter")
return getMeta().getString("type", "MKS vacuumeter")
}
private inner class MKSVacMeasurement : SimpleMeasurement<Double>() { private inner class MKSVacMeasurement : SimpleMeasurement<Double>() {
@ -114,13 +135,14 @@ class MKSVacDevice(context: Context, meta: Meta) : PortSensor<Double>(context, m
@Throws(Exception::class) @Throws(Exception::class)
override fun doMeasure(): Double? { override fun doMeasure(): Double? {
// if (getState("power").booleanValue()) { // if (getState("power").booleanValue()) {
val channel = meta.getInt("channel", 5)
val answer = talk("PR$channel?") val answer = talk("PR$channel?")
if (answer == null || answer.isEmpty()) { if (answer == null || answer.isEmpty()) {
updateState(PortSensor.CONNECTED_STATE, false) updateState(PortSensor.CONNECTED_STATE, false)
this.updateMessage("No connection") this.updateMessage("No connection")
return null return null
} }
val res = java.lang.Double.parseDouble(answer) val res = parseDouble(answer)
return if (res <= 0) { return if (res <= 0) {
this.updateMessage("No power") this.updateMessage("No power")
invalidateState("power") invalidateState("power")

View File

@ -224,29 +224,27 @@ class StorageView(private val context: Context = Global.instance()) : View(title
} }
private fun buildContainer(content: Any, parent: Container): Container { private fun buildContainer(content: Any, parent: Container): Container =
return when (content) { when (content) {
is Storage -> { is Storage -> {
Container(content.fullName.toString(), content) Container(content.fullName.toString(), content)
}
is NumassSet -> {
val id = if (content is NumassDataLoader) {
content.path
} else {
content.name
} }
Container(id.toString(), content) is NumassSet -> {
val id: String = if (content is NumassDataLoader) {
content.path.toString()
} else {
content.name
}
Container(id.toString(), content)
}
is NumassPoint -> {
Container("${parent.id}/${content.voltage}".replace(".", "_"), content)
}
is Loader -> {
Container(content.path.toString(), content);
}
else -> throw IllegalArgumentException("Unknown content type: ${content::class.java}");
} }
is NumassPoint -> {
Container("${parent.id}/${content.voltage}".replace(".", "_"), content)
}
is Loader -> {
Container(content.path.toString(), content);
}
else -> throw IllegalArgumentException("Unknown content type: ${content::class.java}");
}
}
// private fun getSetName(value: NumassSet): String { // private fun getSetName(value: NumassSet): String {