|
| 1 | +import warnings |
| 2 | + |
| 3 | +from gs.backend.data.resources.cli_commands import CLICommand |
| 4 | + |
| 5 | + |
| 6 | +class CommandsPipeline: |
| 7 | + """ |
| 8 | + Recieves, sorts, and packets commands such that they may be sent to the |
| 9 | + satellite. |
| 10 | + """ |
| 11 | + |
| 12 | + def __init__(self) -> None: |
| 13 | + """ |
| 14 | + Lockout should be set at some arbitrary time before session begins. |
| 15 | + Once lockout is True, commands will no longer be recieved |
| 16 | + """ |
| 17 | + self.lockout = False |
| 18 | + self.commands_queue = [] |
| 19 | + self.packet_list = [] |
| 20 | + |
| 21 | + def command_to_byte(self) -> None: |
| 22 | + """ |
| 23 | + Given any cli command, it converts it to a byte. We want to use this |
| 24 | + for sizing the packet, and as a helper function for packets |
| 25 | + """ |
| 26 | + |
| 27 | + pass |
| 28 | + |
| 29 | + def queue_to_packet(self) -> None: |
| 30 | + """ |
| 31 | + Converts all commands in the queue into packets. |
| 32 | + """ |
| 33 | + |
| 34 | + # I am not completely sure how the commands are packed on a technical front so |
| 35 | + # I will consider two cases. |
| 36 | + # Case 1: We can convert bytes (which are commnads) into packets. |
| 37 | + # Case 2: We must convert cli commands into packets by passing a list of IDs |
| 38 | + |
| 39 | + # Case 1 we should just be able to apply the command to byte function to |
| 40 | + # every CLI command in the queue, then we can iterate over the queue again |
| 41 | + # to pack everything into packets (we know exactly when to stop for packets since we know size) |
| 42 | + # of the command |
| 43 | + |
| 44 | + # Case 2 uhh honestly im not sure but i think it involves a helper function which |
| 45 | + # takes a list of commands and packs them all into a packet, then checks size. |
| 46 | + # if the size still permits more packets, add one more command to the list of commands |
| 47 | + # to be packed and repack. repeat that until packet is at desired size. |
| 48 | + # if the size reached desired size, store the packet into packet_list. |
| 49 | + # note that the commands to be reference will likely be the queue. |
| 50 | + # so the helper function arguments be like [cmd1] -> [cmd1, cmd2] -> [cmd1 cmd 2 cmd3] |
| 51 | + # etc until desired size is reached in which we have [packet1], [cmdN] -> [cmdN+1] etc |
| 52 | + |
| 53 | + self.clear_queue() |
| 54 | + # this should return a list of packets, but since packet type is currently |
| 55 | + # undefined / unspecified, it will be casted to none. |
| 56 | + pass |
| 57 | + |
| 58 | + def add_to_queue(self, command: CLICommand) -> tuple[bool, list[CLICommand]]: |
| 59 | + """ |
| 60 | + Inserts a command into queue, and then sorts it by priority and then by time. |
| 61 | +
|
| 62 | +
|
| 63 | + :command: CLICommand which has been rehydrated with the relevant infromation |
| 64 | + :return: tuple containing a status which is True if command has been inserted and |
| 65 | + False otherwise and the command queue |
| 66 | + """ |
| 67 | + |
| 68 | + if self.lockout: |
| 69 | + warnings.warn("Commands queue lockout active. Returned current queue", stacklevel=2) |
| 70 | + return tuple(False, self.commands_queue) |
| 71 | + |
| 72 | + self.commands_queue.append(command) |
| 73 | + self.sort_queue() |
| 74 | + |
| 75 | + return tuple(True, self.commands_queue) |
| 76 | + |
| 77 | + def sort_queue(self) -> list[CLICommand]: |
| 78 | + """ |
| 79 | + This function sorts the queue 2 times. We first sort by time to ensure time descending, |
| 80 | + then we sort by priority to ensure that the highest priority is at the top of the |
| 81 | + queue. |
| 82 | + """ |
| 83 | + self.commands_queue.sort(key=lambda x: x.time) |
| 84 | + self.commands_queue.sort(key=lambda x: x.prio) |
| 85 | + return self.commands_queue |
| 86 | + |
| 87 | + def apply_rule(self) -> list[CLICommand]: |
| 88 | + """ |
| 89 | + There are certain rules which commands must follow. |
| 90 | + For example, some commands CANNOT come before other commands. |
| 91 | + These rules can be defined and applied to the current queue |
| 92 | + """ |
| 93 | + return self.commands_queue |
| 94 | + |
| 95 | + def clear_queue(self) -> list[CLICommand]: |
| 96 | + """ |
| 97 | + Clears the current queue |
| 98 | + """ |
| 99 | + self.commands_queue = [] |
| 100 | + return self.commands_queue |
| 101 | + |
| 102 | + def enable_lockout(self) -> None: |
| 103 | + """ |
| 104 | + Prevents more commands from being recieved. |
| 105 | + """ |
| 106 | + self.lockout = True |
| 107 | + |
| 108 | + def disable_lockout(self) -> None: |
| 109 | + """ |
| 110 | + Allows more commands to be recieved |
| 111 | + """ |
| 112 | + self.lockout = False |
0 commit comments