-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
394 lines (287 loc) · 11.8 KB
/
Copy pathapi.py
File metadata and controls
394 lines (287 loc) · 11.8 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
from bottle import error, get, post, run, request, route, redirect
import fountain
import queries
import constants
from time import time
# ######################################################################################################################
# Defaults and Errors
# ######################################################################################################################
@error(404)
def error404(error):
return "404"
# Blog not here
@route('/blog')
def gtfo():
redirect("http://enlight.club")
@route('/blog/')
def gtfo3():
redirect("http://enlight.club")
@route('/')
def gtfo2():
redirect("http://enlight.club")
@post('/api')
def gDefaultResponse():
"""The default route for the API; returns the version number."""
return {'success': 'true', 'apiVersion': '1B'}
# ###
# Helper Functions
# ###
def log(msg):
"""Prints a message alongside the IP of the client that generated it."""
print('[' + request.remote_addr + '] ' + msg)
def checkAPIKey():
"""Verifies the requesting API key. These need to be verified on POST requests."""
if not 'apikey' in request.json.keys():
log('No API key with request.')
return False
con = fountain.db_connect()
c = con.cursor()
c.execute(queries.QUERY_API_KEY_COUNT, {'key': request.json['apikey']})
# This query is an aggregation (so don't need None check) - the number of matching keys. Should be > 0 (and probably 1).
r = c.fetchone()
isValid = True if r[0] == 1 else False
fountain.db_close(con)
return isValid
def getAPIKeyPriority():
"""Gets the priority of the requesting key."""
if not 'apikey' in request.json.keys():
log('No API key with request')
return 0
log('Getting priority for API key: ' + request.json['apikey'])
con = fountain.db_connect()
c = con.cursor()
c.execute(queries.QUERY_API_KEY_PRIORITY, {'key': request.json['apikey']})
# We'll just get a single number back from this one.
r = c.fetchone()
if r is None:
log('No rows returned - invalid API key?')
return 0
return r[0]
def getAPIKeyFail():
"""Returns a dictionary containing a failure status and message indicating an API key authentication failure."""
return {'success': 'false', 'message': 'Invalid API key.'}
def jsonRow(cursor):
"""Takes the next row from the cursor and formats it as a dictionary (JSON object as far as Bottle is concerned)."""
fields = [f[0] for f in cursor.description]
r = cursor.fetchone()
if r is not None:
i = 0
newDict = {}
for f in fields:
newDict[f] = r[i]
i += 1
return newDict
else:
return None
def jsonRows(cursor):
"""Queries a cursor and returns its rows as a JSON response (a list of dictionaries contained in a JSON object)."""
responseDict = {'success': 'true', 'items': []}
if cursor is None:
return responseDict
jr = jsonRow(cursor)
while jr is not None:
responseDict['items'].append(jr)
jr = jsonRow(cursor)
return responseDict
def getTrueEta(controllerID):
"""Assuming this controller were to be in position zero, about how long would it have control?"""
con = fountain.db_connect()
c = con.cursor()
ret = -2
for row in c.execute(queries.QUERY_CONTROL_QUEUE):
if int(row[0]) != int(controllerID):
continue
if row[1] > 0:
ret = row[1] + row[2] - time()
break
else:
ret = 0
fountain.db_close(con)
return ret
def getTrueQueuePosition(controllerID):
"""Taking into account priority and acquisition time, determines the true queue position"""
estimate = -1
# This will look very similar to the code in the background processing script, but here we'll be counting how many
# controllerIDs are ahead of us.
con = fountain.db_connect()
c = con.cursor()
# Check if we're even in the queue. If not, don't return anything.
c.execute(queries.CHECK_IF_IM_IN_THE_QUEUE, {'controllerID': controllerID})
r = c.fetchone()
if r[0] == 0:
print ("Not even in the queue... Controller ID is in position 0 already: " + str(controllerID))
fountain.db_close(con)
return -2
c.execute("SELECT queuePosition FROM controlQueue WHERE controllerID=:controllerID", {'controllerID': controllerID})
r = c.fetchone()
n = r[0]
fountain.db_close(con)
return n
# We need to check the queue of valid items at the maximum priority level, and all these below it. Group the
# priority levels and descend them to estimate how long it will be.
# for r in c.execute(queries.GET_PRIORITY_LEVELS):
# for row in c.execute(queries.GET_QUEUE_AT_PRIORITY, {'priority': r[0]}):
# # These are ordered with the highest priority first. Check until we find us.
# if int(row[3]) != int(controllerID):
# print(" incrementing priority")
# estimate += 1
# else:
# # Done!
# fountain.db_close(con)
# return estimate # We'll fall out of the loop due to closing the database on top of the iterator.
#
# fountain.db_close(con)
# return estimate # Don't change
# ######################################################################################################################
# Authentication and Control
# ######################################################################################################################
@get('/api/control/query')
def gQueryControl():
"""Returns the current control queue."""
con = fountain.db_connect()
c = con.cursor()
c.execute(queries.QUERY_CONTROL_QUEUE)
res = jsonRows(c)
fountain.db_close(con)
return res
@post('/api/control/query')
def pQueryControl():
"""Returns info about the 'true' queue position of this controllerID. If this is 0, you are in control."""
if not checkAPIKey():
return getAPIKeyFail()
if not 'controllerID' in request.json.keys():
return {'success': 'false', 'message': 'Must specify controllerID to query.'}
# TODO: Implement ETA too
return {'success': 'true', 'trueQueuePosition': getTrueQueuePosition(request.json['controllerID']), 'eta': getTrueEta(request.json['controllerID'])}
@post('/api/control/request')
def pRequestControl():
"""
Requests control for an API key and returns a controllerID to the user which they should watch to see when they gain
control.
"""
if not checkAPIKey():
return getAPIKeyFail()
if not 'requestedLength' in request.json.keys():
return {'success': 'false', 'Message': 'Must specify requested length for control.'}
con = fountain.db_connect()
c = con.cursor()
c.execute(queries.REQUEST_CONTROL, {'priority': getAPIKeyPriority(), 'ttl': request.json['requestedLength'],
'apikey': request.json['apikey']})
# No concurrency worries with lastrowid, I think, as it's per-connection.
controllerID = c.lastrowid
fountain.db_close(con)
# TODO: Implement variable length TTL part and some additional sanity checks.
return {'success': 'true', 'ttl': request.json['requestedLength'], 'controllerID': controllerID}
@post('/api/control/release')
def pReleaseControl():
"""Releases the control from a specific controllerID."""
if not checkAPIKey():
return getAPIKeyFail()
if not 'controllerID' in request.json.keys():
return {'success': 'false', 'message': 'Must specify controllerID to release.'}
con = fountain.db_connect()
c = con.cursor()
c.execute(queries.RELEASE_CONTROL, {'controllerID': request.json['controllerID']})
fountain.db_close(con)
return {'success': 'true', 'message': 'Control released.'}
# ######################################################################################################################
# Valve Interaction
# ######################################################################################################################
@get('/api/valves')
def gValves():
"""Queries the valve descriptions and current valve states."""
con = fountain.db_connect()
c = con.cursor()
c.execute(queries.QUERY_VALVES)
res = jsonRows(c)
fountain.db_close(con)
return res
@post('/api/valves')
def pValves():
"""Updates the valves based on a bitmask."""
if not checkAPIKey():
return getAPIKeyFail()
if not 'controllerID' in request.json.keys():
return {'success': 'false', 'message': 'Must specify controllerID to set valves.'}
if not 'bitmask' in request.json.keys():
return {'success': 'false', 'message': 'Must specify bitmask to set valves.'}
bm = int(request.json['bitmask']) # Store bitmask so we can shift it around while reading out the valve states
con = fountain.db_connect()
c = con.cursor()
for i in range(1, constants.NUM_VALVES + 1):
val = bm & 1
c.execute(queries.SET_VALVE, {'spraying': val, 'id': i})
bm >>= 1
fountain.db_close(con)
return {'success': 'true'}
@get('/api/valves/<id>')
def gValvesID(id):
"""Queries a specific valve."""
con = fountain.db_connect()
c = con.cursor()
c.execute(queries.QUERY_VALVE, {'id': id})
res = jsonRow(c)
fountain.db_close(con)
if res is None or len(res) == 0:
return {'success': 'false', 'message': 'Invalid valve ID.'}
else:
return res
@post('/api/valves/<id>')
def pValvesID(id):
"""Updates a single valve based on ID."""
if not checkAPIKey():
return getAPIKeyFail()
if not 'controllerID' in request.json.keys():
return {'success': 'false', 'message': 'Must specify controllerID to set valves.'}
if not 'spraying' in request.json.keys():
return {'success': 'false', 'message': 'Must specify spraying value to set.'}
con = fountain.db_connect()
c = con.cursor()
# TODO: check control
c.execute(queries.SET_VALVE, {'spraying': int(request.json['spraying']), 'id': id})
fountain.db_close(con)
return {'success': 'true'}
# ######################################################################################################################
# Pattern Management
# ######################################################################################################################
@get('/api/patterns')
def gPatterns():
"""Queries the known, enabled patterns."""
con = fountain.db_connect()
c = con.cursor()
c.execute(queries.QUERY_PATTERNS)
res = jsonRows(c)
fountain.db_close(con)
return res
@post('/api/patterns/<id>')
def pPatternsID(id):
"""Sets a specific pattern to active."""
if not checkAPIKey():
return getAPIKeyFail()
if not 'controllerID' in request.json.keys():
return {'success': 'false', 'message': 'Need to provide controllerID.'}
# TODO: check for control
# TODO: also disengage other patterns from playing (probably a separate query to run before this one)
con = fountain.db_connect()
c = con.cursor()
c.execute(queries.ENGAGE_PATTERN, {'id': id})
fountain.db_close(con)
return {'success': 'true'}
# ######################################################################################################################
# Database Debugging
# ######################################################################################################################
@get('/db/drop')
def gDBDrop():
"""Drop the known tables in the database."""
fountain.db_dropTables()
return "ok"
@get('/db/pop')
def gDBPop():
"""Populate the database with the default values."""
fountain.db_createTables()
fountain.db_loadDefaults()
return "ok"
def startAPI():
"""Starts the server API."""
print("API hook started...")
run(host='0.0.0.0', port=8080, quiet=True)