197 lines
6.5 KiB
Python
197 lines
6.5 KiB
Python
from datetime import datetime , timedelta
|
|
import requests
|
|
from flask import Flask , render_template,redirect,url_for
|
|
import json
|
|
import random
|
|
from apscheduler.schedulers.background import BackgroundScheduler
|
|
from satnogs_api_client import fetch_satellites, DB_BASE_URL,fetch_tle_of_observation
|
|
from satellite_tle import fetch_tles
|
|
|
|
scheduler = BackgroundScheduler()
|
|
app = Flask(__name__)
|
|
|
|
|
|
Passes = []
|
|
Occuring_sats = {}
|
|
Stations = []
|
|
TLEs = {}
|
|
Transmitters = {}
|
|
|
|
class Pass:
|
|
id = 0
|
|
start = None
|
|
end = None
|
|
ground_station = None
|
|
satellite = None
|
|
transmitter = None
|
|
norad = 0
|
|
|
|
|
|
|
|
def getActive():
|
|
start = (datetime.utcnow() - timedelta(0,0,0,0,20)).strftime('%Y-%m-%dT%H:%M:%S%z')
|
|
end = (datetime.utcnow() + timedelta(0,0,0,0,30)).strftime('%Y-%m-%dT%H:%M:%S%z')
|
|
|
|
passesR = requests.get("https://network.satnogs.org/api/observations/?end="+end+"&format=json&start="+start)
|
|
passes = passesR.json()
|
|
if passesR.links.has_key("next"):
|
|
while passesR.links.has_key("next"):
|
|
passesR = requests.get(passesR.links["next"]["url"])
|
|
passes += passesR.json()
|
|
ground_stations = {}
|
|
for x in passes:
|
|
if datetime.strptime(x["start"],'%Y-%m-%dT%H:%M:%Sz') > datetime.utcnow() or datetime.strptime(x["end"],'%Y-%m-%dT%H:%M:%Sz') < datetime.utcnow():
|
|
passes.remove(x)
|
|
else:
|
|
if ground_stations.has_key(x["ground_station"]):
|
|
ground_stations[x["ground_station"]].append(x)
|
|
else:
|
|
ground_stations[x["ground_station"]] = []
|
|
ground_stations[x["ground_station"]].append(x)
|
|
passes = []
|
|
for x in ground_stations:
|
|
start = datetime.utcnow()
|
|
current = {"start":datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S%z')+"z"}
|
|
for y in ground_stations[x]:
|
|
if datetime.strptime(y["start"],'%Y-%m-%dT%H:%M:%Sz') < datetime.strptime(current["start"],'%Y-%m-%dT%H:%M:%Sz'):
|
|
current = y
|
|
passes.append(current)
|
|
Passes =[]
|
|
for x in passes:
|
|
temp = Pass()
|
|
temp.id = x["id"]
|
|
temp.start = datetime.strptime(x["start"],'%Y-%m-%dT%H:%M:%Sz')
|
|
temp.end = datetime.strptime(x["end"],'%Y-%m-%dT%H:%M:%Sz')
|
|
temp.ground_station = x["ground_station"]
|
|
temp.transmitter = x["transmitter"]
|
|
temp.norad = str(x["norad_cat_id"])
|
|
try:
|
|
temp.satellite = requests.get("https://db.satnogs.org/api/satellites/"+str(x["norad_cat_id"])).json()
|
|
except:
|
|
temp.satellite = {"name":""}
|
|
Passes.append(temp)
|
|
|
|
|
|
|
|
return Passes
|
|
|
|
|
|
def GetGroundStations():
|
|
stationsR = requests.get("https://network.satnogs.org/api/stations/")
|
|
stations = stationsR.json()
|
|
while stationsR.links.has_key("next"):
|
|
stationsR = requests.get(stationsR.links["next"]["url"])
|
|
stations += stationsR.json()
|
|
|
|
for x in stations:
|
|
if x["last_seen"] == None:
|
|
stations.remove(x)
|
|
continue
|
|
|
|
if datetime.strptime(x["last_seen"],'%Y-%m-%dT%H:%M:%Sz') < (datetime.utcnow()- timedelta(10,0,0,0)):
|
|
stations.remove(x)
|
|
for x in stations:
|
|
if x["last_seen"] == None:
|
|
stations.remove(x)
|
|
continue
|
|
|
|
if datetime.strptime(x["last_seen"],'%Y-%m-%dT%H:%M:%Sz') < (datetime.utcnow()- timedelta(10,0,0,0)):
|
|
stations.remove(x)
|
|
|
|
|
|
return stations
|
|
|
|
@scheduler.scheduled_job('interval',days=5)
|
|
def updateTransmitters():
|
|
global Transmitters
|
|
print "Updating Transmitters"
|
|
temp = requests.get("https://db.satnogs.org/api/transmitters/").json()
|
|
for x in temp:
|
|
if str(x["norad_cat_id"]) in Transmitters.keys():
|
|
Transmitters[str(x["norad_cat_id"])][x["uuid"]] = [x["description"],"#"+str("%06x" % random.randint(0, 0xFFFFFF))]
|
|
else:
|
|
Transmitters[str(x["norad_cat_id"])]={}
|
|
Transmitters[str(x["norad_cat_id"])][x["uuid"]] = [x["description"],"#"+str("%06x" % random.randint(0, 0xFFFFFF))]
|
|
#print Transmitters
|
|
|
|
@scheduler.scheduled_job('interval',minutes=3)
|
|
def updatePasses():
|
|
global Passes
|
|
global Occuring_sats
|
|
print "Updating Passes"
|
|
Passes = getActive()
|
|
Occuring_sats = {}
|
|
for x in Passes:
|
|
if x.satellite['norad_cat_id'] not in TLEs.keys():
|
|
q = fetch_tle_of_observation(x.id)
|
|
TLEs[ x.norad ] = [str(x.satellite["name"]),str(q[0]),str(q[1])]
|
|
Occuring_sats[x.norad] = TLEs[x.norad]
|
|
|
|
@scheduler.scheduled_job('interval',hours=1)
|
|
def updateStations():
|
|
global Stations
|
|
print "Updating Stations"
|
|
Stations = GetGroundStations()
|
|
|
|
@scheduler.scheduled_job('interval',days=1)
|
|
def updateTLE():
|
|
print "Updating TLE"
|
|
global TlEs
|
|
sats = fetch_satellites(None,DB_BASE_URL)
|
|
satnogs_db_norad_ids = set(sat['norad_cat_id'] for sat in sats if sat['status'] != 're-entered')
|
|
# Remove satellites with temporary norad ids
|
|
temporary_norad_ids = set(filter(lambda norad_id: norad_id >= 99900, satnogs_db_norad_ids))
|
|
satnogs_db_norad_ids = satnogs_db_norad_ids - temporary_norad_ids
|
|
|
|
# Fetch TLEs for the satellites of interest
|
|
|
|
tles = fetch_tles(satnogs_db_norad_ids)
|
|
TLEs = {}
|
|
for norad_id, (source, tle) in tles.items():
|
|
TLEs[norad_id] = [str(tle[0]),str(tle[1]),str(tle[2])]
|
|
print('\nTLEs for {} of {} requested satellites found ({} satellites with temporary norad ids skipped).'.format(len(tles), len(satnogs_db_norad_ids), len(temporary_norad_ids)))
|
|
|
|
|
|
|
|
|
|
@app.route("/")
|
|
def map_view():
|
|
return render_template("map.html")
|
|
|
|
@app.route('/active_stations')
|
|
def api_active_stations():
|
|
sations = []
|
|
for x in Stations:
|
|
sations.append({'id':x['id'],'name':x['name'],'lat_lng':[x["lat"],x['lng']]})
|
|
return json.dumps(sations)
|
|
|
|
@app.route('/stations_from_sat/<string:norad>')
|
|
def api_occuring_observations(norad):
|
|
obs = []
|
|
trans = []
|
|
for x in Passes:
|
|
if x.norad == norad:
|
|
obs.append([x.ground_station,Transmitters[norad][x.transmitter][1]])
|
|
trans.append(x.transmitter)
|
|
#print Transmitters[norad].values()
|
|
|
|
transList = []
|
|
for x in set(trans):
|
|
transList.append(Transmitters[norad][x])
|
|
#print transList,norad
|
|
return json.dumps([obs,transList])
|
|
|
|
@app.route('/occuring_sats')
|
|
def api_occuring_sats():
|
|
return json.dumps(Occuring_sats)
|
|
|
|
|
|
|
|
|
|
|
|
updatePasses()
|
|
updateStations()
|
|
updateTLE()
|
|
updateTransmitters()
|
|
scheduler.start()
|
|
app.run(use_reloader=False,host = "0.0.0.0",port=5001) |