mirror of
https://github.com/Manuel83/craftbeerpi3
synced 2026-08-09 18:52:40 +02:00
- Import KBH
- Import BeerXML - Import REST API - Migration DB - Set Default Screen after Start - Cleanup
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
import beerxml
|
||||
import kbh
|
||||
import restapi
|
||||
@@ -0,0 +1,110 @@
|
||||
from flask import json, request
|
||||
from flask_classy import FlaskView, route
|
||||
from git import Repo, Git
|
||||
import sqlite3
|
||||
from modules.app_config import cbpi
|
||||
from werkzeug.utils import secure_filename
|
||||
import pprint
|
||||
import time
|
||||
import os
|
||||
from modules.steps import Step,StepView
|
||||
import xml.etree.ElementTree
|
||||
|
||||
|
||||
class BeerXMLImport(FlaskView):
|
||||
|
||||
BEER_XML_FILE = "./upload/beer.xml"
|
||||
|
||||
@route('/', methods=['GET'])
|
||||
def get(self):
|
||||
if not os.path.exists(self.BEER_XML_FILE):
|
||||
self.api.notify(headline="File Not Found", message="Please upload a Beer.xml File",
|
||||
type="danger")
|
||||
return ('', 404)
|
||||
|
||||
result = []
|
||||
for idx, r in enumerate(self.getDict().get("RECIPES").get("RECIPE")):
|
||||
result.append({"id": idx, "name": r.get("NAME")})
|
||||
return json.dumps(result)
|
||||
|
||||
|
||||
def allowed_file(self, filename):
|
||||
return '.' in filename and filename.rsplit('.', 1)[1] in set(['xml'])
|
||||
|
||||
@route('/upload', methods=['POST'])
|
||||
def upload_file(self):
|
||||
try:
|
||||
if request.method == 'POST':
|
||||
file = request.files['file']
|
||||
if file and self.allowed_file(file.filename):
|
||||
file.save(os.path.join(self.api.app.config['UPLOAD_FOLDER'], "beer.xml"))
|
||||
self.api.notify(headline="Upload Successful", message="The Beer XML file was uploaded succesfully")
|
||||
return ('', 204)
|
||||
return ('', 404)
|
||||
except Exception as e:
|
||||
self.api.notify(headline="Upload Failed", message="Failed to upload Beer xml", type="danger")
|
||||
return ('', 500)
|
||||
|
||||
@route('/<int:id>', methods=['POST'])
|
||||
def load(self, id):
|
||||
|
||||
recipe = self.getDict().get("RECIPES").get("RECIPE")[id]
|
||||
steps = recipe.get("MASH",{}).get("MASH_STEPS",{}).get("MASH_STEP",[])
|
||||
name = recipe.get("NAME")
|
||||
|
||||
self.api.set_config_parameter("brew_name", name)
|
||||
boil_time = recipe.get("BOIL_TIME", 90)
|
||||
mashstep_type = cbpi.get_config_parameter("step_mash", "MashStep")
|
||||
mash_kettle = cbpi.get_config_parameter("step_mash_kettle", None)
|
||||
|
||||
boilstep_type = cbpi.get_config_parameter("step_boil", "BoilStep")
|
||||
boil_kettle = cbpi.get_config_parameter("step_boil_kettle", None)
|
||||
boil_temp = 100 if cbpi.get_config_parameter("unit", "C") == "C" else 212
|
||||
|
||||
# READ KBH DATABASE
|
||||
Step.delete_all()
|
||||
StepView().reset()
|
||||
|
||||
conn = None
|
||||
try:
|
||||
conn = sqlite3.connect(self.api.app.config['UPLOAD_FOLDER'] + '/kbh.db')
|
||||
c = conn.cursor()
|
||||
for row in steps:
|
||||
Step.insert(**{"name": row.get("NAME"), "type": mashstep_type, "config": {"kettle": mash_kettle, "temp": float(row.get("STEP_TEMP")), "timer": row.get("STEP_TIME")}})
|
||||
Step.insert(**{"name": "ChilStep", "type": "ChilStep", "config": {"timer": 15}})
|
||||
## Add cooking step
|
||||
Step.insert(**{"name": "Boil", "type": boilstep_type, "config": {"kettle": boil_kettle, "temp": boil_temp, "timer": boil_time}})
|
||||
## Add Whirlpool step
|
||||
Step.insert(**{"name": "Whirlpool", "type": "ChilStep", "config": {"timer": 15}})
|
||||
# setBrewName(name)
|
||||
self.api.emit("UPDATE_ALL_STEPS", Step.get_all())
|
||||
self.api.notify(headline="Recipe %s loaded successfully" % name, message="")
|
||||
except Exception as e:
|
||||
self.api.notify(headline="Failed to load Recipe", message=e.message, type="danger")
|
||||
return ('', 500)
|
||||
finally:
|
||||
if conn:
|
||||
conn.close()
|
||||
return ('', 204)
|
||||
|
||||
|
||||
def getDict(self):
|
||||
'''
|
||||
Beer XML file to dict
|
||||
:return: beer.xml file as dict
|
||||
'''
|
||||
try:
|
||||
import xmltodict
|
||||
with open(self.BEER_XML_FILE) as fd:
|
||||
doc = xmltodict.parse(fd.read())
|
||||
return doc
|
||||
except:
|
||||
self.api.notify(headline="Failed to load Beer.xml", message="Please check if you uploaded an beer.xml", type="danger")
|
||||
|
||||
|
||||
|
||||
@cbpi.initalizer()
|
||||
def init(cbpi):
|
||||
|
||||
BeerXMLImport.api = cbpi
|
||||
BeerXMLImport.register(cbpi.app, route_base='/api/beerxml')
|
||||
@@ -0,0 +1,109 @@
|
||||
from flask import json, request
|
||||
from flask_classy import FlaskView, route
|
||||
from git import Repo, Git
|
||||
import sqlite3
|
||||
from modules.app_config import cbpi
|
||||
from werkzeug.utils import secure_filename
|
||||
import pprint
|
||||
import time
|
||||
import os
|
||||
from modules.steps import Step, StepView
|
||||
|
||||
|
||||
class KBH(FlaskView):
|
||||
|
||||
@route('/', methods=['GET'])
|
||||
def get(self):
|
||||
conn = None
|
||||
try:
|
||||
if not os.path.exists(self.api.app.config['UPLOAD_FOLDER'] + '/kbh.db'):
|
||||
self.api.notify(headline="File Not Found", message="Please upload a Kleiner Brauhelfer Database", type="danger")
|
||||
return ('', 404)
|
||||
|
||||
conn = sqlite3.connect(self.api.app.config['UPLOAD_FOLDER'] + '/kbh.db')
|
||||
c = conn.cursor()
|
||||
c.execute('SELECT ID, Sudname, BierWurdeGebraut FROM Sud')
|
||||
data = c.fetchall()
|
||||
result = []
|
||||
for row in data:
|
||||
result.append({"id": row[0], "name": row[1], "brewed": row[2]})
|
||||
return json.dumps(result)
|
||||
except Exception as e:
|
||||
print e
|
||||
self.api.notify(headline="Failed to load KHB database", message="ERROR", type="danger")
|
||||
return ('', 500)
|
||||
finally:
|
||||
if conn:
|
||||
conn.close()
|
||||
|
||||
def allowed_file(self, filename):
|
||||
return '.' in filename and filename.rsplit('.', 1)[1] in set(['sqlite'])
|
||||
|
||||
@route('/upload', methods=['POST'])
|
||||
def upload_file(self):
|
||||
try:
|
||||
if request.method == 'POST':
|
||||
file = request.files['file']
|
||||
if file and self.allowed_file(file.filename):
|
||||
filename = secure_filename(file.filename)
|
||||
file.save(os.path.join(self.api.app.config['UPLOAD_FOLDER'], "kbh.db"))
|
||||
self.api.notify(headline="Upload Successful", message="The Kleiner Brauhelfer Database was uploaded succesfully")
|
||||
return ('', 204)
|
||||
return ('', 404)
|
||||
except Exception as e:
|
||||
self.api.notify(headline="Upload Failed", message="Failed to upload Kleiner Brauhelfer", type="danger")
|
||||
|
||||
return ('', 500)
|
||||
|
||||
@route('/<int:id>', methods=['POST'])
|
||||
def load(self, id):
|
||||
mashstep_type = cbpi.get_config_parameter("step_mash", "MashStep")
|
||||
mashinstep_type = cbpi.get_config_parameter("step_mashin", "MashInStep")
|
||||
chilstep_type = cbpi.get_config_parameter("step_chil", "ChilStep")
|
||||
boilstep_type = cbpi.get_config_parameter("step_boil", "BoilStep")
|
||||
mash_kettle = cbpi.get_config_parameter("step_mash_kettle", None)
|
||||
boil_kettle = cbpi.get_config_parameter("step_boil_kettle", None)
|
||||
boil_temp = 100 if cbpi.get_config_parameter("unit", "C") == "C" else 212
|
||||
|
||||
# READ KBH DATABASE
|
||||
Step.delete_all()
|
||||
StepView().reset()
|
||||
conn = None
|
||||
try:
|
||||
conn = sqlite3.connect(self.api.app.config['UPLOAD_FOLDER'] + '/kbh.db')
|
||||
c = conn.cursor()
|
||||
c.execute('SELECT EinmaischenTemp, Sudname FROM Sud WHERE ID = ?', (id,))
|
||||
row = c.fetchone()
|
||||
name = row[1]
|
||||
|
||||
self.api.set_config_parameter("brew_name", name)
|
||||
Step.insert(**{"name": "MashIn", "type": mashinstep_type, "config": {"kettle": mash_kettle, "temp": row[0]}})
|
||||
### add rest step
|
||||
for row in c.execute('SELECT * FROM Rasten WHERE SudID = ?', (id,)):
|
||||
Step.insert(**{"name": row[5], "type": mashstep_type, "config": {"kettle": mash_kettle, "temp": row[3], "timer": row[4]}})
|
||||
Step.insert(**{"name": "Chil", "type": chilstep_type, "config": {"timer": 15}})
|
||||
## Add cooking step
|
||||
c.execute('SELECT max(Zeit) FROM Hopfengaben WHERE SudID = ?', (id,))
|
||||
row = c.fetchone()
|
||||
Step.insert(**{"name": "Boil", "type": boilstep_type, "config": {"kettle": boil_kettle, "temp": boil_temp, "timer": row[0]}})
|
||||
## Add Whirlpool step
|
||||
Step.insert(**{"name": "Whirlpool", "type": chilstep_type, "config": {"timer": 15}})
|
||||
|
||||
#setBrewName(name)
|
||||
self.api.emit("UPDATE_ALL_STEPS", Step.get_all())
|
||||
self.api.notify(headline="Recipe %s loaded successfully" % name, message="")
|
||||
except Exception as e:
|
||||
self.api.notify(headline="Failed to load Recipe", message=e.message, type="danger")
|
||||
return ('', 500)
|
||||
finally:
|
||||
if conn:
|
||||
conn.close()
|
||||
return ('', 204)
|
||||
|
||||
|
||||
|
||||
@cbpi.initalizer()
|
||||
def init(cbpi):
|
||||
|
||||
KBH.api = cbpi
|
||||
KBH.register(cbpi.app, route_base='/api/kbh')
|
||||
@@ -0,0 +1,62 @@
|
||||
from flask import json, request
|
||||
from flask_classy import FlaskView, route
|
||||
from git import Repo, Git
|
||||
import sqlite3
|
||||
from modules.app_config import cbpi
|
||||
from werkzeug.utils import secure_filename
|
||||
import pprint
|
||||
import time
|
||||
import os
|
||||
from modules.steps import Step,StepView
|
||||
import xml.etree.ElementTree
|
||||
|
||||
|
||||
class RESTImport(FlaskView):
|
||||
|
||||
|
||||
@route('/', methods=['POST'])
|
||||
def load(self):
|
||||
|
||||
try:
|
||||
data = request.json
|
||||
|
||||
name = data.get("name", "No Name")
|
||||
|
||||
self.api.set_config_parameter("brew_name", name)
|
||||
chilstep_type = cbpi.get_config_parameter("step_chil", "ChilStep")
|
||||
mashstep_type = cbpi.get_config_parameter("step_mash", "MashStep")
|
||||
mash_kettle = cbpi.get_config_parameter("step_mash_kettle", None)
|
||||
|
||||
boilstep_type = cbpi.get_config_parameter("step_boil", "BoilStep")
|
||||
boil_kettle = cbpi.get_config_parameter("step_boil_kettle", None)
|
||||
boil_temp = 100 if cbpi.get_config_parameter("unit", "C") == "C" else 212
|
||||
|
||||
# READ KBH DATABASE
|
||||
Step.delete_all()
|
||||
StepView().reset()
|
||||
|
||||
|
||||
for step in data.get("steps"):
|
||||
if step.get("type", None) == "MASH":
|
||||
Step.insert(**{"name": step.get("name","Mash Step"), "type": mashstep_type, "config": {"kettle": mash_kettle, "temp": step.get("temp",0), "timer": step.get("timer",0)}})
|
||||
elif step.get("type", None) == "CHIL":
|
||||
Step.insert(**{"name": step.get("name","Chil"), "type": chilstep_type, "config": {"timer": step.get("timer")}})
|
||||
elif step.get("type", None) == "BOIL":
|
||||
Step.insert(**{"name": step.get("name", "Boil"), "type": boilstep_type, "config": {"kettle": boil_kettle, "timer": step.get("timer"), "temp": boil_temp}})
|
||||
else:
|
||||
pass
|
||||
|
||||
self.api.emit("UPDATE_ALL_STEPS", Step.get_all())
|
||||
self.api.notify(headline="Recipe %s loaded successfully" % name, message="")
|
||||
except Exception as e:
|
||||
self.api.notify(headline="Failed to load Recipe", type="danger", message=str(e))
|
||||
m = str(e.message)
|
||||
return (str(e), 500)
|
||||
|
||||
return ('', 204)
|
||||
|
||||
|
||||
@cbpi.initalizer()
|
||||
def init(cbpi):
|
||||
RESTImport.api = cbpi
|
||||
RESTImport.register(cbpi.app, route_base='/api/recipe/import/v1')
|
||||
Reference in New Issue
Block a user