Skip to content

Commit

Permalink
Merge pull request #1039 from OpenC3/ensure_disconnect
Browse files Browse the repository at this point in the history
Ensure disconnect called at least once on interface.
  • Loading branch information
ryanmelt authored Jan 13, 2024
2 parents c313ed1 + c75c399 commit 740ba0a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 17 deletions.
16 changes: 9 additions & 7 deletions openc3/lib/openc3/interfaces/interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,16 @@ def connect
log_dont_log.upcase!
period = "#{period.to_f}s"
@scheduler.every period do
begin
if log_dont_log == 'DONT_LOG'
cmd(cmd_string, log_message: false)
else
cmd(cmd_string)
if connected?()
begin
if log_dont_log == 'DONT_LOG'
cmd(cmd_string, log_message: false)
else
cmd(cmd_string)
end
rescue Exception => err
Logger.error("Error sending periodic cmd(#{cmd_string}):\n#{err.formatted}")
end
rescue Exception => err
Logger.error("Error sending periodic cmd(#{cmd_string}):\n#{err.formatted}")
end
end
end
Expand Down
11 changes: 10 additions & 1 deletion openc3/lib/openc3/microservices/interface_microservice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,16 @@ def handle_connection_lost(err = nil, reconnect: true)

def connect
@logger.info "#{@interface.name}: Connecting ..."
@interface.connect
begin
@interface.connect
rescue Exception => error
begin
@interface.disconnect # Ensure disconnect is called at least once on a partial connect
rescue Exception
# We want to report any connect errors, not disconnect in this case
end
raise error
end
@interface.state = 'CONNECTED'
if @interface_or_router == 'INTERFACE'
InterfaceStatusModel.set(@interface.as_json(:allow_nan => true), scope: @scope)
Expand Down
19 changes: 10 additions & 9 deletions openc3/python/openc3/interfaces/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,15 +487,16 @@ def protocol_cmd(self, cmd_name, *cmd_args, read_write="READ_WRITE", index=-1):
return handled

def run_periodic_cmd(self, log_dont_log, cmd_string):
try:
if log_dont_log == "DONT_LOG":
cmd(cmd_string, log_message=False)
else:
cmd(cmd_string)
except Exception as error:
Logger.error(
f"Error sending periodic cmd({cmd_string}):\n{traceback.format_exception(error)}"
)
if self.connected():
try:
if log_dont_log == "DONT_LOG":
cmd(cmd_string, log_message=False)
else:
cmd(cmd_string)
except Exception as error:
Logger.error(
f"Error sending periodic cmd({cmd_string}):\n{traceback.format_exception(error)}"
)

def scheduler_thread_body(self):
next_time = time.time()
Expand Down
10 changes: 10 additions & 0 deletions openc3/python/openc3/microservices/interface_microservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,16 @@ def handle_connection_lost(self, error=None, reconnect=True):

def connect(self):
self.logger.info(f"{self.interface.name}: Connecting :")

try:
self.interface.connect()
except RuntimeError as error:
try:
self.interface.disconnect() # Ensure disconnect is called at least once on a partial connect
except RuntimeError:
pass # We want to report any connect errors, not disconnect in this case
raise error

self.interface.connect()
self.interface.state = "CONNECTED"
if self.interface_or_router == "INTERFACE":
Expand Down

0 comments on commit 740ba0a

Please sign in to comment.