-
Notifications
You must be signed in to change notification settings - Fork 1
/
flask_app.py
155 lines (113 loc) · 3.86 KB
/
flask_app.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
from datetime import timedelta
from time import sleep
from flask import Flask, render_template
from flask import jsonify, request, session
from flask_sock import Sock
from skimmer import cSkimmer
WEB_APP_VER = "0.1.0"
app = Flask(__name__)
# setup config for sessions
app.config['SESSION_PERMANENT'] = True
app.config['SESSION_TYPE'] = 'filesystem'
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(hours=48)
app.secret_key = 'WE_SHOULD_PROBABLY_CHANGE_THIS'
sock = Sock(app)
app.config.from_pyfile('skimmerwebapp.cfg')
skimmer = cSkimmer(app.config)
@app.before_first_request
def start_skimmer():
# make session permanent
session.permanent = True
# kick off main skcc skimmer script
skimmer.run()
@app.route("/")
def index():
"""Getting this route will make the skimmer immediately resend the spots
and skeds.
"""
skimmer.force_refresh()
return render_template('index.html')
@app.route("/restart/", methods=['POST'])
def restart():
"""Posting to this route will stop the skimmer then restart it
"""
skimmer.stop()
skimmer.force_refresh()
skimmer.run()
return render_template('index.html')
@app.route('/getstatus')
def get_status():
"""Gets the status of the SKCC Skimmer background application (at startup).
This method gets all the text output lines of the skimmer during it's initial
startup period. Afterwards it serves no function.
"""
s = skimmer.get_status()
j = s.get_lines()
return jsonify(j)
@app.route('/clearspots')
def clear_spots():
"""Allow user to clear the current list of RBN spots.
Returns "nothing" str
"""
skimmer.clear_spots()
return "nothing"
@app.route('/save/', methods=['POST'])
def save():
"""Allows a user to save a SPOT line from the UI to the current session.
POSTing with ?clear=1 will pop the current list of stored_spots from the
session.
"""
data = request.get_json()
#print(data)
if not session.get('stored_spots'):
session['stored_spots'] = []
session['stored_spots'].append(data)
session.modified = True
do_clear = request.args.get('clear', default=0, type=int)
if (do_clear == 1):
session.pop('stored_spots')
return jsonify(success=True)
@app.route('/load', methods=['GET'])
def load():
"""Returns the session's stored_spots array as a JSON object.
Use this on refresh to get the current stored spots.
Returns {Success}
"""
if not session.get('stored_spots'):
return jsonify(success=False, message='no stored spots')
print('load' + str(session['stored_spots']))
return jsonify(session['stored_spots'])
@app.route('/version', methods=['GET'])
def version():
"""Returns the applications version information.
"""
return jsonify(success=True, version=WEB_APP_VER)
@sock.route('/getskedsws')
def get_skeds_ws(ws):
"""Provide a websocket connection that continually sends the current list of
skeds.
This loops forever and delays based on the app.config 'SKED_TIME'
"""
while True:
sleep(app.config['SKED_TIME'] / 1000)
(new_skeds, skeds) = skimmer.get_skeds()
x = jsonify(skeds)
ws.send(x.data.decode('utf-8'))
@sock.route('/getspotsws')
def get_spots_ws(ws):
"""Provide a websocket connection that continually sends the current list of
RBN spots of SKCC members.
This loops forever and delays based on the app.config 'SPOT_TIME'
"""
while True:
sleep(app.config['SPOT_TIME'] / 1000)
(new_spots, spots) = skimmer.get_spots()
x = jsonify(spots)
ws.send(x.data.decode('utf-8'))
# when we run out of a bundled exe this is what starts off the flask application
if __name__ == '__main__':
# run the flask server
if (app.config['BIND_LOCALHOST_ONLY']):
app.run()
else:
app.run(host="0.0.0.0")