Params
A Kotlin Dsl has been provided for creating Parameters to store values like primitive types, collection types or domain specific types. This Dsl is built over abstractions like Parameter
, KeyType
etc offered by CSW. Refer to the CSW doc for more information about this.
Keys
Following table lists all the key types, and their corresponding kotlin dsl provided.
KeyType | Kotlin Dsl |
---|---|
Boolean | booleanKey |
Character | charKey |
Byte | byteKey |
Short | shortKey |
Long | longKey |
Int | intKey |
Float | floatKey |
Double | doubleKey |
String | stringKey |
UtcTime | utcTimeKey |
TaiTime | taiTimeKey |
ByteArray | byteArrayKey |
ShortArray | shortArrayKey |
LongArray | longArrayKey |
IntArray | intArrayKey |
FloatArray | floatArrayKey |
DoubleArray | doubleArrayKey |
ByteMatrix | byteMatrixKey |
ShortMatrix | shortMatrixKey |
LongMatrix | longMatrixKey |
IntMatrix | intMatrixKey |
FloatMatrix | floatMatrixKey |
DoubleMatrix | doubleMatrixKey |
Choice | choiceKey |
Struct | structKey |
RaDec | raDecKey |
EqCoord | eqCoordKey |
SolarSystemCoord | solarSystemCoordKey |
MinorPlanetCoord | minorPlanetCoordKey |
CometCoord | cometCoordKey |
AltAzCoord | altAzCoordKey |
Coord (*) | coordKey |
Example below shows usages of Dsl for different types of keys. Some other helper Dsl like struct
, choicesOf
, arrayData
, matrixData
etc have also been provided for ease of access. Usage of this helper Dsl is also shown in the below example.
- Kotlin
-
// Primitive keys val encoderKey: Key<Int> = intKey("encoder") val flagKey: Key<Boolean> = booleanKey("flag") val eventTimeKey: Key<UTCTime> = utcTimeKey("event-time") // Arrays val arrayKey: Key<ArrayData<Int>> = intArrayKey("arrayKey") val elms: Array<Int> = arrayOf(1, 2, 3, 4) val values1: ArrayData<Int> = arrayData(elms) val values2: ArrayData<Int> = arrayData(5, 6, 7, 8) val arrayParam: Parameter<ArrayData<Int>> = arrayKey.set(values1, values2) // Matrix val matrixKey: Key<MatrixData<Int>> = intMatrixKey("matrixKey") val arr1: Array<Int> = arrayOf(1, 2, 3, 4) val arr2: Array<Int> = arrayOf(5, 6, 7, 8) val elms1: Array<Array<Int>> = arrayOf(arr1, arr2) val data1: MatrixData<Int> = matrixData(elms1) val data2: MatrixData<Int> = matrixData(arr1, arr2) val matrixParameter: Parameter<MatrixData<Int>> = matrixKey.set(data1, data2) // Domain specific types val choiceKey: Key<Choice> = choiceKey("choice", choicesOf("A", "B", "C")) val choiceParam: Parameter<Choice> = choiceKey.set(Choice("A"), Choice("C")) // Struct val intParam: Parameter<Int> = encoderKey.set(1, 2, 3) val paramSet: Set<Parameter<*>> = setOf(intParam, choiceParam) val complexKey: Key<Struct> = structKey("complexKey") val struct1: Struct = struct(paramSet) val struct2: Struct = struct(command.params) val struct3: Struct = struct(intParam, arrayParam) val structParam: Parameter<Struct> = complexKey.set(struct1, struct2, struct3)
Creating Parameters
Example below shows different ways of creating parameters and adding them to a command.
- Kotlin
-
val temperatureKey: Key<Int> = intKey("temperature") val temperatureParam: Parameter<Int> = temperatureKey.set(1, 2, 3) // with values as Array val encoderKey: Key<Int> = intKey("encoder") val encoderValues: Array<Int> = arrayOf(1, 2, 3) val encoderParam: Parameter<Int> = encoderKey.set(*encoderValues) // with units val powerKey: Key<Double> = doubleKey("power") val values: Array<Double> = arrayOf(1.1, 2.2, 3.3) val powerParam: Parameter<Double> = powerKey.set(values, JUnits.watt) // adding a param to command or event val setupCommand: Setup = Setup("ESW.iris_darkNight", "move").add(temperatureParam) val systemEvent: SystemEvent = SystemEvent("ESW.iris_darkNight", "movement").add(temperatureParam) // adding multiple params val setupCommand2: Setup = Setup("ESW.iris_darkNight", "move").madd(temperatureParam, encoderParam) // adding params of one command to other val paramsFromIncomingCommand: Params = command.params val commandForDownstream: Setup = Setup("ESW.iris_darkNight", "move").add(paramsFromIncomingCommand)
Extracting a parameter from Params/Command/Event
Extracting parameter
Finding a parameter from Params/Command/Event.
- Kotlin
-
val params: Params = setupCommand.params val maybeParam: Parameter<Int>? = params.kFind(temperatureParam) val maybeParam2: Parameter<Int>? = setupCommand.kFind(temperatureParam)
Extracting parameter by key
- Kotlin
-
// extracting a param from Params instance val temperatureParameter: Parameter<Int>? = setupCommand.params.kGet(temperatureKey) val temperatureParameter2: Parameter<Int> = (setupCommand.params)(temperatureKey) // alternative // extracting a param directly from the command or event val temperatureParameter3: Parameter<Int>? = setupCommand.kGet(temperatureKey) val temperatureParameter4: Parameter<Int> = setupCommand(temperatureKey) // alternative
Extracting parameter by keyName and KeyType
- Kotlin
-
val keyName = "temperature" val keyType: KeyType<Int> = JKeyType.IntKey() val param: Parameter<Int>? = setupCommand.params.kGet(keyName, keyType)
Extracting values from a parameter
Example below shows accessing values of a parameter, or accessing a specific value of a parameter.
- Kotlin
-
val temperatureKey: Key<Int> = intKey("temperature") val temperatureParam: Parameter<Int> = temperatureKey.set(1, 2, 3) // extracting values from parameter val temperatureValues: List<Int> = temperatureParam.values // extracting first value from parameter val firstValue: Int = temperatureParam.first // extracting value of the parameter at a given index val temperatureValue: Int? = temperatureParam.kGet(1) val temperatureValue2: Int = temperatureParam(1) //alternative
Note that the shorthand alternatives shown with // alternative
comment in above examples, do not return optional
values unlike their corresponding full version. Which means, with shorthand dsl, an error will occur in absence of the specified key/index.
Removing a parameter
A parameter could be removed from Params
instance or from Command
directly. Below example demonstrates both the dsl methods.
- Kotlin
-
// remove param from params by key val updatedParams: Params = setupCommand.params.remove(temperatureKey) // remove param from params val updatedParams2: Params = setupCommand.params.remove(temperatureParameter) // remove param from command by key val updatedCommand: Setup = setupCommand.remove(temperatureKey) // remove param from command val updatedCommand2: Setup = setupCommand.remove(temperatureParameter)