No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

48 líneas
1.3KB

  1. from flask import Flask, json, g
  2. from flask_socketio import SocketIO
  3. import yaml
  4. import logging.config
  5. from modules.core.core import CraftBeerPi, ActorBase, SensorBase
  6. from modules.core.db import DBModel
  7. app = Flask(__name__)
  8. logging.config.dictConfig(yaml.load(open('./config/logger.yaml', 'r')))
  9. app.config['SECRET_KEY'] = 'craftbeerpi'
  10. app.config['UPLOAD_FOLDER'] = './upload'
  11. @app.teardown_appcontext
  12. def close_connection(exception):
  13. db = getattr(g, '_database', None)
  14. if db is not None:
  15. db.close()
  16. class ComplexEncoder(json.JSONEncoder):
  17. def default(self, obj):
  18. try:
  19. if isinstance(obj, DBModel):
  20. return obj.__dict__
  21. elif isinstance(obj, ActorBase):
  22. return obj.state()
  23. elif isinstance(obj, SensorBase):
  24. return obj.get_value()
  25. elif hasattr(obj, "callback"):
  26. return obj()
  27. else:
  28. return None
  29. except TypeError as e:
  30. pass
  31. return None
  32. app.json_encoder = ComplexEncoder
  33. socketio = SocketIO(app, json=json, logging=False)
  34. cbpi = CraftBeerPi(app, socketio)
  35. app.logger.info("##########################################")
  36. app.logger.info("### NEW STARTUP Version 3.0")
  37. app.logger.info("##########################################")