Added basic database models
This commit is contained in:
parent
7bc79139f0
commit
1b49d4e023
7 changed files with 50 additions and 1 deletions
|
@ -14,3 +14,6 @@ requests
|
||||||
|
|
||||||
# Config file
|
# Config file
|
||||||
pyyaml
|
pyyaml
|
||||||
|
|
||||||
|
# Database
|
||||||
|
peewee
|
||||||
|
|
3
src/storage/database.py
Normal file
3
src/storage/database.py
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
from peewee import SqliteDatabase
|
||||||
|
|
||||||
|
database = SqliteDatabase("mash_database.sqlite")
|
7
src/storage/helper.py
Normal file
7
src/storage/helper.py
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
from .database import database
|
||||||
|
from .models import *
|
||||||
|
|
||||||
|
|
||||||
|
def create_tables():
|
||||||
|
with database:
|
||||||
|
database.create_tables([Device, Feature, StateLog])
|
3
src/storage/models/__init__.py
Normal file
3
src/storage/models/__init__.py
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
from base_model import BaseModel
|
||||||
|
from device import Device, Feature
|
||||||
|
from logs import StateLog
|
|
@ -1,2 +1,11 @@
|
||||||
class BaseModel():
|
from ..database import database
|
||||||
|
from peewee import AutoField
|
||||||
|
|
||||||
|
class BaseModel():
|
||||||
|
id = AutoField(
|
||||||
|
primary_key=True,
|
||||||
|
unique=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
database = database
|
||||||
|
|
|
@ -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()
|
11
src/storage/models/logs.py
Normal file
11
src/storage/models/logs.py
Normal file
|
@ -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")
|
Loading…
Reference in a new issue