1
0

__init__.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. #對整個應用做初始化操作
  2. #主要工作:
  3. # 1.構建Flask的應用以及各種配置
  4. #2.構建SQLAlchemy的應用
  5. from flask import Flask, g, request
  6. from flask_sqlalchemy import SQLAlchemy
  7. import pymysql
  8. import eventlet
  9. from flask_mqtt import Mqtt
  10. from flask_migrate import Migrate
  11. #from flask_migrate import Migrate, MigrateCommand
  12. from flask_babel import Babel
  13. # from werkzeug.wrappers import Request
  14. #綠化(綠色線程)所有系統模組,實現IO多路複用
  15. # eventlet.monkey_patch()
  16. pymysql.install_as_MySQLdb()
  17. # # db = SQLAlchemy()
  18. # def register_i18n(app):
  19. # """Register i18n with the Flask application."""
  20. # defalut_language_str = app.config['DEFAULT_LANGUAGE']
  21. # support_language_list = app.config['SUPPORTED_LANGUAGES']
  22. # # 1. 取得子資料夾路徑 land_code
  23. # @app.url_value_preprocessor
  24. # def get_lang_code(endpoint, values):
  25. # if values is not None:
  26. # g.lang_code = values.pop('lang_code', defalut_language_str)
  27. # # 2. 確認 config 內包含 lang_code
  28. # @app.before_request
  29. # def ensure_lang_support():
  30. # lang_code = g.get('lang_code', None)
  31. # if lang_code and lang_code not in support_language_list:
  32. # g.lang_code = request.accept_languages.best_match(support_language_list)
  33. # # 3. 設定多語系
  34. # @app.localeselector
  35. # def get_locale():
  36. # return g.get('lang_code')
  37. # # 4. 確認在 1. pop lang_code 參數後的 lang_code 值
  38. # @app.url_defaults
  39. # def set_langrage_code(endpoint, values):
  40. # if 'lang_code' in values or not g.lang_code:
  41. # return
  42. # if app.url_map.is_endpoint_expecting(endpoint, 'lang_code'):
  43. # values['lang_code'] = g.lang_code
  44. def create_app():
  45. app = Flask(__name__)
  46. #配置啟動模式為調適模式
  47. # app.config['DEBUG'] = True
  48. #配置數據庫的連接字符串
  49. # app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://coffee:skyeye@52.69.200.169:3306/Coffee'
  50. app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:Gold@53743001@localhost:3306/Coffee'
  51. #配置數據庫內容再更新時自動提交
  52. app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
  53. #配置session所需要的秘鑰
  54. app.config['SECRET_KEY'] = 'you guess'
  55. # template 有修改後,會自動去更新
  56. app.config['TEMPLATES_AUTO_RELOAD'] = True
  57. #如果设置成True(默认情况),Flask - SQLAlchemy将会追踪对象的修改并且发送信号。这需要额外的内存, 如果不必要的可以禁用它
  58. app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = True
  59. #資料库连接池的大小。默认是数据库引擎的默认值 (通常是5)
  60. app.config['SQLALCHEMY_POOL_SIZE'] = 100
  61. #指定資料库连接池的超时时间。默认是 10
  62. # app.config['SQLALCHEMY_POOL_TIMEOUT'] = 60
  63. app.config['SQLALCHEMY_POOL_TIMEOUT'] = 60 * 60
  64. #自动回收连接的秒数。这对 MySQL 是必须的,默认 情况下 MySQL 会自动移除闲置 8 小时或者以上的连接。 需要注意地是如果使用 MySQL 的话, Flask-SQLAlchemy 会自动地设置这个值为 2 小时
  65. app.config['SQLALCHEMY_POOL_RECYCLE'] = 60 * 60 * 8
  66. app.config['SQLALCHEMY_MAX_OVERFLOW'] = 15
  67. # app.config['SECRET'] = 'my secret key'
  68. #MQTT相關設置
  69. app.config['MQTT_BROKER_URL'] = '60.250.156.234'
  70. app.config['MQTT_BROKER_PORT'] = 1883
  71. app.config['MQTT_USERNAME'] = 'aisky-client'
  72. app.config['MQTT_PASSWORD'] = 'aiskyc'
  73. #將發送ping到代理的時間間隔設置60秒
  74. app.config['MQTT_KEEPALIVE'] = 60
  75. #出於測試目的將TLS設置為禁用
  76. app.config['MQTT_TLS_ENABLED'] = False
  77. #資料庫的初始化
  78. db = SQLAlchemy(app)
  79. db.init_app(app)
  80. mqtt = Mqtt(app)
  81. # 初始化Flask-Migrate擴展
  82. migrate = Migrate(app, db)
  83. app.config['BABEL_DEFAULT_LOCALE'] = 'zh'
  84. app.config['BABEL_DEFAULT_TIMEZONE'] = 'UTC'
  85. app.config['LANGUAGES'] = ['en', 'zh']
  86. babel = Babel(app)
  87. @babel.localeselector
  88. def get_locale():
  89. return request.accept_languages.best_match(app.config['LANGUAGES'])
  90. #用來使用flask-migrate數據遷移使用,如要用在把下面模型類註釋消掉,並且將main裡的from coffee_manage import mqtt,db註釋掉(因會報導入錯誤)
  91. # # 用戶表
  92. # class User(db.Model):
  93. # __tablename__ = 'user'
  94. #
  95. # userID = db.Column(db.Integer, primary_key=True)
  96. # firstname = db.Column(db.String(30), nullable=False)
  97. # lastname = db.Column(db.String(30), nullable=False)
  98. # mail = db.Column(db.String(50), nullable=False)
  99. # phone = db.Column(db.String(20), nullable=False)
  100. # username = db.Column(db.String(30), nullable=False, unique=True)
  101. # password = db.Column(db.String(40), nullable=False)
  102. # status = db.Column(db.Integer, nullable=False, info='0:admin;1:new;9:disable')
  103. #
  104. # # 發酵槽溫度感測器表
  105. # class TankTemSensor(db.Model):
  106. # __tablename__ = 'tank_tem_sensor'
  107. #
  108. # sn = db.Column(db.Integer, primary_key=True)
  109. # datetime = db.Column(db.DateTime, nullable=False)
  110. # tank_num = db.Column(db.Integer, nullable=False)
  111. # tem = db.Column(db.Text, nullable=False)
  112. #
  113. # # 發酵槽二氧化碳感測器表
  114. # class TankCO2Sensor(db.Model):
  115. # __tablename__ = 'tank_co2_sensor'
  116. #
  117. # sn = db.Column(db.Integer, primary_key=True)
  118. # datetime = db.Column(db.DateTime, nullable=False)
  119. # tank_num = db.Column(db.Integer, nullable=False)
  120. # co2 = db.Column(db.Text, nullable=False)
  121. #
  122. # # 發酵槽酸鹼值感測器表
  123. # class TankPHSensor(db.Model):
  124. # __tablename__ = 'tank_ph_sensor'
  125. #
  126. # sn = db.Column(db.Integer, primary_key=True)
  127. # datetime = db.Column(db.DateTime, nullable=False)
  128. # tank_num = db.Column(db.Integer, nullable=False)
  129. # ph = db.Column(db.Text, nullable=False)
  130. #
  131. # class BeanValve(db.Model):
  132. # __tablename__ = 'bean_valve'
  133. #
  134. # sn = db.Column(db.Integer, primary_key=True)
  135. # datetime = db.Column(db.DateTime, nullable=False)
  136. # duration = db.Column(db.Text, nullable=False)
  137. # start = db.Column(db.Text, nullable=False)
  138. # end = db.Column(db.Text, nullable=False)
  139. #
  140. # class WashMachine(db.Model):
  141. # __tablename__ = 'wash_machine'
  142. #
  143. # sn = db.Column(db.Integer, primary_key=True)
  144. # datetime = db.Column(db.DateTime, nullable=False)
  145. # duration = db.Column(db.Text, nullable=False)
  146. # start = db.Column(db.Text, nullable=False)
  147. # end = db.Column(db.Text, nullable=False)
  148. #
  149. # class ConveyorBelt1(db.Model):
  150. # __tablename__ = 'conveyor_belt1'
  151. #
  152. # sn = db.Column(db.Integer, primary_key=True)
  153. # datetime = db.Column(db.DateTime, nullable=False)
  154. # duration = db.Column(db.Text, nullable=False)
  155. # start = db.Column(db.Text, nullable=False)
  156. # end = db.Column(db.Text, nullable=False)
  157. #
  158. # class Cargo1Disinfect(db.Model):
  159. # __tablename__ = 'cargo1_disinfect'
  160. #
  161. # sn = db.Column(db.Integer, primary_key=True)
  162. # datetime = db.Column(db.DateTime, nullable=False)
  163. # duration = db.Column(db.Text, nullable=False)
  164. # start = db.Column(db.Text, nullable=False)
  165. # end = db.Column(db.Text, nullable=False)
  166. #
  167. # class ColorMachine(db.Model):
  168. # __tablename__ = 'color_machine'
  169. #
  170. # sn = db.Column(db.Integer, primary_key=True)
  171. # datetime = db.Column(db.DateTime, nullable=False)
  172. # duration = db.Column(db.Text, nullable=False)
  173. # start = db.Column(db.Text, nullable=False)
  174. # end = db.Column(db.Text, nullable=False)
  175. #
  176. # class ConveyorBelt2(db.Model):
  177. # __tablename__ = 'conveyor_belt2'
  178. #
  179. # sn = db.Column(db.Integer, primary_key=True)
  180. # datetime = db.Column(db.DateTime, nullable=False)
  181. # duration = db.Column(db.Text, nullable=False)
  182. # start = db.Column(db.Text, nullable=False)
  183. # end = db.Column(db.Text, nullable=False)
  184. #
  185. # class PeelingMachine(db.Model):
  186. # __tablename__ = 'peeling_machine'
  187. #
  188. # sn = db.Column(db.Integer, primary_key=True)
  189. # datetime = db.Column(db.DateTime, nullable=False)
  190. # duration = db.Column(db.Text, nullable=False)
  191. # start = db.Column(db.Text, nullable=False)
  192. # end = db.Column(db.Text, nullable=False)
  193. #
  194. # class ConveyorBelt3(db.Model):
  195. # __tablename__ = 'conveyor_belt3'
  196. #
  197. # sn = db.Column(db.Integer, primary_key=True)
  198. # datetime = db.Column(db.DateTime, nullable=False)
  199. # duration = db.Column(db.Text, nullable=False)
  200. # start = db.Column(db.Text, nullable=False)
  201. # end = db.Column(db.Text, nullable=False)
  202. #
  203. # class Bacteria(db.Model):
  204. # __tablename__ = 'bacteria'
  205. #
  206. # sn = db.Column(db.Integer, primary_key=True)
  207. # datetime = db.Column(db.DateTime, nullable=False)
  208. # tank_num = db.Column(db.Integer, nullable=False)
  209. # duration = db.Column(db.Text, nullable=False)
  210. # start = db.Column(db.Text, nullable=False)
  211. # end = db.Column(db.Text, nullable=False)
  212. #
  213. # class Cargo2Disinfect(db.Model):
  214. # __tablename__ = 'cargo2_disinfect'
  215. #
  216. # sn = db.Column(db.Integer, primary_key=True)
  217. # datetime = db.Column(db.DateTime, nullable=False)
  218. # tank_num = db.Column(db.Integer, nullable=False)
  219. # duration = db.Column(db.Text, nullable=False)
  220. # start = db.Column(db.Text, nullable=False)
  221. # end = db.Column(db.Text, nullable=False)
  222. #
  223. # class Heating(db.Model):
  224. # __tablename__ = 'heating'
  225. #
  226. # sn = db.Column(db.Integer, primary_key=True)
  227. # datetime = db.Column(db.DateTime, nullable=False)
  228. # tank_num = db.Column(db.Integer, nullable=False)
  229. # duration = db.Column(db.Text, nullable=False)
  230. # start = db.Column(db.Text, nullable=False)
  231. # end = db.Column(db.Text, nullable=False)
  232. #
  233. # class Stir(db.Model):
  234. # __tablename__ = 'stir'
  235. #
  236. # sn = db.Column(db.Integer, primary_key=True)
  237. # datetime = db.Column(db.DateTime, nullable=False)
  238. # tank_num = db.Column(db.Integer, nullable=False)
  239. # duration = db.Column(db.Text, nullable=False)
  240. # start = db.Column(db.Text, nullable=False)
  241. # end = db.Column(db.Text, nullable=False)
  242. #
  243. # class WaterInjection(db.Model):
  244. # __tablename__ = 'water_injection'
  245. #
  246. # sn = db.Column(db.Integer, primary_key=True)
  247. # datetime = db.Column(db.DateTime, nullable=False)
  248. # tank_num = db.Column(db.Integer, nullable=False)
  249. # duration = db.Column(db.Text, nullable=False)
  250. # start = db.Column(db.Text, nullable=False)
  251. # end = db.Column(db.Text, nullable=False)
  252. #
  253. # class TopValve(db.Model):
  254. # __tablename__ = 'top_valve'
  255. #
  256. # sn = db.Column(db.Integer, primary_key=True)
  257. # datetime = db.Column(db.DateTime, nullable=False)
  258. # tank_num = db.Column(db.Integer, nullable=False)
  259. # duration = db.Column(db.Text, nullable=False)
  260. # start = db.Column(db.Text, nullable=False)
  261. # end = db.Column(db.Text, nullable=False)
  262. #
  263. # class BottomValve(db.Model):
  264. # __tablename__ = 'bottom_valve'
  265. #
  266. # sn = db.Column(db.Integer, primary_key=True)
  267. # datetime = db.Column(db.DateTime, nullable=False)
  268. # tank_num = db.Column(db.Integer, nullable=False)
  269. # duration = db.Column(db.Text, nullable=False)
  270. # start = db.Column(db.Text, nullable=False)
  271. # end = db.Column(db.Text, nullable=False)
  272. #
  273. # class Temperature(db.Model):
  274. # __tablename__ = 'temperature'
  275. #
  276. # sn = db.Column(db.Integer, primary_key=True)
  277. # datetime = db.Column(db.DateTime, nullable=False)
  278. # tank_num = db.Column(db.Integer, nullable=False)
  279. # duration = db.Column(db.Text, nullable=False)
  280. # start = db.Column(db.Text, nullable=False)
  281. # end = db.Column(db.Text, nullable=False)
  282. #
  283. # class Hoist(db.Model):
  284. # __tablename__ = 'hoist'
  285. #
  286. # sn = db.Column(db.Integer, primary_key=True)
  287. # datetime = db.Column(db.DateTime, nullable=False)
  288. # duration = db.Column(db.Text, nullable=False)
  289. # start = db.Column(db.Text, nullable=False)
  290. # end = db.Column(db.Text, nullable=False)
  291. #
  292. # class Dryer(db.Model):
  293. # __tablename__ = 'dryer'
  294. #
  295. # sn = db.Column(db.Integer, primary_key=True)
  296. # datetime = db.Column(db.DateTime, nullable=False)
  297. # duration = db.Column(db.Text, nullable=False)
  298. # start = db.Column(db.Text, nullable=False)
  299. # end = db.Column(db.Text, nullable=False)
  300. #
  301. # class Relay(db.Model):
  302. # __tablename__ = 'relay'
  303. #
  304. # sn = db.Column(db.Integer, primary_key=True)
  305. # datetime = db.Column(db.DateTime, nullable=False)
  306. # tank_num = db.Column(db.Integer, nullable=False)
  307. # status = db.Column(db.Text, nullable=False)
  308. #將main藍圖程序與app關聯到一起
  309. from .main import main as main_blueprint
  310. app.register_blueprint(main_blueprint)
  311. #將user藍圖程序與app關聯到一起
  312. from .user import user as user_blueprint
  313. app.register_blueprint(user_blueprint)
  314. # Rita 將lang藍圖程序與app關聯到一起
  315. # from .main import main
  316. # app.register_blueprint(main, url_prefix='/<lang_code>')
  317. return app, db, mqtt