Skip to content

Commit 746f2bf

Browse files
Commands pipeline sorting by priority and time along with early commands pipeline structure. Requires that packet handling be done to be completed.
1 parent 39a6cfb commit 746f2bf

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from datetime import datetime
2+
3+
4+
class CLICommand:
5+
"""
6+
An abstraction of the CLI commands so that adapting the commns pipeline packet logic
7+
into GS will be easier.
8+
"""
9+
10+
def __init__(self, name: str, params: list[str], cmd_id: int, prio: int) -> None:
11+
"""
12+
This abstracts the CLI commands in a way which makes it accessable for GS.
13+
The reason this is created is so that we are able to have a 1:1 clone of the
14+
CLI commands which allow for easier adoption of prexisting pipelines built for CLI.
15+
16+
:name: name which matches the CLI command name
17+
:id: id which matches the id in the satelite
18+
:params: list of command as a string, matches CLI command param options
19+
:prio: command priority. integer which goes from 1 to n where n is the number of
20+
priorities we have. 1 is the highest priority
21+
:time: tracks the time at which a command has been created
22+
"""
23+
self.name = name
24+
self.cmd_id = cmd_id
25+
self.params = params
26+
self.prio = prio
27+
self.time = datetime.now()
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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

gs/backend/data/resources/rules.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Rule:
2+
"""
3+
Absraction for command rules meant to be used with the commands pipeline
4+
Basically, enforces patterns like cmd1 cannot be ahead of cmd2.
5+
6+
Still a work in progress
7+
"""
8+
9+
def __init__(self) -> None:
10+
"""
11+
Contains necessary variables to abstract rules which can be enforced.
12+
13+
Work in progress.
14+
"""
15+
pass

0 commit comments

Comments
 (0)