socket
This commit is contained in:
parent
3df799a491
commit
69cd99d3e3
2 changed files with 17 additions and 11 deletions
26
main.py
26
main.py
|
@ -1,5 +1,5 @@
|
||||||
import queue
|
import queue
|
||||||
from flask import Flask, render_template, request, make_response
|
from flask import Flask, render_template, request, make_response, flash
|
||||||
from flask_socketio import SocketIO #https://pythonprogramminglanguage.com/python-flask-websocket/
|
from flask_socketio import SocketIO #https://pythonprogramminglanguage.com/python-flask-websocket/
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import time
|
import time
|
||||||
|
@ -37,7 +37,7 @@ def list():
|
||||||
return '<form action="/list/user" method="get"> Search for User: <input name="user"><input type="submit"></form> <form action="/adduser" method="get"><button type="submit">Add User</button></form> <br></br>' + text
|
return '<form action="/list/user" method="get"> Search for User: <input name="user"><input type="submit"></form> <form action="/adduser" method="get"><button type="submit">Add User</button></form> <br></br>' + text
|
||||||
|
|
||||||
@app.route("/list/user", methods=['GET'])
|
@app.route("/list/user", methods=['GET'])
|
||||||
def user():
|
def user_info():
|
||||||
username = request.args.get("user")
|
username = request.args.get("user")
|
||||||
c.execute("SELECT * FROM users WHERE username = '%s'" % username)
|
c.execute("SELECT * FROM users WHERE username = '%s'" % username)
|
||||||
user = c.fetchall()
|
user = c.fetchall()
|
||||||
|
@ -89,28 +89,36 @@ def get_addtag_request():
|
||||||
except: #except im Normalen Code!
|
except: #except im Normalen Code!
|
||||||
return "Wrong user id!"
|
return "Wrong user id!"
|
||||||
users.put(user_id)
|
users.put(user_id)
|
||||||
|
print(f"put: {len(users.queue)}")
|
||||||
return render_template("addtag.html", user=user_id)
|
return render_template("addtag.html", user=user_id)
|
||||||
|
|
||||||
@socketio.on('addtag')
|
@socketio.on('addtag')
|
||||||
def request_addtag(data):
|
def request_addtag(data):
|
||||||
global finished
|
global finished
|
||||||
|
print(f"add tag: {len(users.queue)}")
|
||||||
if len(users.queue) > 0:
|
if len(users.queue) > 0:
|
||||||
user = users.queue[len(users.queue) - 1]
|
user = users.queue[len(users.queue) - 1]
|
||||||
if user == data["data"]:
|
if user == data["data"]:
|
||||||
socketio.emit("wait", "wait")
|
socketio.emit("wait", "wait")
|
||||||
i = 0
|
i = 0
|
||||||
while finished != data["data"]:
|
while finished != data["data"]:
|
||||||
|
print("waiting")
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
i += 1
|
i += 1
|
||||||
if i > 20:
|
if i > 20:
|
||||||
|
print("timeout")
|
||||||
socketio.emit("error", "timeout")
|
socketio.emit("error", "timeout")
|
||||||
nothing = users.get()
|
nothing = users.get()
|
||||||
pass
|
print(f"nothing: {nothing}")
|
||||||
c.execute(f"SELECT * FROM users WHERE id ={user}")
|
print("cleared")
|
||||||
username = c.fetchall()[0][1]
|
break
|
||||||
c.execute(f"SELECT * FROM tags WHERE userid={user}")
|
else:
|
||||||
tag_id = c.fetchall()[0][0]
|
finished = None
|
||||||
socketio.emit("finished", f"Added {tag_id} to {username}")
|
c.execute(f"SELECT * FROM users WHERE id ={user}")
|
||||||
|
username = c.fetchall()[0][1]
|
||||||
|
c.execute(f"SELECT * FROM tags WHERE userid={user}")
|
||||||
|
tag_id = c.fetchall()[0][0]
|
||||||
|
socketio.emit("finished", f"Added {tag_id} to {username}")
|
||||||
else:
|
else:
|
||||||
socketio.emit("busy", "busy")
|
socketio.emit("busy", "busy")
|
||||||
else:
|
else:
|
||||||
|
@ -119,7 +127,6 @@ def request_addtag(data):
|
||||||
#api
|
#api
|
||||||
@app.route("/api/tag_id", methods=['GET'])
|
@app.route("/api/tag_id", methods=['GET'])
|
||||||
def get_id():
|
def get_id():
|
||||||
global users
|
|
||||||
global finished
|
global finished
|
||||||
tag_id = int(request.args.get("id"))
|
tag_id = int(request.args.get("id"))
|
||||||
c.execute(f"SELECT * FROM tags WHERE tagid ={tag_id}")
|
c.execute(f"SELECT * FROM tags WHERE tagid ={tag_id}")
|
||||||
|
@ -143,4 +150,3 @@ def get_id():
|
||||||
def main():
|
def main():
|
||||||
atexit.register(exit_handler)
|
atexit.register(exit_handler)
|
||||||
socketio.run(app, host='0.0.0.0', port=5000)
|
socketio.run(app, host='0.0.0.0', port=5000)
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
socket.emit('addtag', {data: user})
|
socket.emit('addtag', {data: user})
|
||||||
});
|
});
|
||||||
socket.on("wait", function(){
|
socket.on("wait", function(){
|
||||||
document.write('<p>wait</p>')
|
document.write('<p>Pleas hold your tag on to the nfc reader</p>')
|
||||||
});
|
});
|
||||||
socket.on("error", function(data) {
|
socket.on("error", function(data) {
|
||||||
alert(data)
|
alert(data)
|
||||||
|
|
Loading…
Reference in a new issue