Skip to content

Commit

Permalink
Merge pull request #931 from OpenC3/messages
Browse files Browse the repository at this point in the history
Remove Logger from script files. Fix cmd log_message param
  • Loading branch information
jmthomas authored Nov 14, 2023
2 parents 4a4a534 + 79b1f3b commit 4083e01
Show file tree
Hide file tree
Showing 15 changed files with 445 additions and 416 deletions.
18 changes: 18 additions & 0 deletions docs.openc3.com/docs/guides/scripting-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,8 @@ cmd("<Target Name>", "<Command Name>", {"Param #1 Name": <Param #1 Value>, "Para
| Command Name | Name of this command. Also referred to as its mnemonic. |
| Param #x Name | Name of a command parameter. If there are no parameters then the 'with' keyword should not be given. |
| Param #x Value | Value of the command parameter. Values are automatically converted to the appropriate type. |
| timeout | Optional named parameter to change the default timeout value of 5 seconds |
| log_message | Optional named parameter to prevent logging of the command |

Ruby Example:

Expand All @@ -470,13 +472,15 @@ cmd("INST COLLECT with DURATION 10, TYPE NORMAL")
# In Ruby the brackets around parameters are optional
cmd("INST", "COLLECT", "DURATION" => 10, "TYPE" => "NORMAL")
cmd("INST", "COLLECT", { "DURATION" => 10, "TYPE" => "NORMAL" })
cmd("INST ABORT", timeout: 10, log_message: false)
```

Python Example:

```python
cmd("INST COLLECT with DURATION 10, TYPE NORMAL")
cmd("INST", "COLLECT", { "DURATION": 10, "TYPE": "NORMAL" })
cmd("INST ABORT", timeout=10, log_message=False)
```

### cmd_no_range_check
Expand All @@ -503,6 +507,8 @@ cmd_no_range_check("<Target Name>", "<Command Name>", {"Param #1 Name": <Param #
| Command Name | Name of this command. Also referred to as its mnemonic. |
| Param #x Name | Name of a command parameter. If there are no parameters then the 'with' keyword should not be given. |
| Param #x Value | Value of the command parameter. Values are automatically converted to the appropriate type. |
| timeout | Optional named parameter to change the default timeout value of 5 seconds |
| log_message | Optional named parameter to prevent logging of the command |

Ruby Example:

Expand Down Expand Up @@ -542,6 +548,8 @@ cmd_no_hazardous_check("<Target Name>", "<Command Name>", {"Param #1 Name": <Par
| Command Name | Name of this command. Also referred to as its mnemonic. |
| Param #x Name | Name of a command parameter. If there are no parameters then the 'with' keyword should not be given. |
| Param #x Value | Value of the command parameter. Values are automatically converted to the appropriate type. |
| timeout | Optional named parameter to change the default timeout value of 5 seconds |
| log_message | Optional named parameter to prevent logging of the command |

Ruby / Python Example:

Expand Down Expand Up @@ -574,6 +582,8 @@ cmd_no_checks("<Target Name>", "<Command Name>", {"Param #1 Name": <Param #1 Val
| Command Name | Name of this command. Also referred to as its mnemonic. |
| Param #x Name | Name of a command parameter. If there are no parameters then the 'with' keyword should not be given. |
| Param #x Value | Value of the command parameter. Values are automatically converted to the appropriate type. |
| timeout | Optional named parameter to change the default timeout value of 5 seconds |
| log_message | Optional named parameter to prevent logging of the command |

Ruby Example:

Expand Down Expand Up @@ -613,6 +623,8 @@ cmd_raw("<Target Name>", "<Command Name>", {"<Param #1 Name>": <Param #1 Value>,
| Command Name | Name of this command. Also referred to as its mnemonic. |
| Param #x Name | Name of a command parameter. If there are no parameters then the 'with' keyword should not be given. |
| Param #x Value | Value of the command parameter. Values are automatically converted to the appropriate type. |
| timeout | Optional named parameter to change the default timeout value of 5 seconds |
| log_message | Optional named parameter to prevent logging of the command |

Ruby Example:

Expand Down Expand Up @@ -652,6 +664,8 @@ cmd_raw_no_range_check("<Target Name>", "<Command Name>", {"<Param #1 Name>": <P
| Command Name | Name of this command. Also referred to as its mnemonic. |
| Param #x Name | Name of a command parameter. If there are no parameters then the 'with' keyword should not be given. |
| Param #x Value | Value of the command parameter. Values are automatically converted to the appropriate type. |
| timeout | Optional named parameter to change the default timeout value of 5 seconds |
| log_message | Optional named parameter to prevent logging of the command |

Ruby Example:

Expand Down Expand Up @@ -691,6 +705,8 @@ cmd_raw_no_hazardous_check("<Target Name>", "<Command Name>", {"<Param #1 Name>"
| Command Name | Name of this command. Also referred to as its mnemonic. |
| Param #x Name | Name of a command parameter. If there are no parameters then the 'with' keyword should not be given. |
| Param #x Value | Value of the command parameter. Values are automatically converted to the appropriate type. |
| timeout | Optional named parameter to change the default timeout value of 5 seconds |
| log_message | Optional named parameter to prevent logging of the command |

Ruby / Python Example:

Expand Down Expand Up @@ -723,6 +739,8 @@ cmd_raw_no_checks("<Target Name>", "<Command Name>", {"<Param #1 Name>": <Param
| Command Name | Name of this command. Also referred to as its mnemonic. |
| Param #x Name | Name of a command parameter. If there are no parameters then the 'with' keyword should not be given. |
| Param #x Value | Value of the command parameter. Values are automatically converted to the appropriate type. |
| timeout | Optional named parameter to change the default timeout value of 5 seconds |
| log_message | Optional named parameter to prevent logging of the command |

Ruby Example:

Expand Down
62 changes: 31 additions & 31 deletions openc3/lib/openc3/script/api_shared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def check_exception(method_name, *args, **kwargs)
method += ", #{orig_kwargs}" unless orig_kwargs.empty?
method += ")"
rescue Exception => error
Logger.info "CHECK: #{method} raised #{error.class}:#{error.message}"
puts "CHECK: #{method} raised #{error.class}:#{error.message}"
else
raise(CheckError, "#{method} should have raised an exception but did not.")
end
Expand Down Expand Up @@ -107,10 +107,10 @@ def check_tolerance(*args, type: :CONVERTED, scope: $openc3_scope, token: $openc
end

if all_checks_ok
Logger.info message
puts message
else
if $disconnect
Logger.error message
puts "ERROR: #{message}"
else
raise CheckError, message
end
Expand All @@ -120,11 +120,11 @@ def check_tolerance(*args, type: :CONVERTED, scope: $openc3_scope, token: $openc
check_str = "CHECK: #{_upcase(target_name, packet_name, item_name)}"
range_str = "range #{range.first} to #{range.last} with value == #{value}"
if range.include?(value)
Logger.info "#{check_str} was within #{range_str}"
puts "#{check_str} was within #{range_str}"
else
message = "#{check_str} failed to be within #{range_str}"
if $disconnect
Logger.error message
puts "ERROR: #{message}"
else
raise CheckError, message
end
Expand All @@ -142,11 +142,11 @@ def check_tolerance_raw(*args, scope: $openc3_scope, token: $openc3_token)
def check_expression(exp_to_eval, context = nil, scope: $openc3_scope, token: $openc3_token)
success = _openc3_script_wait_implementation_expression(exp_to_eval, 0, DEFAULT_TLM_POLLING_RATE, context, scope: scope, token: token)
if success
Logger.info "CHECK: #{exp_to_eval} is TRUE"
puts "CHECK: #{exp_to_eval} is TRUE"
else
message = "CHECK: #{exp_to_eval} is FALSE"
if $disconnect
Logger.error message
puts "ERROR: #{message}"
else
raise CheckError, message
end
Expand All @@ -171,15 +171,15 @@ def wait(*args, type: :CONVERTED, quiet: false, scope: $openc3_scope, token: $op
start_time = Time.now.sys
openc3_script_sleep()
time_diff = Time.now.sys - start_time
Logger.info("WAIT: Indefinite for actual time of #{time_diff} seconds") unless quiet
puts "WAIT: Indefinite for actual time of #{time_diff} seconds" unless quiet

# wait(5) # absolute wait time
when 1
if args[0].kind_of? Numeric
start_time = Time.now.sys
openc3_script_sleep(args[0])
time_diff = Time.now.sys - start_time
Logger.info("WAIT: #{args[0]} seconds with actual time of #{time_diff} seconds") unless quiet
puts "WAIT: #{args[0]} seconds with actual time of #{time_diff} seconds" unless quiet
else
raise "Non-numeric wait time specified"
end
Expand Down Expand Up @@ -254,9 +254,9 @@ def wait_tolerance(*args, type: :CONVERTED, quiet: false, scope: $openc3_scope,
end

if success
Logger.info message unless quiet
puts message unless quiet
else
Logger.warn message unless quiet
puts "WARN: #{message}" unless quiet
end
else
success, value = _openc3_script_wait_implementation_tolerance(target_name, packet_name, item_name, type, expected_value, tolerance, timeout, polling_rate, scope: scope, token: token)
Expand All @@ -265,9 +265,9 @@ def wait_tolerance(*args, type: :CONVERTED, quiet: false, scope: $openc3_scope,
wait_str = "WAIT: #{_upcase(target_name, packet_name, item_name)}"
range_str = "range #{range.first} to #{range.last} with value == #{value} after waiting #{time} seconds"
if success
Logger.info "#{wait_str} was within #{range_str}" unless quiet
puts "#{wait_str} was within #{range_str}" unless quiet
else
Logger.warn "#{wait_str} failed to be within #{range_str}" unless quiet
puts "WARN: #{wait_str} failed to be within #{range_str}" unless quiet
end
end
time
Expand All @@ -284,9 +284,9 @@ def wait_expression(exp_to_eval, timeout, polling_rate = DEFAULT_TLM_POLLING_RAT
success = _openc3_script_wait_implementation_expression(exp_to_eval, timeout, polling_rate, context, scope: scope, token: token)
time_diff = Time.now.sys - start_time
if success
Logger.info "WAIT: #{exp_to_eval} is TRUE after waiting #{time_diff} seconds" unless quiet
puts "WAIT: #{exp_to_eval} is TRUE after waiting #{time_diff} seconds" unless quiet
else
Logger.warn "WAIT: #{exp_to_eval} is FALSE after waiting #{time_diff} seconds" unless quiet
puts "WARN: WAIT: #{exp_to_eval} is FALSE after waiting #{time_diff} seconds" unless quiet
end
time_diff
end
Expand All @@ -312,11 +312,11 @@ def wait_check(*args, type: :CONVERTED, scope: $openc3_scope, token: $openc3_tok
end
with_value_str = "with value == #{value} after waiting #{time_diff} seconds"
if success
Logger.info "#{check_str} success #{with_value_str}"
puts "#{check_str} success #{with_value_str}"
else
message = "#{check_str} failed #{with_value_str}"
if $disconnect
Logger.error message
puts "ERROR: #{message}"
else
raise CheckError, message
end
Expand Down Expand Up @@ -363,10 +363,10 @@ def wait_check_tolerance(*args, type: :CONVERTED, scope: $openc3_scope, token: $
end

if success
Logger.info message
puts message
else
if $disconnect
Logger.error message
puts "ERROR: #{message}"
else
raise CheckError, message
end
Expand All @@ -378,11 +378,11 @@ def wait_check_tolerance(*args, type: :CONVERTED, scope: $openc3_scope, token: $
check_str = "CHECK: #{_upcase(target_name, packet_name, item_name)}"
range_str = "range #{range.first} to #{range.last} with value == #{value} after waiting #{time_diff} seconds"
if success
Logger.info "#{check_str} was within #{range_str}"
puts "#{check_str} was within #{range_str}"
else
message = "#{check_str} failed to be within #{range_str}"
if $disconnect
Logger.error message
puts "ERROR: #{message}"
else
raise CheckError, message
end
Expand All @@ -409,11 +409,11 @@ def wait_check_expression(exp_to_eval,
context, scope: scope, token: token, &block)
time_diff = Time.now.sys - start_time
if success
Logger.info "CHECK: #{exp_to_eval} is TRUE after waiting #{time_diff} seconds"
puts "CHECK: #{exp_to_eval} is TRUE after waiting #{time_diff} seconds"
else
message = "CHECK: #{exp_to_eval} is FALSE after waiting #{time_diff} seconds"
if $disconnect
Logger.error message
puts "ERROR: #{message}"
else
raise CheckError, message
end
Expand Down Expand Up @@ -540,7 +540,7 @@ def _check(*args, scope: $openc3_scope, token: $openc3_token)
if comparison_to_eval
_check_eval(target_name, packet_name, item_name, comparison_to_eval, value)
else
Logger.info "CHECK: #{_upcase(target_name, packet_name, item_name)} == #{value}"
puts "CHECK: #{_upcase(target_name, packet_name, item_name)} == #{value}"
end
end

Expand Down Expand Up @@ -621,17 +621,17 @@ def _wait_packet(check,
value = 0 unless value
time_diff = Time.now.sys - start_time
if success
Logger.info "#{type}: #{target_name.upcase} #{packet_name.upcase} received #{value - initial_count} times after waiting #{time_diff} seconds" unless quiet
puts "#{type}: #{target_name.upcase} #{packet_name.upcase} received #{value - initial_count} times after waiting #{time_diff} seconds" unless quiet
else
message = "#{type}: #{target_name.upcase} #{packet_name.upcase} expected to be received #{num_packets} times but only received #{value - initial_count} times after waiting #{time_diff} seconds"
if check
if $disconnect
Logger.error message
puts "ERROR: #{message}"
else
raise CheckError, message
end
else
Logger.warn message unless quiet
puts "WARN: #{message}" unless quiet
end
end
time_diff
Expand All @@ -645,9 +645,9 @@ def _execute_wait(target_name, packet_name, item_name, value_type, comparison_to
wait_str = "WAIT: #{_upcase(target_name, packet_name, item_name)} #{comparison_to_eval}"
value_str = "with value == #{value} after waiting #{time_diff} seconds"
if success
Logger.info "#{wait_str} success #{value_str}" unless quiet
puts "#{wait_str} success #{value_str}" unless quiet
else
Logger.warn "#{wait_str} failed #{value_str}" unless quiet
puts "WARN: #{wait_str} failed #{value_str}" unless quiet
end
end

Expand Down Expand Up @@ -873,11 +873,11 @@ def _check_eval(target_name, packet_name, item_name, comparison_to_eval, value)
value_str = value.is_a?(String) ? "'#{value}'" : value
with_value = "with value == #{value_str}"
if eval(string)
Logger.info "#{check_str} success #{with_value}"
puts "#{check_str} success #{with_value}"
else
message = "#{check_str} failed #{with_value}"
if $disconnect
Logger.error message
puts "ERROR: #{message}"
else
raise CheckError, message
end
Expand Down
20 changes: 12 additions & 8 deletions openc3/lib/openc3/script/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ def _cmd_string(target_name, cmd_name, cmd_params, raw)
# NOTE: This is a helper method and should not be called directly
def _log_cmd(target_name, cmd_name, cmd_params, raw, no_range, no_hazardous)
if no_range
Logger.warn "Command #{target_name} #{cmd_name} being sent ignoring range checks"
puts "WARN: Command #{target_name} #{cmd_name} being sent ignoring range checks"
end
if no_hazardous
Logger.warn "Command #{target_name} #{cmd_name} being sent ignoring hazardous warnings"
puts "WARN: Command #{target_name} #{cmd_name} being sent ignoring hazardous warnings"
end
Logger.info _cmd_string(target_name, cmd_name, cmd_params, raw)
puts _cmd_string(target_name, cmd_name, cmd_params, raw)
end

def _cmd_disconnect(cmd, raw, no_range, no_hazardous, *args, scope: $openc3_scope)
Expand Down Expand Up @@ -108,7 +108,7 @@ def _cmd_disconnect(cmd, raw, no_range, no_hazardous, *args, scope: $openc3_scop
# Send the command and log the results
# This method signature has to include the keyword params present in cmd_api.rb cmd_implementation()
# NOTE: This is a helper method and should not be called directly
def _cmd(cmd, cmd_no_hazardous, *args, scope: $openc3_scope, token: $openc3_token, timeout: nil, **kwargs)
def _cmd(cmd, cmd_no_hazardous, *args, timeout: nil, log_message: nil, scope: $openc3_scope, token: $openc3_token, **kwargs)
extract_string_kwargs_to_args(args, kwargs)
raw = cmd.include?('raw')
no_range = cmd.include?('no_range') || cmd.include?('no_checks')
Expand All @@ -118,14 +118,18 @@ def _cmd(cmd, cmd_no_hazardous, *args, scope: $openc3_scope, token: $openc3_toke
_cmd_disconnect(cmd, raw, no_range, no_hazardous, *args, scope: scope)
else
begin
target_name, cmd_name, cmd_params = $api_server.method_missing(cmd, *args, scope: scope, token: token, timeout: timeout)
_log_cmd(target_name, cmd_name, cmd_params, raw, no_range, no_hazardous)
target_name, cmd_name, cmd_params = $api_server.method_missing(cmd, *args, timeout: timeout, log_message: log_message, scope: scope, token: token)
if log_message.nil? or log_message
_log_cmd(target_name, cmd_name, cmd_params, raw, no_range, no_hazardous)
end
rescue HazardousError => e
# This opens a prompt at which point they can cancel and stop the script
# or say Yes and send the command. Thus we don't care about the return value.
prompt_for_hazardous(e.target_name, e.cmd_name, e.hazardous_description)
target_name, cmd_name, cmd_params = $api_server.method_missing(cmd_no_hazardous, *args, scope: scope, token: token, timeout: timeout)
_log_cmd(target_name, cmd_name, cmd_params, raw, no_range, no_hazardous)
target_name, cmd_name, cmd_params = $api_server.method_missing(cmd_no_hazardous, *args, timeout: timeout, log_message: log_message, scope: scope, token: token)
if log_message.nil? or log_message
_log_cmd(target_name, cmd_name, cmd_params, raw, no_range, no_hazardous)
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion openc3/lib/openc3/script/limits.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module Script
define_method(method_name) do |*args, **kw_args, &block|
kw_args[:scope] = $openc3_scope unless kw_args[:scope]
if $disconnect
Logger.info "DISCONNECT: #{method_name}(#{args}) ignored"
puts "DISCONNECT: #{method_name}(#{args}) ignored"
else
$api_server.public_send(method_name, *args, **kw_args, &block)
end
Expand Down
Loading

0 comments on commit 4083e01

Please sign in to comment.