__init__.py 14 KB

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