forked from SmallSatGasTeam/CubeWorks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
64 lines (53 loc) · 2.13 KB
/
main.py
File metadata and controls
64 lines (53 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import asyncio
from datetime import datetime
from time import sleep
from Components import *
from Drivers import *
from mission_modes import *
from mission_logic import *
async def startLoop(db):
"""
Creates a context dictionary to contain values,
Creates a lock to prevent race conditions when running drivers,
Initializes all drivers in a list,
Starts gathering from Driver.run() on loop
"""
context = {"MissionMode": MissionMode.MODE_0, "clock": UTCTime()}
sleep(.01)
lock = asyncio.Lock()
drivers = [ContextPrinter(), db, Clock(), Drvr_00(), Drvr_01(), Drvr_02(), Drvr_03(), Drvr_04(), Drvr_05(), Drvr_06(), Drvr_07(), Drvr_08()]
await asyncio.gather(*[d.run(context, lock) for d in drivers], runLogic(drivers, context, lock))
def UTCTime():
"""
Returns the UTC time in miliseconds since the Unix epoch.
The integer returned should be 64 bits in size and be on the order of 1500000000000.
"""
return int((datetime.utcnow() - datetime.utcfromtimestamp(0)).total_seconds() * 1000)
if __name__ == '__main__':
"""
The main entry point for CubeWorks. Performs the following tasks:
1. Sets the sleep duration before beginning operations
2. Initializes the database
3. Computes the time to wait before beginning operations in case of reboot
4. Waits if the sleep duration has not elapsed
5. Begins the main asyncio loop
"""
# Time in miliseconds to sleep before initializing drivers
SLEEP_DURATION = 5 * 1000 # 5 seconds
# Initialilze database
db = Database()
# Query database for initial time
initial_time = db.getFirstBoot()
if initial_time is None:
# Record initial boot time in database
print('initial time not found: assuming first boot')
initial_time = UTCTime()
db.setFirstBoot(initial_time)
delta = UTCTime() - initial_time
if delta < SLEEP_DURATION:
print(f'wait time not elapsed. waiting...')
sleep((SLEEP_DURATION - delta) / 1000)
try:
asyncio.run(startLoop(db))
except KeyboardInterrupt:
pass