Refactoring in core. Added delegates for values and states

This commit is contained in:
Alexander Nozik 2017-11-21 21:43:47 +03:00
parent ae17288664
commit 2d8ed4da69
4 changed files with 7 additions and 75 deletions

View File

@ -8,6 +8,7 @@ import hep.dataforge.control.devices.StateDef
import hep.dataforge.control.ports.Port
import hep.dataforge.control.ports.PortFactory
import hep.dataforge.description.ValueDef
import hep.dataforge.kodex.useEachMeta
import hep.dataforge.meta.Meta
import hep.dataforge.names.Name
import hep.dataforge.values.ValueType
@ -50,11 +51,8 @@ class LambdaHub(context: Context, meta: Meta) : DeviceHub, AbstractDevice(contex
port.close()
}
override fun optDevice(name: Name): Optional<Device> {
return magnets.stream().filter { it.name == name.toUnescaped() }.map { it as Device }.findFirst()
}
override fun optDevice(name: Name): Optional<Device> =
magnets.stream().filter { it.name == name.toUnescaped() }.map { it as Device }.findFirst()
override fun deviceNames(): Stream<Name> {
return magnets.stream().map { Name.ofSingle(it.name) }
}
override fun getDeviceNames(): Stream<Name> = magnets.stream().map { Name.ofSingle(it.name) }
}

View File

@ -26,70 +26,4 @@ class LambdaPortController(context: Context, port: Port) : GenericPortController
return action(this)
}
}
// override fun getContext(): Context = _context
//
// private var address: Int = -1;
//
// private val channel = ConflatedChannel<String>();
//
// private val listeners = ReferenceRegistry<LambdaPortListener>()
//
// fun open() {
// try {
// port.holdBy(this)
// if (!port.isOpen) {
// port.open()
// }
// } catch (e: PortException) {
// throw RuntimeException("Can't hold the port $port LAMBDA port controller", e)
// }
// }
//
//
// private suspend fun setAddress(address: Int, timeout: Duration) {
// synchronized(this) {
// port.send(this, "ADR $address\r")
// val res = channel.receive()
// if (res == "OK") {
// this.address = address
// }
// }
// }
//
// private suspend fun sendMessage(message: String): String {
//
// }
//
// suspend fun fireSequence(address: Int, timeout: Duration, vararg messages: String) {
// setAddress(address, timeout);
// for (message in messages) {
// sendMessage(message);
// }
// }
//
// override fun close() {
// port.releaseBy(this)
// }
//
// override fun acceptPhrase(message: String) {
// async {
// channel.send(message);
// }
// listeners.forEach {
// if (it.address == address) {
// context.parallelExecutor().submit { it.action(message) }
// }
// }
// }
//
// override fun acceptError(errorMessage: String?, error: Throwable?) {
// listeners.forEach {
// if (it.address == address) {
// context.parallelExecutor().submit { it.onError(errorMessage, error) }
// }
// }
// }
//
// class LambdaPortListener(val address: Int, val action: (String) -> Unit, val onError: (String?, Throwable?) -> Unit = { _, _ -> })
}

View File

@ -58,7 +58,7 @@ class VacCollectorDevice(context: Context, meta: Meta, val sensors: Collection<S
override fun optDevice(name: Name): Optional<Device> =
Optional.ofNullable(sensors.find { it.name == name.toUnescaped() })
override fun deviceNames(): Stream<Name> = sensors.stream().map { Name.ofSingle(it.name) }
override fun getDeviceNames(): Stream<Name> = sensors.stream().map { Name.ofSingle(it.name) }
override fun init() {
@ -141,7 +141,7 @@ class VacCollectorDevice(context: Context, meta: Meta, val sensors: Collection<S
private fun terminator(): Values {
val p = ValueMap.Builder()
p.putValue("timestamp", DateTimeUtils.now())
deviceNames().forEach { n -> p.putValue(n.toUnescaped(), null) }
getDeviceNames().forEach { n -> p.putValue(n.toUnescaped(), null) }
return p.build()
}

View File

@ -91,7 +91,7 @@ val deviceInterceptor = InterceptorFactory { context, meta ->
get("listDevices") {
call.json {
val devices = jsonArray();
for (name in deviceManager.deviceNames()) {
for (name in deviceManager.getDeviceNames()) {
val device = deviceManager.optDevice(name).get();
devices.add(jsonObject {
add("name", name.toUnescaped())