Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # coding=utf-8
- from __future__ import absolute_import
- import octoprint.plugin
- import octoprint.settings
- import octoprint.util
- from octoprint.events import eventManager, Events
- from flask import jsonify, request
- import logging
- import logging.handlers
- import RPi.GPIO as GPIO
- class Powerpo(octoprint.plugin.StartupPlugin,octoprint.plugin.SettingsPlugin,octoprint.plugin.EventHandlerPlugin,octoprint.plugin.BlueprintPlugin):
- def initialize(self):
- self._logger.setLevel(logging.DEBUG)
- self._logger.info("Running RPi.GPIO version '{0}'...".format(GPIO.VERSION))
- if GPIO.VERSION < "0.6":
- raise Exception("RPi.GPIO must be greater than 0.6")
- GPIO.setmode(GPIO.BCM)
- GPIO.setwarnings(False)
- self._logger.info("power pause [%s] initialized..."%self._identifier)
- def on_after_startup(self):
- self.PIN_POWER = self._settings.get(["pin"])
- if self.PIN_POWER != -1:
- self._logger.info("Power pause Plugin setup on GPIO [%s]..."%self.PIN_POWER)
- GPIO.setup(self.PIN_POWER, GPIO.IN)
- def get_settings_defaults(self):
- return dict(pin = -1)
- @octoprint.plugin.BlueprintPlugin.route("/status", methods=["GET"])
- def check_status(self):
- status = "-1"
- if self.PIN_POWER != -1:
- status = "1" if GPIO.input(self.PIN_POWER) else "0"
- return jsonify( status = status )
- def on_event(self, event, payload):
- if event == Events.PRINT_STARTED:
- self._logger.info("Printing started. Power is ON ")
- self.setup_gpio()
- elif event in (Events.PRINT_DONE, Events.PRINT_FAILED, Events.PRINT_CANCELLED):
- self._logger.info("Printing paused. Power failure!!!.")
- try:
- GPIO.remove_event_detect(self.PIN_FILAMENT)
- except:
- pass
- def setup_gpio(self):
- try:
- GPIO.remove_event_detect(self.PIN_POWER)
- except:
- pass
- if self.PIN_POWER != -1:
- GPIO.add_event_detect(self.PIN_POWER, GPIO.FALLING, callback=self.check_gpio)
- def check_gpio(self, channel):
- state = GPIO.input(self.PIN_POWER)
- self._logger.debug("Detected sensor [%s] state [%s]? !"%(channel, state))
- if not state: #safety pin ?
- self._logger.debug("Sensor [%s]!"%state)
- if self._printer.is_printing():
- self._printer.toggle_pause_print()
- __plugin_name__ = "Power-pause"
- __plugin_version__ = "1.0.1"
- __plugin_description__ = "Use a voltage measurement to pause printing when power goes out."
- def __plugin_load__():
- global __plugin_implementation__
- __plugin_implementation__ = Powerpo()
Add Comment
Please, Sign In to add comment