Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

151 rinda
5.4KB

  1. import logging
  2. from flask import json, request
  3. from flask_classy import FlaskView, route
  4. from git import Repo, Git
  5. import sqlite3
  6. from modules.core.core import cbpi
  7. from werkzeug.utils import secure_filename
  8. import pprint
  9. import time
  10. import os
  11. from modules.step import Step, StepView
  12. class KBH(FlaskView):
  13. def __init__(self):
  14. self.logger = logging.getLogger(__name__)
  15. @route('/', methods=['GET'])
  16. def get(self):
  17. """
  18. Get all recipes from uploaded kleinerbrauhelfer database
  19. ---
  20. tags:
  21. - kleinerbrauhelfer
  22. responses:
  23. 200:
  24. description: Recipes from kleinerbrauhelfer database
  25. """
  26. conn = None
  27. try:
  28. if not os.path.exists(self.api.app.config['UPLOAD_FOLDER'] + '/kbh.db'):
  29. self.api.notify(headline="File Not Found", message="Please upload a Kleiner Brauhelfer Database", type="danger")
  30. return ('', 404)
  31. conn = sqlite3.connect(self.api.app.config['UPLOAD_FOLDER'] + '/kbh.db')
  32. c = conn.cursor()
  33. c.execute('SELECT ID, Sudname, BierWurdeGebraut FROM Sud')
  34. data = c.fetchall()
  35. result = []
  36. for row in data:
  37. result.append({"id": row[0], "name": row[1], "brewed": row[2]})
  38. return json.dumps(result)
  39. except Exception as e:
  40. self.logger.error(e)
  41. self.api.notify(headline="Failed to load KHB database", message="ERROR", type="danger")
  42. return ('', 500)
  43. finally:
  44. if conn:
  45. conn.close()
  46. def allowed_file(self, filename):
  47. return '.' in filename and filename.rsplit('.', 1)[1] in set(['sqlite'])
  48. @route('/upload', methods=['POST'])
  49. def upload_file(self):
  50. """
  51. Upload KleinerBrauhelfer Database File
  52. ---
  53. tags:
  54. - kleinerbrauhelfer
  55. responses:
  56. 200:
  57. description: File uploaed
  58. """
  59. try:
  60. if request.method == 'POST':
  61. file = request.files['file']
  62. if file and self.allowed_file(file.filename):
  63. filename = secure_filename(file.filename)
  64. file.save(os.path.join(self.api.app.config['UPLOAD_FOLDER'], "kbh.db"))
  65. self.api.notify(headline="Upload Successful", message="The Kleiner Brauhelfer Database was uploaded succesfully")
  66. return ('', 204)
  67. return ('', 404)
  68. except Exception as e:
  69. self.api.notify(headline="Upload Failed", message="Failed to upload Kleiner Brauhelfer", type="danger")
  70. return ('', 500)
  71. @route('/<int:id>', methods=['POST'])
  72. def load(self, id):
  73. """
  74. Load Recipe from Kleinerbrauhelfer Database
  75. ---
  76. tags:
  77. - kleinerbrauhelfer
  78. parameters:
  79. - in: path
  80. name: id
  81. schema:
  82. type: integer
  83. required: true
  84. description: Numeric ID of recipe
  85. responses:
  86. 200:
  87. description: Recipe loaded
  88. """
  89. mashstep_type = cbpi.get_config_parameter("step_mash", "MashStep")
  90. mashinstep_type = cbpi.get_config_parameter("step_mashin", "MashInStep")
  91. chilstep_type = cbpi.get_config_parameter("step_chil", "ChilStep")
  92. boilstep_type = cbpi.get_config_parameter("step_boil", "BoilStep")
  93. mash_kettle = cbpi.get_config_parameter("step_mash_kettle", None)
  94. boil_kettle = cbpi.get_config_parameter("step_boil_kettle", None)
  95. boil_temp = 100 if cbpi.get_config_parameter("unit", "C") == "C" else 212
  96. # READ KBH DATABASE
  97. Step.delete_all()
  98. StepView().reset()
  99. conn = None
  100. try:
  101. conn = sqlite3.connect(self.api.app.config['UPLOAD_FOLDER'] + '/kbh.db')
  102. c = conn.cursor()
  103. c.execute('SELECT EinmaischenTemp, Sudname FROM Sud WHERE ID = ?', (id,))
  104. row = c.fetchone()
  105. name = row[1]
  106. self.api.set_config_parameter("brew_name", name)
  107. Step.insert(**{"name": "MashIn", "type": mashinstep_type, "config": {"kettle": mash_kettle, "temp": row[0]}})
  108. ### add rest step
  109. for row in c.execute('SELECT * FROM Rasten WHERE SudID = ?', (id,)):
  110. Step.insert(**{"name": row[5], "type": mashstep_type, "config": {"kettle": mash_kettle, "temp": row[3], "timer": row[4]}})
  111. Step.insert(**{"name": "Chil", "type": chilstep_type, "config": {"timer": 15}})
  112. ## Add cooking step
  113. c.execute('SELECT max(Zeit) FROM Hopfengaben WHERE SudID = ?', (id,))
  114. row = c.fetchone()
  115. Step.insert(**{"name": "Boil", "type": boilstep_type, "config": {"kettle": boil_kettle, "temp": boil_temp, "timer": row[0]}})
  116. ## Add Whirlpool step
  117. Step.insert(**{"name": "Whirlpool", "type": chilstep_type, "config": {"timer": 15}})
  118. #setBrewName(name)
  119. self.api.emit("UPDATE_ALL_STEPS", Step.get_all())
  120. self.api.notify(headline="Recipe %s loaded successfully" % name, message="")
  121. except Exception as e:
  122. self.api.notify(headline="Failed to load Recipe", message=e.message, type="danger")
  123. return ('', 500)
  124. finally:
  125. if conn:
  126. conn.close()
  127. return ('', 204)
  128. @cbpi.addon.core.initializer()
  129. def init(cbpi):
  130. KBH.api = cbpi
  131. KBH.register(cbpi._app, route_base='/api/kbh')