25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

119 satır
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. mashin_temp = self.getMashinTemp(id)
  48. mashinstep_type = cbpi.get_config_parameter("step_mashin", "MashInStep")
  49. mashstep_type = cbpi.get_config_parameter("step_mash", "MashStep")
  50. mash_kettle = cbpi.get_config_parameter("step_mash_kettle", None)
  51. boilstep_type = cbpi.get_config_parameter("step_boil", "BoilStep")
  52. boil_kettle = cbpi.get_config_parameter("step_boil_kettle", None)
  53. boil_temp = 100 if cbpi.get_config_parameter("unit", "C") == "C" else 212
  54. # READ KBH DATABASE
  55. Step.delete_all()
  56. StepView().reset()
  57. try:
  58. ## Add Mashin step
  59. Step.insert(**{"name": "MashIn", "type": mashinstep_type, "config": {"kettle": mash_kettle, "temp": mashin_temp}})
  60. for row in steps:
  61. Step.insert(**{"name": row.get("name"), "type": mashstep_type, "config": {"kettle": mash_kettle, "temp": float(row.get("temp")), "timer": row.get("timer")}})
  62. Step.insert(**{"name": "ChilStep", "type": "ChilStep", "config": {"timer": 15}})
  63. ## Add cooking step
  64. Step.insert(**{"name": "Boil", "type": boilstep_type, "config": {"kettle": boil_kettle, "temp": boil_temp, "timer": boil_time}})
  65. ## Add Whirlpool step
  66. Step.insert(**{"name": "Whirlpool", "type": "ChilStep", "config": {"timer": 15}})
  67. self.api.emit("UPDATE_ALL_STEPS", Step.get_all())
  68. self.api.notify(headline="Recipe %s loaded successfully" % name, message="")
  69. except Exception as e:
  70. self.api.notify(headline="Failed to load Recipe", message=e.message, type="danger")
  71. return ('', 500)
  72. return ('', 204)
  73. def getRecipeName(self, id):
  74. e = xml.etree.ElementTree.parse(self.BEER_XML_FILE).getroot()
  75. return e.find('./RECIPE[%s]/NAME' % (str(id))).text
  76. def getBoilTime(self, id):
  77. e = xml.etree.ElementTree.parse(self.BEER_XML_FILE).getroot()
  78. return float(e.find('./RECIPE[%s]/BOIL_TIME' % (str(id))).text)
  79. def getMashinTemp(self, id):
  80. e = xml.etree.ElementTree.parse(self.BEER_XML_FILE).getroot()
  81. return float('./RECIPE[%s]/MASH/MASH_STEPS/INFUSE_TEMP' % (str(id))).text)
  82. def getSteps(self, id):
  83. e = xml.etree.ElementTree.parse(self.BEER_XML_FILE).getroot()
  84. steps = []
  85. for e in e.findall('./RECIPE[%s]/MASH/MASH_STEPS/MASH_STEP' % (str(id))):
  86. if self.api.get_config_parameter("unit", "C") == "C":
  87. temp = float(e.find("STEP_TEMP").text)
  88. else:
  89. temp = round(9.0 / 5.0 * float(e.find("STEP_TEMP").text) + 32, 2)
  90. steps.append({"name": e.find("NAME").text, "temp": temp, "timer": float(e.find("STEP_TIME").text)})
  91. return steps
  92. @cbpi.initalizer()
  93. def init(cbpi):
  94. BeerXMLImport.api = cbpi
  95. BeerXMLImport.register(cbpi.app, route_base='/api/beerxml')