Update Recording model to use proper timestamps
parent
eeba793162
commit
dcddb07e11
|
@ -1,12 +1,14 @@
|
||||||
import atexit
|
import atexit
|
||||||
from flask import Blueprint
|
from flask import Blueprint
|
||||||
from .. import db
|
from .. import db
|
||||||
from .mqtt_client import tear_down_mqtt, setup_mqtt
|
|
||||||
|
|
||||||
devices = Blueprint('devices', __name__)
|
devices = Blueprint('devices', __name__)
|
||||||
|
|
||||||
# Models
|
|
||||||
from .models import Recording
|
from .models import Recording
|
||||||
|
# Models
|
||||||
|
|
||||||
|
# Mqtt
|
||||||
|
from .mqtt_client import tear_down_mqtt, setup_mqtt
|
||||||
|
|
||||||
# When app dies, stop mqtt connection
|
# When app dies, stop mqtt connection
|
||||||
def on_stop():
|
def on_stop():
|
||||||
|
|
|
@ -2,21 +2,28 @@ from datetime import datetime
|
||||||
from . import db
|
from . import db
|
||||||
from sqlalchemy.dialects.postgresql import JSON
|
from sqlalchemy.dialects.postgresql import JSON
|
||||||
|
|
||||||
|
|
||||||
class Recording(db.Model):
|
class Recording(db.Model):
|
||||||
__tablename__ = 'recordings'
|
__tablename__ = 'recordings'
|
||||||
|
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
recorded_at = db.Column(db.DateTime, primary_key=True, index=True,
|
recorded_at = db.Column(db.DateTime, index=True,
|
||||||
default=datetime.utcnow())
|
default=datetime.utcnow())
|
||||||
device_id = db.Column(db.Integer, primary_key=True)
|
received_at = db.Column(db.DateTime, index=True,
|
||||||
|
default=datetime.utcnow())
|
||||||
|
device_id = db.Column(db.Integer)
|
||||||
record_type = db.Column(db.Integer, nullable=False)
|
record_type = db.Column(db.Integer, nullable=False)
|
||||||
record_value = db.Column(db.String, nullable=False)
|
record_value = db.Column(db.String, nullable=False)
|
||||||
raw_record = db.Column(JSON, nullable=True)
|
raw_record = db.Column(JSON, nullable=True)
|
||||||
|
|
||||||
def __init__(self, device_id, record_type, record_value):
|
def __init__(self, device_id, record_type,
|
||||||
|
record_value, recorded_at, raw_json):
|
||||||
self.device_id = device_id
|
self.device_id = device_id
|
||||||
self.record_type = record_type
|
self.record_type = record_type
|
||||||
self.record_value = record_value
|
self.record_value = record_value
|
||||||
|
self.recorded_at = recorded_at
|
||||||
|
self.received_at = datetime.utcnow()
|
||||||
|
self.raw_record = raw_json
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<Recording %r>' % self.recorded_at
|
return '<Recording %r>' % self.recorded_at
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
|
from datetime import datetime
|
||||||
from flask_mqtt import Mqtt
|
from flask_mqtt import Mqtt
|
||||||
|
from .models import Recording
|
||||||
|
from . import db
|
||||||
|
|
||||||
mqtt = Mqtt()
|
mqtt = Mqtt()
|
||||||
|
|
||||||
|
@ -34,10 +37,7 @@ def handle_subscribe(client, userdata, mid, granted_qos):
|
||||||
|
|
||||||
|
|
||||||
def handle_mqtt_message(client, userdata, message):
|
def handle_mqtt_message(client, userdata, message):
|
||||||
data = dict(
|
recording = Recording(1, 1, "315", datetime.utcnow(), "{}")
|
||||||
topic=message.topic,
|
db.session.add(recording)
|
||||||
payload=message.payload.decode()
|
db.session.commit()
|
||||||
)
|
|
||||||
print(message.payload.decode())
|
print(message.payload.decode())
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
"""empty message
|
"""empty message
|
||||||
|
|
||||||
Revision ID: 33133bc3ce1d
|
Revision ID: 56ec2b819bd8
|
||||||
Revises:
|
Revises:
|
||||||
Create Date: 2018-04-27 10:45:32.919141
|
Create Date: 2018-04-27 16:05:14.796856
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from alembic import op
|
from alembic import op
|
||||||
|
@ -10,7 +10,7 @@ import sqlalchemy as sa
|
||||||
from sqlalchemy.dialects import postgresql
|
from sqlalchemy.dialects import postgresql
|
||||||
|
|
||||||
# revision identifiers, used by Alembic.
|
# revision identifiers, used by Alembic.
|
||||||
revision = '33133bc3ce1d'
|
revision = '56ec2b819bd8'
|
||||||
down_revision = None
|
down_revision = None
|
||||||
branch_labels = None
|
branch_labels = None
|
||||||
depends_on = None
|
depends_on = None
|
||||||
|
@ -20,13 +20,15 @@ def upgrade():
|
||||||
# ### commands auto generated by Alembic - please adjust! ###
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
op.create_table('recordings',
|
op.create_table('recordings',
|
||||||
sa.Column('id', sa.Integer(), nullable=False),
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
sa.Column('recorded_at', sa.DateTime(), nullable=False),
|
sa.Column('recorded_at', sa.DateTime(), nullable=True),
|
||||||
sa.Column('device_id', sa.Integer(), nullable=False),
|
sa.Column('received_at', sa.DateTime(), nullable=True),
|
||||||
|
sa.Column('device_id', sa.Integer(), nullable=True),
|
||||||
sa.Column('record_type', sa.Integer(), nullable=False),
|
sa.Column('record_type', sa.Integer(), nullable=False),
|
||||||
sa.Column('record_value', sa.String(), nullable=False),
|
sa.Column('record_value', sa.String(), nullable=False),
|
||||||
sa.Column('raw_record', postgresql.JSON(astext_type=sa.Text()), nullable=True),
|
sa.Column('raw_record', postgresql.JSON(astext_type=sa.Text()), nullable=True),
|
||||||
sa.PrimaryKeyConstraint('id', 'recorded_at', 'device_id')
|
sa.PrimaryKeyConstraint('id')
|
||||||
)
|
)
|
||||||
|
op.create_index(op.f('ix_recordings_received_at'), 'recordings', ['received_at'], unique=False)
|
||||||
op.create_index(op.f('ix_recordings_recorded_at'), 'recordings', ['recorded_at'], unique=False)
|
op.create_index(op.f('ix_recordings_recorded_at'), 'recordings', ['recorded_at'], unique=False)
|
||||||
# ### end Alembic commands ###
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
@ -34,5 +36,6 @@ def upgrade():
|
||||||
def downgrade():
|
def downgrade():
|
||||||
# ### commands auto generated by Alembic - please adjust! ###
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
op.drop_index(op.f('ix_recordings_recorded_at'), table_name='recordings')
|
op.drop_index(op.f('ix_recordings_recorded_at'), table_name='recordings')
|
||||||
|
op.drop_index(op.f('ix_recordings_received_at'), table_name='recordings')
|
||||||
op.drop_table('recordings')
|
op.drop_table('recordings')
|
||||||
# ### end Alembic commands ###
|
# ### end Alembic commands ###
|
Loading…
Reference in New Issue