Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tomvdb committed Feb 18, 2022
1 parent e8a560e commit b095a36
Show file tree
Hide file tree
Showing 27 changed files with 994 additions and 746 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,6 @@ dmypy.json

# Pyre type checker
.pyre/

app/app.db
app/config.json
18 changes: 17 additions & 1 deletion app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,32 @@
from flask_bootstrap import Bootstrap5
import os
from flask_sqlalchemy import SQLAlchemy
from app import config_functions

basedir = os.path.abspath(os.path.dirname(__file__))

app = Flask(__name__)


app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'app.db')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SECRET_KEY'] = 'you-will-never-guess'

db = SQLAlchemy(app)


# config file
config = config_functions.Config()
config_file = os.path.join(basedir, 'config.json')

if os.path.isfile(config_file):
config.load_config(config_file)
else:
config.create_empty_config(config_file)
config.save_config()

print(config.config_data)

bootstrap = Bootstrap5(app)

from app import routes, routes_collections, routes_satellites, routes_observers, routes_jsapi
from app import routes, routes_collections, routes_satellites, routes_jsapi
Binary file removed app/app.db
Binary file not shown.
24 changes: 24 additions & 0 deletions app/config_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import json

class Config:

config_data = None
config_path = None

def load_config(self,config_file):
with open(config_file, "r") as f:
self.config_data = json.load(f)
self.config_path = config_file
return self.config_data

def save_config(self):
with open(self.config_path, 'w') as outfile:
json.dump(self.config_data, outfile)

def create_empty_config(self, config_file):
self.config_data = {
'qth_latitude' : 0,
'qth_longitude' : 0,
}

self.config_path = config_file
3 changes: 2 additions & 1 deletion app/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ class AddSatelliteCollectionForm(FlaskForm):
collection_name = SelectField('Collection: ', validators=[DataRequired()], coerce=int)
submit = SubmitField('Add to Collection')

'''
class AddObserverForm(FlaskForm):
observer_name = StringField('Observer Name', validators=[DataRequired()])
observer_latitude = StringField('Observer Latitude', validators=[DataRequired()])
observer_longitude = StringField('Observer Longitude', validators=[DataRequired()])
observer_elevation = StringField('Observer Elevation', validators=[DataRequired()])
submit = SubmitField('Save')
'''
12 changes: 0 additions & 12 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,3 @@ class Satellite(db.Model):
def __repr__(self):
return '<Satellite {}>'.format(self.satellite_name)

class Observer(db.Model):
__tablename__ = "observer"

observer_id = db.Column(db.Integer, primary_key=True)
observer_name = db.Column(db.Text, nullable=False)
observer_latitude = db.Column(db.Text, nullable=False)
observer_longitude = db.Column(db.Text, nullable=False)
observer_elevation = db.Column(db.Integer, nullable=False)
observer_default = db.Column(db.Integer, nullable=False)

def __repr__(self):
return '<Observer {}>'.format(self.observer_name)
4 changes: 3 additions & 1 deletion app/routes.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from flask import render_template, flash, redirect, url_for
from app import app, db
from app.models import SatelliteCollection

@app.route('/')
@app.route('/index')
def index():
def index():

return render_template('index.html', title='Home')


60 changes: 56 additions & 4 deletions app/routes_collections.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
from flask import render_template, flash, redirect, url_for
from flask import render_template, flash, redirect, url_for, Response
from app import app, db
from app.models import Satellite, SatelliteCollection, SatelliteCollectionAssignmment
from app.forms import CreateCollectionForm, AddSatelliteCollectionForm
from sqlalchemy import and_
from skyfield.api import load, wgs84, EarthSatellite, Distance

from app.satellite_functions import get_satellite_record

@app.route('/collections')
def view_collections():
collections = SatelliteCollection.query.all()
return render_template('collections.html', title='Collections', collections = collections)

@app.route('/get_tle_file/<collection_id>')
def get_tle_file(collection_id):
content = ""

satellites = SatelliteCollectionAssignmment.query.filter_by(collection_id=collection_id).all()

for sat in satellites:
content += sat.Satellite.satellite_tle0 + "\n"
content += sat.Satellite.satellite_tle1 + "\n"
content += sat.Satellite.satellite_tle2 + "\n"

return Response(content, mimetype='text/plain')

