From e3b58a083e65cafd3042cdc2650cf8b39101f1e4 Mon Sep 17 00:00:00 2001 From: "Ryan J. Dillon" Date: Tue, 22 Dec 2020 11:20:43 +0100 Subject: [PATCH 1/5] Add import for WebSocketError to application module --- kiln-controller.py | 1 + 1 file changed, 1 insertion(+) diff --git a/kiln-controller.py b/kiln-controller.py index 1a061b8..8e5e5aa 100755 --- a/kiln-controller.py +++ b/kiln-controller.py @@ -11,6 +11,7 @@ import geventwebsocket #from bottle import post, get from gevent.pywsgi import WSGIServer from geventwebsocket.handler import WebSocketHandler +from geventwebsocket import WebSocketError try: From 3b919a4a59afa7fe90bce5c5469fa4ace1b05268 Mon Sep 17 00:00:00 2001 From: "Ryan J. Dillon" Date: Tue, 22 Dec 2020 15:26:57 +0100 Subject: [PATCH 2/5] 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") From 9de2db739a4607252642b8afbc4f1af8a420a218 Mon Sep 17 00:00:00 2001 From: "Ryan J. Dillon" Date: Tue, 22 Dec 2020 15:27:55 +0100 Subject: [PATCH 3/5] Remove trailing whitespace --- lib/oven.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/oven.py b/lib/oven.py index ce9dce2..0e53292 100644 --- a/lib/oven.py +++ b/lib/oven.py @@ -118,7 +118,7 @@ class Oven (threading.Thread): heat_off = float(self.time_step * (1 - pid)) time_left = self.totaltime - self.runtime - log.info("temp=%.1f, target=%.1f, pid=%.3f, heat_on=%.2f, heat_off=%.2f, run_time=%d, total_time=%d, time_left=%d" % + log.info("temp=%.1f, target=%.1f, pid=%.3f, heat_on=%.2f, heat_off=%.2f, run_time=%d, total_time=%d, time_left=%d" % (self.temp_sensor.temperature + config.thermocouple_offset, self.target, pid, @@ -161,13 +161,13 @@ class Oven (threading.Thread): if config.heater_invert: GPIO.output(config.gpio_heat, GPIO.LOW) time.sleep(self.time_step * value) - GPIO.output(config.gpio_heat, GPIO.HIGH) + GPIO.output(config.gpio_heat, GPIO.HIGH) else: GPIO.output(config.gpio_heat, GPIO.HIGH) time.sleep(self.time_step * value) GPIO.output(config.gpio_heat, GPIO.LOW) else: - #for runs that are simulations + # for runs that are simulations time.sleep(self.time_step * value) else: self.heat = 0.0 @@ -224,7 +224,7 @@ class TempSensorReal(TempSensor): maxtries = 5 sleeptime = self.time_step / float(maxtries) - maxtemp = 0 + maxtemp = 0 for x in range(0,maxtries): try: temp = self.thermocouple.get() From 084d302b43f08d2eb303ec2c5ea9bab815751f6f Mon Sep 17 00:00:00 2001 From: "Ryan J. Dillon" Date: Tue, 22 Dec 2020 15:29:11 +0100 Subject: [PATCH 4/5] Prevent CPU overload on idle condition --- lib/oven.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/oven.py b/lib/oven.py index 0e53292..3ff9dc3 100644 --- a/lib/oven.py +++ b/lib/oven.py @@ -98,7 +98,9 @@ class Oven (threading.Thread): pid = 0 while True: - if self.state == Oven.STATE_RUNNING: + if self.state == Oven.STATE_IDLE: + time.sleep(1) + elif self.state == Oven.STATE_RUNNING: if self.simulate: self.runtime += 0.5 else: @@ -137,13 +139,14 @@ class Oven (threading.Thread): if(self.temp_sensor.temperature + config.thermocouple_offset >= config.emergency_shutoff_temp): log.info("emergency!!! temperature too high, shutting down") self.reset() - - #Capture the last temperature value. This must be done before set_heat, since there is a sleep in there now. + + # Capture the last temperature value. This must be done before set_heat, + # since there is a sleep in there now. last_temp = self.temp_sensor.temperature + config.thermocouple_offset - + self.set_heat(pid) - if self.runtime >= self.totaltime: + if self.runtime > self.totaltime: log.info("schedule ended, shutting down") self.reset() From bac1a2d78963a91ec8173d2dd895e36a4736591e Mon Sep 17 00:00:00 2001 From: "Ryan J. Dillon" Date: Tue, 22 Dec 2020 15:31:06 +0100 Subject: [PATCH 5/5] Remove space in print statements --- lib/ovenWatcher.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ovenWatcher.py b/lib/ovenWatcher.py index d26be62..d9774fe 100644 --- a/lib/ovenWatcher.py +++ b/lib/ovenWatcher.py @@ -66,10 +66,10 @@ class OvenWatcher(threading.Thread): 'log': self.lastlog_subset(), #'started': self.started } - print (backlog) + print(backlog) backlog_json = json.dumps(backlog) try: - print (backlog_json) + print(backlog_json) observer.send(backlog_json) except: log.error("Could not send backlog to new observer")