__init__.py 12 KB

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