Added basic database models

This commit is contained in:
Maximilian Giller 2024-06-19 02:38:17 +02:00
parent 7bc79139f0
commit 1b49d4e023
7 changed files with 50 additions and 1 deletions

View file

@ -14,3 +14,6 @@ requests
# Config file # Config file
pyyaml pyyaml
# Database
peewee

3
src/storage/database.py Normal file
View file

@ -0,0 +1,3 @@
from peewee import SqliteDatabase
database = SqliteDatabase("mash_database.sqlite")

7
src/storage/helper.py Normal file
View file

@ -0,0 +1,7 @@
from .database import database
from .models import *
def create_tables():
with database:
database.create_tables([Device, Feature, StateLog])

View file

@ -0,0 +1,3 @@
from base_model import BaseModel
from device import Device, Feature
from logs import StateLog

View file

@ -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

View file

@ -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()

View 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")