Skip to content
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

8 using external file #10

Merged
merged 11 commits into from
Sep 8, 2024
Merged
28 changes: 26 additions & 2 deletions sierra_status/src/usb_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ def send_at_command(
if "OK\r\n" in result or "ERROR\r\n" in result:
break
animate_spinner()

except serial.SerialException as e:
logging.error(f"Serial communication error: {e}")
except ValueError as e:
Expand Down Expand Up @@ -125,6 +124,13 @@ def get_em_cops(port: str, baudrate: int = DEFAULT_BAUDRATE) -> str:
def creat_status_file(result: str, model: str) -> None:
"""
Creates a status file with the provided result.

Args:
result (str): The status information to be written to the file.
model (str): The model of the module.

Returns:
None
"""
try:
time_stamp = time.strftime("%Y%m%d_%H%M%S", time.localtime())
Expand All @@ -141,15 +147,33 @@ def start_process(
) -> None:
"""
Main function to retrieve the status of an EM9xxx module using AT commands.

Args:
port (str): The serial port to use.
model (str): The model of the module.
log_level (int): The logging level to use.
search (int): The search parameter to use.
baudrate (int, optional): The baud rate to use for the serial connection. Defaults to the DEFAULT_BAUDRATE.

Returns:
None
"""
start_time = time.time()
logging.basicConfig(
level=log_level, format="%(asctime)s - %(levelname)s - %(message)s"
)
logging.info(
f"Starting process for port {port} with model {model} and baudrate {baudrate}"
f"""Start time: {time.strftime('%Y-%m-%d_%H:%M:%S', time.localtime())}
Starting process for port {port}
with model {model} and baudrate {baudrate}"""
)
result = get_module_status(port, search, model, baudrate)
if result:
time_stamp = time.strftime("%Y%m%d_%H%M%S", time.localtime())
result = f"Finished time: {time_stamp}\n" + result
creat_status_file(result, model)
else:
logging.error("No result received from the module.")
logging.info(
f"Total time for running this script: {time.time() - start_time:.2f} seconds"
)
7 changes: 5 additions & 2 deletions tests/test_usb_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,15 @@ def test_creat_status_file(self, mock_strftime, mock_file) -> None:

@patch("sierra_status.src.usb_handle.get_module_status")
@patch("sierra_status.src.usb_handle.creat_status_file")
@patch("sierra_status.src.usb_handle.time.strftime")
def test_start_process_with_result(
self, mock_creat_status_file, mock_get_module_status
self, mock_strftime, mock_creat_status_file, mock_get_module_status
) -> None:
mock_get_module_status.return_value = "Test Status"
mock_strftime.return_value = "20230101_120000"
start_process(self.mock_port, "TestModel", logging.INFO, 0)
mock_creat_status_file.assert_called_with("Test Status", "TestModel")
expected_result = "Finished time: 20230101_120000\nTest Status"
mock_creat_status_file.assert_called_with(expected_result, "TestModel")

@patch("sierra_status.src.usb_handle.get_module_status")
@patch("sierra_status.src.usb_handle.creat_status_file")
Expand Down