Skip to content

Command validator returns true, false, nil #1561

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,28 @@

class InstCmdValidator < OpenC3::CommandValidator
def pre_check(command)
# Record the current value of CMD_ACPT_CNT for comparison in post_check
@cmd_acpt_cnt = tlm("<%= target_name %> HEALTH_STATUS CMD_ACPT_CNT")
return [true, nil]
end

def post_check(command)
if command.packet_name == 'TIME_OFFSET'
# This is just an example of how to return a failure with a message
# Return Failure with a message
return [false, 'TIME_OFFSET failure description']
end
if command.packet_name == 'MEMLOAD'
# Return Unknown with a message
return [nil, 'MEMLOAD validation unknown']
end
if command.packet_name == 'CLEAR'
wait_check("<%= target_name %> HEALTH_STATUS CMD_ACPT_CNT == 0", 10)
# Return Success with a message
return [true, "CMD_ACPT_CNT cleared"]
else
wait_check("<%= target_name %> HEALTH_STATUS CMD_ACPT_CNT > #{@cmd_acpt_cnt}", 10)
# Return Success without a message
return [true, nil]
end
return [true, nil]
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,25 @@

class Inst2CmdValidator(CommandValidator):
def pre_check(self, command):
# Record the current value of CMD_ACPT_CNT for comparison in post_check
self.cmd_acpt_cnt = tlm("<%= target_name %> HEALTH_STATUS CMD_ACPT_CNT")
return [True, None]

def post_check(self, command):
if command.packet_name == "TIME_OFFSET":
# This is just an example of how to return a failure with a message
# Return Failure with a message
return [False, "TIME_OFFSET failure description"]
if command.packet_name == "MEMLOAD":
# Return Unknown with a message
return [None, "MEMLOAD validation unknown"]
if command.packet_name == "CLEAR":
wait_check("<%= target_name %> HEALTH_STATUS CMD_ACPT_CNT == 0", 10)
# Return Success with a message
return [True, "CMD_ACPT_CNT cleared"]
else:
wait_check(
f"<%= target_name %> HEALTH_STATUS CMD_ACPT_CNT > {self.cmd_acpt_cnt}",
10,
)
return [True, None]
# Return Success without a message
return [True, None]
6 changes: 4 additions & 2 deletions openc3/lib/openc3/microservices/interface_microservice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ def run
result = false
reason = e.message
end
unless result
# Explicitly check for false to allow nil to represent unknown
if result == false
message = "pre_check returned false for #{msg_hash['cmd_string']} due to #{reason}"
raise WriteRejectError.new(message)
end
Expand All @@ -239,7 +240,8 @@ def run
CommandTopic.write_packet(command, scope: @scope)
InterfaceStatusModel.set(@interface.as_json(:allow_nan => true), queued: true, scope: @scope)

unless result
# Explicitly check for false to allow nil to represent unknown
if result == false
message = "post_check returned false for #{msg_hash['cmd_string']} due to #{reason}"
raise WriteRejectError.new(message)
end
Expand Down
4 changes: 4 additions & 0 deletions openc3/lib/openc3/packets/command_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ def initialize(command = nil)
end

def pre_check(command)
# Return true to indicate Success, false to indicate Failure,
# and nil to indicate Unknown. The second value is the optional message.
return [true, nil]
end

def post_check(command)
# Return true to indicate Success, false to indicate Failure,
# and nil to indicate Unknown. The second value is the optional message.
return [true, nil]
end
end
Expand Down
4 changes: 4 additions & 0 deletions openc3/python/openc3/packets/command_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ def __init__(self, command=None):
self.args = []

def pre_check(self, command):
# Return True to indicate Success, False to indicate Failure,
# and None to indicate Unknown. The second value is the optional message.
return [True, None]

def post_check(self, command):
# Return True to indicate Success, False to indicate Failure,
# and None to indicate Unknown. The second value is the optional message.
return [True, None]

def args(self):
Expand Down
Loading