Using the Configuration Service in Scripts

The Configuration Service (CS) is available to script writers using the provided DSL. The Configuration Service DSL is a wrapper over the client Configuration Service module provided by CSW. The detailed documentation of Configuration Service provided by CSW here is useful to understand usage of CS and limits.

The CS DSL provides methods to check if a file exists in the Configuration Service and to retrieve a file’s contents.

existsConfig

The existsConfig DSL method checks if provided file path exists in Configuration Service with a specific revision id (if provided) and returns true or false based on whether the file exists.

Kotlin
sourceval commandsFile = "/wfos/commands.conf"
val commandsConfigExist: Boolean = existsConfig(commandsFile)

// terminate script if required configuration file does not exist
if (!commandsConfigExist) finishWithError("Configuration file [$commandsFile] not found in configuration service")

getConfig

The getConfig DSL method retrieves the content of the file present at the provided path in Configuration Service. It returns null if file is not available in Configuration Service.

In the below example, we are performing following steps:

  1. Retrieve a configuration file from Configuration Service
  2. Fail/Terminate script if configuration file does not exist
  3. Parse retrieved configuration file and convert it to MotorCommands domain model
  4. When Sequencer receives set-motor-speed command, then submit set-speed command to downstream motor HCD
  5. When Sequencer receives rotate-motor command, then send set-resolution command to downstream motor HCD
Kotlin
sourceval wfosCommandsFile = "/wfos/commands.conf"

// retrieve configuration file from config service, terminate script if configuration file does not exist
val commandsConfig: Config = getConfig(wfosCommandsFile)
        ?: finishWithError("Configuration file [$wfosCommandsFile] not found in configuration service")

val motorCommands = MotorCommands.from(commandsConfig)

// on receiving `set-motor-speed` command, send `set-speed` command to downstream motor hcd
onSetup("set-motor-speed") {
    val motorSpeedParam = motorSpeedKey.set(motorCommands.setMotorSpeed)
    val setSpeedCommand = Setup(motorPrefixStr, "set-speed").add(motorSpeedParam)
    motorHcd.submit(setSpeedCommand)
}

// on receiving `set-step-motor-resolution` command, send `set-resolution` command to downstream motor hcd
onSetup("set-step-motor-resolution") {
    val setResolutionParam = motorResolutionKey.set(motorCommands.setStepMotorResolution)
    val setResolutionCommand = Setup(motorPrefixStr, "set-resolution").add(setResolutionParam)
    motorHcd.submit(setResolutionCommand)
}

The following example shows sample code for converting a Config object retrieved from the Configuration Service to custom domain models. Note that TMT standard for configuration files is HOCON as supported by CSW.

Refer to this guide for complete usage of Config.

Kotlin
source/**
 * ======== Sample commands.conf file ========
 * wfos.motor.commands {
 *      set-motor-speed = 50
 *      set-step-motor-resolution = "1080p"
 * }
 */
data class MotorCommands(val setMotorSpeed: Long, val setStepMotorResolution: String) {

    // static factory to create `MotorCommands` from `Config` object
    // Ex. MotorCommands.from(config)
    companion object {
        fun from(wfosCommandsConfig: Config): MotorCommands {
            val motorCommandsConfig: Config = wfosCommandsConfig.getConfig("wfos.motor.commands")
            return MotorCommands(
                    motorCommandsConfig.getLong("set-motor-speed"),
                    motorCommandsConfig.getString("set-step-motor-resolution")
            )
        }
    }
}

Source code for examples