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 or not the file exists.
- Kotlin
-
val 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:
- Retrieve a configuration file from Configuration Service
- Fail/Terminate script if configuration file does not exist
- Parse retrieved configuration file and convert it to
MotorCommands
domain model - When Sequencer receives
set-motor-speed
command, then submitset-speed
command to downstream motor HCD - When Sequencer receives
rotate-motor
command, then sendset-resolution
command to downstream motor HCD
- Kotlin
-
val 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
-
/** * ======== 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") ) } } }