From 3b919a4a59afa7fe90bce5c5469fa4ace1b05268 Mon Sep 17 00:00:00 2001 From: "Ryan J. Dillon" Date: Tue, 22 Dec 2020 15:26:57 +0100 Subject: [PATCH] Check for valid websocket message before processing --- kiln-controller.py | 52 ++++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/kiln-controller.py b/kiln-controller.py index 8e5e5aa..9e7795e 100755 --- a/kiln-controller.py +++ b/kiln-controller.py @@ -111,31 +111,33 @@ def handle_control(): while True: try: message = wsock.receive() - log.info("Received (control): %s" % message) - msgdict = json.loads(message) - if msgdict.get("cmd") == "RUN": - log.info("RUN command received") - profile_obj = msgdict.get('profile') - if profile_obj: - profile_json = json.dumps(profile_obj) - profile = Profile(profile_json) - oven.run_profile(profile) - ovenWatcher.record(profile) - elif msgdict.get("cmd") == "SIMULATE": - log.info("SIMULATE command received") - #profile_obj = msgdict.get('profile') - #if profile_obj: - # profile_json = json.dumps(profile_obj) - # profile = Profile(profile_json) - #simulated_oven = Oven(simulate=True, time_step=0.05) - #simulation_watcher = OvenWatcher(simulated_oven) - #simulation_watcher.add_observer(wsock) - #simulated_oven.run_profile(profile) - #simulation_watcher.record(profile) - elif msgdict.get("cmd") == "STOP": - log.info("Stop command received") - oven.abort_run() - except WebSocketError: + if message: + log.info("Received (control): %s" % message) + msgdict = json.loads(message) + if msgdict.get("cmd") == "RUN": + log.info("RUN command received") + profile_obj = msgdict.get('profile') + if profile_obj: + profile_json = json.dumps(profile_obj) + profile = Profile(profile_json) + oven.run_profile(profile) + ovenWatcher.record(profile) + elif msgdict.get("cmd") == "SIMULATE": + log.info("SIMULATE command received") + #profile_obj = msgdict.get('profile') + #if profile_obj: + # profile_json = json.dumps(profile_obj) + # profile = Profile(profile_json) + #simulated_oven = Oven(simulate=True, time_step=0.05) + #simulation_watcher = OvenWatcher(simulated_oven) + #simulation_watcher.add_observer(wsock) + #simulated_oven.run_profile(profile) + #simulation_watcher.record(profile) + elif msgdict.get("cmd") == "STOP": + log.info("Stop command received") + oven.abort_run() + except WebSocketError as e: + log.error(e) break log.info("websocket (control) closed")