Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import threading
- from flask import Flask
- #from RPi import GPIO
- app = Flask(__name__)
- @app.route('/open')
- def open():
- message = door.do_open()
- return '<h1>' + message + '</h1>'
- @app.route('/close')
- def close():
- message = door.do_close()
- return '<h1>' + message + '</h1>'
- class Door:
- def __init__(self, open_pin, close_pin, wait_time=25):
- self.open_pin = open_pin
- self.close_pin = close_pin
- self.active = False
- self.wait_time = wait_time
- #self.setup()
- def setup(self):
- GPIO.setmode(GPIO.BCM)
- GPIO.setup(self.open_pin, GPIO.OUT, initial=False)
- GPIO.setup(self.close_pin, GPIO.OUT, initial=False)
- def cleanup(self):
- GPIO.cleanup([self.open_pin, self.close_pin])
- def do_open(self):
- if not self.active:
- threading.Thread(target=self._open).start()
- return 'Opening door'
- return 'Another task is already running'
- def do_close(self):
- if not self.active:
- threading.Thread(target=self._close).start()
- return 'Closing door'
- return 'Another task is already running'
- def _open(self):
- self.active = True
- print('Moving up')
- #GPIO.output(self.close_pin, False)
- #GPIO.output(self.open_pin, True)
- time.sleep(self.wait_time)
- print('Done')
- #GPIO.output(self.open_pin, False)
- self.active = False
- def _close(self):
- self.active = True
- print('Moving down')
- #GPIO.output(self.open_pin, False)
- #GPIO.output(self.close_pin, True)
- time.sleep(self.wait_time)
- print('Done')
- #GPIO.output(self.close_pin, False)
- self.active = False
- def __enter__(self):
- print('Entering context manager')
- return self
- def __exit__(self, *args):
- #self.cleanup()
- print('Cleanup outputs')
- if __name__ == '__main__':
- with Door(open_pin=18, close_pin=17, wait_time=4) as door:
- app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement