From 57e5ea1f424ea2567e10c3945d9273a92a547332 Mon Sep 17 00:00:00 2001 From: Martin Ashby Date: Tue, 15 May 2018 22:12:09 +0100 Subject: Basic should be working --- server.py | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 server.py (limited to 'server.py') diff --git a/server.py b/server.py new file mode 100644 index 0000000..84511f5 --- /dev/null +++ b/server.py @@ -0,0 +1,70 @@ +import json +from flask import Flask, send_from_directory +from flask_sockets import Sockets +from gevent import pywsgi +from geventwebsocket.handler import WebSocketHandler + +try: + import unicornhathd as unicorn + print("unicorn hat hd detected") +except ImportError: + from unicorn_hat_sim import unicornhathd as unicorn + +NO_OP = 'NO_OP' +SET_PIXEL = 'SET_PIXEL' +CLEAR = 'CLEAR' + +all_clients = set() + +app = Flask(__name__, static_folder='build/') +sockets = Sockets(app) + +def send_state(): + global all_clients + jsonPixels = json.dumps(unicorn.get_pixels()) + for client in all_clients: + client.send(jsonPixels) + +def execute_command(command): + cmd_type = command['type'] + if cmd_type == NO_OP: + pass + elif cmd_type == SET_PIXEL: + x = int(command['x']) + y = int(command['y']) + r = int(command['r']) + g = int(command['g']) + b = int(command['b']) + unicorn.set_pixel(x, y, r, g, b) + unicorn.show() + elif cmd_type == CLEAR: + unicorn.clear() + unicorn.show() + +@sockets.route('/ws') +def do_websocket(websocket): + global all_clients + all_clients.add(websocket) + try: + while not websocket.closed: + command = websocket.receive() + cmd = json.loads(command) + execute_command(cmd) + send_state() + finally: + all_clients.remove(websocket) + + +@app.route('/') +def send_static(path): + return send_from_directory('build/', path) + +def main(): + try: + server = pywsgi.WSGIServer(('', 3001), app, handler_class=WebSocketHandler) + server.serve_forever() + except: + unicorn.off() + +if __name__=="__main__": + main() \ No newline at end of file -- cgit v1.2.3-ZIG