Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

118 wiersze
4.7KB

  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. import xml.etree.ElementTree
  12. class BeerXMLImport(FlaskView):
  13. BEER_XML_FILE = "./upload/beer.xml"
  14. @route('/', methods=['GET'])
  15. def get(self):
  16. if not os.path.exists(self.BEER_XML_FILE):
  17. self.api.notify(headline="File Not Found", message="Please upload a Beer.xml File",
  18. type="danger")
  19. return ('', 404)
  20. result = []
  21. e = xml.etree.ElementTree.parse(self.BEER_XML_FILE).getroot()
  22. result = []
  23. for idx, val in enumerate(e.findall('RECIPE')):
  24. result.append({"id": idx+1, "name": val.find("NAME").text})
  25. return json.dumps(result)
  26. def allowed_file(self, filename):
  27. return '.' in filename and filename.rsplit('.', 1)[1] in set(['xml'])
  28. @route('/upload', methods=['POST'])
  29. def upload_file(self):
  30. try:
  31. if request.method == 'POST':
  32. file = request.files['file']
  33. if file and self.allowed_file(file.filename):
  34. file.save(os.path.join(self.api.app.config['UPLOAD_FOLDER'], "beer.xml"))
  35. self.api.notify(headline="Upload Successful", message="The Beer XML file was uploaded succesfully")
  36. return ('', 204)
  37. return ('', 404)
  38. except Exception as e:
  39. self.api.notify(headline="Upload Failed", message="Failed to upload Beer xml", type="danger")
  40. return ('', 500)
  41. @route('/<int:id>', methods=['POST'])
  42. def load(self, id):
  43. steps = self.getSteps(id)
  44. name = self.getRecipeName(id)
  45. self.api.set_config_parameter("brew_name", name)
  46. boil_time = self.getBoilTime(id)
  47. mashinstep_type = cbpi.get_config_parameter("step_mashin", "MashInStep")
  48. mashstep_type = cbpi.get_config_parameter("step_mash", "MashStep")
  49. mash_kettle = cbpi.get_config_parameter("step_mash_kettle", None)
  50. boilstep_type = cbpi.get_config_parameter("step_boil", "BoilStep")
  51. boil_kettle = cbpi.get_config_parameter("step_boil_kettle", None)
  52. boil_temp = 100 if cbpi.get_config_parameter("unit", "C") == "C" else 212
  53. # READ KBH DATABASE
  54. Step.delete_all()
  55. StepView().reset()
  56. try:
  57. ## Add Mashin step
  58. Step.insert(**{"name": "MashIn", "type": mashinstep_type, "config": {"kettle": mash_kettle, "temp": mashin_temp}})
  59. for row in steps:
  60. Step.insert(**{"name": row.get("name"), "type": mashstep_type, "config": {"kettle": mash_kettle, "temp": float(row.get("temp")), "timer": row.get("timer")}})
  61. Step.insert(**{"name": "ChilStep", "type": "ChilStep", "config": {"timer": 15}})
  62. ## Add cooking step
  63. Step.insert(**{"name": "Boil", "type": boilstep_type, "config": {"kettle": boil_kettle, "temp": boil_temp, "timer": boil_time}})
  64. ## Add Whirlpool step
  65. Step.insert(**{"name": "Whirlpool", "type": "ChilStep", "config": {"timer": 15}})
  66. self.api.emit("UPDATE_ALL_STEPS", Step.get_all())
  67. self.api.notify(headline="Recipe %s loaded successfully" % name, message="")
  68. except Exception as e:
  69. self.api.notify(headline="Failed to load Recipe", message=e.message, type="danger")
  70. return ('', 500)
  71. return ('', 204)
  72. def getRecipeName(self, id):
  73. e = xml.etree.ElementTree.parse(self.BEER_XML_FILE).getroot()
  74. return e.find('./RECIPE[%s]/NAME' % (str(id))).text
  75. def getBoilTime(self, id):
  76. e = xml.etree.ElementTree.parse(self.BEER_XML_FILE).getroot()
  77. return float(e.find('./RECIPE[%s]/BOIL_TIME' % (str(id))).text)
  78. def getMashinTemp(self, id):
  79. e = xml.etree.ElementTree.parse(self.BEER_XML_FILE).getroot()
  80. return float('./RECIPE[%s]/MASH/MASH_STEPS/INFUSE_TEMP' % (str(id))).text)
  81. def getSteps(self, id):
  82. e = xml.etree.ElementTree.parse(self.BEER_XML_FILE).getroot()
  83. steps = []
  84. for e in e.findall('./RECIPE[%s]/MASH/MASH_STEPS/MASH_STEP' % (str(id))):
  85. if self.api.get_config_parameter("unit", "C") == "C":
  86. temp = float(e.find("STEP_TEMP").text)
  87. else:
  88. temp = round(9.0 / 5.0 * float(e.find("STEP_TEMP").text) + 32, 2)
  89. steps.append({"name": e.find("NAME").text, "temp": temp, "timer": float(e.find("STEP_TIME").text)})
  90. return steps
  91. @cbpi.initalizer()
  92. def init(cbpi):
  93. BeerXMLImport.api = cbpi
  94. BeerXMLImport.register(cbpi.app, route_base='/api/beerxml')