@app.route('/create_collection', methods=['GET', 'POST'])
def create_collection():
collections = SatelliteCollection.query.all()
Expand Down Expand Up @@ -44,8 +60,45 @@ def view_collection(collection_id):

satellites = SatelliteCollectionAssignmment.query.filter_by(collection_id=collection_id).all()

return render_template('view_collection.html', title=collection.collection_name + " Satellite Collection", satellites = satellites)
ts = load.timescale()

satlist = []
for satCollection in satellites:
sat = satCollection.Satellite
satRecord = get_satellite_record(sat.satellite_tle0, sat.satellite_tle1, sat.satellite_tle2)

satObject = {}
satObject['satellite_id'] = sat.satellite_id
satObject['satellite_name'] = sat.satellite_name

days = ts.now() - satRecord.epoch

if abs(days) > 10:
satObject['outdated'] = True
else:
satObject['outdated'] = False

satObject['satellite_epoch_days'] = days

satlist.append(satObject)

return render_template('view_collection.html', title=collection.collection_name + " Satellite Collection", collection_id=collection_id, satellites = satlist)

@app.route('/remove_satellite_collection/<collection_id>/<satellite_id>', methods=['GET', 'POST'])
def remove_satellite_collection(collection_id, satellite_id):

assignment = SatelliteCollectionAssignmment.query.filter(and_(SatelliteCollectionAssignmment.collection_id==collection_id, SatelliteCollectionAssignmment.satellite_id==satellite_id)).first()

if assignment == None:
return redirect(url_for('view_collection', collection_id=collection_id))

db.session.delete(assignment)
db.session.commit()
flash("Satellite removed from collection")

return redirect(url_for('view_collection', collection_id=collection_id))


@app.route('/add_satellite_collection/<satellite_id>', methods=['GET', 'POST'])
def add_satellite_collection(satellite_id):

Expand Down Expand Up @@ -77,10 +130,9 @@ def add_satellite_collection(satellite_id):
db.session.commit()

flash("Satellite added to collection")
return redirect( url_for("view_collection", collection_id = form.collection_name.data))
return redirect( url_for("view_satellites"))
else:
flash("Satellite already on this collection")


return render_template('showbasicform.html', title='Add "' + satellite.satellite_name + '" Satellite to Collection', satellite=satellite, form=form)

13 changes: 4 additions & 9 deletions app/routes_jsapi.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from flask import jsonify
from app import app, db
from app.models import Satellite, SatelliteCollection, SatelliteCollectionAssignmment, Observer
from app.models import Satellite, SatelliteCollection, SatelliteCollectionAssignmment
from app.forms import CreateCollectionForm, AddSatelliteCollectionForm
from sqlalchemy import and_
from app import config

from app.satellite_functions import calc_current_pos, get_next_pass, get_orbit

Expand All @@ -17,14 +18,8 @@ def satellite_data(collection_id):

data['collection_name'] = collection.collection_name

# get default observer
observer = Observer.query.filter_by(observer_default=1).first()

if observer == None:
return jsonify({ 'success' : False, 'message' : 'No default observer'})

data['observer'] = {}
data['observer']['coordinates'] = (float(observer.observer_latitude), float(observer.observer_longitude))
data['observer']['coordinates'] = (float(config.config_data['qth_latitude']), float(config.config_data['qth_longitude']))

# get satellites in this collection
satCollection = SatelliteCollectionAssignmment.query.filter_by(collection_id=collection_id).all()
Expand All @@ -40,7 +35,7 @@ def satellite_data(collection_id):
satObject['tle1'] = satellite.satellite_tle1
satObject['tle2'] = satellite.satellite_tle2

next_pass = get_next_pass(satellite.satellite_tle0, satellite.satellite_tle1, satellite.satellite_tle2, float(observer.observer_latitude), float(observer.observer_longitude), 0)
next_pass = get_next_pass(satellite.satellite_tle0, satellite.satellite_tle1, satellite.satellite_tle2, float(config.config_data['qth_latitude']), float(config.config_data['qth_longitude']), 0)
satObject['next_pass'] = next_pass


Expand Down
53 changes: 0 additions & 53 deletions app/routes_observers.py

This file was deleted.

Loading

0 comments on commit b095a36

Please sign in to comment.