-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebserver.py
More file actions
69 lines (53 loc) · 1.86 KB
/
webserver.py
File metadata and controls
69 lines (53 loc) · 1.86 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
from flask import Flask, render_template, send_file
from flask_socketio import SocketIO, emit
from drawpi.svgreader import slice, parse
from threading import Thread
import logging
from drawpi.runner import main
app = Flask(__name__)
app.config['SECRET_KEY'] = 'TopSecretMagic'
socketio = SocketIO(app)
thread = None
class RunningThread(Thread):
def __init__(self, commands, *args, **kwargs):
super().__init__(*args, **kwargs)
self.commands = commands
def run(self):
try:
main(self.commands)
socketio.emit("StatusUpdate", {"error": False, "text":"Draw Completed Successfully"})
except Exception as e:
logging.error(str(e))
socketio.emit("StatusUpdate", {"error": True, "text":"The application errored: "+ str(e)})
socketio.emit("NewStatus", "ready")
class LogSenderHandler(logging.Handler):
def emit(self, record):
log = self.format(record)
socketio.emit("CommandOutput", log)
return True
@app.route("/")
def index():
return send_file("site/index.html")
@socketio.on("SVGLoad")
def load_svg(text):
emit("SVGProcessed", slice(parse(text)))
@socketio.on("RunCommands")
def run_commands(commands):
global thread
if thread is None or not thread.is_alive():
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
level=logging.DEBUG,
handlers=[LogSenderHandler()]
)
thread = RunningThread(commands)
thread.start()
socketio.emit("NewStatus", "running")
emit("StatusUpdate", {"error": False, "text":"Draw has begun"})
else:
emit("StatusUpdate", {"error": True, "text":"Cannot Start while Running"})
def run():
socketio.run(app)
if __name__ == "__main__":
run()