Last active
November 7, 2017 06:33
-
-
Save amirandap/747ff6fcc3dffc5ad62c2ceefa114a12 to your computer and use it in GitHub Desktop.
homebridgeflask
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, jsonify, Response | |
import json | |
import RPi.GPIO as GPIO | |
import time | |
from flask import Flask, jsonify, Response | |
from concurrent.futures import ThreadPoolExecutor | |
executor = ThreadPoolExecutor(2) | |
app = Flask(__name__) | |
accs = { | |
'device': 'porton', | |
'state': 'CLOSED', | |
'light': False, | |
'success': True | |
} | |
def set_closing(): | |
time.sleep(5) | |
accs['state'] = 'Closing' | |
def set_closed(): | |
time.sleep(10) | |
accs['state'] = 'Closed' | |
@app.route('/api/v1/accs/porton', methods=['POST']) | |
def open_door(): | |
accs['state'] = 'OPENING' | |
accs['success'] = True | |
GPIO.setmode(GPIO.BOARD) | |
GPIO.setwarnings(False) | |
GPIO.setup(7, GPIO.OUT, initial=0) | |
GPIO.setup(11, GPIO.OUT, initial=0) | |
GPIO.setup(13, GPIO.OUT, initial=0) | |
GPIO.setup(15, GPIO.OUT, initial=0) | |
GPIO.output(7, 1) | |
time.sleep(0.75) | |
GPIO.output(7, 0) | |
GPIO.cleanup() | |
executor.submit(set_closing) | |
executor.submit(set_closed) | |
return jsonify(accs) | |
@app.route('/test') | |
def test(): | |
def generate(): | |
for i in range(5): | |
time.sleep(2) | |
yield str(i) | |
app.logger.info('request finished') | |
yield '' | |
return Response(generate(), mimetype='text/plain') | |
@app.route('/api/v1/accs/porton/close', methods=['POST']) | |
def close_door(): | |
accs['state'] = 'CLOSED' | |
accs['success'] = True | |
return jsonify(accs),201 | |
@app.route('/api/v1/accs/ascensor', methods=['POST']) | |
def open_light(): | |
accs['success'] = True | |
return jsonify(accs),201 | |
@app.route('/api/v1/accs/porton', methods=['GET']) | |
def get_status(): | |
return jsonify(accs) | |
if __name__ == "__main__": | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment