Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

148 строки
5.0KB

  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.core.core import cbpi
  6. from werkzeug.utils import secure_filename
  7. import pprint
  8. import time
  9. import os
  10. from modules.step 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. """
  17. Get BeerXML
  18. ---
  19. tags:
  20. - beerxml
  21. responses:
  22. 200:
  23. description: BeerXML file stored in CraftBeerPI
  24. """
  25. if not os.path.exists(self.BEER_XML_FILE):
  26. self.api.notify(headline="File Not Found", message="Please upload a Beer.xml File",
  27. type="danger")
  28. return ('', 404)
  29. result = []
  30. e = xml.etree.ElementTree.parse(self.BEER_XML_FILE).getroot()
  31. result = []
  32. for idx, val in enumerate(e.findall('RECIPE')):
  33. result.append({"id": idx+1, "name": val.find("NAME").text})
  34. return json.dumps(result)
  35. def allowed_file(self, filename):
  36. return '.' in filename and filename.rsplit('.', 1)[1] in set(['xml'])
  37. @route('/upload', methods=['POST'])
  38. def upload_file(self):
  39. """
  40. Upload BeerXML File
  41. ---
  42. tags:
  43. - beerxml
  44. responses:
  45. 200:
  46. description: BeerXML File Uploaded
  47. """
  48. try:
  49. if request.method == 'POST':
  50. file = request.files['file']
  51. if file and self.allowed_file(file.filename):
  52. file.save(os.path.join(self.api.app.config['UPLOAD_FOLDER'], "beer.xml"))
  53. self.api.notify(headline="Upload Successful", message="The Beer XML file was uploaded succesfully")
  54. return ('', 204)
  55. return ('', 404)
  56. except Exception as e:
  57. self.api.notify(headline="Upload Failed", message="Failed to upload Beer xml", type="danger")
  58. return ('', 500)
  59. @route('/<int:id>', methods=['POST'])
  60. def load(self, id):
  61. """
  62. Load Recipe from BeerXML
  63. ---
  64. tags:
  65. - beerxml
  66. parameters:
  67. - in: path
  68. name: id
  69. schema:
  70. type: integer
  71. required: true
  72. description: Recipe ID from BeerXML
  73. responses:
  74. 200:
  75. description: Recipe loaed
  76. """
  77. steps = self.getSteps(id)
  78. name = self.getRecipeName(id)
  79. self.api.set_config_parameter("brew_name", name)
  80. boil_time = self.getBoilTime(id)
  81. mashstep_type = cbpi.get_config_parameter("step_mash", "MashStep")
  82. mash_kettle = cbpi.get_config_parameter("step_mash_kettle", None)
  83. boilstep_type = cbpi.get_config_parameter("step_boil", "BoilStep")
  84. boil_kettle = cbpi.get_config_parameter("step_boil_kettle", None)
  85. boil_temp = 100 if cbpi.get_config_parameter("unit", "C") == "C" else 212
  86. # READ KBH DATABASE
  87. Step.delete_all()
  88. StepView().reset()
  89. try:
  90. for row in steps:
  91. Step.insert(**{"name": row.get("name"), "type": mashstep_type, "config": {"kettle": mash_kettle, "temp": float(row.get("temp")), "timer": row.get("timer")}})
  92. Step.insert(**{"name": "ChilStep", "type": "ChilStep", "config": {"timer": 15}})
  93. ## Add cooking step
  94. Step.insert(**{"name": "Boil", "type": boilstep_type, "config": {"kettle": boil_kettle, "temp": boil_temp, "timer": boil_time}})
  95. ## Add Whirlpool step
  96. Step.insert(**{"name": "Whirlpool", "type": "ChilStep", "config": {"timer": 15}})
  97. self.api.emit("UPDATE_ALL_STEPS", Step.get_all())
  98. self.api.notify(headline="Recipe %s loaded successfully" % name, message="")
  99. except Exception as e:
  100. self.api.notify(headline="Failed to load Recipe", message=e.message, type="danger")
  101. return ('', 500)
  102. return ('', 204)
  103. def getRecipeName(self, id):
  104. e = xml.etree.ElementTree.parse(self.BEER_XML_FILE).getroot()
  105. return e.find('./RECIPE[%s]/NAME' % (str(id))).text
  106. def getBoilTime(self, id):
  107. e = xml.etree.ElementTree.parse(self.BEER_XML_FILE).getroot()
  108. return float(e.find('./RECIPE[%s]/BOIL_TIME' % (str(id))).text)
  109. def getSteps(self, id):
  110. e = xml.etree.ElementTree.parse(self.BEER_XML_FILE).getroot()
  111. steps = []
  112. for e in e.findall('./RECIPE[%s]/MASH/MASH_STEPS/MASH_STEP' % (str(id))):
  113. if self.api.get_config_parameter("unit", "C") == "C":
  114. temp = float(e.find("STEP_TEMP").text)
  115. else:
  116. temp = round(9.0 / 5.0 * float(e.find("STEP_TEMP").text) + 32, 2)
  117. steps.append({"name": e.find("NAME").text, "temp": temp, "timer": float(e.find("STEP_TIME").text)})
  118. return steps
  119. @cbpi.addon.core.initializer()
  120. def init(cbpi):
  121. BeerXMLImport.api = cbpi
  122. BeerXMLImport.register(cbpi._app, route_base='/api/beerxml')