diff options
author | Martin Ashby <martin@martin-laptop.lan> | 2018-05-17 18:18:11 +0100 |
---|---|---|
committer | Martin Ashby <martin@martin-laptop.lan> | 2018-05-17 18:18:11 +0100 |
commit | d40c7d9f09d9b012837d6060a4c598b23b19646f (patch) | |
tree | 8fa1f99664b3b6a74f7a0fd5856b7d0c41767b1f /server.py | |
parent | aa86cb02fd95404fdfba8cf9d13cb5c18138b6b5 (diff) | |
download | unicornpaint-d40c7d9f09d9b012837d6060a4c598b23b19646f.tar.gz unicornpaint-d40c7d9f09d9b012837d6060a4c598b23b19646f.tar.bz2 unicornpaint-d40c7d9f09d9b012837d6060a4c598b23b19646f.tar.xz unicornpaint-d40c7d9f09d9b012837d6060a4c598b23b19646f.zip |
Palette, tools, load & save working
Diffstat (limited to 'server.py')
-rw-r--r-- | server.py | 50 |
1 files changed, 44 insertions, 6 deletions
@@ -3,6 +3,9 @@ from flask import Flask, send_from_directory from flask_sockets import Sockets from gevent import pywsgi from geventwebsocket.handler import WebSocketHandler +import os +import os.path +import pickle try: import unicornhathd as unicorn @@ -10,24 +13,31 @@ try: except ImportError: from unicorn_hat_sim import unicornhathd as unicorn +# Actions that can be sent NO_OP = 'NO_OP' SET_PIXEL = 'SET_PIXEL' CLEAR = 'CLEAR' +SAVE = 'SAVE' +LOAD = 'LOAD' +# GLobal references to app & clients all_clients = set() - app = Flask(__name__, static_folder='build/') sockets = Sockets(app) def send_state(): global all_clients - jsonPixels = json.dumps(unicorn.get_pixels()) + state = { + "saves": os.listdir(SAVES_DIR), + "pixels": unicorn.get_pixels() + } + stateJson = json.dumps(state) for client in all_clients: - client.send(jsonPixels) + client.send(stateJson) def execute_command(command): cmd_type = command['type'] - if cmd_type == NO_OP: + if cmd_type == NO_OP: # Used just to get state without doing an action pass elif cmd_type == SET_PIXEL: x = int(command['x']) @@ -40,7 +50,28 @@ def execute_command(command): elif cmd_type == CLEAR: unicorn.clear() unicorn.show() + elif cmd_type == SAVE: + saveName = command["saveName"] + save(saveName) + elif cmd_type == LOAD: + saveName = command["saveName"] + load(saveName) + +# Constants +SAVES_DIR = 'saves/' +def save(saveName): + with open(os.path.join(SAVES_DIR, saveName), "wb") as f: + pickle.dump(unicorn.get_pixels(), f) + +def load(saveName): + with open(os.path.join(SAVES_DIR, saveName), "rb") as f: + pixels = pickle.load(f) + for x, row in enumerate(pixels): + for y, pixel in enumerate(row): + unicorn.set_pixel(x, y, *pixel) + unicorn.show() + @sockets.route('/ws') def do_websocket(websocket): global all_clients @@ -48,6 +79,8 @@ def do_websocket(websocket): try: while not websocket.closed: command = websocket.receive() + if not command: + break cmd = json.loads(command) execute_command(cmd) send_state() @@ -61,11 +94,16 @@ def send_static(path): def main(): try: + os.mkdir(SAVES_DIR) + except: + pass + try: print("Serving on port 3001") server = pywsgi.WSGIServer(('', 3001), app, handler_class=WebSocketHandler) server.serve_forever() - except: + finally: unicorn.off() if __name__=="__main__": - main()
\ No newline at end of file + main() +
\ No newline at end of file |