Helper Functions for checking logged creation errors, starting the network, etc.
import os
from pathlib import Path
import sys
def check_errors(rsi_object):
"""
Check for errors in the given rsi_object and print any errors that are found. If the error log contains any errors (not just warnings), raises an exception with the error log as the message.
Returns a tuple containing a boolean indicating whether the error log contained any errors and the error log string.
"""
error_string_builder = ""
i = rsi_object.ErrorLogCountGet()
while rsi_object.ErrorLogCountGet() > 0:
error:RapidCode.RsiError = rsi_object.ErrorLogGet()
error_type = "WARNING" if error.isWarning else "ERROR"
error_string_builder += f"{error_type}: {error.text}\n"
if len(error_string_builder) > 0:
print(error_string_builder)
if "ERROR" in error_string_builder:
raise Exception(error_string_builder)
return "ERROR" in error_string_builder, error_string_builder
def start_the_network(controller):
"""
Attempts to start the network using the given MotionController object. If the network fails to start, it reads and prints any log messages that may be helpful in determining the cause of the problem, and then raises an RsiError exception.
"""
if controller.network_state_get() != RapidCode.RSINetworkState.RSINetworkStateOPERATIONAL:
print("Starting Network..")
controller.network_start()
if controller.network_state_get() != RapidCode.RSINetworkState.RSINetworkStateOPERATIONAL:
messages_to_read = controller.network_log_message_count_get()
for i in range(messages_to_read):
print(controller.network_log_message_get(i))
print("Expected OPERATIONAL state but the network did not get there.")
else:
print("Network Started")
def find_rapid_code_directory(start_directory=os.path.dirname(os.path.abspath(__file__))):
"""
Attempts find the install directory of RapidCode.
"""
start_path=Path(start_directory)
likely_rapidcode_path_1 = start_path
likely_rapidcode_path_2 = start_path.parent.parent.absolute()
likely_rapidcode_path_3 = start_path.parent.parent.parent.absolute() / "Release"
likely_rapidcode_path_4 = start_path.parent.parent.absolute()
try:
file_name = "RapidCode.py"
if file_name in os.listdir(likely_rapidcode_path_1):
rapidcode_dir = likely_rapidcode_path_1
elif(file_name in os.listdir(likely_rapidcode_path_2)):
rapidcode_dir=likely_rapidcode_path_2
elif(file_name in os.listdir(likely_rapidcode_path_3)):
rapidcode_dir=likely_rapidcode_path_3
elif(file_name in os.listdir(likely_rapidcode_path_4)):
rapidcode_dir=likely_rapidcode_path_4
else:
raise Exception("RapidCode search path exhausted.")
except:
raise Exception("Could not find RapidCode Directory. Try entering the path manually likely C:/RSI/X.X.X")
return str(rapidcode_dir)