diff --git a/tmtc_core b/tmtc_core index c1575e2bf8eb6b0a8c906cb84a544e9f8c75784d..20d4d8a69a31ba7954d943c46176ae0cb9bc81ac 160000 --- a/tmtc_core +++ b/tmtc_core @@ -1 +1 @@ -Subproject commit c1575e2bf8eb6b0a8c906cb84a544e9f8c75784d +Subproject commit 20d4d8a69a31ba7954d943c46176ae0cb9bc81ac diff --git a/utility/obsw_binary_uploader.py b/utility/obsw_binary_uploader.py index fbd00cc53c8f77f16b2c04aae66a3f6b07a075f5..d27c23236231f3115e67d70a47072d72684271bb 100644 --- a/utility/obsw_binary_uploader.py +++ b/utility/obsw_binary_uploader.py @@ -6,13 +6,15 @@ This module will be used to upload binaries to the OBC via a communication port, a supplied binary. The binary will be sent via the specified communication interface. It will be possible to encode the data (for example using DLE encoding) """ +import os import time import tkinter as tk from tkinter import filedialog from collections import deque +from glob import glob from tmtc_core.comIF.obsw_com_interface import CommunicationInterface -from tc.obsw_tc_service23_sdcard import FileTransferHelper, generate_generic_folder_structure +from tc.obsw_tc_service23_sdcard import FileTransferHelper import config.obsw_config as g from tmtc_core.utility.obsw_tmtc_printer import TmTcPrinter, DisplayMode from tmtc_core.utility.obsw_logger import get_logger @@ -23,17 +25,46 @@ LOGGER = get_logger() def perform_file_upload(com_if: CommunicationInterface, tmtc_printer: TmTcPrinter, tm_listener: TmListener): + gui_cl_prompt = input("GUI(0) or command line version (1)? [0/1]: ") + if gui_cl_prompt == 0: + gui_cl_prompt = True + else: + gui_cl_prompt = False # TODO: prompt whether this is a binary upload or a normal file upload. Or use op code # two different commands to achieve the same. - print("Please select file to upload") - root = tk.Tk() - root.withdraw() - root.wm_attributes('-topmost', 1) - - # TODO: implement command line version which just parses the _bin folder, displays the options - # and asks for a selection. - file_path = filedialog.askopenfilename(parent=root) - print("File select: " + str(file_path)) + print("Please select file to upload: ") + file_path = "" + if gui_cl_prompt: + root = tk.Tk() + root.withdraw() + root.wm_attributes('-topmost', 1) + file_path = filedialog.askopenfilename(parent=root) + print("File select: " + str(file_path)) + if file_path == (): + LOGGER.warning("Invalid file path, exiting binary upload mode.") + return + else: + result = [y for x in os.walk("../_bin") for y in glob(os.path.join(x[0], '*.bin'))] + print("Files found in _bin folder: ") + for idx, path in enumerate(result): + print("Selection " + str(idx) + ": " + str(path)) + select_valid = False + selection = input("Please enter desired selection [c to cancel]: ") + while not select_valid: + if selection == 'c': + print("Exiting binary upload mode..") + return + if not selection.isdigit(): + selection = input("Invalid input, try again [c to cancel]: ") + if selection.isdigit(): + if int(selection) < len(result): + file_path = result[int(selection)] + select_valid = True + else: + selection = input("Invalid input, try again [c to cancel]: ") + + print_string = file_path.rsplit('/', 1)[-1] + " was selected." + LOGGER.info(print_string) calc_hamming_code = input("Calculate and send hamming code? [y/n]: ") if calc_hamming_code in ['y', 'yes', 1]: calc_hamming_code = True @@ -103,23 +134,62 @@ def perform_file_upload(com_if: CommunicationInterface, tmtc_printer: TmTcPrinte tm_listener.set_listener_mode(TmListener.ListenerModes.MANUAL) print_string = "BinaryUploader: Detected file size: " + str(file_transfer_helper.file_size()) LOGGER.info(print_string) - print_string = "BinaryUploader: " + str(file_transfer_helper.number_of_packets) + \ + total_num_packets = file_transfer_helper.number_of_packets + print_string = "BinaryUploader: " + str(total_num_packets) + \ " packets generated with " + \ str(file_transfer_helper.number_of_append_packets) + " append packets." LOGGER.info(print_string) - + interval = 0.5 + total_time = interval * total_num_packets idx = 1 + reception_deque = deque() while tc_queue: (tc_packet, tc_info) = tc_queue.pop() if not isinstance(tc_packet, str): - print_string = "Sending packet " + str(idx) + ".." - LOGGER.info(print_string) + # print_string = "Sending packet " + str(idx) + ".." + # LOGGER.info(print_string) idx += 1 com_if.send_telecommand(tc_packet, tc_info) + tmtc_printer.print_telecommand(tc_packet, tc_info) elif tc_packet == "print": LOGGER.info(tc_info) time.sleep(0.5) - tmtc_printer.print_telemetry_queue(tm_listener.retrieve_tm_packet_queue()) + remaining_time_string = "Remaining time: " + str(total_time - (idx - 2) * interval) + \ + " seconds" + print_progress_bar(idx - 2, total_num_packets, print_end="\n", suffix=remaining_time_string) + + # sys.stdout.write("\033[F") # Cursor up one line + reception_deque.extend(tm_listener.retrieve_tm_packet_queue()) print_string = "BinaryUploader: All binary packets were sent!" LOGGER.info(print_string) + print_string = str(reception_deque.__len__()) + " replies received." + LOGGER.info(print_string) + time.sleep(1.5) + tm_listener.clear_tm_packet_queue() + LOGGER.info("Transitioning back to listener mode..") + + +# https://stackoverflow.com/questions/3173320/text-progress-bar-in-the-console +# Thank you Greensticks :-) +def print_progress_bar(iteration, total, prefix='', suffix='', decimals=1, length=100, + fill='â–ˆ', print_end="\r"): + """ + Call in a loop to create terminal progress bar + @params: + iteration - Required : current iteration (Int) + total - Required : total iterations (Int) + prefix - Optional : prefix string (Str) + suffix - Optional : suffix string (Str) + decimals - Optional : positive number of decimals in percent complete (Int) + length - Optional : character length of bar (Int) + fill - Optional : bar fill character (Str) + print_end - Optional : end character (e.g. "\r", "\r\n") (Str) + """ + percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total))) + filled_length = int(length * iteration // total) + bar = fill * filled_length + '-' * (length - filled_length) + print(f'\r{prefix} |{bar}| {percent}% {suffix}', end=print_end) + # Print New Line on Complete + if iteration == total: + print() \ No newline at end of file