Scripting API Guide
This document provides the information necessary to write test procedures using the COSMOS scripting API. Scripting in COSMOS is designed to be simple and intuitive. The code completion ability for command and telemetry mnemonics makes Script Runner the ideal place to write your procedures, however any text editor will do. If there is functionality that you don't see here or perhaps an easier syntax for doing something, please submit a ticket.
Concepts
Programming Languages
COSMOS scripting is implemented using either Ruby or Python. Ruby and Python are very similar scripting languages and in many cases the COSMOS APIs are identical between the two. This guide is written to support both with additional language specific information found in the Script Writing Guide.
Using Script Runner
Script Runner is a graphical application that provides the ideal environment for running and implementing your test procedures. The Script Runner tool is broken into 4 main sections. At the top of the tool is a menu bar that allows you to do such things as open and save files, perform a syntax check, and execute your script.
Next is a tool bar that displays the currently executing script and three buttons, "Start/Go", "Pause/Retry", and "Stop". The Start/Go button is used to start the script and continue past errors or waits. The Pause/Retry button will pause the executing script. If an error is encountered the Pause button changes to Retry to re-execute the errored line. Finally, the Stop button will stop the executing script at any time.
Third is the display of the actual script. While the script is not running, you may edit and compose scripts in this area. A handy code completion feature is provided that will list out the available commands or telemetry points as you are writing your script. Simply begin writing a cmd( or tlm( line to bring up code completion. This feature greatly reduces typos in command and telemetry mnemonics.
Finally, the bottom of the display is the log messages. All commands that are sent, errors that occur, and user print statements appear in this area.
Telemetry Types
There are four different ways that telemetry values can be retrieved in COSMOS. The following chart explains their differences.
Telemetry Type | Description |
---|---|
Raw | Raw telemetry is exactly as it is in the telemetry packet before any conversions. All telemetry items will have a raw value except for Derived telemetry points which have no real location in a packet. Requesting raw telemetry on a derived item will return nil. |
Converted | Converted telemetry is raw telemetry that has gone through a conversion factor such as a state conversion or a polynomial conversion. If a telemetry item does not have a conversion defined, then converted telemetry will be the same as raw telemetry. This is the most common type of telemety used in scripts. |
Formatted | Formatted telemetry is converted telemetry that has gone through a printf style conversion into a string. Formatted telemetry will always have a string representation. If no format string is defined for a telemetry point, then formatted telemetry will be the same as converted telemetry except represented as string. |
Formatted with Units | Formatted with Units telemetry is the same as Formatted telemetry except that a space and the units of the telemetry item are appended to the end of the string. If no units are defined for a telemetry item then this type is the same as Formatted telemetry. |
Script Runner API
The following methods are designed to be used in Script Runner procedures. Many can also be used in custom built COSMOS tools. Please see the COSMOS Tool API section for methods that are more efficient to use in custom tools.
Migration from COSMOS v5 to v6
The following API methods have been removed from COSMOS v6. Most of the deprecated API methods still remain for backwards compatibility.
Method | Tool | Status |
---|---|---|
get_all_target_info | Command and Telemetry Server | Removed, use get_target_interfaces |
play_wav_file | Script Runner | Removed |
status_bar | Script Runner | Removed |
Migration from COSMOS v4 to v5
The following API methods are either deprecated (will not be ported to COSMOS 5) or currently unimplemented (eventually will be ported to COSMOS 5):
Method | Tool | Status |
---|---|---|
clear | Telemetry Viewer | Deprecated, use clear_screen |
clear_all | Telemetry Viewer | Deprecated, use clear_all_screens |
close_local_screens | Telemetry Viewer | Deprecated, use clear_screen |
clear_disconnected_targets | Script Runner | Deprecated |
cmd_tlm_clear_counters | Command and Telemetry Server | Deprecated |
cmd_tlm_reload | Command and Telemetry Server | Deprecated |
display | Telemetry Viewer | Deprecated, use display_screen |
get_all_packet_logger_info | Command and Telemetry Server | Deprecated |
get_all_target_info | Command and Telemetry Server | Deprecated, use get_target_interfaces |
get_background_tasks | Command and Telemetry Server | Deprecated |
get_all_cmd_info | Command and Telemetry Server | Deprecated, use get_all_cmds |
get_all_tlm_info | Command and Telemetry Server | Deprecated, use get_all_tlm |
get_cmd_list | Command and Telemetry Server | Deprecated, use get_all_cmds |
get_cmd_log_filename | Command and Telemetry Server | Deprecated |
get_cmd_param_list | Command and Telemetry Server | Deprecated, use get_cmd |
get_cmd_tlm_disconnect | Script Runner | Deprecated, use $disconnect |
get_disconnected_targets | Script Runner | Unimplemented |
get_interface_info | Command and Telemetry Server | Deprecated, use get_interface |
get_interface_targets | Command and Telemetry Server | Deprecated |
get_output_logs_filenames | Command and Telemetry Server | Deprecated |
get_packet | Command and Telemetry Server | Deprecated, use get_packets |
get_packet_data | Command and Telemetry Server | Deprecated, use get_packets |
get_packet_logger_info | Command and Telemetry Server | Deprecated |
get_packet_loggers | Command and Telemetry Server | Deprecated |
get_replay_mode | Replay | Deprecated |
get_router_info | Command and Telemetry Server | Deprecated, use get_router |
get_scriptrunner_message_log_filename | Command and Telemetry Server | Deprecated |
get_server_message | Command and Telemetry Server | Deprecated |
get_server_message_log_filename | Command and Telemetry Server | Deprecated |
get_server_status | Command and Telemetry Server | Deprecated |
get_stale | Command and Telemetry Server | Deprecated |
get_target_ignored_items | Command and Telemetry Server | Deprecated, use get_target |
get_target_ignored_parameters | Command and Telemetry Server | Deprecated, use get_target |
get_target_info | Command and Telemetry Server | Deprecated, use get_target |
get_target_list | Command and Telemetry Server | Deprecated, use get_target_names |
get_tlm_details | Command and Telemetry Server | Deprecated |
get_tlm_item_list | Command and Telemetry Server | Deprecated |
get_tlm_list | Command and Telemetry Server | Deprecated |
get_tlm_log_filename | Command and Telemetry Server | Deprecated |
interface_state | Command and Telemetry Server | Deprecated, use get_interface |
override_tlm_raw | Command and Telemetry Server | Deprecated, use override_tlm |
open_directory_dialog | Script Runner | Deprecated |
play_wav_file | Script Runner | Deprecated |
replay_move_end | Replay | Deprecated |
replay_move_index | Replay | Deprecated |
replay_move_start | Replay | Deprecated |
replay_play | Replay | Deprecated |
replay_reverse_play | Replay | Deprecated |
replay_select_file | Replay | Deprecated |
replay_set_playback_delay | Replay | Deprecated |
replay_status | Replay | Deprecated |
replay_step_back | Replay | Deprecated |
replay_step_forward | Replay | Deprecated |
replay_stop | Replay | Deprecated |
require_utility | Script Runner | Deprecated but exists for backwards compatibility, use load_utility |
router_state | Command and Telemetry Server | Deprecated, use get_router |
save_file_dialog | Script Runner | Deprecated |
save_setting | Command and Telemetry Server | Deprecated but exists for backwards compatibility, use set_setting |
set_cmd_tlm_disconnect | Script Runner | Deprecated, use disconnect_script |
set_disconnected_targets | Script Runner | Unimplemented |
set_replay_mode | Replay | Deprecated |
set_stdout_max_lines | Script Runner | Deprecated |
set_tlm_raw | Script Runner | Deprecated, use set_tlm |
show_backtrace | Script Runner | Deprecated, backtrace always shown |
status_bar | Script Runner | Deprecated |
shutdown_cmd_tlm | Command and Telemetry Server | Deprecated |
start_cmd_log | Command and Telemetry Server | Deprecated |
start_logging | Command and Telemetry Server | Deprecated |
start_new_scriptrunner_message_log | Command and Telemetry Server | Deprecated |
start_new_server_message_log | Command and Telemetry Server | Deprecated |
start_tlm_log | Command and Telemetry Server | Deprecated |
stop_background_task | Command and Telemetry Server | Deprecated |
stop_cmd_log | Command and Telemetry Server | Deprecated |
stop_logging | Command and Telemetry Server | Deprecated |
stop_tlm_log | Command and Telemetry Server | Deprecated |
subscribe_limits_events | Command and Telemetry Server | Deprecated |
subscribe_packet_data | Command and Telemetry Server | Deprecated, use subscribe_packets |
subscribe_server_messages | Command and Telemetry Server | Unimplemented |
tlm_variable | Script Runner | Deprecated, use tlm() and pass type |
unsubscribe_limits_events | Command and Telemetry Server | Deprecated |
unsubscribe_packet_data | Command and Telemetry Server | Deprecated |
unsubscribe_server_messages | Command and Telemetry Server | Deprecated |
wait_raw | Script Runner | Deprecated, use wait(..., type: :RAW) |
wait_check_raw | Script Runner | Deprecated, use wait_check(..., type: :RAW) |
wait_tolerance_raw | Script Runner | Deprecated, use wait_tolerance(..., type: :RAW) |
wait_check_tolerance_raw | Script Runner | Deprecated, use wait_check_tolerance(..., type: :RAW) |
Retrieving User Input
These methods allow the user to enter values that are needed by the script.
ask
Prompts the user for input with a question. User input is automatically converted from a string to the appropriate data type. For example if the user enters "1", the number 1 as an integer will be returned.
Ruby / Python Syntax:
ask("<question>", <blank_or_default>, <password>)
Parameter | Description |
---|---|
question | Question to prompt the user with. |
blank_or_default | Whether or not to allow empty responses (optional - defaults to false). If a non-boolean value is passed it is used as a default value. |
password | Whether to treat the entry as a password which is displayed with dots and not logged. Default is false. |
Ruby Example:
value = ask("Enter an integer")
value = ask("Enter a value or nothing", true)
value = ask("Enter a value", 10)
password = ask("Enter your password", false, true)
Python Example:
value = ask("Enter an integer")
value = ask("Enter a value or nothing", True)
value = ask("Enter a value", 10)
password = ask("Enter your password", False, True)
ask_string
Prompts the user for input with a question. User input is always returned as a string. For example if the user enters "1", the string "1" will be returned.
Ruby / Python Syntax:
ask_string("<question>", <blank_or_default>, <password>)
Parameter | Description |
---|---|
question | Question to prompt the user with. |
blank_or_default | Whether or not to allow empty responses (optional - defaults to false). If a non-boolean value is passed it is used as a default value. |
password | Whether to treat the entry as a password which is displayed with dots and not logged. Default is false. |
Ruby Example:
string = ask_string("Enter a String")
string = ask_string("Enter a value or nothing", true)
string = ask_string("Enter a value", "test")
password = ask_string("Enter your password", false, true)
Python Example:
string = ask_string("Enter a String")
string = ask_string("Enter a value or nothing", True)
string = ask_string("Enter a value", "test")
password = ask_string("Enter your password", False, True)
message_box
vertical_message_box
combo_box
The message_box, vertical_message_box, and combo_box methods create a message box with arbitrary buttons or selections that the user can click. The text of the button clicked is returned.
Ruby / Python Syntax:
message_box("<message>", "<button text 1>", ...)
vertical_message_box("<message>", "<button text 1>", ...)
combo_box("<message>", "<selection text 1>", ...)
Parameter | Description |
---|---|
message | Message to prompt the user with. |
button/selection text | Text for a button or selection |
Ruby Example:
value = message_box("Select the sensor number", 'One', 'Two')
value = vertical_message_box("Select the sensor number", 'One', 'Two')
value = combo_box("Select the sensor number", 'One', 'Two')
case value
when 'One'
puts 'Sensor One'
when 'Two'
puts 'Sensor Two'
end
Python Example:
value = message_box("Select the sensor number", 'One', 'Two')
value = vertical_message_box("Select the sensor number", 'One', 'Two')
value = combo_box("Select the sensor number", 'One', 'Two')
match value:
case 'One':
print('Sensor One')
case 'Two':
print('Sensor Two')
get_target_file
Return a file handle to a file in the target directory
Ruby Syntax:
get_target_file("<File Path>", original: false)
Python Syntax:
get_target_file("<File Path>", original=False)
Parameter | Description |
---|---|
path | The path to the file in the target directory. Should assume to start with a TARGET name, e.g. INST/procedures/proc.rb |
original | Whether to get the original file from the plug-in, or any modifications to the file. Default is false which means to grab the modified file. If the modified file does not exist the API will automatically try to pull the original. |
Ruby Example:
file = get_target_file("INST/data/attitude.bin")
puts file.read().formatted # format a binary file
file.unlink # delete file
file = get_target_file("INST/procedures/checks.rb", original: true)
puts file.read()
file.unlink # delete file
Python Example:
from openc3.utilities.string import formatted
file = get_target_file("INST/data/attitude.bin")
print(formatted(file.read())) # format a binary file
file.close() # delete file
file = get_target_file("INST/procedures/checks.rb", original=True)
print(file.read())
file.close() # delete file