Advertisement
Guest User

Untitled

a guest
Jul 5th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.32 KB | None | 0 0
  1. from flask import *
  2. import door_open
  3. from door_open import LEDInit
  4. from time import sleep
  5. import pickle
  6. LEDInit('starting')
  7.  
  8. app = Flask(__name__)
  9. pibtn = door_open
  10. lstatus = 0
  11.  
  12.  
  13. def writestatus(ledst):
  14.     with open('status.pickle', 'wb') as filef:
  15.         pickle._dump(ledst, filef)
  16.  
  17.  
  18. def loadststus():
  19.     with open('status.pickle', 'rb') as filel:
  20.         status = pickle.load(filel)
  21.     return status
  22.  
  23.  
  24. writestatus(lstatus)
  25. print(loadststus())
  26.  
  27.  
  28. @app.route("/")
  29. def index():
  30.     return render_template('index.html', ledstat=loadststus())
  31.  
  32.  
  33. LEDInit('init')
  34. @app.route("/led/<int:state>")
  35. def led(state):
  36.     if state == 0:
  37.         writestatus(state)
  38.         door_open.__init__()
  39.         door_open.set_led(False)
  40.         print(loadststus())
  41.     elif state == 1:
  42.         writestatus(state)
  43.         door_open.__init__()
  44.         door_open.set_led(True)
  45.         print(loadststus())
  46.     else:
  47.         return ('Unknown LED statement', 400)
  48.     return ('', 204)
  49.  
  50. @app.route("/switch")
  51. def switcΡ€():
  52.     def ReadLedState():
  53.         while True:
  54.             ledstatement = loadststus()
  55.             yield 'data: {0}\n\n'.format(ledstatement)
  56.             sleep(1)
  57.     return Response(loadststus(), mimetype='text/event-stream')
  58.  
  59.  
  60. if __name__ == "__main__":
  61.     app.run(host='0.0.0.0', debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement