25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
4.5KB

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