Move API client into auto_scheduler.api
Signed-off-by: Fabian P. Schmidt <kerel@mailbox.org>merge-requests/14/head
parent
abc45cf836
commit
fa8292477b
|
@ -0,0 +1,136 @@
|
|||
import requests
|
||||
import logging
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
import settings
|
||||
|
||||
|
||||
def get_paginated_endpoint(url, max_entries=None):
|
||||
r = requests.get(url=url)
|
||||
r.raise_for_status()
|
||||
|
||||
data = r.json()
|
||||
|
||||
while 'next' in r.links and (not max_entries or len(data) < max_entries):
|
||||
next_page_url = r.links['next']['url']
|
||||
|
||||
r = requests.get(url=next_page_url)
|
||||
r.raise_for_status()
|
||||
|
||||
data.extend(r.json())
|
||||
|
||||
return data
|
||||
|
||||
|
||||
def get_satellite_info():
|
||||
# Open session
|
||||
logging.info("Fetching satellite information from DB.")
|
||||
r = requests.get('{}/api/satellites'.format(settings.DB_BASE_URL))
|
||||
logging.info("Satellites received!")
|
||||
|
||||
# Select alive satellites
|
||||
norad_cat_ids = []
|
||||
for o in r.json():
|
||||
if o["status"] == "alive":
|
||||
norad_cat_ids.append(o["norad_cat_id"])
|
||||
|
||||
return norad_cat_ids
|
||||
|
||||
|
||||
def get_active_transmitter_info(fmin, fmax):
|
||||
# Open session
|
||||
logging.info("Fetching transmitter information from DB.")
|
||||
r = requests.get('{}/api/transmitters'.format(settings.DB_BASE_URL))
|
||||
logging.info("Transmitters received!")
|
||||
|
||||
# Loop
|
||||
transmitters = []
|
||||
for o in r.json():
|
||||
if o["downlink_low"]:
|
||||
if o["alive"] and o["downlink_low"] > fmin and o["downlink_low"] <= fmax:
|
||||
transmitter = {"norad_cat_id": o["norad_cat_id"],
|
||||
"uuid": o["uuid"]}
|
||||
transmitters.append(transmitter)
|
||||
logging.info("Transmitters filtered based on ground station capability.")
|
||||
return transmitters
|
||||
|
||||
|
||||
def get_transmitter_stats():
|
||||
logging.debug("Requesting transmitter success rates for all satellite")
|
||||
transmitters = get_paginated_endpoint('{}/api/transmitters/'.format(settings.NETWORK_BASE_URL))
|
||||
return transmitters
|
||||
|
||||
|
||||
def get_scheduled_passes_from_network(ground_station, tmin, tmax):
|
||||
# Get first page
|
||||
client = requests.session()
|
||||
|
||||
# Loop
|
||||
start = True
|
||||
scheduledpasses = []
|
||||
|
||||
logging.info("Requesting scheduled passes for ground station %d" % ground_station)
|
||||
while True:
|
||||
if start:
|
||||
r = client.get('{}/api/observations/?ground_station={:d}'.format(
|
||||
settings.NETWORK_BASE_URL,
|
||||
ground_station))
|
||||
start = False
|
||||
else:
|
||||
nextpage = r.links.get("next")
|
||||
r = client.get(nextpage["url"])
|
||||
|
||||
# r.json() is a list of dicts
|
||||
for o in r.json():
|
||||
satpass = {
|
||||
"id": o['norad_cat_id'],
|
||||
"tr": datetime.strptime(
|
||||
o['start'].replace(
|
||||
"Z",
|
||||
""),
|
||||
"%Y-%m-%dT%H:%M:%S"),
|
||||
"ts": datetime.strptime(
|
||||
o['end'].replace(
|
||||
"Z",
|
||||
""),
|
||||
"%Y-%m-%dT%H:%M:%S"),
|
||||
"scheduled": True,
|
||||
"altt": o['max_altitude'],
|
||||
"priority": 1,
|
||||
"uuid": o['transmitter'],
|
||||
"name": ''}
|
||||
|
||||
if satpass['ts'] > tmin and satpass['tr'] < tmax:
|
||||
scheduledpasses.append(satpass)
|
||||
if satpass['ts'] < tmin:
|
||||
break
|
||||
|
||||
logging.info("Scheduled passes for ground station %d retrieved!" % ground_station)
|
||||
return scheduledpasses
|
||||
|
||||
|
||||
def get_groundstation_info(ground_station_id):
|
||||
|
||||
logging.info("Requesting information for ground station %d" % ground_station_id)
|
||||
client = requests.session()
|
||||
|
||||
# Loop
|
||||
found = False
|
||||
r = client.get("{}/api/stations/?id={:d}".format(
|
||||
settings.NETWORK_BASE_URL,
|
||||
ground_station_id))
|
||||
for o in r.json():
|
||||
if o['id'] == ground_station_id:
|
||||
if o['status'] == 'Online' or o['status'] == 'Testing':
|
||||
found = True
|
||||
else:
|
||||
found = False
|
||||
break
|
||||
|
||||
if found:
|
||||
logging.info('Ground station information retrieved!')
|
||||
return o
|
||||
else:
|
||||
logging.info('No ground station information found!')
|
||||
return {}
|
|
@ -5,24 +5,10 @@ import random
|
|||
from datetime import datetime, timedelta
|
||||
import ephem
|
||||
import lxml
|
||||
import settings
|
||||
from tqdm import tqdm
|
||||
|
||||
def get_paginated_endpoint(url, max_entries=None):
|
||||
r = requests.get(url=url)
|
||||
r.raise_for_status()
|
||||
import settings
|
||||
|
||||
data = r.json()
|
||||
|
||||
while 'next' in r.links and (not max_entries or len(data) < max_entries):
|
||||
next_page_url = r.links['next']['url']
|
||||
|
||||
r = requests.get(url=next_page_url)
|
||||
r.raise_for_status()
|
||||
|
||||
data.extend(r.json())
|
||||
|
||||
return data
|
||||
|
||||
def read_priorities_transmitters(filename):
|
||||
with open(filename, "r") as fp:
|
||||
|
@ -38,91 +24,6 @@ def read_priorities_transmitters(filename):
|
|||
sattrans[sat] = transmitter
|
||||
return (satprio, sattrans)
|
||||
|
||||
def get_satellite_info():
|
||||
# Open session
|
||||
logging.info("Fetching satellite information from DB.")
|
||||
r = requests.get('{}/api/satellites'.format(settings.DB_BASE_URL))
|
||||
logging.info("Satellites received!")
|
||||
|
||||
# Select alive satellites
|
||||
norad_cat_ids = []
|
||||
for o in r.json():
|
||||
if o["status"] == "alive":
|
||||
norad_cat_ids.append(o["norad_cat_id"])
|
||||
|
||||
return norad_cat_ids
|
||||
|
||||
def get_active_transmitter_info(fmin, fmax):
|
||||
# Open session
|
||||
logging.info("Fetching transmitter information from DB.")
|
||||
r = requests.get('{}/api/transmitters'.format(settings.DB_BASE_URL))
|
||||
logging.info("Transmitters received!")
|
||||
|
||||
# Loop
|
||||
transmitters = []
|
||||
for o in r.json():
|
||||
if o["downlink_low"]:
|
||||
if o["alive"] and o["downlink_low"] > fmin and o["downlink_low"] <= fmax:
|
||||
transmitter = {"norad_cat_id": o["norad_cat_id"],
|
||||
"uuid": o["uuid"]}
|
||||
transmitters.append(transmitter)
|
||||
logging.info("Transmitters filtered based on ground station capability.")
|
||||
return transmitters
|
||||
|
||||
|
||||
def get_transmitter_stats():
|
||||
logging.debug("Requesting transmitter success rates for all satellite")
|
||||
transmitters = get_paginated_endpoint('{}/api/transmitters/'.format(settings.NETWORK_BASE_URL))
|
||||
return transmitters
|
||||
|
||||
|
||||
def get_scheduled_passes_from_network(ground_station, tmin, tmax):
|
||||
# Get first page
|
||||
client = requests.session()
|
||||
|
||||
# Loop
|
||||
start = True
|
||||
scheduledpasses = []
|
||||
|
||||
logging.info("Requesting scheduled passes for ground station %d" % ground_station)
|
||||
while True:
|
||||
if start:
|
||||
r = client.get('{}/api/observations/?ground_station={:d}'.format(
|
||||
settings.NETWORK_BASE_URL,
|
||||
ground_station))
|
||||
start = False
|
||||
else:
|
||||
nextpage = r.links.get("next")
|
||||
r = client.get(nextpage["url"])
|
||||
|
||||
# r.json() is a list of dicts
|
||||
for o in r.json():
|
||||
satpass = {
|
||||
"id": o['norad_cat_id'],
|
||||
"tr": datetime.strptime(
|
||||
o['start'].replace(
|
||||
"Z",
|
||||
""),
|
||||
"%Y-%m-%dT%H:%M:%S"),
|
||||
"ts": datetime.strptime(
|
||||
o['end'].replace(
|
||||
"Z",
|
||||
""),
|
||||
"%Y-%m-%dT%H:%M:%S"),
|
||||
"scheduled": True,
|
||||
"altt": o['max_altitude'],
|
||||
"priority": 1,
|
||||
"uuid": o['transmitter'],
|
||||
"name": ''}
|
||||
|
||||
if satpass['ts'] > tmin and satpass['tr'] < tmax:
|
||||
scheduledpasses.append(satpass)
|
||||
if satpass['ts'] < tmin:
|
||||
break
|
||||
|
||||
logging.info("Scheduled passes for ground station %d retrieved!" % ground_station)
|
||||
return scheduledpasses
|
||||
|
||||
|
||||
def overlap(satpass, scheduledpasses, wait_time_seconds):
|
||||
"""Check if this pass overlaps with already scheduled passes"""
|
||||
|
@ -272,32 +173,6 @@ def find_passes(satellites, observer, tmin, tmax, minimum_altitude):
|
|||
return passes
|
||||
|
||||
|
||||
def get_groundstation_info(ground_station_id):
|
||||
|
||||
logging.info("Requesting information for ground station %d" % ground_station_id)
|
||||
client = requests.session()
|
||||
|
||||
# Loop
|
||||
found = False
|
||||
r = client.get("{}/api/stations/?id={:d}".format(
|
||||
settings.NETWORK_BASE_URL,
|
||||
ground_station_id))
|
||||
for o in r.json():
|
||||
if o['id'] == ground_station_id:
|
||||
if o['status'] == 'Online' or o['status'] == 'Testing':
|
||||
found = True
|
||||
else:
|
||||
found = False
|
||||
break
|
||||
|
||||
if found:
|
||||
logging.info('Ground station information retrieved!')
|
||||
return o
|
||||
else:
|
||||
logging.info('No ground station information found!')
|
||||
return {}
|
||||
|
||||
|
||||
def get_last_update(fname):
|
||||
try:
|
||||
fp = open(fname, "r")
|
|
@ -8,10 +8,17 @@ import os
|
|||
import lxml.html
|
||||
import argparse
|
||||
import logging
|
||||
from utils import get_active_transmitter_info, get_transmitter_stats, \
|
||||
get_groundstation_info, get_last_update, get_scheduled_passes_from_network, ordered_scheduler, \
|
||||
efficiency, find_passes, schedule_observation, read_priorities_transmitters, \
|
||||
get_satellite_info
|
||||
from auto_scheduler.utils import get_last_update, \
|
||||
ordered_scheduler, \
|
||||
efficiency, \
|
||||
find_passes, \
|
||||
schedule_observation, \
|
||||
read_priorities_transmitters
|
||||
from auto_scheduler.api import get_satellite_info, \
|
||||
get_active_transmitter_info, \
|
||||
get_transmitter_stats, \
|
||||
get_scheduled_passes_from_network, \
|
||||
get_groundstation_info
|
||||
import settings
|
||||
from tqdm import tqdm
|
||||
import sys
|
||||
|
|
Loading…
Reference in New Issue