big changes
This commit is contained in:
@@ -1,16 +1,40 @@
|
||||
from peewee import *
|
||||
import datetime
|
||||
import logging
|
||||
logger = logging.getLogger(__name__)
|
||||
from threading import Thread
|
||||
|
||||
#db = SqliteDatabase('data.db')
|
||||
db = MySQLDatabase("AIO_sensors", host="192.168.1.101", port=3306, user="pi", passwd="supersecret")
|
||||
# whyyy?
|
||||
|
||||
class Metric(Model):
|
||||
time = DateTimeField()
|
||||
from . import keys
|
||||
dbk = keys.db_keys
|
||||
|
||||
|
||||
db = PostgresqlDatabase(dbk["name"], user=dbk["username"], password=dbk["password"], host=dbk["url"], port=dbk["port"], autorollback=True)
|
||||
|
||||
|
||||
|
||||
class DBModel(Model):
|
||||
# specific to the above DB
|
||||
class Meta:
|
||||
database = db
|
||||
|
||||
def save(self):
|
||||
# fail-safe writing of the db-object. Usually threaded because the caller is threaded
|
||||
try:
|
||||
# db.connect()
|
||||
super().save()
|
||||
# db.close()
|
||||
except Exception as e:
|
||||
logger.error("Could not write to db. Dropping content of {}".format(self.__class__.__name__))
|
||||
print(e)
|
||||
# db.atomic().rollback()
|
||||
|
||||
|
||||
class Metric(DBModel):
|
||||
time = DateTimeField()
|
||||
|
||||
|
||||
|
||||
|
||||
class SensorMetric(Metric):
|
||||
# this is a continuous metric
|
||||
temperature = IntegerField()
|
||||
@@ -19,11 +43,16 @@ class SensorMetric(Metric):
|
||||
|
||||
|
||||
class ChatMetric(Metric):
|
||||
# this gets cumulated over one hour (or one day, or...)
|
||||
activity = CharField()
|
||||
read = BooleanField()
|
||||
send = BooleanField()
|
||||
execute = BooleanField()
|
||||
|
||||
|
||||
class ErrorMetric(Metric):
|
||||
# same as above
|
||||
error = CharField()
|
||||
error = TextField()
|
||||
|
||||
|
||||
class List(DBModel):
|
||||
name = CharField(unique=True)
|
||||
content = TextField() # unlimited length, use to serialise list into
|
@@ -1,32 +1,58 @@
|
||||
from models import db
|
||||
from models import *
|
||||
import datetime as dt
|
||||
from random import randint
|
||||
from . import models
|
||||
|
||||
|
||||
def create_tables():
|
||||
with db:
|
||||
db.create_tables([SensorMetric, ChatMetric, ErrorMetric])
|
||||
class DBLogging:
|
||||
"""Create a connection to a remote database and log some quantities that will be visualized otherwhere"""
|
||||
def __init__(self):
|
||||
self.db = models.db
|
||||
|
||||
self.sensors = models.SensorMetric
|
||||
self.chats = models.ChatMetric
|
||||
self.errors = models.ErrorMetric
|
||||
self.lists = models.List
|
||||
|
||||
# self.create_tables()
|
||||
|
||||
# def create_tables(self):
|
||||
# with self.db as db:
|
||||
# db.create_tables([self.sensors, self.chats, self.errors, self.lists])
|
||||
|
||||
|
||||
create_tables()
|
||||
# read from json, excel, txt ... whatever
|
||||
now = dt.datetime.timestamp(dt.datetime.now())
|
||||
|
||||
|
||||
for i in range(1000):
|
||||
with db:
|
||||
sensor_data = SensorMetric.create(
|
||||
time = now + i,
|
||||
temperature = 23,
|
||||
humidity = 30 + randint(0,20),
|
||||
luminosity = 1
|
||||
)
|
||||
chat = ChatMetric(
|
||||
time = now + i,
|
||||
activity = "Hello world"
|
||||
)
|
||||
errors = ErrorMetric(
|
||||
time = now + i,
|
||||
error = "Could not load module"
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# writin to the db gets handled through the model directly
|
||||
|
||||
# create_tables()
|
||||
# # read from json, excel, txt ... whatever
|
||||
# now = dt.datetime.timestamp(dt.datetime.now())
|
||||
|
||||
|
||||
# for i in range(1000):
|
||||
# with db:
|
||||
# sensor_data = SensorMetric.create(
|
||||
# time = now + i,
|
||||
# temperature = 23,
|
||||
# humidity = 30 + randint(0,20),
|
||||
# luminosity = 1
|
||||
# )
|
||||
# chat = ChatMetric(
|
||||
# time = now + i,
|
||||
# activity = "Hello world"
|
||||
# )
|
||||
# errors = ErrorMetric(
|
||||
# time = now + i,
|
||||
# error = "Could not load module"
|
||||
# )
|
Reference in New Issue
Block a user