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:
+15
-15
@@ -9,7 +9,7 @@ class BaseView(FlaskView):
|
||||
cache_key = None
|
||||
api = cbpi
|
||||
|
||||
@route('/<int:id>')
|
||||
@route('/<int:id>', methods=["GET"])
|
||||
def getOne(self, id):
|
||||
|
||||
if self.api.cache.get(self.cache_key) is not None:
|
||||
@@ -17,36 +17,36 @@ class BaseView(FlaskView):
|
||||
else:
|
||||
return json.dumps(self.model.get_one(id))
|
||||
|
||||
@route('/')
|
||||
@route('/', methods=["GET"])
|
||||
def getAll(self):
|
||||
if self.api.cache.get(self.cache_key) is not None:
|
||||
return json.dumps(self.api.cache.get(self.cache_key))
|
||||
else:
|
||||
return json.dumps(self.model.get_all())
|
||||
|
||||
def pre_post_callback(self, data):
|
||||
def _pre_post_callback(self, data):
|
||||
pass
|
||||
|
||||
|
||||
def post_post_callback(self, m):
|
||||
def _post_post_callback(self, m):
|
||||
pass
|
||||
|
||||
@route('/', methods=["POST"])
|
||||
def post(self):
|
||||
data = request.json
|
||||
self.pre_post_callback(data)
|
||||
self._pre_post_callback(data)
|
||||
m = self.model.insert(**data)
|
||||
if self.api.cache.get(self.cache_key) is not None:
|
||||
self.api.cache.get(self.cache_key)[m.id] = m
|
||||
|
||||
self.post_post_callback(m)
|
||||
self._post_post_callback(m)
|
||||
|
||||
return json.dumps(m)
|
||||
|
||||
def pre_put_callback(self, m):
|
||||
def _pre_put_callback(self, m):
|
||||
pass
|
||||
|
||||
def post_put_callback(self, m):
|
||||
def _post_put_callback(self, m):
|
||||
pass
|
||||
|
||||
|
||||
@@ -59,32 +59,32 @@ class BaseView(FlaskView):
|
||||
except:
|
||||
pass
|
||||
if self.api.cache.get(self.cache_key) is not None:
|
||||
self.pre_put_callback(self.api.cache.get(self.cache_key)[id])
|
||||
self._pre_put_callback(self.api.cache.get(self.cache_key)[id])
|
||||
self.api.cache.get(self.cache_key)[id].__dict__.update(**data)
|
||||
m = self.model.update(**self.api.cache.get(self.cache_key)[id].__dict__)
|
||||
self.post_put_callback(self.api.cache.get(self.cache_key)[id])
|
||||
self._post_put_callback(self.api.cache.get(self.cache_key)[id])
|
||||
return json.dumps(self.api.cache.get(self.cache_key)[id])
|
||||
else:
|
||||
m = self.model.update(**data)
|
||||
|
||||
self.post_put_callback(m)
|
||||
self._post_put_callback(m)
|
||||
return json.dumps(m)
|
||||
|
||||
|
||||
def pre_delete_callback(self, m):
|
||||
def _pre_delete_callback(self, m):
|
||||
pass
|
||||
|
||||
def post_delete_callback(self, id):
|
||||
def _post_delete_callback(self, id):
|
||||
pass
|
||||
|
||||
@route('/<int:id>', methods=["DELETE"])
|
||||
def delete(self, id):
|
||||
if self.api.cache.get(self.cache_key) is not None:
|
||||
self.pre_delete_callback(self.api.cache.get(self.cache_key)[id])
|
||||
self._pre_delete_callback(self.api.cache.get(self.cache_key)[id])
|
||||
del self.api.cache.get(self.cache_key)[id]
|
||||
m = self.model.delete(id)
|
||||
|
||||
def post_delete_callback(self, id):
|
||||
def _post_delete_callback(self, id):
|
||||
pass
|
||||
return ('',204)
|
||||
|
||||
|
||||
+14
-3
@@ -159,9 +159,8 @@ class SensorAPI(object):
|
||||
def log_action(self, text):
|
||||
filename = "./logs/action.log"
|
||||
formatted_time = strftime("%Y-%m-%d %H:%M:%S", localtime())
|
||||
|
||||
|
||||
with open(filename, "a") as file:
|
||||
text = text.encode("utf-8")
|
||||
file.write("%s,%s\n" % (formatted_time, text))
|
||||
|
||||
def shutdown_sensor(self, id):
|
||||
@@ -243,6 +242,15 @@ class CraftBeerPi(ActorAPI, SensorAPI):
|
||||
else:
|
||||
return cfg.value
|
||||
|
||||
def set_config_parameter(self, name, value):
|
||||
from modules.config import Config
|
||||
with self.app.app_context():
|
||||
update_data = {"name": name, "value": value}
|
||||
self.cache.get("config")[name].__dict__.update(**update_data)
|
||||
c = Config.update(**update_data)
|
||||
self.emit("UPDATE_CONFIG", c)
|
||||
|
||||
|
||||
def add_config_parameter(self, name, value, type, description, options=None):
|
||||
from modules.config import Config
|
||||
with self.app.app_context():
|
||||
@@ -281,10 +289,13 @@ class CraftBeerPi(ActorAPI, SensorAPI):
|
||||
def actor(self, cls):
|
||||
return self.__parseProps("actor_types", cls)
|
||||
|
||||
|
||||
|
||||
def actor2(self, description="", power=True, **options):
|
||||
|
||||
def decorator(f):
|
||||
print f()
|
||||
print f
|
||||
print options
|
||||
print description
|
||||
return f
|
||||
@@ -458,7 +469,7 @@ class CraftBeerPi(ActorAPI, SensorAPI):
|
||||
def job(interval, method):
|
||||
while True:
|
||||
try:
|
||||
method()
|
||||
method(self)
|
||||
except Exception as e:
|
||||
self.app.logger.error("Exception" + method.__name__ + ": " + str(e))
|
||||
self.socketio.sleep(interval)
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
import sqlite3
|
||||
import os
|
||||
from modules import cbpi
|
||||
from db import get_db
|
||||
|
||||
def execute_file(curernt_version, data):
|
||||
if curernt_version >= data["version"]:
|
||||
cbpi.app.logger.info("SKIP DB FILE: %s" % data["file"])
|
||||
return
|
||||
try:
|
||||
with sqlite3.connect("craftbeerpi.db") as conn:
|
||||
with open('./update/%s' % data["file"], 'r') as f:
|
||||
d = f.read()
|
||||
sqlCommands = d.split(";")
|
||||
cur = conn.cursor()
|
||||
for s in sqlCommands:
|
||||
cur.execute(s)
|
||||
cur.execute("INSERT INTO schema_info (version,filename) values (?,?)", (data["version"], data["file"]))
|
||||
conn.commit()
|
||||
|
||||
except sqlite3.OperationalError as err:
|
||||
print "EXCEPT"
|
||||
print err
|
||||
|
||||
@cbpi.initalizer(order=-9999)
|
||||
def init(app=None):
|
||||
|
||||
with cbpi.app.app_context():
|
||||
conn = get_db()
|
||||
cur = conn.cursor()
|
||||
current_version = None
|
||||
try:
|
||||
cur.execute("SELECT max(version) as m FROM schema_info")
|
||||
m = cur.fetchone()
|
||||
current_version = m["m"]
|
||||
except:
|
||||
pass
|
||||
result = []
|
||||
for filename in os.listdir("./update"):
|
||||
if filename.endswith(".sql"):
|
||||
d = {"version": int(filename[:filename.index('_')]), "file": filename}
|
||||
result.append(d)
|
||||
execute_file(current_version, d)
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ class Base(object):
|
||||
|
||||
@classmethod
|
||||
def init_global(cls):
|
||||
print "GLOBAL ACTOR INIT"
|
||||
pass
|
||||
|
||||
def get_config_parameter(self, key, default_value):
|
||||
return self.api.get_config_parameter(key, default_value)
|
||||
@@ -14,10 +14,10 @@ class Base(object):
|
||||
self.api.socketio.sleep(seconds)
|
||||
|
||||
def init(self):
|
||||
print "INIT BASE"
|
||||
pass
|
||||
|
||||
def stop(self):
|
||||
print "STOP HARDWARE"
|
||||
pass
|
||||
|
||||
def update(self, **kwds):
|
||||
pass
|
||||
@@ -99,10 +99,10 @@ class ActorBase(Base):
|
||||
return 1
|
||||
|
||||
def set_power(self, power):
|
||||
print "SET POWER TO %s" % power
|
||||
pass
|
||||
|
||||
def on(self, power=0):
|
||||
print "ON"
|
||||
pass
|
||||
|
||||
def off(self):
|
||||
print "OFF"
|
||||
pass
|
||||
|
||||
@@ -44,7 +44,7 @@ class KettleAPI(NotificationAPI):
|
||||
return self.api.cache.get("kettle").get(id).target_temp
|
||||
|
||||
def set_target_temp(self, temp, id=None):
|
||||
temp = int(temp)
|
||||
temp = float(temp)
|
||||
|
||||
try:
|
||||
if id is None:
|
||||
@@ -59,7 +59,7 @@ class Timer(object):
|
||||
timer_end = Property.Number("TIMER_END", configurable=False)
|
||||
|
||||
def start_timer(self, timer):
|
||||
print "START TIMER NEW"
|
||||
|
||||
if self.timer_end is not None:
|
||||
return
|
||||
self.timer_end = int(time.time()) + timer
|
||||
@@ -100,13 +100,13 @@ class StepBase(Timer, ActorAPI, SensorAPI, KettleAPI):
|
||||
self.n = True
|
||||
|
||||
def init(self):
|
||||
print "INIT STEP"
|
||||
pass
|
||||
|
||||
def finish(self):
|
||||
print "FINSIH STEP"
|
||||
pass
|
||||
|
||||
def reset(self):
|
||||
print "REST STEP"
|
||||
pass
|
||||
|
||||
def execute(self):
|
||||
print "-------------"
|
||||
|
||||
Reference in New Issue
Block a user