From e954dbefc086e16a71c51968bad1f2e675530f40 Mon Sep 17 00:00:00 2001 From: Vitor de Miranda Henrique Date: Fri, 15 Jun 2018 13:53:00 -0500 Subject: [PATCH 01/11] dev new features --- octoprint_enclosure/__init__.py | 115 +++++++++++++++--- octoprint_enclosure/static/js/enclosure.js | 7 +- .../templates/enclosure_settings.jinja2 | 88 ++++++++------ 3 files changed, 157 insertions(+), 53 deletions(-) diff --git a/octoprint_enclosure/__init__.py b/octoprint_enclosure/__init__.py index 683d4d6..72e108e 100644 --- a/octoprint_enclosure/__init__.py +++ b/octoprint_enclosure/__init__.py @@ -118,16 +118,29 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, self.print_complete = False def get_settings_version(self): - return 4 + return 6 def on_settings_migrate(self, target, current=None): - self._logger.warn("######### settings not compatible #########") - self._logger.warn("######### current settings version %s target settings version %s #########", - current, target) - self._settings.set(["rpi_outputs"], []) - self._settings.set(["rpi_inputs"], []) - self.rpi_outputs = self._settings.get(["rpi_outputs"]) - self.rpi_inputs = self._settings.get(["rpi_inputs"]) + self._logger.warn("######### current settings version %s target settings version %s #########", current, target) + + if current >= 4 and target == 6: + self._logger.warn( + "######### migrating settings to v6 #########") + for rpi_output in old_outputs: + if 'shutdown_on_failed' not in rpi_output: + rpi_output['shutdown_on_failed'] = False + if 'shutdown_on_failed' not in rpi_output: + if current == 4 and target == 5: + self._logger.warn("######### migrating settings from v4 to v5 #########") + old_outputs = self._settings.get(["rpi_outputs"]) + for rpi_output in old_outputs: + rpi_output['shutdown_on_failed'] = False + self._settings.set(["rpi_outputs"], old_outputs) + else: + self._logger.warn("######### settings not compatible #########") + self._settings.set(["rpi_outputs"], []) + self._settings.set(["rpi_inputs"], []) + self.rpi_inputs = self._settings.get(["rpi_inputs"]) # ~~ Blueprintplugin mixin @octoprint.plugin.BlueprintPlugin.route("/setEnclosureTempHum", methods=["GET"]) @@ -175,6 +188,19 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, self.write_gpio(self.to_int(rpi_output['gpio_pin']), val) return flask.jsonify(success=True) + @octoprint.plugin.BlueprintPlugin.route("/sendShellCommand", methods=["GET"]) + def send_shell_command(self): + output_index = self.to_int(flask.request.values["index_id"]) + + rpi_output = [r_out for r_out in self.rpi_outputs if self.to_int( + r_out['index_id']) == output_index].pop() + + if rpi_output: + command = rpi_output['shell_script'] + self.shell_command(command) + return flask.jsonify(success=True) + + @octoprint.plugin.BlueprintPlugin.route("/setAutoStartUp", methods=["GET"]) def set_auto_startup(self): index = flask.request.values["index_id"] @@ -262,6 +288,16 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, return flask.jsonify(success=True) + def shell_command(self, command): + try: + stdout = (Popen(command, shell=True, stdout=PIPE).stdout).read() + self._plugin_manager.send_plugin_message( + self._identifier, dict(is_msg=True, msg=stdout, msg_type="success")) + except Exception as ex: + self.log_error(ex) + self._plugin_manager.send_plugin_message( + self._identifier, dict(is_msg=True, msg="Could not execute shell script", msg_type="error")) + def send_neopixel_command(self, led_pin, led_count, led_brightness, red, green, blue, address, neopixel_dirrect, index_id, queue_id=None): """Send neopixel command @@ -790,7 +826,7 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, tempstr + " as pin numbers. Please update GPIO accordingly!" self._logger.info(warn_msg) self._plugin_manager.send_plugin_message( - self._identifier, dict(isMsg=True, msg=warn_msg)) + self._identifier, dict(is_msg=True, msg=warn_msg, msg_type="error")) GPIO.setwarnings(False) except Exception as ex: self.log_error(ex) @@ -978,6 +1014,9 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, str(rpi_input['label']) + \ ". Sending GCODE command" self.send_notification(msg) + if rpi_output['output_type'] == 'shell_output': + command = rpi_output['shell_script'] + self.shell_command(command) except Exception as ex: self.log_error(ex) pass @@ -1007,11 +1046,18 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, self._printer.cancel_print() elif rpi_input['printer_action'] == 'toggle': self._logger.info("Printer action toggle.") + if self._printer.is_operational(): + self._printer.toggle_pause_print() + else: + self._printer.connect() + elif rpi_input['printer_action'] == 'start': + self._printer.start_print() + elif rpi_input['printer_action'] == 'toggle_job': if self._printer.is_operational(): if self._printer.is_printing(): - self._printer.pause_print() - elif self._printer.is_paused(): - self._printer.resume_print() + self._printer.cancel_print() + elif self._printer.is_ready(): + self._printer.start_print() else: self._printer.connect() elif rpi_input['printer_action'] == 'stop_temp_hum_control': @@ -1157,8 +1203,6 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, delay_seconds = self.to_float(shutdown_time) self.schedule_auto_shutdown_outputs( rpi_output, delay_seconds) - if rpi_output['output_type'] == 'temp_hum_control': - rpi_output['temp_ctr_set_value'] = 0 self.run_tasks() self.update_ui() @@ -1198,6 +1242,10 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, if (rpi_output['output_type'] == 'neopixel_indirect' or rpi_output['output_type'] == 'neopixel_direct'): self.add_neopixel_output_to_queue( rpi_output, shutdown_delay_seconds, 0, 0, 0, sufix) + if rpi_output['output_type'] == 'temp_hum_control': + value = 0 + self.add_temperature_output_temperature_queue( + delay_seconds, rpi_output, value, sufix) if self._settings.get(["debug"]) is True: self._logger.info("Events scheduled to run %s", self.event_queue) @@ -1240,7 +1288,8 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, self.add_neopixel_output_to_queue( rpi_output, delay_seconds, red, green, blue, sufix) if rpi_output['output_type'] == 'temp_hum_control': - rpi_output['temp_ctr_set_value'] = rpi_output['temp_ctr_default_value'] + value = rpi_output['temp_ctr_default_value'] + self.add_temperature_output_temperature_queue(delay_seconds, rpi_output, value, sufix) if self._settings.get(["debug"]) is True: self._logger.info("Events scheduled to run %s", self.event_queue) @@ -1325,6 +1374,42 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, self.event_queue.append(dict(queue_id=queue_id, thread=thread)) + def add_temperature_output_temperature_queue(self, delay_seconds, rpi_output, value, sufix): + queue_id = '{0!s}_{1!s}'.format(rpi_output['index_id'], sufix) + if self._settings.get(["debug"]) is True: + self._logger.info("Scheduling temperature control id %s on %s delay_seconds", queue_id, delay_seconds) + + thread = threading.Timer(delay_seconds, + self.write_temperature_to_output, + args=[self.to_int(rpi_output['index_id']), value, queue_id]) + + self.event_queue.append(dict(queue_id=queue_id, thread=thread)) + + def write_temperature_to_output(self, rpi_output_index, value, queue_id=None): + try: + rpi_output = [r_out for r_out in self.rpi_outputs if self.to_int( + r_out['index_id']) == rpi_output_index].pop() + + if rpi_output['output_type'] == 'temp_hum_control': + rpi_output['temp_ctr_set_value'] = value + + if self._settings.get(["debug"]) is True: + if queue_id is not None: + self._logger.info("Runing scheduled queue id %s", queue_id) + self._logger.info( + "Setting temperature to output index: %s value %s", rpi_output['index_id'], value) + + self.update_ui() + if queue_id is not None: + self.stop_queue_item(queue_id) + + except Exception as ex: + template = "An exception of type {0} occurred on {1} when writing on pin {2}. Arguments:\n{3!r}" + message = template.format( + type(ex).__name__, inspect.currentframe().f_code.co_name, gpio, ex.args) + self._logger.warn(message) + pass + def get_startup_delay_from_output(self, rpi_output): start_up_time = rpi_output['startup_time'] if self.is_hour(start_up_time): diff --git a/octoprint_enclosure/static/js/enclosure.js b/octoprint_enclosure/static/js/enclosure.js index 36c391a..14488e0 100644 --- a/octoprint_enclosure/static/js/enclosure.js +++ b/octoprint_enclosure/static/js/enclosure.js @@ -21,7 +21,7 @@ $(function () { self.settings_possible_outputs = ko.pureComputed(function () { return ko.utils.arrayFilter(self.settingsViewModel.settings.plugins.enclosure.rpi_outputs(), function (item) { - return ((item.output_type() === "regular" && !item.toggle_timer()) || item.output_type() === "gcode_output"); + return ((item.output_type() === "regular" && !item.toggle_timer()) || item.output_type() === "gcode_output" || item.output_type() === "shell_output"); }); }); @@ -226,11 +226,11 @@ $(function () { }) } - if (data.isMsg) { + if (data.is_msg) { new PNotify({ title: "Enclosure", text: data.msg, - type: "error" + type: data.msg_type }); } }; @@ -348,6 +348,7 @@ $(function () { index_id: ko.observable(nextIndex), label: ko.observable("Ouput " + nextIndex), output_type: ko.observable("regular"), + shell_script: ko.observable(""), gpio_pin: ko.observable(0), gpio_status: ko.observable(false), hide_btn_ui: ko.observable(false), diff --git a/octoprint_enclosure/templates/enclosure_settings.jinja2 b/octoprint_enclosure/templates/enclosure_settings.jinja2 index 1be738a..1e0cc68 100644 --- a/octoprint_enclosure/templates/enclosure_settings.jinja2 +++ b/octoprint_enclosure/templates/enclosure_settings.jinja2 @@ -53,6 +53,12 @@ +
+ +
@@ -70,6 +76,15 @@ Id used for API control
+ +
+ +
+ + Shell script to be executed +
+
+
@@ -88,45 +103,45 @@
- -
-
- - Link PWM ouput to temperature. PWM output will be interpolated between the point from duty A, temperature A -> duty B, temperature B. - This output will automatomatically start when a print starts and will default to the default duty cycle when print is complete. - -
+
+
+ + Link PWM ouput to temperature. PWM output will be interpolated between the point from duty A, temperature A -> duty + B, temperature B. This output will automatomatically start when a print starts and will default to the default + duty cycle when print is complete. +
- +
+ -
- -
- -
+
+ +
+
-
- -
- -
+
+
+ +
+
-
- -
- -
+
+
+ +
+
-
- -
- -
+
+
+ +
+
+
@@ -212,15 +227,16 @@ Time delay in secconds to turn on GPIO when print starts OR exact time that the event should happen, note that events will only be scheduled after a print starts, time should be formated as HH:MM on a 24 hours format, don't forget to check timezone of your raspberry pi. - Attention - Hour schedule does not work with Link PWM to Temperature option + Attention + Hour schedule does not work with + Link PWM to Temperature option
- +
- +
+ +

+ + Shell Script +

+
+ +
+ +

@@ -292,4 +302,4 @@

- + \ No newline at end of file From 8a284caaa221195d93a30f165ba94faa1ee6d51b Mon Sep 17 00:00:00 2001 From: Vitor de Miranda Henrique Date: Fri, 15 Jun 2018 14:23:22 -0500 Subject: [PATCH 03/11] improved msg when sending shell scripts --- octoprint_enclosure/__init__.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/octoprint_enclosure/__init__.py b/octoprint_enclosure/__init__.py index f6b1aca..aa227d7 100644 --- a/octoprint_enclosure/__init__.py +++ b/octoprint_enclosure/__init__.py @@ -194,7 +194,7 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, return flask.jsonify(success=True) @octoprint.plugin.BlueprintPlugin.route("/sendShellCommand", methods=["GET"]) - def send_shell_command(self): + def send_send_shell_command(self): output_index = self.to_int(flask.request.values["index_id"]) rpi_output = [r_out for r_out in self.rpi_outputs if self.to_int( r_out['index_id']) == output_index].pop() @@ -289,11 +289,13 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, return flask.jsonify(success=True) - def shell_command(self, command): + def send_shell_command(self, command): try: stdout = (Popen(command, shell=True, stdout=PIPE).stdout).read() - self._plugin_manager.send_plugin_message( - self._identifier, dict(is_msg=True, msg=stdout, msg_type="success")) + + response = stdout or "Command executed with no return value." + + self._plugin_manager.send_plugin_message(self._identifier, dict(is_msg=True, msg=response, msg_type="success")) except Exception as ex: self.log_error(ex) self._plugin_manager.send_plugin_message( @@ -1056,7 +1058,7 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, self.send_notification(msg) if rpi_output['output_type'] == 'shell_output': command = rpi_output['shell_script'] - self.shell_command(command) + self.send_shell_command(command) self.log_error(ex) pass From 23f4d47a35335829a5627a5ff87530cc1db7da37 Mon Sep 17 00:00:00 2001 From: Vitor de Miranda Henrique Date: Fri, 15 Jun 2018 14:27:48 -0500 Subject: [PATCH 04/11] cleaning empty lines --- README.md | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/README.md b/README.md index f7f5ef3..f1cbe4f 100644 --- a/README.md +++ b/README.md @@ -200,8 +200,4 @@ You just need to add the following section: - control - gcodeviewer - terminal - - plugin_enclosure
-
-
-     
-
+      - plugin_enclosure
\ No newline at end of file

From 3d9a99c82da66f743580bdae7122f1d2a696fcdb Mon Sep 17 00:00:00 2001
From: Vitor de Miranda Henrique 
Date: Fri, 15 Jun 2018 14:29:00 -0500
Subject: [PATCH 05/11] .

---
 README.md | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index f1cbe4f..3c6bb57 100644
--- a/README.md
+++ b/README.md
@@ -200,4 +200,5 @@ You just need to add the following section:
       - control
       - gcodeviewer
       - terminal
-      - plugin_enclosure
\ No newline at end of file
+      - plugin_enclosure
+
\ No newline at end of file From 4ed768096751aa8fe0538051299f4136105cc28f Mon Sep 17 00:00:00 2001 From: Vitor de Miranda Henrique Date: Mon, 18 Jun 2018 13:56:40 -0500 Subject: [PATCH 06/11] fixing handle_initial_gpio_control --- octoprint_enclosure/__init__.py | 56 ++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/octoprint_enclosure/__init__.py b/octoprint_enclosure/__init__.py index aa227d7..2473b92 100644 --- a/octoprint_enclosure/__init__.py +++ b/octoprint_enclosure/__init__.py @@ -125,7 +125,8 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, return 6 def on_settings_migrate(self, target, current=None): - self._logger.warn("######### current settings version %s target settings version %s #########", current, target) + self._logger.warn( + "######### current settings version %s target settings version %s #########", current, target) if current >= 4 and target == 6: self._logger.warn( @@ -137,12 +138,13 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, if 'shell_script' not in rpi_output: rpi_output['shell_script'] = "" if current == 4 and target == 5: - self._logger.warn("######### migrating settings from v4 to v5 #########") + self._logger.warn( + "######### migrating settings from v4 to v5 #########") old_outputs = self._settings.get(["rpi_outputs"]) for rpi_output in old_outputs: rpi_output['shutdown_on_failed'] = False self._settings.set(["rpi_outputs"], old_outputs) - else: + else: self._logger.warn("######### settings not compatible #########") self._settings.set(["rpi_outputs"], []) self._settings.set(["rpi_inputs"], []) @@ -201,7 +203,6 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, self.send_gcode_command(rpi_output['shell_script']) return flask.jsonify(success=True) - @octoprint.plugin.BlueprintPlugin.route("/setAutoStartUp", methods=["GET"]) def set_auto_startup(self): index = flask.request.values["index_id"] @@ -295,7 +296,8 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, response = stdout or "Command executed with no return value." - self._plugin_manager.send_plugin_message(self._identifier, dict(is_msg=True, msg=response, msg_type="success")) + self._plugin_manager.send_plugin_message( + self._identifier, dict(is_msg=True, msg=response, msg_type="success")) except Exception as ex: self.log_error(ex) self._plugin_manager.send_plugin_message( @@ -616,7 +618,8 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, temp, hum = stdout.split("|") return (self.to_float(temp.strip()), self.to_float(hum.strip())) except Exception as ex: - self._logger.info("Failed to excecute python scripts, try disabling use SUDO on advanced section of the plugin.") + self._logger.info( + "Failed to excecute python scripts, try disabling use SUDO on advanced section of the plugin.") self.log_error(ex) return (0, 0) @@ -637,7 +640,8 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, temp, hum = stdout.split("|") return (self.to_float(temp.strip()), self.to_float(hum.strip())) except Exception as ex: - self._logger.info("Failed to excecute python scripts, try disabling use SUDO on advanced section of the plugin.") + self._logger.info( + "Failed to excecute python scripts, try disabling use SUDO on advanced section of the plugin.") self.log_error(ex) return (0, 0) @@ -658,7 +662,8 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, temp, hum = stdout.split("|") return (self.to_float(temp.strip()), self.to_float(hum.strip())) except Exception as ex: - self._logger.info("Failed to excecute python scripts, try disabling use SUDO on advanced section of the plugin.") + self._logger.info( + "Failed to excecute python scripts, try disabling use SUDO on advanced section of the plugin.") self.log_error(ex) return (0, 0) @@ -700,7 +705,8 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, self._logger.info("TMP102 result: %s", stdout) return self.to_float(stdout.strip()) except Exception as ex: - self._logger.info("Failed to excecute python scripts, try disabling use SUDO on advanced section.") + self._logger.info( + "Failed to excecute python scripts, try disabling use SUDO on advanced section.") self.log_error(ex) return 0 @@ -969,8 +975,10 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, last_detected_time = list(filter(lambda item: item['index_id'] == filament_sensor['index_id'], self.last_filament_end_detected)).pop()['time'] time_now = time.time() - time_difference = self.to_int(time_now - last_detected_time) - time_out_value = self.to_int(filament_sensor['filament_sensor_timeout']) + time_difference = self.to_int( + time_now - last_detected_time) + time_out_value = self.to_int( + filament_sensor['filament_sensor_timeout']) if time_difference > time_out_value: self._logger.info("Detected end of filament.") for item in self.last_filament_end_detected: @@ -1033,7 +1041,9 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, pass def handle_initial_gpio_control(self): - for rpi_input in [r_inp for r_inp in self.rpi_inputs if r_inp['action_type'] == 'output_control']: + + for filament_sensor in list(filter(lambda item: item['input_type'] == 'gpio' and + item['action_type'] == 'output_control', self.rpi_inputs)): gpio_pin = self.to_int(rpi_input['gpio_pin']) controlled_io = self.to_int(rpi_input['controlled_io']) if (rpi_input['edge'] == 'fall') ^ GPIO.input(gpio_pin): @@ -1092,8 +1102,10 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, else: self._printer.connect() elif rpi_input['printer_action'] == 'start': + self._logger.info("Printer action start.") self._printer.start_print() elif rpi_input['printer_action'] == 'toggle_job': + self._logger.info("Printer action toggle_job.") if self._printer.is_operational(): if self._printer.is_printing(): self._printer.cancel_print() @@ -1138,7 +1150,7 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, def write_pwm(self, gpio, pwm_value, queue_id=None): try: if queue_id is not None and self._settings.get(["debug"]) is True: - self._logger.info("Runing scheduled queue id %s", queue_id) + self._logger.info("Runing scheduled queue id %s", queue_id) for pwm in self.pwm_intances: if gpio in pwm: pwm_object = pwm[gpio] @@ -1343,7 +1355,8 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, rpi_output, delay_seconds, red, green, blue, sufix) if rpi_output['output_type'] == 'temp_hum_control': value = rpi_output['temp_ctr_default_value'] - self.add_temperature_output_temperature_queue(delay_seconds, rpi_output, value, sufix) + self.add_temperature_output_temperature_queue( + delay_seconds, rpi_output, value, sufix) if self._settings.get(["debug"]) is True: self._logger.info("Events scheduled to run %s", self.event_queue) @@ -1379,7 +1392,8 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, queue_id = '{0!s}_{1!s}'.format(index_id, sufix) if self._settings.get(["debug"]) is True: - self._logger.info("Scheduling neopixel output id %s for on %s delay_seconds", queue_id, delay_seconds) + self._logger.info( + "Scheduling neopixel output id %s for on %s delay_seconds", queue_id, delay_seconds) thread = threading.Timer(delay_seconds, self.send_neopixel_command, @@ -1391,7 +1405,8 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, queue_id = '{0!s}_{1!s}'.format(rpi_output['index_id'], sufix) if self._settings.get(["debug"]) is True: - self._logger.info("Scheduling pwm output id %s for on %s delay_seconds", queue_id, delay_seconds) + self._logger.info( + "Scheduling pwm output id %s for on %s delay_seconds", queue_id, delay_seconds) thread = threading.Timer(delay_seconds, self.write_pwm, @@ -1407,7 +1422,8 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, args=[rpi_output, value, queue_id]) if self._settings.get(["debug"]) is True: - self._logger.info("Scheduling pwm linked temp output id %s on %s delay_seconds", queue_id, delay_seconds) + self._logger.info( + "Scheduling pwm linked temp output id %s on %s delay_seconds", queue_id, delay_seconds) self.event_queue.append(dict(queue_id=queue_id, thread=thread)) @@ -1420,7 +1436,8 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, queue_id = '{0!s}_{1!s}'.format(rpi_output['index_id'], sufix) if self._settings.get(["debug"]) is True: - self._logger.info("Scheduling regular output id %s on %s delay_seconds", queue_id, delay_seconds) + self._logger.info( + "Scheduling regular output id %s on %s delay_seconds", queue_id, delay_seconds) thread = threading.Timer(delay_seconds, self.write_gpio, @@ -1431,7 +1448,8 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, def add_temperature_output_temperature_queue(self, delay_seconds, rpi_output, value, sufix): queue_id = '{0!s}_{1!s}'.format(rpi_output['index_id'], sufix) if self._settings.get(["debug"]) is True: - self._logger.info("Scheduling temperature control id %s on %s delay_seconds", queue_id, delay_seconds) + self._logger.info( + "Scheduling temperature control id %s on %s delay_seconds", queue_id, delay_seconds) thread = threading.Timer(delay_seconds, self.write_temperature_to_output, From 2f3de65c44e0f4a69d24c1bd02a1cd856d498391 Mon Sep 17 00:00:00 2001 From: Vitor de Miranda Henrique Date: Mon, 18 Jun 2018 14:27:20 -0500 Subject: [PATCH 07/11] another fix for handle_initial_gpio_control --- octoprint_enclosure/__init__.py | 41 +++++++++++---------------------- 1 file changed, 13 insertions(+), 28 deletions(-) diff --git a/octoprint_enclosure/__init__.py b/octoprint_enclosure/__init__.py index 2473b92..4d31d64 100644 --- a/octoprint_enclosure/__init__.py +++ b/octoprint_enclosure/__init__.py @@ -1041,34 +1041,19 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, pass def handle_initial_gpio_control(self): - - for filament_sensor in list(filter(lambda item: item['input_type'] == 'gpio' and - item['action_type'] == 'output_control', self.rpi_inputs)): - gpio_pin = self.to_int(rpi_input['gpio_pin']) - controlled_io = self.to_int(rpi_input['controlled_io']) - if (rpi_input['edge'] == 'fall') ^ GPIO.input(gpio_pin): - rpi_output = [r_out for r_out in self.rpi_outputs if self.to_int( - r_out['index_id']) == controlled_io].pop() - if rpi_output['output_type'] == 'regular': - val = GPIO.LOW if rpi_input['controlled_io_set_value'] == 'low' else GPIO.HIGH - self.write_gpio(self.to_int( - rpi_output['gpio_pin']), val) - for notification in self.notifications: - if notification['gpioAction']: - msg = "GPIO control action caused by input " + str(rpi_input['label']) + ". Setting GPIO" + str( - rpi_input['controlled_io']) + " to: " + str(rpi_input['controlled_io_set_value']) - self.send_notification(msg) - if rpi_output['output_type'] == 'gcode_output': - self.send_gcode_command(rpi_output['gcode']) - for notification in self.notifications: - if notification['gpioAction']: - msg = "GPIO control action caused by input " + \ - str(rpi_input['label']) + \ - ". Sending GCODE command" - self.send_notification(msg) - if rpi_output['output_type'] == 'shell_output': - command = rpi_output['shell_script'] - self.send_shell_command(command) + try: + for filament_sensor in list(filter(lambda item: item['input_type'] == 'gpio' and + item['action_type'] == 'output_control', self.rpi_inputs)): + gpio_pin = self.to_int(rpi_input['gpio_pin']) + controlled_io = self.to_int(rpi_input['controlled_io']) + if (rpi_input['edge'] == 'fall') ^ GPIO.input(gpio_pin): + rpi_output = [r_out for r_out in self.rpi_outputs if self.to_int( + r_out['index_id']) == controlled_io].pop() + if rpi_output['output_type'] == 'regular': + val = GPIO.LOW if rpi_input['controlled_io_set_value'] == 'low' else GPIO.HIGH + self.write_gpio(self.to_int( + rpi_output['gpio_pin']), val) + except Exception as ex: self.log_error(ex) pass From 6708be8c2d65b6bfb603257587aa207f8b5657a6 Mon Sep 17 00:00:00 2001 From: Vitor de Miranda Henrique Date: Mon, 18 Jun 2018 14:27:48 -0500 Subject: [PATCH 08/11] identation fix --- octoprint_enclosure/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/octoprint_enclosure/__init__.py b/octoprint_enclosure/__init__.py index 4d31d64..8ab261c 100644 --- a/octoprint_enclosure/__init__.py +++ b/octoprint_enclosure/__init__.py @@ -1043,7 +1043,7 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, def handle_initial_gpio_control(self): try: for filament_sensor in list(filter(lambda item: item['input_type'] == 'gpio' and - item['action_type'] == 'output_control', self.rpi_inputs)): + item['action_type'] == 'output_control', self.rpi_inputs)): gpio_pin = self.to_int(rpi_input['gpio_pin']) controlled_io = self.to_int(rpi_input['controlled_io']) if (rpi_input['edge'] == 'fall') ^ GPIO.input(gpio_pin): From b6b39a404e3c46d3634580af7e94ad89d7fcef93 Mon Sep 17 00:00:00 2001 From: Vitor de Miranda Henrique Date: Mon, 18 Jun 2018 14:33:14 -0500 Subject: [PATCH 09/11] typo --- octoprint_enclosure/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/octoprint_enclosure/__init__.py b/octoprint_enclosure/__init__.py index 8ab261c..8d764bf 100644 --- a/octoprint_enclosure/__init__.py +++ b/octoprint_enclosure/__init__.py @@ -1042,8 +1042,8 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, def handle_initial_gpio_control(self): try: - for filament_sensor in list(filter(lambda item: item['input_type'] == 'gpio' and - item['action_type'] == 'output_control', self.rpi_inputs)): + for rpi_input in list(filter(lambda item: item['input_type'] == 'gpio' and + item['action_type'] == 'output_control', self.rpi_inputs)): gpio_pin = self.to_int(rpi_input['gpio_pin']) controlled_io = self.to_int(rpi_input['controlled_io']) if (rpi_input['edge'] == 'fall') ^ GPIO.input(gpio_pin): From 35c83314ee3ef9b5ef813887ac6fbdd7b33c4173 Mon Sep 17 00:00:00 2001 From: Vitor de Miranda Henrique Date: Mon, 18 Jun 2018 14:36:34 -0500 Subject: [PATCH 10/11] when have i deleted handle_gpio_control? --- octoprint_enclosure/__init__.py | 36 +++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/octoprint_enclosure/__init__.py b/octoprint_enclosure/__init__.py index 8d764bf..22685f4 100644 --- a/octoprint_enclosure/__init__.py +++ b/octoprint_enclosure/__init__.py @@ -1057,6 +1057,42 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, self.log_error(ex) pass + def handle_gpio_control(self, channel): + try: + if self._settings.get(["debug"]) is True: + self._logger.info( + "GPIO event triggered on channel %s", channel) + for rpi_input in [r_inp for r_inp in self.rpi_inputs if self.to_int(r_inp['gpio_pin']) == self.to_int(channel)]: + gpio_pin = self.to_int(rpi_input['gpio_pin']) + controlled_io = self.to_int(rpi_input['controlled_io']) + if (rpi_input['edge'] == 'fall') ^ GPIO.input(gpio_pin): + rpi_output = [r_out for r_out in self.rpi_outputs if self.to_int( + r_out['index_id']) == controlled_io].pop() + if rpi_output['output_type'] == 'regular': + if rpi_input['controlled_io_set_value'] == 'toggle': + val = GPIO.LOW if GPIO.input(self.to_int( + rpi_output['gpio_pin'])) == GPIO.HIGH else GPIO.HIGH + else: + val = GPIO.LOW if rpi_input['controlled_io_set_value'] == 'low' else GPIO.HIGH + self.write_gpio(self.to_int( + rpi_output['gpio_pin']), val) + for notification in self.notifications: + if notification['gpioAction']: + msg = "GPIO control action caused by input " + str(rpi_input['label']) + ". Setting GPIO" + str( + rpi_input['controlled_io']) + " to: " + str(rpi_input['controlled_io_set_value']) + self.send_notification(msg) + if rpi_output['output_type'] == 'gcode_output': + self.send_gcode_command(rpi_output['gcode']) + for notification in self.notifications: + if notification['gpioAction']: + msg = "GPIO control action caused by input " + \ + str(rpi_input['label']) + \ + ". Sending GCODE command" + self.send_notification(msg) + except Exception as ex: + self.log_error(ex) + pass + def send_gcode_command(self, command): for line in command.split('\n'): if line: From 97f74087a41fbca5127b8b84ab16bd4f87bda99d Mon Sep 17 00:00:00 2001 From: Vitor de Miranda Henrique Date: Mon, 18 Jun 2018 14:42:30 -0500 Subject: [PATCH 11/11] add shell controll output --- octoprint_enclosure/__init__.py | 58 ++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/octoprint_enclosure/__init__.py b/octoprint_enclosure/__init__.py index 22685f4..ac9ddc2 100644 --- a/octoprint_enclosure/__init__.py +++ b/octoprint_enclosure/__init__.py @@ -1062,33 +1062,37 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, if self._settings.get(["debug"]) is True: self._logger.info( "GPIO event triggered on channel %s", channel) - for rpi_input in [r_inp for r_inp in self.rpi_inputs if self.to_int(r_inp['gpio_pin']) == self.to_int(channel)]: - gpio_pin = self.to_int(rpi_input['gpio_pin']) - controlled_io = self.to_int(rpi_input['controlled_io']) - if (rpi_input['edge'] == 'fall') ^ GPIO.input(gpio_pin): - rpi_output = [r_out for r_out in self.rpi_outputs if self.to_int( - r_out['index_id']) == controlled_io].pop() - if rpi_output['output_type'] == 'regular': - if rpi_input['controlled_io_set_value'] == 'toggle': - val = GPIO.LOW if GPIO.input(self.to_int( - rpi_output['gpio_pin'])) == GPIO.HIGH else GPIO.HIGH - else: - val = GPIO.LOW if rpi_input['controlled_io_set_value'] == 'low' else GPIO.HIGH - self.write_gpio(self.to_int( - rpi_output['gpio_pin']), val) - for notification in self.notifications: - if notification['gpioAction']: - msg = "GPIO control action caused by input " + str(rpi_input['label']) + ". Setting GPIO" + str( - rpi_input['controlled_io']) + " to: " + str(rpi_input['controlled_io_set_value']) - self.send_notification(msg) - if rpi_output['output_type'] == 'gcode_output': - self.send_gcode_command(rpi_output['gcode']) - for notification in self.notifications: - if notification['gpioAction']: - msg = "GPIO control action caused by input " + \ - str(rpi_input['label']) + \ - ". Sending GCODE command" - self.send_notification(msg) + rpi_input = [r_inp for r_inp in self.rpi_inputs if self.to_int( + r_inp['gpio_pin']) == self.to_int(channel)].pop() + gpio_pin = self.to_int(rpi_input['gpio_pin']) + controlled_io = self.to_int(rpi_input['controlled_io']) + if (rpi_input['edge'] == 'fall') ^ GPIO.input(gpio_pin): + rpi_output = [r_out for r_out in self.rpi_outputs if self.to_int( + r_out['index_id']) == controlled_io].pop() + if rpi_output['output_type'] == 'regular': + if rpi_input['controlled_io_set_value'] == 'toggle': + val = GPIO.LOW if GPIO.input(self.to_int( + rpi_output['gpio_pin'])) == GPIO.HIGH else GPIO.HIGH + else: + val = GPIO.LOW if rpi_input['controlled_io_set_value'] == 'low' else GPIO.HIGH + self.write_gpio(self.to_int( + rpi_output['gpio_pin']), val) + for notification in self.notifications: + if notification['gpioAction']: + msg = "GPIO control action caused by input " + str(rpi_input['label']) + ". Setting GPIO" + str( + rpi_input['controlled_io']) + " to: " + str(rpi_input['controlled_io_set_value']) + self.send_notification(msg) + if rpi_output['output_type'] == 'gcode_output': + self.send_gcode_command(rpi_output['gcode']) + for notification in self.notifications: + if notification['gpioAction']: + msg = "GPIO control action caused by input " + \ + str(rpi_input['label']) + \ + ". Sending GCODE command" + self.send_notification(msg) + if rpi_output['output_type'] == 'shell_output': + command = rpi_output['shell_script'] + self.shell_command(command) except Exception as ex: self.log_error(ex) pass