amrith

Powerpo.py

Mar 31st, 2018
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.46 KB | None | 0 0
  1. # coding=utf-8
  2. from __future__ import absolute_import
  3.  
  4. import octoprint.plugin
  5. import octoprint.settings
  6. import octoprint.util
  7.  
  8. from octoprint.events import eventManager, Events
  9. from flask import jsonify, request
  10.  
  11. import logging
  12. import logging.handlers
  13. import RPi.GPIO as GPIO
  14.  
  15.  
  16. class Powerpo(octoprint.plugin.StartupPlugin,octoprint.plugin.SettingsPlugin,octoprint.plugin.EventHandlerPlugin,octoprint.plugin.BlueprintPlugin):
  17.     def initialize(self):
  18.         self._logger.setLevel(logging.DEBUG)
  19.        
  20.         self._logger.info("Running RPi.GPIO version '{0}'...".format(GPIO.VERSION))
  21.         if GPIO.VERSION < "0.6":
  22.             raise Exception("RPi.GPIO must be greater than 0.6")
  23.            
  24.         GPIO.setmode(GPIO.BCM)
  25.         GPIO.setwarnings(False)
  26.        
  27.         self._logger.info("power pause [%s] initialized..."%self._identifier)
  28.  
  29.     def on_after_startup(self):
  30.         self.PIN_POWER = self._settings.get(["pin"])
  31.        
  32.         if self.PIN_POWER != -1:
  33.             self._logger.info("Power pause Plugin setup on GPIO [%s]..."%self.PIN_POWER)
  34.             GPIO.setup(self.PIN_POWER, GPIO.IN)
  35.  
  36.     def get_settings_defaults(self):
  37.         return dict(pin = -1)
  38.  
  39.     @octoprint.plugin.BlueprintPlugin.route("/status", methods=["GET"])
  40.  
  41.     def check_status(self):
  42.         status = "-1"
  43.         if self.PIN_POWER != -1:
  44.             status = "1" if GPIO.input(self.PIN_POWER) else "0"
  45.         return jsonify( status = status )
  46.  
  47.     def on_event(self, event, payload):
  48.         if event == Events.PRINT_STARTED:
  49.             self._logger.info("Printing started. Power is ON ")
  50.             self.setup_gpio()
  51.         elif event in (Events.PRINT_DONE, Events.PRINT_FAILED, Events.PRINT_CANCELLED):
  52.             self._logger.info("Printing paused. Power failure!!!.")
  53.             try:
  54.                 GPIO.remove_event_detect(self.PIN_FILAMENT)
  55.             except:
  56.                 pass
  57.                
  58.     def setup_gpio(self):
  59.         try:
  60.             GPIO.remove_event_detect(self.PIN_POWER)
  61.         except:
  62.             pass
  63.         if self.PIN_POWER != -1:
  64.             GPIO.add_event_detect(self.PIN_POWER, GPIO.FALLING, callback=self.check_gpio)
  65.  
  66.     def check_gpio(self, channel):
  67.         state = GPIO.input(self.PIN_POWER)
  68.         self._logger.debug("Detected sensor [%s] state [%s]? !"%(channel, state))
  69.         if not state: #safety pin ?
  70.             self._logger.debug("Sensor [%s]!"%state)
  71.             if self._printer.is_printing():
  72.                 self._printer.toggle_pause_print()
  73.  
  74. __plugin_name__ = "Power-pause"
  75. __plugin_version__ = "1.0.1"
  76. __plugin_description__ = "Use a voltage measurement to pause printing when power goes out."
  77.  
  78. def __plugin_load__():
  79.         global __plugin_implementation__
  80.         __plugin_implementation__ = Powerpo()
Add Comment
Please, Sign In to add comment