Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.72 KB | None | 0 0
  1. from flask import Flask, render_template
  2. from flask import request
  3. from flask_socketio import SocketIO, emit
  4.  
  5. temperature_value = 0
  6. app = Flask(__name__, template_folder='.')
  7. app.config['SECRET_KEY'] = 'secret!'
  8. socketio = SocketIO(app)
  9.  
  10. @app.route('/postjson', methods = ['POST'])
  11. def postJsonHandler():
  12.     global temperature_value
  13.     content = request.get_json()
  14.     print ("postjson", flush = True)
  15.     print (content, flush = True)
  16.     temperature_value = content['temperature']
  17.     source = content['source'].lower()
  18.     if source == 'nyirad':
  19.         socketio.emit('temperature', {'data': temperature_value}, namespace='/nyirad')
  20.     elif source == 'odense':
  21.         socketio.emit('temperature', {'data': temperature_value}, namespace='/odense')
  22.     else:
  23.         print ("Unknown source({})", source, flush = True)
  24.     return 'JSON posted'
  25.  
  26. @app.route('/')
  27. @app.route('/index')
  28. def index():
  29.     return render_template('index.html',
  30.                            async_mode=socketio.async_mode)
  31.  
  32. @app.route('/odense')
  33. def odense_temperature():
  34.     global temperature_value
  35.     return render_template('/odense/temperature.html',
  36.                            async_mode=socketio.async_mode)
  37.  
  38. @app.route('/nyirad')
  39. def nyirad_temperature():
  40.     return render_template('/nyirad/temperature.html',
  41.                            async_mode=socketio.async_mode)
  42.  
  43. @socketio.on('connect', namespace='/odense')
  44. def odense_client_connect():
  45.     print ("Client connected - Odense", flush = True)
  46.     pass
  47.  
  48. @socketio.on('connect', namespace='/nyirad')
  49. def odense_client_connect():
  50.     print ("Client connected - Nyirad", flush = True)
  51.     pass
  52.  
  53. if __name__ == "__main__":
  54.     socketio.run(app, host='0.0.0.0', debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement