From 5a516f5c40fe73b9187dde57591cd374420d1344 Mon Sep 17 00:00:00 2001 From: de Miranda Henrique Date: Thu, 18 Jan 2018 17:02:46 -0600 Subject: [PATCH] more code refactoring + fixing issue #68 --- code.code-workspace | 12 + octoprint_enclosure/__init__.py | 434 +++++------ .../static/css/bootstrap-colorpicker.css | 0 .../alpha-horizontal.png | Bin .../img/bootstrap-colorpicker/alpha.png | Bin .../bootstrap-colorpicker/hue-horizontal.png | Bin .../static/img/bootstrap-colorpicker/hue.png | Bin .../img/bootstrap-colorpicker/saturation.png | Bin .../static/js/bootstrap-colorpicker.min.js | 0 octoprint_enclosure/static/js/enclosure.js | 673 +++++++++--------- .../templates/enclosure_tab.jinja2 | 2 +- 11 files changed, 563 insertions(+), 558 deletions(-) create mode 100644 code.code-workspace mode change 100755 => 100644 octoprint_enclosure/static/css/bootstrap-colorpicker.css mode change 100755 => 100644 octoprint_enclosure/static/img/bootstrap-colorpicker/alpha-horizontal.png mode change 100755 => 100644 octoprint_enclosure/static/img/bootstrap-colorpicker/alpha.png mode change 100755 => 100644 octoprint_enclosure/static/img/bootstrap-colorpicker/hue-horizontal.png mode change 100755 => 100644 octoprint_enclosure/static/img/bootstrap-colorpicker/hue.png mode change 100755 => 100644 octoprint_enclosure/static/img/bootstrap-colorpicker/saturation.png mode change 100755 => 100644 octoprint_enclosure/static/js/bootstrap-colorpicker.min.js diff --git a/code.code-workspace b/code.code-workspace new file mode 100644 index 0000000..616c67c --- /dev/null +++ b/code.code-workspace @@ -0,0 +1,12 @@ +{ + "folders": [ + { + "path": "." + } + ], + "settings": { + "terminal.integrated.env.osx": { + "PATH": "/Users/vitormhenrique/.platformio/penv/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin" + } + } +} \ No newline at end of file diff --git a/octoprint_enclosure/__init__.py b/octoprint_enclosure/__init__.py index c52a94a..dc89f00 100644 --- a/octoprint_enclosure/__init__.py +++ b/octoprint_enclosure/__init__.py @@ -23,12 +23,12 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, octoprint.plugin.BlueprintPlugin, octoprint.plugin.EventHandlerPlugin): - previousTempControlStatus = False - currentTempControlStatus = False - enclosureSetTemperature = 0.0 - enclosureCurrentTemperature = 0.0 - enclosureCurrentHumidity = 0.0 - lastFilamentEndDetected = 0 + previous_temp_control_status = False + current_temp_control_status = False + enclosure_set_temperature = 0.0 + enclosure_current_temperature = 0.0 + enclosure_current_humidity = 0.0 + last_filament_end_detected = 0 temperature_reading = [] temperature_control = [] rpi_outputs = [] @@ -39,19 +39,34 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, pwm_intances = [] queue = [] - def startTimer(self): - self._checkTempTimer = RepeatedTimer( - 10, self.checkEnclosureTemp, None, None, True) - self._checkTempTimer.start() + def start_timer(self): + """ + Function to start timer that checks enclosure temperature + """ + + self._check_temp_timer = RepeatedTimer( + 10, self.check_enclosure_temp, None, None, True) + self._check_temp_timer.start() + + @staticmethod + def to_float(value): + """Converts value to flow + + Arguments: + value {any} -- value to be + + Returns: + float -- value converted + """ - def toFloat(self, value): try: val = float(value) return val except: return 0 - def toInt(self, value): + @staticmethod + def to_int(value): try: val = int(value) return val @@ -67,99 +82,114 @@ def on_after_startup(self): self.rpi_outputs = self._settings.get(["rpi_outputs"]) self.rpi_inputs = self._settings.get(["rpi_inputs"]) self.notifications = self._settings.get(["notifications"]) - self.fixData() + self.fix_data() self.previous_rpi_outputs = [] - self.startTimer() - self.startGPIO() - self.configureGPIO() - self.updateOutputUI() + self.start_timer() + self.start_gpio() + self.configure_gpio() + self.update_output_ui() # ~~ Blueprintplugin mixin @octoprint.plugin.BlueprintPlugin.route("/setEnclosureTemperature", methods=["GET"]) - def setEnclosureTemperature(self): - self.enclosureSetTemperature = flask.request.values["enclosureSetTemp"] + def set_enclosure_temperature(self): + self.enclosure_set_temperature = flask.request.values["enclosureSetTemp"] if self._settings.get(["debug"]) is True: self._logger.info( - "DEBUG -> Seting enclosure temperature: %s", self.enclosureSetTemperature) - self.handleTemperatureControl() - return flask.jsonify(enclosureSetTemperature=self.enclosureSetTemperature, enclosureCurrentTemperature=self.enclosureCurrentTemperature) + "DEBUG -> Seting enclosure temperature: %s", self.enclosure_set_temperature) + self.handle_temperature_control() + return flask.jsonify(enclosureSetTemperature=self.enclosure_set_temperature, enclosureCurrentTemperature=self.enclosure_current_temperature) @octoprint.plugin.BlueprintPlugin.route("/getEnclosureSetTemperature", methods=["GET"]) - def getEnclosureSetTemperature(self): - return str(self.enclosureSetTemperature) + def get_enclosure_set_temperature(self): + return str(self.enclosure_set_temperature) @octoprint.plugin.BlueprintPlugin.route("/clearGPIOMode", methods=["GET"]) - def clearGPIOMode(self): + def clear_gpio_mode(self): GPIO.cleanup() return flask.jsonify(success=True) @octoprint.plugin.BlueprintPlugin.route("/getUpdateBtnStatus", methods=["GET"]) - def getUpdateBtnStatus(self): - self.updateOutputUI() + def get_update_btn_status(self): + self.update_output_ui() return flask.make_response("Ok.", 200) @octoprint.plugin.BlueprintPlugin.route("/getOutputStatus", methods=["GET"]) - def getOutputStatus(self): - getOutputStatusresult = '' + def get_output_status(self): + result = '' for rpi_output in self.rpi_outputs: - pin = self.toInt(rpi_output['gpioPin']) + pin = self.to_int(rpi_output['gpioPin']) if rpi_output['outputType'] == 'regular': val = GPIO.input(pin) if not rpi_output['activeLow'] else ( not GPIO.input(pin)) - if getOutputStatusresult: - getOutputStatusresult = getOutputStatusresult + ', ' - getOutputStatusresult = getOutputStatusresult + \ + if result: + result = result + ', ' + result = result + \ '"' + str(pin) + '": ' + str(val).lower() - return '{' + getOutputStatusresult + '}' + return '{' + result + '}' @octoprint.plugin.BlueprintPlugin.route("/getEnclosureTemperature", methods=["GET"]) - def getEnclosureTemperature(self): - return str(self.enclosureCurrentTemperature) + def get_enclosure_temperature(self): + return str(self.enclosure_current_temperature) @octoprint.plugin.BlueprintPlugin.route("/setIO", methods=["GET"]) - def setIO(self): - io = flask.request.values["io"] + def set_io(self): + gpio = flask.request.values["io"] value = True if flask.request.values["status"] == "on" else False for rpi_output in self.rpi_outputs: - if self.toInt(io) == self.toInt(rpi_output['gpioPin']): + if self.to_int(gpio) == self.to_int(rpi_output['gpioPin']): val = (not value) if rpi_output['activeLow'] else value - self.writeGPIO(self.toInt(io), val) + self.write_gpio(self.to_int(gpio), val) return flask.jsonify(success=True) @octoprint.plugin.BlueprintPlugin.route("/setPWM", methods=["GET"]) - def setPWM(self): - io = flask.request.values["io"] - pwmVal = flask.request.values["pwmVal"] - self.writePWM(self.toInt(io), self.toInt(pwmVal)) + def set_pwm(self): + gpio = flask.request.values["io"] + pwm_val = flask.request.values["pwmVal"] + self.write_pwm(self.to_int(gpio), self.to_int(pwm_val)) return flask.make_response("Ok.", 200) @octoprint.plugin.BlueprintPlugin.route("/setNeopixel", methods=["GET"]) - def setNeopixel(self): - io = flask.request.values["io"] + def set_neopixel(self): + """ set_neopixel method get request from octoprint and send the comand to arduino or neopixel""" + gpio = flask.request.values["io"] red = flask.request.values["red"] green = flask.request.values["green"] blue = flask.request.values["blue"] for rpi_output in self.rpi_outputs: - if self.toInt(io) == self.toInt(rpi_output['gpioPin']) and rpi_output['outputType'] == 'neopixel': - ledCount = rpi_output['neopixelCount'] - ledBrightness = rpi_output['neopixelBrightness'] + if self.to_int(gpio) == self.to_int(rpi_output['gpioPin']) and rpi_output['outputType'] == 'neopixel': + led_count = rpi_output['neopixelCount'] + led_brightness = rpi_output['neopixelBrightness'] address = rpi_output['microAddress'] - self.sendNeopixelCommand( - io, ledCount, ledBrightness, red, green, blue, address) + self.send_neopixel_command( + gpio, led_count, led_brightness, red, green, blue, address) return flask.make_response("Ok.", 200) # ~~ Plugin Internal methods - def fixData(self): + def fix_data(self): + """ Fix setting dada commin from old releases of the plugin""" + if not self._settings.get(["settingsVersion"]) == "3.6": self._settings.set(["rpi_outputs"], []) self._settings.set(["rpi_inputs"], []) - def sendNeopixelCommand(self, ledPin, ledCount, ledBrightness, red, green, blue, address): + def send_neopixel_command(self, led_pin, led_count, led_brightness, red, green, blue, address): + """Send neopixel command + + Arguments: + led_pin {int} -- GPIO number + ledCount {int} -- number of LEDS + ledBrightness {int} -- brightness from 0 to 255 + red {int} -- red value from 0 to 255 + green {int} -- gren value from 0 to 255 + blue {int} -- blue value from 0 to 255 + address {int} -- i2c address from microcontroler + """ + try: script = os.path.dirname( os.path.realpath(__file__)) + "/neopixel.py " - cmd = "sudo python " + script + str(ledPin) + " " + str(ledCount) + " " + str( - ledBrightness) + " " + str(red) + " " + str(green) + " " + str(blue) + " " + str(address) + cmd = "sudo python " + script + str(led_pin) + " " + str(led_count) + " " + str( + led_brightness) + " " + str(red) + " " + str(green) + " " + str(blue) + " " + str(address) if self._settings.get(["debug"]) is True: self._logger.info("Sending neopixel cmd: %s", cmd) stdout = (Popen(cmd, shell=True, stdout=PIPE).stdout).read() @@ -170,25 +200,25 @@ def sendNeopixelCommand(self, ledPin, ledCount, ledBrightness, red, green, blue, self._logger.warn(message) pass - def checkEnclosureTemp(self): + def check_enclosure_temp(self): try: for temp_reader in self.temperature_reading: if temp_reader['isEnabled']: if temp_reader['sensorType'] in ["11", "22", "2302"]: self._logger.info("sensorType dht") - temp, hum = self.readDhtTemp( + temp, hum = self.read_dht_temp( temp_reader['sensorType'], temp_reader['gpioPin']) elif temp_reader['sensorType'] == "18b20": - temp = self.read18b20Temp() + temp = self.read_18b20_temp() hum = 0 elif temp_reader['sensorType'] == "bme280": - temp, hum = self.readBME280Temp( + temp, hum = self.read_bme280_temp( temp_reader['sensorAddress']) elif temp_reader['sensorType'] == "si7021": - temp, hum = self.readSI7021Temp( + temp, hum = self.read_si7021_temp( temp_reader['sensorAddress']) elif temp_reader['sensorType'] == "tmp102": - temp = self.readTmp102Temp( + temp = self.read_tmp102_temp( temp_reader['sensorAddress']) hum = 0 else: @@ -197,19 +227,19 @@ def checkEnclosureTemp(self): hum = 0 if temp != -1 and hum != -1: - self.enclosureCurrentTemperature = round(self.toFloat( - temp), 1) if not temp_reader['useFahrenheit'] else round(self.toFloat(temp) * 1.8 + 32, 1) - self.enclosureCurrentHumidity = round( - self.toFloat(hum), 1) + self.enclosure_current_temperature = round(self.to_float( + temp), 1) if not temp_reader['useFahrenheit'] else round(self.to_float(temp) * 1.8 + 32, 1) + self.enclosure_current_humidity = round( + self.to_float(hum), 1) if self._settings.get(["debug"]) is True and self._settings.get(["enableTemperatureLog"]) is True: self._logger.info( - "Temperature: %s humidity %s", self.enclosureCurrentTemperature, self.enclosureCurrentHumidity) + "Temperature: %s humidity %s", self.enclosure_current_temperature, self.enclosure_current_humidity) self._plugin_manager.send_plugin_message(self._identifier, dict( - enclosuretemp=self.enclosureCurrentTemperature, enclosureHumidity=self.enclosureCurrentHumidity)) - self.handleTemperatureControl() - self.handleTemperatureEvents() + enclosuretemp=self.enclosure_current_temperature, enclosureHumidity=self.enclosure_current_humidity)) + self.handle_temperature_control() + self.handle_temperature_events() except Exception as ex: template = "An exception of type {0} occurred on {1}. Arguments:\n{1!r}" message = template.format( @@ -217,22 +247,22 @@ def checkEnclosureTemp(self): self._logger.warn(message) pass - def handleTemperatureEvents(self): + def handle_temperature_events(self): for rpi_input in self.rpi_inputs: - if self.toFloat(rpi_input['setTemp']) == 0: + if self.to_float(rpi_input['setTemp']) == 0: continue - if rpi_input['eventType'] == 'temperature' and (self.toFloat(rpi_input['setTemp']) < self.toFloat(self.enclosureCurrentTemperature)): + if rpi_input['eventType'] == 'temperature' and (self.to_float(rpi_input['setTemp']) < self.to_float(self.enclosure_current_temperature)): for rpi_output in self.rpi_outputs: - if self.toInt(rpi_input['controlledIO']) == self.toInt(rpi_output['gpioPin']): + if self.to_int(rpi_input['controlledIO']) == self.to_int(rpi_output['gpioPin']): val = GPIO.LOW if rpi_output['activeLow'] else GPIO.HIGH - self.writeGPIO(self.toInt(rpi_output['gpioPin']), val) + self.write_gpio(self.to_int(rpi_output['gpioPin']), val) for notification in self.notifications: if notification['temperatureAction']: msg = "Temperature action: enclosure temperature exceed " + \ rpi_input['setTemp'] - self.sendNotification(msg) + self.send_notification(msg) - def readDhtTemp(self, sensor, pin): + def read_dht_temp(self, sensor, pin): try: script = os.path.dirname( os.path.realpath(__file__)) + "/getDHTTemp.py " @@ -243,7 +273,7 @@ def readDhtTemp(self, sensor, pin): if self._settings.get(["debug"]) is True and self._settings.get(["enableTemperatureLog"]) is True: self._logger.info("Dht result: %s", stdout) temp, hum = stdout.split("|") - return (self.toFloat(temp.strip()), self.toFloat(hum.strip())) + return (self.to_float(temp.strip()), self.to_float(hum.strip())) except Exception as ex: template = "An exception of type {0} occurred on {1}. Arguments:\n{1!r}" message = template.format( @@ -251,7 +281,7 @@ def readDhtTemp(self, sensor, pin): self._logger.warn(message) return (0, 0) - def readBME280Temp(self, address): + def read_bme280_temp(self, address): try: script = os.path.dirname( os.path.realpath(__file__)) + "/BME280.py " @@ -262,7 +292,7 @@ def readBME280Temp(self, address): if self._settings.get(["debug"]) is True and self._settings.get(["enableTemperatureLog"]) is True: self._logger.info("BME280 result: %s", stdout) temp, hum = stdout.split("|") - return (self.toFloat(temp.strip()), self.toFloat(hum.strip())) + return (self.to_float(temp.strip()), self.to_float(hum.strip())) except Exception as ex: template = "An exception of type {0} occurred on {1}. Arguments:\n{1!r}" message = template.format( @@ -270,7 +300,7 @@ def readBME280Temp(self, address): self._logger.warn(message) return (0, 0) - def readSI7021Temp(self, address): + def read_si7021_temp(self, address): try: script = os.path.dirname( os.path.realpath(__file__)) + "/SI7021.py " @@ -281,7 +311,7 @@ def readSI7021Temp(self, address): if self._settings.get(["debug"]) is True and self._settings.get(["enableTemperatureLog"]) is True: self._logger.info("SI7021 result: %s", stdout) temp, hum = stdout.split("|") - return (self.toFloat(temp.strip()), self.toFloat(hum.strip())) + return (self.to_float(temp.strip()), self.to_float(hum.strip())) except Exception as ex: template = "An exception of type {0} occurred on {1}. Arguments:\n{1!r}" message = template.format( @@ -289,14 +319,14 @@ def readSI7021Temp(self, address): self._logger.warn(message) return (0, 0) - def read18b20Temp(self): + def read_18b20_temp(self): os.system('modprobe w1-gpio') os.system('modprobe w1-therm') - lines = self.readraw18b20Temp() + lines = self.read_raw_18b20_temp() while lines[0].strip()[-3:] != 'YES': time.sleep(0.2) - lines = self.readraw18b20Temp() + lines = self.read_raw_18b20_temp() equals_pos = lines[1].find('t=') if equals_pos != -1: temp_string = lines[1][equals_pos + 2:] @@ -304,16 +334,16 @@ def read18b20Temp(self): return '{0:0.1f}'.format(temp_c) return 0 - def readraw18b20Temp(self): + def read_raw_18b20_temp(self): base_dir = '/sys/bus/w1/devices/' device_folder = glob.glob(base_dir + '28*')[0] device_file = device_folder + '/w1_slave' - f = open(device_file, 'r') - lines = f.readlines() - f.close() + device_file_result = open(device_file, 'r') + lines = device_file_result.readlines() + device_file_result.close() return lines - def readTmp102Temp(self, address): + def read_tmp102_temp(self, address): try: script = os.path.dirname(os.path.realpath(__file__)) + "/tmp102.py" args = ["python", script, str(address)] @@ -323,7 +353,7 @@ def readTmp102Temp(self, address): stdout, _ = proc.communicate() if self._settings.get(["debug"]) is True and self._settings.get(["enableTemperatureLog"]) is True: self._logger.info("TMP102 result: %s", stdout) - return self.toFloat(stdout.strip()) + return self.to_float(stdout.strip()) except Exception as ex: template = "An exception of type {0} occurred on {1}. Arguments:\n{1!r}" message = template.format( @@ -331,32 +361,32 @@ def readTmp102Temp(self, address): self._logger.warn(message) return 0 - def handleTemperatureControl(self): + def handle_temperature_control(self): for control in self.temperature_control: if control['isEnabled'] is True: if control['controlType'] is 'heater': - self.currentTempControlStatus = self.toFloat( - self.enclosureCurrentTemperature) < self.toFloat(self.enclosureSetTemperature) + self.current_temp_control_status = self.to_float( + self.enclosure_current_temperature) < self.to_float(self.enclosure_set_temperature) else: - if self.toFloat(self.enclosureSetTemperature) == 0: - self.currentTempControlStatus = False + if self.to_float(self.enclosure_set_temperature) == 0: + self.current_temp_control_status = False else: - self.currentTempControlStatus = self.toFloat( - self.enclosureCurrentTemperature) > self.toFloat(self.enclosureSetTemperature) - if self.currentTempControlStatus != self.previousTempControlStatus: - if self.currentTempControlStatus: + self.current_temp_control_status = self.to_float( + self.enclosure_current_temperature) > self.to_float(self.enclosure_set_temperature) + if self.current_temp_control_status != self.previous_temp_control_status: + if self.current_temp_control_status: self._logger.info( "Turning gpio to control temperature on.") val = False if control['activeLow'] else True - self.writeGPIO(self.toInt(control['gpioPin']), val) + self.write_gpio(self.to_int(control['gpioPin']), val) else: self._logger.info( "Turning gpio to control temperature off.") val = True if control['activeLow'] else False - self.writeGPIO(self.toInt(control['gpioPin']), val) - self.previousTempControlStatus = self.currentTempControlStatus + self.write_gpio(self.to_int(control['gpioPin']), val) + self.previous_temp_control_status = self.current_temp_control_status - def startGPIO(self): + def start_gpio(self): try: currentMode = GPIO.getmode() setMode = GPIO.BOARD if self._settings.get( @@ -383,20 +413,20 @@ def startGPIO(self): self._logger.warn(message) pass - def clearGPIO(self): + def clear_gpio(self): try: for control in self.temperature_control: if control['isEnabled']: - GPIO.cleanup(self.toInt(control['gpioPin'])) + GPIO.cleanup(self.to_int(control['gpioPin'])) for rpi_output in self.rpi_outputs: - if self.toInt(rpi_output['gpioPin']) not in self.previous_rpi_outputs: - GPIO.cleanup(self.toInt(rpi_output['gpioPin'])) + if self.to_int(rpi_output['gpioPin']) not in self.previous_rpi_outputs: + GPIO.cleanup(self.to_int(rpi_output['gpioPin'])) for rpi_input in self.rpi_inputs: try: - GPIO.remove_event_detect(self.toInt(rpi_input['gpioPin'])) + GPIO.remove_event_detect(self.to_int(rpi_input['gpioPin'])) except: pass - GPIO.cleanup(self.toInt(rpi_input['gpioPin'])) + GPIO.cleanup(self.to_int(rpi_input['gpioPin'])) except Exception as ex: template = "An exception of type {0} occurred on {1}. Arguments:\n{1!r}" message = template.format( @@ -404,9 +434,9 @@ def clearGPIO(self): self._logger.warn(message) pass - def clearChannel(self, channel): + def clear_channel(self, channel): try: - GPIO.cleanup(self.toInt(channel)) + GPIO.cleanup(self.to_int(channel)) except Exception as ex: template = "An exception of type {0} occurred on {1}. Arguments:\n{1!r}" message = template.format( @@ -414,40 +444,40 @@ def clearChannel(self, channel): self._logger.warn(message) pass - def configureGPIO(self): + def configure_gpio(self): try: for control in self.temperature_control: if control['isEnabled']: - GPIO.setup(self.toInt( + GPIO.setup(self.to_int( control['gpioPin']), GPIO.OUT, initial=GPIO.HIGH if control['activeLow'] else GPIO.LOW) for rpi_output in self.rpi_outputs: - pin = self.toInt(rpi_output['gpioPin']) + pin = self.to_int(rpi_output['gpioPin']) if rpi_output['outputType'] == 'regular': - if self.toInt(rpi_output['gpioPin']) not in self.previous_rpi_outputs: + if self.to_int(rpi_output['gpioPin']) not in self.previous_rpi_outputs: initialValue = GPIO.HIGH if rpi_output['activeLow'] else GPIO.LOW GPIO.setup(pin, GPIO.OUT, initial=initialValue) if rpi_output['outputType'] == 'pwm': for pwm in (pwm for pwm in self.pwm_intances if pin in pwm): self.pwm_intances.remove(pwm) - self.clearChannel(pin) + self.clear_channel(pin) GPIO.setup(pin, GPIO.OUT) - p = GPIO.PWM(pin, self.toInt(rpi_output['frequency'])) + p = GPIO.PWM(pin, self.to_int(rpi_output['frequency'])) self.pwm_intances.append({pin: p}) if rpi_output['outputType'] == 'neopixel': - self.clearChannel(pin) + self.clear_channel(pin) for rpi_input in self.rpi_inputs: pullResistor = pull_up_down = GPIO.PUD_UP if rpi_input[ 'inputPull'] == 'inputPullUp' else GPIO.PUD_DOWN - GPIO.setup(self.toInt( + GPIO.setup(self.to_int( rpi_input['gpioPin']), GPIO.IN, pullResistor) - if rpi_input['eventType'] == 'gpio' and self.toInt(rpi_input['gpioPin']) != 0: + if rpi_input['eventType'] == 'gpio' and self.to_int(rpi_input['gpioPin']) != 0: edge = GPIO.RISING if rpi_input['edge'] == 'rise' else GPIO.FALLING - GPIO.add_event_detect(self.toInt( - rpi_input['gpioPin']), edge, callback=self.handleGPIOControl, bouncetime=200) - if rpi_input['eventType'] == 'printer' and rpi_input['printerAction'] != 'filament' and self.toInt(rpi_input['gpioPin']) != 0: + GPIO.add_event_detect(self.to_int( + rpi_input['gpioPin']), edge, callback=self.handle_gpio_Control, bouncetime=200) + if rpi_input['eventType'] == 'printer' and rpi_input['printerAction'] != 'filament' and self.to_int(rpi_input['gpioPin']) != 0: edge = GPIO.RISING if rpi_input['edge'] == 'rise' else GPIO.FALLING - GPIO.add_event_detect(self.toInt( - rpi_input['gpioPin']), edge, callback=self.handlePrinterAction, bouncetime=200) + GPIO.add_event_detect(self.to_int( + rpi_input['gpioPin']), edge, callback=self.handle_printer_action, bouncetime=200) except Exception as ex: template = "An exception of type {0} occurred on {1}. Arguments:\n{1!r}" message = template.format( @@ -455,14 +485,14 @@ def configureGPIO(self): self._logger.warn(message) pass - def handleFilammentDetection(self, channel): + def handle_filamment_detection(self, channel): try: for rpi_input in self.rpi_inputs: - if channel == self.toInt(rpi_input['gpioPin']) and rpi_input['eventType'] == 'printer' and rpi_input['printerAction'] == 'filament' \ - and ((rpi_input['edge'] == 'fall') ^ GPIO.input(self.toInt(rpi_input['gpioPin']))): - if time.time() - self.lastFilamentEndDetected > self._settings.get_int(["filamentSensorTimeout"]): + if channel == self.to_int(rpi_input['gpioPin']) and rpi_input['eventType'] == 'printer' and rpi_input['printerAction'] == 'filament' \ + and ((rpi_input['edge'] == 'fall') ^ GPIO.input(self.to_int(rpi_input['gpioPin']))): + if time.time() - self.last_filament_end_detected > self._settings.get_int(["filamentSensorTimeout"]): self._logger.info("Detected end of filament.") - self.lastFilamentEndDetected = time.time() + self.last_filament_end_detected = time.time() for line in self._settings.get(["filamentSensorGcode"]).split('\n'): if line: self._printer.commands(line.strip().upper()) @@ -473,7 +503,7 @@ def handleFilammentDetection(self, channel): if notification['filamentChange']: msg = "Filament change action caused by sensor: " + \ str(rpi_input['label']) - self.sendNotification(msg) + self.send_notification(msg) else: self._logger.info( "Prevented end of filament detection, filament sensor timeout not elapsed.") @@ -484,18 +514,18 @@ def handleFilammentDetection(self, channel): self._logger.warn(message) pass - def startFilamentDetection(self): - self.stopFilamentDetection() + def start_filament_detection(self): + self.stop_filament_detection() try: for rpi_input in self.rpi_inputs: - if rpi_input['eventType'] == 'printer' and rpi_input['printerAction'] == 'filament' and self.toInt(rpi_input['gpioPin']) != 0: + if rpi_input['eventType'] == 'printer' and rpi_input['printerAction'] == 'filament' and self.to_int(rpi_input['gpioPin']) != 0: edge = GPIO.RISING if rpi_input['edge'] == 'rise' else GPIO.FALLING - if GPIO.input(self.toInt(rpi_input['gpioPin'])) == (edge == GPIO.RISING): + if GPIO.input(self.to_int(rpi_input['gpioPin'])) == (edge == GPIO.RISING): self._printer.pause_print() self._logger.info("Started printing with no filament.") else: - GPIO.add_event_detect(self.toInt( - rpi_input['gpioPin']), edge, callback=self.handleFilammentDetection, bouncetime=200) + GPIO.add_event_detect(self.to_int( + rpi_input['gpioPin']), edge, callback=self.handle_filamment_detection, bouncetime=200) except Exception as ex: template = "An exception of type {0} occurred on {1}. Arguments:\n{1!r}" message = template.format( @@ -503,11 +533,11 @@ def startFilamentDetection(self): self._logger.warn(message) pass - def stopFilamentDetection(self): + def stop_filament_detection(self): try: for rpi_input in self.rpi_inputs: if rpi_input['eventType'] == 'printer' and rpi_input['printerAction'] == 'filament': - GPIO.remove_event_detect(self.toInt(rpi_input['gpioPin'])) + GPIO.remove_event_detect(self.to_int(rpi_input['gpioPin'])) except Exception as ex: template = "An exception of type {0} occurred on {1}. Arguments:\n{1!r}" message = template.format( @@ -515,29 +545,29 @@ def stopFilamentDetection(self): self._logger.warn(message) pass - def cancelEventsOnQueue(self): + def cancel_events_on_queue(self): for task in self.queue: task.cancel() - def handleGPIOControl(self, channel): + def handle_gpio_Control(self, channel): try: for rpi_input in self.rpi_inputs: - if channel == self.toInt(rpi_input['gpioPin']) and rpi_input['eventType'] == 'gpio' and \ - ((rpi_input['edge'] == 'fall') ^ GPIO.input(self.toInt(rpi_input['gpioPin']))): + if channel == self.to_int(rpi_input['gpioPin']) and rpi_input['eventType'] == 'gpio' and \ + ((rpi_input['edge'] == 'fall') ^ GPIO.input(self.to_int(rpi_input['gpioPin']))): for rpi_output in self.rpi_outputs: - if self.toInt(rpi_input['controlledIO']) == self.toInt(rpi_output['gpioPin']) and rpi_output['outputType'] == 'regular': + if self.to_int(rpi_input['controlledIO']) == self.to_int(rpi_output['gpioPin']) and rpi_output['outputType'] == 'regular': if rpi_input['setControlledIO'] == 'toggle': - val = GPIO.LOW if GPIO.input(self.toInt( + val = GPIO.LOW if GPIO.input(self.to_int( rpi_output['gpioPin'])) == GPIO.HIGH else GPIO.HIGH else: val = GPIO.LOW if rpi_input['setControlledIO'] == 'low' else GPIO.HIGH - self.writeGPIO(self.toInt( + self.write_gpio(self.to_int( rpi_output['gpioPin']), 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['controlledIO']) + " to: " + str(rpi_input['setControlledIO']) - self.sendNotification(msg) + self.send_notification(msg) except Exception as ex: template = "An exception of type {0} occurred on {1}. Arguments:\n{1!r}" message = template.format( @@ -545,11 +575,11 @@ def handleGPIOControl(self, channel): self._logger.warn(message) pass - def handlePrinterAction(self, channel): + def handle_printer_action(self, channel): try: for rpi_input in self.rpi_inputs: - if channel == self.toInt(rpi_input['gpioPin']) and rpi_input['eventType'] == 'printer' and \ - ((rpi_input['edge'] == 'fall') ^ GPIO.input(self.toInt(rpi_input['gpioPin']))): + if channel == self.to_int(rpi_input['gpioPin']) and rpi_input['eventType'] == 'printer' and \ + ((rpi_input['edge'] == 'fall') ^ GPIO.input(self.to_int(rpi_input['gpioPin']))): if rpi_input['printerAction'] == 'resume': self._logger.info("Printer action resume.") self._printer.resume_print() @@ -562,14 +592,14 @@ def handlePrinterAction(self, channel): elif rpi_input['printerAction'] == 'stopTemperatureControl': self._logger.info( "Printer action stoping temperature control.") - self.enclosureSetTemperature = 0 - self.handleTemperatureControl() + self.enclosure_set_temperature = 0 + self.handle_temperature_control() for notification in self.notifications: if notification['printerAction']: msg = "Printer action: " + \ rpi_input['printerAction'] + \ " caused by input: " + str(rpi_input['label']) - self.sendNotification(msg) + self.send_notification(msg) except Exception as ex: template = "An exception of type {0} occurred on {1}. Arguments:\n{1!r}" message = template.format( @@ -577,12 +607,12 @@ def handlePrinterAction(self, channel): self._logger.warn(message) pass - def writeGPIO(self, gpio, value): + def write_gpio(self, gpio, value): try: GPIO.output(gpio, value) if self._settings.get(["debug"]) is True: self._logger.info("Writing on gpio: %s value %s", gpio, value) - self.updateOutputUI() + self.update_output_ui() except Exception as ex: template = "An exception of type {0} occurred on {1}. Arguments:\n{1!r}" message = template.format( @@ -590,7 +620,7 @@ def writeGPIO(self, gpio, value): self._logger.warn(message) pass - def writePWM(self, gpio, pwmValue): + def write_pwm(self, gpio, pwmValue): try: for pwm in self.pwm_intances: if gpio in pwm: @@ -601,7 +631,7 @@ def writePWM(self, gpio, pwmValue): if self._settings.get(["debug"]) is True: self._logger.info( "Writing PWM on gpio: %s value %s", gpio, pwmValue) - self.updateOutputUI() + self.update_output_ui() break except Exception as ex: template = "An exception of type {0} occurred on {1}. Arguments:\n{1!r}" @@ -610,26 +640,23 @@ def writePWM(self, gpio, pwmValue): self._logger.warn(message) pass - def updateOutputUI(self): + def update_output_ui(self): try: result = [] result_pwm = [] for rpi_output in self.rpi_outputs: - pin = self.toInt(rpi_output['gpioPin']) + pin = self.to_int(rpi_output['gpioPin']) if rpi_output['outputType'] == 'regular': val = GPIO.input(pin) if not rpi_output['activeLow'] else ( not GPIO.input(pin)) result.append({pin: val}) if rpi_output['outputType'] == 'pwm': - # self._logger.info("outputType is PWM") - # self._logger.info("Got pin number: %s",pin) - # self._logger.info("pwm_intances: %s",self.pwm_intances) for pwm in self.pwm_intances: if pin in pwm: if 'dutycycle' in pwm: pwmVal = pwm['dutycycle'] - val = self.toInt(pwmVal) + val = self.to_int(pwmVal) else: val = 100 result_pwm.append({pin: val}) @@ -643,13 +670,13 @@ def updateOutputUI(self): self._logger.warn(message) pass - def getOutputList(self): + def get_output_list(self): result = [] for rpi_output in self.rpi_outputs: - result.append(self.toInt(rpi_output['gpioPin'])) + result.append(self.to_int(rpi_output['gpioPin'])) return result - def sendNotification(self, message): + def send_notification(self, message): try: provider = self._settings.get(["notificationProvider"]) if provider == 'ifttt': @@ -659,7 +686,7 @@ def sendNotification(self, message): self._logger.info( "Sending notification to: %s with msg: %s with key: %s", provider, message, api_key) try: - res = self.iftttNotification(message, event, api_key) + res = self.ifttt_notification(message, event, api_key) except requests.exceptions.ConnectionError: self._logger.info("Error: Could not connect to IFTTT") except requests.exceptions.HTTPError: @@ -685,8 +712,9 @@ def sendNotification(self, message): self._logger.warn(message) pass - def iftttNotification(self, message, event, api_key): - url = "https://maker.ifttt.com/trigger/{e}/with/key/{k}/".format(e=event, k=api_key) + def ifttt_notification(self, message, event, api_key): + url = "https://maker.ifttt.com/trigger/{e}/with/key/{k}/".format( + e=event, k=api_key) payload = {'value1': message} return requests.post(url, data=payload) @@ -694,25 +722,25 @@ def iftttNotification(self, message, event, api_key): def on_event(self, event, payload): if event == Events.CONNECTED: - self.updateOutputUI() + self.update_output_ui() if event == Events.PRINT_RESUMED: - self.startFilamentDetection() + self.start_filament_detection() if event == Events.PRINT_STARTED: - self.cancelEventsOnQueue() - self.startFilamentDetection() + self.cancel_events_on_queue() + self.start_filament_detection() for rpi_output in self.rpi_outputs: if rpi_output['autoStartup'] and rpi_output['outputType'] == 'regular': value = False if rpi_output['activeLow'] else True - self.queue.append(threading.Timer(self.toFloat(rpi_output['startupTimeDelay']), - self.writeGPIO, - args=[self.toInt(rpi_output['gpioPin']), value])) + self.queue.append(threading.Timer(self.to_float(rpi_output['startupTimeDelay']), + self.write_gpio, + args=[self.to_int(rpi_output['gpioPin']), value])) if rpi_output['autoStartup'] and rpi_output['outputType'] == 'pwm': - value = self.toInt(rpi_output['dutycycle']) - self.queue.append(threading.Timer(self.toFloat(rpi_output['startupTimeDelay']), - self.writePWM, - args=[self.toInt(rpi_output['gpioPin']), value])) + value = self.to_int(rpi_output['dutycycle']) + self.queue.append(threading.Timer(self.to_float(rpi_output['startupTimeDelay']), + self.write_pwm, + args=[self.to_int(rpi_output['gpioPin']), value])) if rpi_output['autoStartup'] and rpi_output['outputType'] == 'neopixel': gpioPin = rpi_output['gpioPin'] ledCount = rpi_output['neopixelCount'] @@ -727,44 +755,44 @@ def on_event(self, event, payload): stringColor = stringColor[stringColor.index(',') + 1:] blue = stringColor[:stringColor.index(')')] - self.queue.append(threading.Timer(self.toFloat(rpi_output['startupTimeDelay']), - self.sendNeopixelCommand, + self.queue.append(threading.Timer(self.to_float(rpi_output['startupTimeDelay']), + self.send_neopixel_command, args=[gpioPin, ledCount, ledBrightness, red, green, blue, address])) for task in self.queue: task.start() for control in self.temperature_control: if control['autoStartup'] is True: - self.enclosureSetTemperature = self.toInt( + self.enclosure_set_temperature = self.to_int( control['defaultTemp']) self._plugin_manager.send_plugin_message( - self._identifier, dict(enclosureSetTemp=self.enclosureSetTemperature)) + self._identifier, dict(enclosureSetTemp=self.enclosure_set_temperature)) elif event in (Events.PRINT_DONE, Events.PRINT_FAILED, Events.PRINT_CANCELLED): - self.stopFilamentDetection() - self.enclosureSetTemperature = 0 + self.stop_filament_detection() + self.enclosure_set_temperature = 0 self._plugin_manager.send_plugin_message( - self._identifier, dict(enclosureSetTemp=self.enclosureSetTemperature)) + self._identifier, dict(enclosureSetTemp=self.enclosure_set_temperature)) for rpi_output in self.rpi_outputs: if rpi_output['autoShutdown'] and rpi_output['outputType'] == 'regular': value = True if rpi_output['activeLow'] else False - self.queue.append(threading.Timer(self.toFloat(rpi_output['shutdownTimeDelay']), - self.writeGPIO, - args=[self.toInt(rpi_output['gpioPin']), value])) + self.queue.append(threading.Timer(self.to_float(rpi_output['shutdownTimeDelay']), + self.write_gpio, + args=[self.to_int(rpi_output['gpioPin']), value])) if rpi_output['autoShutdown'] and rpi_output['outputType'] == 'pwm': value = 0 - self.queue.append(threading.Timer(self.toFloat(rpi_output['shutdownTimeDelay']), - self.writePWM, - args=[self.toInt(rpi_output['gpioPin']), value])) + self.queue.append(threading.Timer(self.to_float(rpi_output['shutdownTimeDelay']), + self.write_pwm, + args=[self.to_int(rpi_output['gpioPin']), value])) if rpi_output['autoShutdown'] and rpi_output['outputType'] == 'neopixel': gpioPin = rpi_output['gpioPin'] ledCount = rpi_output['neopixelCount'] ledBrightness = rpi_output['neopixelBrightness'] address = rpi_output['microAddress'] - self.queue.append(threading.Timer(self.toFloat(rpi_output['shutdownTimeDelay']), - self.sendNeopixelCommand, + self.queue.append(threading.Timer(self.to_float(rpi_output['shutdownTimeDelay']), + self.send_neopixel_command, args=[gpioPin, ledCount, 0, 0, 0, 0, address])) for task in self.queue: - task.start() + task.start() if event == Events.PRINT_DONE: for notification in self.notifications: @@ -775,29 +803,29 @@ def on_event(self, event, payload): datetime.timedelta(seconds=elapsed_time_in_seconds)) msg = "Print job finished: " + file_name + \ "finished printing in " + file_name, elapsed_time - self.sendNotification(msg) + self.send_notification(msg) # ~~ SettingsPlugin mixin def on_settings_save(self, data): self._logger.info("data: %s", data) - outputsBeforeSave = self.getOutputList() + outputsBeforeSave = self.get_output_list() octoprint.plugin.SettingsPlugin.on_settings_save(self, data) self.temperature_reading = self._settings.get(["temperature_reading"]) self.temperature_control = self._settings.get(["temperature_control"]) self.rpi_outputs = self._settings.get(["rpi_outputs"]) self.rpi_inputs = self._settings.get(["rpi_inputs"]) self.notifications = self._settings.get(["notifications"]) - outputsAfterSave = self.getOutputList() + outputsAfterSave = self.get_output_list() commonPins = list(set(outputsBeforeSave) & set(outputsAfterSave)) for pin in (pin for pin in outputsBeforeSave if pin not in commonPins): - self.clearChannel(pin) + self.clear_channel(pin) self.previous_rpi_outputs = commonPins - self.clearGPIO() + self.clear_gpio() if self._settings.get(["debug"]) is True: self._logger.info("temperature_reading: %s", @@ -806,8 +834,8 @@ def on_settings_save(self, data): self.temperature_control) self._logger.info("rpi_outputs: %s", self.rpi_outputs) self._logger.info("rpi_inputs: %s", self.rpi_inputs) - self.startGPIO() - self.configureGPIO() + self.start_gpio() + self.configure_gpio() def get_settings_defaults(self): return dict( diff --git a/octoprint_enclosure/static/css/bootstrap-colorpicker.css b/octoprint_enclosure/static/css/bootstrap-colorpicker.css old mode 100755 new mode 100644 diff --git a/octoprint_enclosure/static/img/bootstrap-colorpicker/alpha-horizontal.png b/octoprint_enclosure/static/img/bootstrap-colorpicker/alpha-horizontal.png old mode 100755 new mode 100644 diff --git a/octoprint_enclosure/static/img/bootstrap-colorpicker/alpha.png b/octoprint_enclosure/static/img/bootstrap-colorpicker/alpha.png old mode 100755 new mode 100644 diff --git a/octoprint_enclosure/static/img/bootstrap-colorpicker/hue-horizontal.png b/octoprint_enclosure/static/img/bootstrap-colorpicker/hue-horizontal.png old mode 100755 new mode 100644 diff --git a/octoprint_enclosure/static/img/bootstrap-colorpicker/hue.png b/octoprint_enclosure/static/img/bootstrap-colorpicker/hue.png old mode 100755 new mode 100644 diff --git a/octoprint_enclosure/static/img/bootstrap-colorpicker/saturation.png b/octoprint_enclosure/static/img/bootstrap-colorpicker/saturation.png old mode 100755 new mode 100644 diff --git a/octoprint_enclosure/static/js/bootstrap-colorpicker.min.js b/octoprint_enclosure/static/js/bootstrap-colorpicker.min.js old mode 100755 new mode 100644 diff --git a/octoprint_enclosure/static/js/enclosure.js b/octoprint_enclosure/static/js/enclosure.js index 3e28bc1..fa6fd69 100644 --- a/octoprint_enclosure/static/js/enclosure.js +++ b/octoprint_enclosure/static/js/enclosure.js @@ -1,382 +1,347 @@ -$(function() { - function EnclosureViewModel(parameters) { - var self = this; +$(function () { + function EnclosureViewModel(parameters) { + var self = this; - self.pluginName = "enclosure"; + self.pluginName = "enclosure"; - self.global_settings = parameters[0]; - self.connection = parameters[1]; - self.printerStateViewModel = parameters[2]; + self.global_settings = parameters[0]; + self.connection = parameters[1]; + self.printerStateViewModel = parameters[2]; - self.temperature_reading = ko.observableArray(); - self.temperature_control = ko.observableArray(); - self.rpi_outputs = ko.observableArray(); - self.rpi_inputs = ko.observableArray(); - self.filamentSensorGcode = ko.observable(); + self.temperature_reading = ko.observableArray(); + self.temperature_control = ko.observableArray(); + self.rpi_outputs = ko.observableArray(); + self.rpi_inputs = ko.observableArray(); + self.filamentSensorGcode = ko.observable(); - self.enclosureTemp = ko.observable(); - self.enclosureSetTemperature = ko.observable(); - self.enclosureHumidity = ko.observable(); + self.enclosureTemp = ko.observable(); + self.enclosureSetTemperature = ko.observable(); + self.enclosureHumidity = ko.observable(); - self.previousGpioStatus; - self.previousGpioPWMStatus; - self.navbarTemp= ko.observable(); - self.navbarHum= ko.observable(); + self.previousGpioStatus; + self.previousGpioPWMStatus; + self.navbarTemp = ko.observable(); + self.navbarHum = ko.observable(); - self.showTempNavbar= ko.observable(); + self.showTempNavbar = ko.observable(); - self.notificationProvider = ko.observable(); - self.event_name = ko.observable(); - self.apiKEY = ko.observable(); - self.notifications = ko.observable(); + self.notificationProvider = ko.observable(); + self.event_name = ko.observable(); + self.apiKEY = ko.observable(); + self.notifications = ko.observable(); - self.onDataUpdaterPluginMessage = function(plugin, data) { - if (plugin != "enclosure") { - return; - } + self.onDataUpdaterPluginMessage = function (plugin, data) { + if (plugin != "enclosure") { + return; + } - if (data.hasOwnProperty("enclosuretemp")) { - self.enclosureTemp(data.enclosuretemp); + if (data.hasOwnProperty("enclosuretemp")) { + self.enclosureTemp(data.enclosuretemp); - self.temperature_reading().forEach(function(element) { - if("useFahrenheit" in element ){ - useFahrenheit = element['useFahrenheit']() + self.temperature_reading().forEach(function (element) { + if ("useFahrenheit" in element) { + useFahrenheit = element['useFahrenheit']() - if(useFahrenheit){ - self.navbarTemp(_.sprintf("Enc: %.1f°F", data.enclosuretemp)); - }else{ - self.navbarTemp(_.sprintf("Enc: %.1f°C", data.enclosuretemp)); - } - } - }); - } - if (data.hasOwnProperty("enclosureHumidity")) { - self.enclosureHumidity(data.enclosureHumidity); - self.navbarHum(_.sprintf("Hum: %.1f%%", data.enclosureHumidity)); + if (useFahrenheit) { + self.navbarTemp(_.sprintf("Enc: %.1f°F", data.enclosuretemp)); + } else { + self.navbarTemp(_.sprintf("Enc: %.1f°C", data.enclosuretemp)); } + } + }); + } + if (data.hasOwnProperty("enclosureHumidity")) { + self.enclosureHumidity(data.enclosureHumidity); + self.navbarHum(_.sprintf("Hum: %.1f%%", data.enclosureHumidity)); + } + + if (data.hasOwnProperty("enclosureSetTemp")) { + if (parseFloat(data.enclosureSetTemp) > 0.0) { + $("#enclosureSetTemp").attr("placeholder", data.enclosureSetTemp); + } else { + $("#enclosureSetTemp").attr("placeholder", "off"); + } + } + + if (!data.rpi_output) { + data.rpi_output = self.previousGpioStatus; + } + + if (!data.rpi_output_pwm) { + data.rpi_output_pwm = self.previousGpioPWMStatus; + } + + if (data.rpi_output) { + data.rpi_output.forEach(function (gpio) { + key = Object.keys(gpio)[0]; + if (gpio[key]) { + $("#btn_off_" + key).removeClass('active'); + $("#btn_on_" + key).addClass('active'); + } else { + $("#btn_off_" + key).addClass('active'); + $("#btn_on_" + key).removeClass('active'); + } + }); + self.previousGpioStatus = data.rpi_output; + } + + if (data.rpi_output_pwm) { + data.rpi_output_pwm.forEach(function (gpio) { + key = Object.keys(gpio)[0]; + val = gpio[key]; + if (parseFloat(val) != 100) { + $("#dutycycle_" + key).attr("placeholder", val); + } else { + $("#dutycycle_" + key).attr("placeholder", "off"); + } + }); + self.previousGpioPWMStatus = data.rpi_output_pwm; + } - if (data.hasOwnProperty("enclosureSetTemp")){ - if (parseFloat(data.enclosureSetTemp)>0.0){ - $("#enclosureSetTemp").attr("placeholder", data.enclosureSetTemp); - }else{ - $("#enclosureSetTemp").attr("placeholder", "off"); - } - } + if (data.isMsg) { + new PNotify({ title: "Enclosure", text: data.msg, type: "error" }); + } + }; - if(!data.rpi_output){ - data.rpi_output = self.previousGpioStatus; - } + self.enableBtn = ko.computed(function () { + // return self.connection.loginState.isUser() && self.printerStateViewModel.isOperational(); + return self.connection.loginState.isUser(); + }); - if(!data.rpi_output_pwm){ - data.rpi_output_pwm = self.previousGpioPWMStatus; - } - if(data.rpi_output){ - data.rpi_output.forEach(function(gpio) { - key = Object.keys(gpio)[0]; - if(gpio[key]){ - $("#btn_off_"+key).removeClass('active'); - $("#btn_on_"+key).addClass('active'); - }else{ - $("#btn_off_"+key).addClass('active'); - $("#btn_on_"+key).removeClass('active'); - } - }); - self.previousGpioStatus = data.rpi_output; - } + self.getCleanTemperature = function (temp) { + if (temp === undefined || !_.isNumber(temp)) return "-"; + if (temp < 10) return gettext("off"); + return temp; + } - if(data.rpi_output_pwm){ - data.rpi_output_pwm.forEach(function(gpio) { - key = Object.keys(gpio)[0]; - val = gpio[key]; - if (parseFloat(val)!=100){ - $("#dutycycle_"+key).attr("placeholder", val); - }else{ - $("#dutycycle_"+key).attr("placeholder", "off"); - } - }); - self.previousGpioPWMStatus = data.rpi_output_pwm; - } - if (data.isMsg) { - new PNotify({title:"Enclosure", text:data.msg, type: "error"}); - } - }; + self.onBeforeBinding = function () { + self.settings = self.global_settings.settings.plugins.enclosure; + self.temperature_reading(self.settings.temperature_reading()); + // self.temperature_control(self.settings.temperature_control.slice(0)); + self.rpi_outputs(self.settings.rpi_outputs()); + self.rpi_inputs(self.settings.rpi_inputs()); + self.filamentSensorGcode(self.settings.filamentSensorGcode()); - self.enableBtn = ko.computed(function() { - // return self.connection.loginState.isUser() && self.printerStateViewModel.isOperational(); - return self.connection.loginState.isUser(); - }); + self.notificationProvider(self.settings.notificationProvider()); + self.showTempNavbar(self.settings.showTempNavbar()); - self.onBeforeBinding = function () { - self.settings = self.global_settings.settings.plugins.enclosure; - self.temperature_reading(self.settings.temperature_reading()); - // self.temperature_control(self.settings.temperature_control.slice(0)); - self.rpi_outputs(self.settings.rpi_outputs()); - self.rpi_inputs(self.settings.rpi_inputs()); - self.filamentSensorGcode(self.settings.filamentSensorGcode()); - - self.notificationProvider(self.settings.notificationProvider()); - self.showTempNavbar(self.settings.showTempNavbar()); - - self.event_name(self.settings.event_name()); - self.apiKEY(self.settings.apiKEY()); - self.notifications(self.settings.notifications()); - }; - - self.onStartupComplete = function () { - self.getUpdateBtnStatus(); - }; - - self.onDataUpdaterReconnect = function () { - self.getUpdateBtnStatus(); - }; - - self.onSettingsShown = function(){ - self.fixUI(); - }; - - self.showColorPicker = function(){ - $('[name=colorpicker]').colorpicker({format: 'rgb'}); - } + self.event_name(self.settings.event_name()); + self.apiKEY(self.settings.apiKEY()); + self.notifications(self.settings.notifications()); + }; - self.onSettingsHidden = function(){ - self.getUpdateBtnStatus(); - }; - - self.getRegularOutputs = function(){ - return self.global_settings.settings.plugins.enclosure.rpi_outputs().filter(function(rpi_outputs) { - return rpi_outputs.outputType == 'regular'; - }); - }; - self.setTemperature = function(){ - if(self.isNumeric($("#enclosureSetTemp").val())){ - $.ajax({ - url: self.buildPluginUrl("/setEnclosureTemperature"), - type: "GET", - dataType: "json", - data: {"enclosureSetTemp": Number($("#enclosureSetTemp").val())}, - success: function(data) { - $("#enclosureSetTemp").val(''); - $("#enclosureSetTemp").attr("placeholder", self.getStatusHeater(data.enclosureSetTemperature,data.enclosureCurrentTemperature)); - } - }); - }else{ - alert("Temperature is not a number"); - } - }; - - self.addRpiOutput = function(){ - self.global_settings.settings.plugins.enclosure.rpi_outputs.push({label: ko.observable("Ouput "+ - (self.global_settings.settings.plugins.enclosure.rpi_outputs().length+1)) , - gpioPin:ko.observable(0),activeLow: true, - autoStartup:ko.observable(false), startupTimeDelay:0, autoShutdown:ko.observable(false),shutdownTimeDelay:0, - outputType:ko.observable('regular'),frequency:50,dutycycle:0,color:"rgb(255,0,0)",neopixelCount:0,neopixelBrightness:255,microAddress:0}); - }; - - self.removeRpiOutput = function(definition) { - self.global_settings.settings.plugins.enclosure.rpi_outputs.remove(definition); - }; - - self.addRpiInput = function(){ - self.global_settings.settings.plugins.enclosure.rpi_inputs.push({label:ko.observable( "Input "+ - (self.global_settings.settings.plugins.enclosure.rpi_inputs().length+1)), gpioPin: 0,inputPull: "inputPullUp", - eventType:ko.observable("temperature"),setTemp:100,controlledIO:ko.observable(""),setControlledIO:"low", - edge:"fall",printerAction:"filament"}); - }; - - self.removeRpiInput = function(definition) { - self.global_settings.settings.plugins.enclosure.rpi_inputs.remove(definition); - }; - - self.turnOffHeater = function(){ - $.ajax({ - url: self.buildPluginUrl("/setEnclosureTemperature"), - type: "GET", - dataType: "json", - data: {"enclosureSetTemp":0}, - success: function(data) { - $("#enclosureSetTemp").val(''); - $("#enclosureSetTemp").attr("placeholder", self.getStatusHeater(data.enclosureSetTemperature,data.enclosureCurrentTemperature)); - } - }); - }; - - self.clearGPIOMode = function(){ - $.ajax({ - url: self.buildPluginUrl("/clearGPIOMode"), - type: "GET", - dataType: "json", - success: function(data) { - new PNotify({title:"Enclosure", text:"GPIO Mode cleared successfully", type: "success"}); - } - }); - }; - - self.getUpdateBtnStatus = function(){ - - $.ajax({ - url: self.buildPluginUrl("/getUpdateBtnStatus"), - type: "GET" - }); - }; - - self.requestEnclosureTemperature = function(){ - return $.ajax({ - type: "GET", - url: self.buildPluginUrl("/getEnclosureTemperature"), - async: false - }).responseText; - }; - - self.requestEnclosureSetTemperature = function(){ - return $.ajax({ - type: "GET", - url: self.buildPluginUrl("/getEnclosureSetTemperature"), - async: false - }).responseText; - }; - - self.getStatusHeater = function(setTemp,currentTemp){ - if (parseFloat(setTemp)>0.0){ - return cleanTemperature(setTemp); - } - return "off"; - }; - - self.handleIO = function(data, event){ - $.ajax({ - type: "GET", - dataType: "json", - data: {"io": data[0], "status": data[1]}, - url: self.buildPluginUrl("/setIO"), - async: false - }); - }; - - self.handlePWM = function(data, event){ - io = parseInt(data[0]); - pwmVal = parseInt($("#dutycycle_"+io).val()); - if(pwmVal<0 || pwmVal>100 || isNaN(pwmVal)){ - $("#dutycycle_"+io).val('') - new PNotify({title:"Enclosure", text:"Duty Cycle value needs to be between 0 and 100!", type: "error"}); - }else{ - // console.log(pwmVal); - $("#dutycycle_"+io).val('') - $("#dutycycle_"+io).attr("placeholder", pwmVal); - $.ajax({ - type: "GET", - dataType: "json", - data: {"io": io, "pwmVal": pwmVal}, - url: self.buildPluginUrl("/setPWM"), - }); - } - }; - - self.handleNeopixel = function(data, event){ - io = parseInt(data[0]); - tempStr = ($("#color_"+io).val()).replace("rgb(", ""); - - r = parseInt(tempStr.substring(0, tempStr.indexOf(","))); - tempStr = tempStr.slice(tempStr.indexOf(",")+1); - g = parseInt(tempStr.substring(0, tempStr.indexOf(","))); - tempStr = tempStr.slice(tempStr.indexOf(",")+1); - b = parseInt(tempStr.substring(0, tempStr.indexOf(")"))); - - if(r<0 || r>255 || g<0 || g>255 || b<0 || b >255 || isNaN(r) || isNaN(g) || isNaN(b)){ - new PNotify({title:"Enclosure", text:"Color needs to follow the format rgb(value_red,value_green,value_blue)!", type: "error"}); - }else { - $.ajax({ - type: "GET", - dataType: "json", - data: {"io": io, "red": r,"green": g,"blue": b}, - url: self.buildPluginUrl("/setNeopixel"), - }); - } - }; - - self.fixUI = function(){ - if($('#enableTemperatureReading').is(':checked')){ - $('#enableHeater').prop('disabled', false); - $('#temperature_reading_content').show("blind"); - // $('#temperature_control_content').show("blind"); - }else{ - $('#enableHeater').prop('disabled', true); - $('#enableHeater').prop('checked', false); - $('#temperature_reading_content').hide("blind"); - // $('#temperature_control_content').hide("blind"); - } + self.onStartupComplete = function () { + self.getUpdateBtnStatus(); + }; - if($('#enableHeater').is(':checked')){ - $('#temperature_control_content').show("blind"); - }else{ - $('#temperature_control_content').hide("blind"); - } + self.onDataUpdaterReconnect = function () { + self.getUpdateBtnStatus(); + }; - }; - - // self.fixEventTypeUI = function(){ - // $('[name^="eventType_"]').each(function() { - // if($( this ).is(':checked')){ - // selectedType = $( this ).val(); - // idNumber = $( this ).attr('name').replace("eventType_", ""); - // self.eventTypeUI(idNumber,selectedType); - // } - // }); - // }; - // - // self.eventTypeUI = function(idNumber,selectedType){ - // - // $('#input_io_'+idNumber).hide(); - // $('#temp_controlled_'+idNumber).hide(); - // $('#filament_controlled_'+idNumber).hide(); - // $('#gpio_controlled_'+idNumber).hide(); - // - // if(selectedType=='temperature'){ - // $('#temp_controlled_'+idNumber).show(); - // console.log("temperature"); - // }else if(selectedType=='filament'){ - // $('#filament_controlled_'+idNumber).show(); - // $('#input_io_'+idNumber).show(); - // console.log("filament"); - // }else if(selectedType=='gpio'){ - // $('#gpio_controlled_'+idNumber).show(); - // $('#input_io_'+idNumber).show(); - // console.log("gpio"); - // } - // }; - - // - // self.fixAutoStartupUI = function(idNumber){ - // if($('#autoStartup_'+idNumber).is(':checked')){ - // $('#autoStartupField_'+idNumber).show("blind"); - // }else{ - // $('#autoStartupField_'+idNumber).hide("blind"); - // } - // }; - // - // self.fixAutoShutdownUI = function(idNumber){ - // if($('#autoShutdown_'+idNumber).is(':checked')){ - // $('#autoShutdownField_'+idNumber).show("blind"); - // }else{ - // $('#autoShutdownField_'+idNumber).hide("blind"); - // } - // }; - - self.isNumeric = function(n){ - return !isNaN(parseFloat(n)) && isFinite(n); - }; - - self.buildPluginUrl = function(path) { - return window.PLUGIN_BASEURL + self.pluginName + path; - }; + self.onSettingsShown = function () { + self.fixUI(); + }; + + self.showColorPicker = function () { + $('[name=colorpicker]').colorpicker({ format: 'rgb' }); } - OCTOPRINT_VIEWMODELS.push([ - EnclosureViewModel, - ["settingsViewModel","connectionViewModel","printerStateViewModel"], - ["#tab_plugin_enclosure","#settings_plugin_enclosure","#navbar_plugin_enclosure"] - ]); + self.onSettingsHidden = function () { + self.getUpdateBtnStatus(); + }; + + self.getRegularOutputs = function () { + return self.global_settings.settings.plugins.enclosure.rpi_outputs().filter(function (rpi_outputs) { + return rpi_outputs.outputType == 'regular'; + }); + }; + self.setTemperature = function () { + if (self.isNumeric($("#enclosureSetTemp").val())) { + $.ajax({ + url: self.buildPluginUrl("/setEnclosureTemperature"), + type: "GET", + dataType: "json", + data: { "enclosureSetTemp": Number($("#enclosureSetTemp").val()) }, + success: function (data) { + $("#enclosureSetTemp").val(''); + $("#enclosureSetTemp").attr("placeholder", self.getStatusHeater(data.enclosureSetTemperature, data.enclosureCurrentTemperature)); + } + }); + } else { + alert("Temperature is not a number"); + } + }; + + self.addRpiOutput = function () { + self.global_settings.settings.plugins.enclosure.rpi_outputs.push({ + label: ko.observable("Ouput " + + (self.global_settings.settings.plugins.enclosure.rpi_outputs().length + 1)), + gpioPin: ko.observable(0), activeLow: true, + autoStartup: ko.observable(false), startupTimeDelay: 0, autoShutdown: ko.observable(false), shutdownTimeDelay: 0, + outputType: ko.observable('regular'), frequency: 50, dutycycle: 0, color: "rgb(255,0,0)", + neopixelCount: 0, neopixelBrightness: 255, microAddress: 0 + }); + }; + + self.removeRpiOutput = function (definition) { + self.global_settings.settings.plugins.enclosure.rpi_outputs.remove(definition); + }; + + self.addRpiInput = function () { + self.global_settings.settings.plugins.enclosure.rpi_inputs.push({ + label: ko.observable("Input " + + (self.global_settings.settings.plugins.enclosure.rpi_inputs().length + 1)), gpioPin: 0, inputPull: "inputPullUp", + eventType: ko.observable("temperature"), setTemp: 100, controlledIO: ko.observable(""), setControlledIO: "low", + edge: "fall", printerAction: "filament" + }); + }; + + self.removeRpiInput = function (definition) { + self.global_settings.settings.plugins.enclosure.rpi_inputs.remove(definition); + }; + + self.turnOffHeater = function () { + $.ajax({ + url: self.buildPluginUrl("/setEnclosureTemperature"), + type: "GET", + dataType: "json", + data: { "enclosureSetTemp": 0 }, + success: function (data) { + $("#enclosureSetTemp").val(''); + $("#enclosureSetTemp").attr("placeholder", self.getStatusHeater(data.enclosureSetTemperature, data.enclosureCurrentTemperature)); + } + }); + }; + + self.clearGPIOMode = function () { + $.ajax({ + url: self.buildPluginUrl("/clearGPIOMode"), + type: "GET", + dataType: "json", + success: function (data) { + new PNotify({ title: "Enclosure", text: "GPIO Mode cleared successfully", type: "success" }); + } + }); + }; + + self.getUpdateBtnStatus = function () { + + $.ajax({ + url: self.buildPluginUrl("/getUpdateBtnStatus"), + type: "GET" + }); + }; + + self.requestEnclosureTemperature = function () { + return $.ajax({ + type: "GET", + url: self.buildPluginUrl("/getEnclosureTemperature"), + async: false + }).responseText; + }; + + self.requestEnclosureSetTemperature = function () { + return $.ajax({ + type: "GET", + url: self.buildPluginUrl("/getEnclosureSetTemperature"), + async: false + }).responseText; + }; + + self.getStatusHeater = function (setTemp, currentTemp) { + if (parseFloat(setTemp) > 0.0) { + return self.getCleanTemperature(setTemp); + } + return "off"; + }; + + self.handleIO = function (data, event) { + $.ajax({ + type: "GET", + dataType: "json", + data: { "io": data[0], "status": data[1] }, + url: self.buildPluginUrl("/setIO"), + async: false + }); + }; + + self.handlePWM = function (data, event) { + io = parseInt(data[0]); + pwmVal = parseInt($("#dutycycle_" + io).val()); + if (pwmVal < 0 || pwmVal > 100 || isNaN(pwmVal)) { + $("#dutycycle_" + io).val('') + new PNotify({ title: "Enclosure", text: "Duty Cycle value needs to be between 0 and 100!", type: "error" }); + } else { + // console.log(pwmVal); + $("#dutycycle_" + io).val('') + $("#dutycycle_" + io).attr("placeholder", pwmVal); + $.ajax({ + type: "GET", + dataType: "json", + data: { "io": io, "pwmVal": pwmVal }, + url: self.buildPluginUrl("/setPWM"), + }); + } + }; + + self.handleNeopixel = function (data, event) { + io = parseInt(data[0]); + tempStr = ($("#color_" + io).val()).replace("rgb(", ""); + + r = parseInt(tempStr.substring(0, tempStr.indexOf(","))); + tempStr = tempStr.slice(tempStr.indexOf(",") + 1); + g = parseInt(tempStr.substring(0, tempStr.indexOf(","))); + tempStr = tempStr.slice(tempStr.indexOf(",") + 1); + b = parseInt(tempStr.substring(0, tempStr.indexOf(")"))); + + if (r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255 || isNaN(r) || isNaN(g) || isNaN(b)) { + new PNotify({ title: "Enclosure", text: "Color needs to follow the format rgb(value_red,value_green,value_blue)!", type: "error" }); + } else { + $.ajax({ + type: "GET", + dataType: "json", + data: { "io": io, "red": r, "green": g, "blue": b }, + url: self.buildPluginUrl("/setNeopixel"), + }); + } + }; + + self.fixUI = function () { + if ($('#enableTemperatureReading').is(':checked')) { + $('#enableHeater').prop('disabled', false); + $('#temperature_reading_content').show("blind"); + // $('#temperature_control_content').show("blind"); + } else { + $('#enableHeater').prop('disabled', true); + $('#enableHeater').prop('checked', false); + $('#temperature_reading_content').hide("blind"); + // $('#temperature_control_content').hide("blind"); + } + + if ($('#enableHeater').is(':checked')) { + $('#temperature_control_content').show("blind"); + } else { + $('#temperature_control_content').hide("blind"); + } + + }; + + self.isNumeric = function (n) { + return !isNaN(parseFloat(n)) && isFinite(n); + }; + + self.buildPluginUrl = function (path) { + return window.PLUGIN_BASEURL + self.pluginName + path; + }; + } + + OCTOPRINT_VIEWMODELS.push([ + EnclosureViewModel, + ["settingsViewModel", "connectionViewModel", "printerStateViewModel"], + ["#tab_plugin_enclosure", "#settings_plugin_enclosure", "#navbar_plugin_enclosure"] + ]); }); diff --git a/octoprint_enclosure/templates/enclosure_tab.jinja2 b/octoprint_enclosure/templates/enclosure_tab.jinja2 index 57b50e6..9c4553e 100644 --- a/octoprint_enclosure/templates/enclosure_tab.jinja2 +++ b/octoprint_enclosure/templates/enclosure_tab.jinja2 @@ -18,7 +18,7 @@
+ data-bind="attr: {placeholder:getCleanTemperature($root.requestEnclosureSetTemperature())}"> °F °C