craftbeerpi4-pione/cbpi/database/model.py

165 lines
5.2 KiB
Python
Raw Normal View History

2018-12-16 21:42:47 +01:00
import json
2019-01-02 21:20:44 +01:00
2018-12-16 21:42:47 +01:00
import aiosqlite
2019-01-02 21:20:44 +01:00
2019-01-05 20:43:48 +01:00
from cbpi.database.orm_framework import DBModel
2019-01-02 21:20:44 +01:00
DATABASE_FILE = "./craftbeerpi.db"
2018-11-01 19:50:04 +01:00
2019-01-02 21:20:44 +01:00
2018-11-01 19:50:04 +01:00
class ActorModel(DBModel):
2018-11-01 21:25:42 +01:00
__fields__ = ["name", "type", "config"]
2018-11-01 19:50:04 +01:00
__table_name__ = "actor"
__json_fields__ = ["config"]
2019-01-02 21:20:44 +01:00
__validation_schema__ = {
'id': int,
'name': str,
'type': str,
'config': dict
}
2018-11-01 19:50:04 +01:00
class SensorModel(DBModel):
2018-11-01 21:25:42 +01:00
__fields__ = ["name", "type", "config"]
2018-11-01 19:50:04 +01:00
__table_name__ = "sensor"
2018-11-01 21:25:42 +01:00
__json_fields__ = ["config"]
2019-01-02 21:20:44 +01:00
__validation_schema__ = {
'id': int,
'name': str,
'type': str,
'config': dict
}
2018-11-18 15:40:10 +01:00
class ConfigModel(DBModel):
__fields__ = ["type", "value", "description", "options"]
__table_name__ = "config"
__json_fields__ = ["options"]
__priamry_key__ = "name"
2019-01-02 21:20:44 +01:00
2018-11-18 15:40:10 +01:00
class KettleModel(DBModel):
2019-01-02 21:20:44 +01:00
__fields__ = ["name", "sensor", "heater", "automatic", "logic", "config", "agitator", "target_temp"]
2018-11-18 15:40:10 +01:00
__table_name__ = "kettle"
2018-12-08 14:21:00 +01:00
__json_fields__ = ["config"]
class StepModel(DBModel):
2019-01-02 21:20:44 +01:00
__fields__ = ["order", "name", "type", "stepstate", "state", "start", "end", "config", "kettleid"]
2018-12-08 14:21:00 +01:00
__table_name__ = "step"
__json_fields__ = ["config", "stepstate"]
2018-12-16 21:42:47 +01:00
@classmethod
async def update_step_state(cls, step_id, state):
async with aiosqlite.connect(DATABASE_FILE) as db:
2018-12-16 21:42:47 +01:00
cursor = await db.execute("UPDATE %s SET stepstate = ? WHERE id = ?" % cls.__table_name__, (json.dumps(state), step_id))
await db.commit()
2019-01-04 09:29:09 +01:00
@classmethod
async def update_state(cls, step_id, state, end=None):
async with aiosqlite.connect(DATABASE_FILE) as db:
if end is not None:
await db.execute("UPDATE %s SET state = ?, end = ? WHERE id = ?" % cls.__table_name__, (state, end, step_id))
else:
await db.execute("UPDATE %s SET state = ? WHERE id = ?" % cls.__table_name__, (state, step_id))
await db.commit()
2018-12-16 21:42:47 +01:00
@classmethod
async def get_by_state(cls, state, order=True):
async with aiosqlite.connect(DATABASE_FILE) as db:
2018-12-16 21:42:47 +01:00
db.row_factory = aiosqlite.Row
db.row_factory = DBModel.dict_factory
2019-01-02 21:20:44 +01:00
async with db.execute("SELECT * FROM %s WHERE state = ? ORDER BY %s.'order'" % (cls.__table_name__, cls.__table_name__,), state) as cursor:
2018-12-16 21:42:47 +01:00
row = await cursor.fetchone()
if row is not None:
return cls(row)
else:
return None
@classmethod
async def reset_all_steps(cls):
2019-01-04 09:29:09 +01:00
print("RESET ALL STEPS NOW")
async with aiosqlite.connect(DATABASE_FILE) as db:
2019-01-02 21:20:44 +01:00
await db.execute("UPDATE %s SET state = 'I', stepstate = NULL , start = NULL, end = NULL " % cls.__table_name__)
await db.commit()
class DashboardModel(DBModel):
__fields__ = ["name"]
__table_name__ = "dashboard"
__json_fields__ = []
class DashboardContentModel(DBModel):
__fields__ = ["dbid", "type", "element_id", "x", "y","config"]
__table_name__ = "dashboard_content"
__json_fields__ = ["config"]
__validation_schema__ = {
'dbid': int,
'element_id': int,
'type': str,
'x': int,
'y': int,
'config': dict
}
@classmethod
async def get_by_dashboard_id(cls, id, as_array=False):
result = []
async with aiosqlite.connect(DATABASE_FILE) as db:
db.row_factory = DBModel.dict_factory
async with db.execute("SELECT * FROM %s WHERE dbid = ?" % (cls.__table_name__), (id,)) as cursor:
async for row in cursor:
result.append(cls(row))
await cursor.close()
return result
@classmethod
async def update_coordinates(cls, id, x, y):
async with aiosqlite.connect(DATABASE_FILE) as db:
await db.execute("UPDATE %s SET x = ?, y = ? WHERE id = ?" % (cls.__table_name__), (x, y, id,))
2018-12-16 21:42:47 +01:00
await db.commit()
2019-01-02 21:20:44 +01:00
@classmethod
async def delete_by_dashboard_id(cls, id):
async with aiosqlite.connect(DATABASE_FILE) as db:
await db.execute("DELETE FROM %s WHERE dbid = ?" % (cls.__table_name__), (id,))
await db.commit()
2019-01-07 22:05:52 +01:00
class TranslationModel(DBModel):
__fields__ = ["key", "text", "language_code"]
__table_name__ = "translation"
__json_fields__ = []
__priamry_key__ = "key"
@classmethod
async def get_all(cls):
result = {}
async with aiosqlite.connect(DATABASE_FILE) as db:
sql = "SELECT * FROM %s" % cls.__table_name__
db.row_factory = DBModel.dict_factory
async with db.execute(sql) as cursor:
async for row in cursor:
code = row.get("language_code")
key = row.get("key")
text = row.get("text")
if code not in result:
result[code] = {}
result[code][key] = text
await cursor.close()
return result
@classmethod
async def add_key(cls, locale, key):
async with aiosqlite.connect(DATABASE_FILE) as db:
await db.execute("INSERT INTO %s (language_code, key, text) VALUES (?,?, ' ')" % (cls.__table_name__), (locale, key))
await db.commit()