Fix nullability in actions

This commit is contained in:
Alexander Nozik 2023-05-25 10:07:51 +03:00
parent 25d7fe0a8e
commit 7b792afd7a

View File

@ -162,7 +162,7 @@ class PiMotionMasterDevice(
send("STP") send("STP")
} }
val connect by metaAction(descriptorBuilder = { val connect by action(MetaConverter.meta, MetaConverter.unit, descriptorBuilder = {
info = "Connect to specific port and initialize axis" info = "Connect to specific port and initialize axis"
}) { portSpec -> }) { portSpec ->
//Clear current actions if present //Clear current actions if present
@ -171,36 +171,32 @@ class PiMotionMasterDevice(
} }
//Update port //Update port
//address = portSpec.node //address = portSpec.node
port = portFactory(portSpec ?: Meta.EMPTY, context) port = portFactory(portSpec, context)
updateLogical(connected, true) updateLogical(connected, true)
// connector.open() // connector.open()
//Initialize axes //Initialize axes
if (portSpec != null) { val idn = read(identity)
val idn = read(identity) failIfError { "Can't connect to $portSpec. Error code: $it" }
failIfError { "Can't connect to $portSpec. Error code: $it" } logger.info { "Connected to $idn on $portSpec" }
logger.info { "Connected to $idn on $portSpec" } val ids = request("SAI?").map { it.trim() }
val ids = request("SAI?").map { it.trim() } if (ids != axes.keys.toList()) {
if (ids != axes.keys.toList()) { //re-define axes if needed
//re-define axes if needed axes = ids.associateWith { Axis(this, it) }
axes = ids.associateWith { Axis(this, it) }
}
Meta(ids.map { it.asValue() }.asValue())
execute(initialize)
failIfError()
} }
null Meta(ids.map { it.asValue() }.asValue())
execute(initialize)
failIfError()
} }
val disconnect by metaAction({ val disconnect by unitAction({
info = "Disconnect the program from the device if it is connected" info = "Disconnect the program from the device if it is connected"
}) { }) {
port?.let{ port?.let {
execute(stop) execute(stop)
it.close() it.close()
} }
port = null port = null
updateLogical(connected, false) updateLogical(connected, false)
null
} }
@ -212,7 +208,7 @@ class PiMotionMasterDevice(
class Axis( class Axis(
val mm: PiMotionMasterDevice, val mm: PiMotionMasterDevice,
val axisId: String val axisId: String,
) : DeviceBySpec<Axis>(Axis, mm.context) { ) : DeviceBySpec<Axis>(Axis, mm.context) {
/** /**
@ -244,7 +240,7 @@ class PiMotionMasterDevice(
private fun axisBooleanProperty( private fun axisBooleanProperty(
command: String, command: String,
descriptorBuilder: PropertyDescriptor.() -> Unit = {} descriptorBuilder: PropertyDescriptor.() -> Unit = {},
) = booleanProperty( ) = booleanProperty(
read = { read = {
readAxisBoolean("$command?") readAxisBoolean("$command?")
@ -257,7 +253,7 @@ class PiMotionMasterDevice(
private fun axisNumberProperty( private fun axisNumberProperty(
command: String, command: String,
descriptorBuilder: PropertyDescriptor.() -> Unit = {} descriptorBuilder: PropertyDescriptor.() -> Unit = {},
) = doubleProperty( ) = doubleProperty(
read = { read = {
mm.requestAndParse("$command?", axisId)[axisId]?.toDoubleOrNull() mm.requestAndParse("$command?", axisId)[axisId]?.toDoubleOrNull()
@ -334,11 +330,11 @@ class PiMotionMasterDevice(
info = "Velocity value for closed-loop operation" info = "Velocity value for closed-loop operation"
} }
val move by metaAction { val move by action(MetaConverter.meta, MetaConverter.unit) {
val target = it.double ?: it?.get("target").double ?: error("Unacceptable target value $it") val target = it.double ?: it["target"].double ?: error("Unacceptable target value $it")
write(closedLoop, true) write(closedLoop, true)
//optionally set velocity //optionally set velocity
it?.get("velocity").double?.let { v -> it["velocity"].double?.let { v ->
write(velocity, v) write(velocity, v)
} }
write(targetPosition, target) write(targetPosition, target)
@ -347,7 +343,6 @@ class PiMotionMasterDevice(
read(position) read(position)
delay(200) delay(200)
} }
null
} }
} }