Advertisement
DeaD_EyE

door_control.py // no security // no last state

Aug 3rd, 2017
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.13 KB | None | 0 0
  1. import time
  2. import threading
  3.  
  4. from flask import Flask
  5. #from RPi import GPIO
  6.  
  7.  
  8. app = Flask(__name__)
  9.  
  10.  
  11. @app.route('/open')
  12. def open():
  13.     message = door.do_open()
  14.     return '<h1>' + message + '</h1>'
  15.  
  16.  
  17. @app.route('/close')
  18. def close():
  19.     message = door.do_close()
  20.     return '<h1>' + message + '</h1>'
  21.  
  22.  
  23. class Door:
  24.     def __init__(self, open_pin, close_pin, wait_time=25):
  25.         self.open_pin = open_pin
  26.         self.close_pin = close_pin
  27.         self.active = False
  28.         self.wait_time = wait_time
  29.         #self.setup()
  30.    
  31.     def setup(self):
  32.         GPIO.setmode(GPIO.BCM)
  33.         GPIO.setup(self.open_pin, GPIO.OUT, initial=False)
  34.         GPIO.setup(self.close_pin, GPIO.OUT, initial=False)
  35.        
  36.     def cleanup(self):
  37.         GPIO.cleanup([self.open_pin, self.close_pin])
  38.        
  39.     def do_open(self):
  40.         if not self.active:
  41.             threading.Thread(target=self._open).start()
  42.             return 'Opening door'
  43.         return 'Another task is already running'
  44.    
  45.     def do_close(self):
  46.         if not self.active:
  47.             threading.Thread(target=self._close).start()
  48.             return 'Closing door'
  49.         return 'Another task is already running'
  50.            
  51.     def _open(self):
  52.         self.active = True
  53.         print('Moving up')
  54.         #GPIO.output(self.close_pin, False)
  55.         #GPIO.output(self.open_pin, True)
  56.         time.sleep(self.wait_time)
  57.         print('Done')
  58.         #GPIO.output(self.open_pin, False)
  59.         self.active = False
  60.  
  61.     def _close(self):
  62.         self.active = True
  63.         print('Moving down')
  64.         #GPIO.output(self.open_pin, False)
  65.         #GPIO.output(self.close_pin, True)
  66.         time.sleep(self.wait_time)
  67.         print('Done')
  68.         #GPIO.output(self.close_pin, False)
  69.         self.active = False
  70.        
  71.     def __enter__(self):
  72.         print('Entering context manager')
  73.         return self
  74.  
  75.     def __exit__(self, *args):
  76.         #self.cleanup()
  77.         print('Cleanup outputs')
  78.        
  79.        
  80. if __name__ == '__main__':
  81.     with Door(open_pin=18, close_pin=17, wait_time=4) as door:
  82.         app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement