diff --git a/requirements.txt b/requirements.txt index 51561a4..0aa2468 100644 --- a/requirements.txt +++ b/requirements.txt @@ -14,3 +14,6 @@ requests # Config file pyyaml + +# Database +peewee diff --git a/src/storage/database.py b/src/storage/database.py new file mode 100644 index 0000000..6d40cc1 --- /dev/null +++ b/src/storage/database.py @@ -0,0 +1,3 @@ +from peewee import SqliteDatabase + +database = SqliteDatabase("mash_database.sqlite") diff --git a/src/storage/helper.py b/src/storage/helper.py new file mode 100644 index 0000000..1aa3d60 --- /dev/null +++ b/src/storage/helper.py @@ -0,0 +1,7 @@ +from .database import database +from .models import * + + +def create_tables(): + with database: + database.create_tables([Device, Feature, StateLog]) diff --git a/src/storage/models/__init__.py b/src/storage/models/__init__.py new file mode 100644 index 0000000..9b87031 --- /dev/null +++ b/src/storage/models/__init__.py @@ -0,0 +1,3 @@ +from base_model import BaseModel +from device import Device, Feature +from logs import StateLog diff --git a/src/storage/models/base_model.py b/src/storage/models/base_model.py index f29489f..6707220 100644 --- a/src/storage/models/base_model.py +++ b/src/storage/models/base_model.py @@ -1,2 +1,11 @@ +from ..database import database +from peewee import AutoField + class BaseModel(): - \ No newline at end of file + id = AutoField( + primary_key=True, + unique=True, + ) + + class Meta: + database = database diff --git a/src/storage/models/device.py b/src/storage/models/device.py index e69de29..2de30fa 100644 --- a/src/storage/models/device.py +++ b/src/storage/models/device.py @@ -0,0 +1,13 @@ +from .base_model import BaseModel +from peewee import CharField, DateTimeField, ForeignKeyField, BooleanField + + +class Device(BaseModel): + friendly_name = CharField(max_length=200) + + +class Feature(BaseModel): + friendly_name = CharField(max_length=200) + device = ForeignKeyField(Device, backref="sensors") + type = CharField(max_length=150) + is_sensor = BooleanField() diff --git a/src/storage/models/logs.py b/src/storage/models/logs.py new file mode 100644 index 0000000..b60f701 --- /dev/null +++ b/src/storage/models/logs.py @@ -0,0 +1,11 @@ +from .base_model import BaseModel +from .device import Feature +from peewee import DateTimeField, ForeignKeyField, DecimalField, BooleanField + + +class StateLog(BaseModel): + timestamp = DateTimeField() + feature = ForeignKeyField(Feature, backref="values") + char_value = DecimalField(5, 2, auto_round=True) + numeric_value = DecimalField(12, 3, auto_round=True) + previous_state = ForeignKeyField("StateLog")