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.

124 satır
4.9KB

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