-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscheduler.py
115 lines (80 loc) · 3.37 KB
/
scheduler.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import os
import tempfile
import schedule
import time
import sys
import datetime
import calcorbit
import manage_bookings
import pyorbital
from urllib import error
import logging
def print_usage():
print("python3 -c [satName] [%dd/%mm/%yy_%HH:%MM:%SS]")
def update_tle():
logging.debug("Updating tle")
try:
tmp_tle_filename = tempfile.NamedTemporaryFile().name
pyorbital.tlefile.fetch(tmp_tle_filename)
logging.info("TLE successfully updated")
tle_genuine_filename = "tle.txt"
os.replace(tmp_tle_filename, tle_genuine_filename)
logging.debug(tmp_tle_filename + " successfully renamed to " + tle_genuine_filename + " - Information updated")
except error.URLError as err:
message = "Could not update TLE file - " + str(err)
logging.warning(message)
def update_booking_data(path_to_json_files):
logging.debug("Updating bookings")
bookings = manage_bookings.read_bookings(path_to_json_files)
for booking in bookings:
schedule_start(calcorbit.norad_to_name(booking['norad_id']),booking['start_time'])
def start_tracking_procedures(sat_name, reservation_datetime):
""" Starts all procedures related to the sat tracking per se.
Runs initializers, tries to download latest data about the satellite,
connects to the Arduino through serial communication.
:updating_job: the updating data job which was executing until now
:return: the current job already canceled
"""
logging.info("Tracking procedures for " + sat_name + " in progress")
update_tle()
calcorbit.init_tracking(sat_name, reservation_datetime)
return schedule.CancelJob
def schedule_start(sat_name, reservation_time):
""" Scheduler for the beginning of the satellite tracking procedures
:sat_name: the name of the satellite to be tracked, e.g. "QMR-KWT"
:reservation time: datetime of the booked time for using the ground station
"""
logging.info("Satellite's tracking scheduled")
logging.info(type(reservation_time))
# schedule the start 3 minutes prior to the event in order to try to download the tle files
schedule.every().day.at(reservation_time).do(start_tracking_procedures, sat_name, reservation_time)
def schedule_downloads():
""" Schedules the update of the tle file - once a week
:return: the scheduling job
"""
schedule.every(10).seconds.do(update_tle)
logging.info("Updating TLE scheduled")
def schedule_booking(path):
""" Schedules the reading of the bookings once a week
:return: the scheduling job
"""
schedule.every(10).seconds.do(update_booking_data, path)
logging.info("Updating Bookings scheduled")
if __name__ == "__main__":
logging.basicConfig(format='%(asctime)s %(levelname)s:%(message)s', level=logging.DEBUG)
# default satellite for tracking - QMR-KWT
sat_name = 'QMR-KWT'
# default reservation time - now + 1 min
reservation_datetime = datetime.datetime.now() + datetime.timedelta(seconds=20)
print(len(sys.argv))
if len(sys.argv) >= 3:
sat_name = sys.argv[2]
reservation_datetime = datetime.datetime.strptime(sys.argv[3], '%d/%m/%y_%H:%M:%S')
print(sat_name)
print(reservation_datetime)
schedule_downloads()
schedule_booking('../')
# schedule_start(sat_name, reservation_datetime)
while True:
schedule.run_pending()
time.sleep(1)