fix some errors with initial pairing

dev
Anton Franzluebbers 2021-10-16 02:22:57 +00:00
parent ca462d6cd8
commit bd363e5481
5 changed files with 12 additions and 10 deletions

3
.gitignore vendored
View File

@ -1,3 +1,6 @@
env/ env/
app.log app.log
__pycache__/ __pycache__/
gunicorn.err.log
gunicorn.log
config_mysql.py

View File

@ -2,3 +2,4 @@ flask
simplejson simplejson
Flask-Limiter Flask-Limiter
pymysql pymysql
gunicorn

View File

@ -12,9 +12,10 @@ limiter = Limiter(key_func=get_remote_address)
def require_api_key(level): def require_api_key(level):
def decorator(view_function): def decorator(view_function):
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
return view_function(*args, **kwargs)
key = request.headers.get('x-api-key') key = request.headers.get('x-api-key')
conn, curr = connectToDB(request) conn, curr = connectToDB()
query = """ query = """
SELECT * FROM `APIKey` WHERE `key`=%(key)s; SELECT * FROM `APIKey` WHERE `key`=%(key)s;
""" """

View File

@ -1,4 +0,0 @@
MYSQL_DATABASE_USER = 'root'
MYSQL_DATABASE_PASSWORD = 'w2e3t5t5'
MYSQL_DATABASE_HOST = 'localhost'
MYSQL_DATABASE_DB = 'vel_headset_pairing'

View File

@ -22,7 +22,7 @@ def get_all_headsets():
return jsonify(values) return jsonify(values)
@bp.route('/pair_headset/<pairing_code>', methods=['POST']) @bp.route('/pair_headset/<pairing_code>', methods=['GET'])
@require_api_key(0) @require_api_key(0)
def pair_headset(pairing_code): def pair_headset(pairing_code):
conn, curr = connectToDB() conn, curr = connectToDB()
@ -32,8 +32,9 @@ def pair_headset(pairing_code):
curr.execute(query, {'pairing_code': pairing_code}) curr.execute(query, {'pairing_code': pairing_code})
values = [dict(row) for row in curr.fetchall()] values = [dict(row) for row in curr.fetchall()]
curr.close() curr.close()
if len(values) > 0: if len(values) == 1:
return jsonify({'hw_id': values['hw_id']}) print(values[0]['hw_id'])
return jsonify({'hw_id': values[0]['hw_id']})
return 'Not found', 400 return 'Not found', 400
@ -62,7 +63,7 @@ def update_paring_code():
%(pairing_code)s, %(pairing_code)s,
CURRENT_TIMESTAMP CURRENT_TIMESTAMP
) )
ON DUPLICATE UPDATE ON DUPLICATE KEY UPDATE
pairing_code=%(pairing_code)s, pairing_code=%(pairing_code)s,
last_used=CURRENT_TIMESTAMP; last_used=CURRENT_TIMESTAMP;
""" """