Updates to control using coroutines

This commit is contained in:
Alexander Nozik 2018-03-29 23:00:57 +03:00
parent 700707a7a9
commit f2d292b853
4 changed files with 33 additions and 28 deletions

View File

@ -143,11 +143,11 @@ abstract class DeviceDisplay<D : Device> : Component(), Connection, DeviceListen
} }
} }
override fun notifyDeviceStateChanged(device: Device, name: String, state: Value) { override fun notifyStateChanged(device: Device, name: String, state: Value) {
bindings[name]?.invalidate() bindings[name]?.invalidate()
} }
override fun notifyDeviceStateChanged(device: Device, name: String, state: Meta) { override fun notifyMetaStateChanged(device: Device, name: String, state: Meta) {
metaBindings[name]?.invalidate() metaBindings[name]?.invalidate()
} }

View File

@ -12,6 +12,11 @@ import java.time.Instant
* Created by darksnake on 06-Dec-16. * Created by darksnake on 06-Dec-16.
*/ */
object ConsoleVac { object ConsoleVac {
private fun Sensor.read():Double{
this.measure()
}
@Throws(Exception::class) @Throws(Exception::class)
@JvmStatic @JvmStatic
fun main(args: Array<String>) { fun main(args: Array<String>) {
@ -33,7 +38,7 @@ object ConsoleVac {
var className: String = cli.getOptionValue("c") ?: throw RuntimeException("Vacuumeter class not defined") var className: String = cli.getOptionValue("c") ?: throw RuntimeException("Vacuumeter class not defined")
if (!className.contains(".")) { if (!className.contains(".")) {
className = "inr.numass.readvac.devices." + className className = "inr.numass.readvac.devices.$className"
} }
val name = cli.getOptionValue("n", "sensor") val name = cli.getOptionValue("n", "sensor")

View File

@ -34,6 +34,7 @@ import inr.numass.control.StorageHelper
import kotlinx.coroutines.experimental.launch import kotlinx.coroutines.experimental.launch
import kotlinx.coroutines.experimental.time.delay import kotlinx.coroutines.experimental.time.delay
import java.time.Duration import java.time.Duration
import java.time.Instant
import java.util.* import java.util.*
import java.util.stream.Stream import java.util.stream.Stream
@ -53,11 +54,11 @@ class VacCollectorDevice(context: Context, meta: Meta, val sensors: Collection<S
notifyResult(it) notifyResult(it)
} }
override fun notifyDeviceStateChanged(device: Device, name: String, state: Value) { override fun notifyStateChanged(device: Device, name: String, state: Value) {
} }
override fun notifyDeviceStateChanged(device: Device, name: String, state: Meta) { override fun notifyMetaStateChanged(device: Device, name: String, state: Meta) {
if (name == MEASUREMENT_RESULT_STATE) { if (name == MEASUREMENT_RESULT_STATE) {
collector.put(device.name, state.getValue("value")) collector.put(device.name, state.getValue("value"))
} }
@ -110,8 +111,8 @@ class VacCollectorDevice(context: Context, meta: Meta, val sensors: Collection<S
} }
private fun notifyResult(values: Values) { private fun notifyResult(values: Values, timestamp: Instant = Instant.now()) {
notifyResult(produceResult(values.toMeta())) super.notifyResult(values,timestamp)
helper.push(values) helper.push(values)
} }

View File

@ -8,9 +8,11 @@ package inr.numass.control.readvac
import hep.dataforge.control.devices.Device import hep.dataforge.control.devices.Device
import hep.dataforge.control.devices.PortSensor.Companion.CONNECTED_STATE import hep.dataforge.control.devices.PortSensor.Companion.CONNECTED_STATE
import hep.dataforge.control.devices.Sensor import hep.dataforge.control.devices.Sensor
import hep.dataforge.kodex.stringValue
import hep.dataforge.kodex.timeValue import hep.dataforge.kodex.timeValue
import hep.dataforge.kodex.value import hep.dataforge.kodex.value
import hep.dataforge.meta.Meta import hep.dataforge.meta.Meta
import hep.dataforge.values.Value
import inr.numass.control.DeviceDisplay import inr.numass.control.DeviceDisplay
import inr.numass.control.switch import inr.numass.control.switch
import javafx.application.Platform import javafx.application.Platform
@ -48,27 +50,13 @@ open class VacDisplay : DeviceDisplay<Sensor>() {
return VacView(); return VacView();
} }
override fun evaluateDeviceException(device: Device, message: String, exception: Throwable) {
if (!message.isEmpty()) {
Platform.runLater {
status = "ERROR: $message"
}
}
}
override fun onMeasurementFailed(measurement: Measurement<*>, exception: Throwable) {
Platform.runLater {
value = "Err"
}
}
fun message(message: String) { fun message(message: String) {
Platform.runLater { Platform.runLater {
status = message status = message
} }
} }
fun onResult(res: Any, time: Instant) { private fun onResult(res: Any, time: Instant) {
val result = Number::class.java.cast(res).toDouble() val result = Number::class.java.cast(res).toDouble()
val resString = FORMAT.format(result) val resString = FORMAT.format(result)
Platform.runLater { Platform.runLater {
@ -78,21 +66,32 @@ open class VacDisplay : DeviceDisplay<Sensor>() {
} }
} }
override fun notifyDeviceStateChanged(device: Device, name: String, state: Meta) { override fun notifyMetaStateChanged(device: Device, name: String, state: Meta) {
super.notifyDeviceStateChanged(device, name, state) super.notifyMetaStateChanged(device, name, state)
when (name) { when (name) {
Sensor.MEASUREMENT_RESULT_STATE -> { Sensor.MEASUREMENT_RESULT_STATE -> {
val res by state.value(Sensor.RESULT_VALUE) if(state.getBoolean(Sensor.RESULT_SUCCESS)) {
val time by state.timeValue(Sensor.RESULT_TIMESTAMP) val res by state.value(Sensor.RESULT_VALUE)
onResult(res, time) val time by state.timeValue(Sensor.RESULT_TIMESTAMP)
onResult(res, time)
} else{
Platform.runLater {
value = "Err"
}
}
} }
Sensor.MEASUREMENT_ERROR_STATE -> { Sensor.MEASUREMENT_ERROR_STATE -> {
val message by state.stringValue("message")
message(message)
} }
} }
} }
override fun notifyStateChanged(device: Device, name: String, state: Value) {
super.notifyStateChanged(device, name, state)
}
fun getTitle(): String { fun getTitle(): String {
return device.meta.getString("title", device.name); return device.meta.getString("title", device.name);
} }