1
0

__init__1221.py 13 KB

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