Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

57 Zeilen
1.6KB

  1. import json
  2. import sys, os
  3. from flask import Flask, render_template, redirect, json, g
  4. from flask_socketio import SocketIO, emit
  5. from flask_sqlalchemy import SQLAlchemy
  6. import logging
  7. from sqlalchemy.ext.declarative import DeclarativeMeta
  8. from modules.core.core import CraftBeerPi, ActorBase, SensorBase
  9. from modules.core.db import DBModel
  10. app = Flask(__name__)
  11. logging.basicConfig(filename='./logs/app.log',level=logging.INFO)
  12. app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///../craftbeerpi.db'
  13. #app.config['SQLALCHEMY_ECHO'] = False
  14. app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
  15. app.config['SECRET_KEY'] = 'craftbeerpi'
  16. app.config['UPLOAD_FOLDER'] = './upload'
  17. db = SQLAlchemy(app)
  18. @app.teardown_appcontext
  19. def close_connection(exception):
  20. db = getattr(g, '_database', None)
  21. if db is not None:
  22. db.close()
  23. class ComplexEncoder(json.JSONEncoder):
  24. def default(self, obj):
  25. try:
  26. if isinstance(obj, DBModel):
  27. return obj.__dict__
  28. elif isinstance(obj, ActorBase):
  29. return obj.state()
  30. elif isinstance(obj, SensorBase):
  31. return obj.get_value()
  32. elif hasattr(obj, "callback"):
  33. return obj()
  34. else:
  35. return None
  36. except TypeError as e:
  37. pass
  38. return None
  39. app.json_encoder = ComplexEncoder
  40. socketio = SocketIO(app, json=json)
  41. cbpi = CraftBeerPi(app, socketio)
  42. app.logger.info("##########################################")
  43. app.logger.info("### NEW STARTUP Version 3.0")
  44. app.logger.info("##########################################")