diff --git a/app/mod_devices/__init__.py b/app/mod_devices/__init__.py index 7e17330..7843e20 100644 --- a/app/mod_devices/__init__.py +++ b/app/mod_devices/__init__.py @@ -1,12 +1,14 @@ import atexit from flask import Blueprint from .. import db -from .mqtt_client import tear_down_mqtt, setup_mqtt devices = Blueprint('devices', __name__) -# Models from .models import Recording +# Models + +# Mqtt +from .mqtt_client import tear_down_mqtt, setup_mqtt # When app dies, stop mqtt connection def on_stop(): diff --git a/app/mod_devices/models.py b/app/mod_devices/models.py index 40964b8..85ab29b 100644 --- a/app/mod_devices/models.py +++ b/app/mod_devices/models.py @@ -2,21 +2,28 @@ from datetime import datetime from . import db from sqlalchemy.dialects.postgresql import JSON + class Recording(db.Model): __tablename__ = 'recordings' id = db.Column(db.Integer, primary_key=True) - recorded_at = db.Column(db.DateTime, primary_key=True, index=True, - default=datetime.utcnow()) - device_id = db.Column(db.Integer, primary_key=True) + recorded_at = db.Column(db.DateTime, index=True, + default=datetime.utcnow()) + 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_value = db.Column(db.String, nullable=False) 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.record_type = record_type self.record_value = record_value + self.recorded_at = recorded_at + self.received_at = datetime.utcnow() + self.raw_record = raw_json def __repr__(self): return '' % self.recorded_at diff --git a/app/mod_devices/mqtt_client.py b/app/mod_devices/mqtt_client.py index 886ffa2..8da6e50 100644 --- a/app/mod_devices/mqtt_client.py +++ b/app/mod_devices/mqtt_client.py @@ -1,4 +1,7 @@ +from datetime import datetime from flask_mqtt import Mqtt +from .models import Recording +from . import db mqtt = Mqtt() @@ -34,10 +37,7 @@ def handle_subscribe(client, userdata, mid, granted_qos): def handle_mqtt_message(client, userdata, message): - data = dict( - topic=message.topic, - payload=message.payload.decode() - ) + recording = Recording(1, 1, "315", datetime.utcnow(), "{}") + db.session.add(recording) + db.session.commit() print(message.payload.decode()) - - diff --git a/migrations/versions/33133bc3ce1d_.py b/migrations/versions/56ec2b819bd8_.py similarity index 66% rename from migrations/versions/33133bc3ce1d_.py rename to migrations/versions/56ec2b819bd8_.py index dcfbcfe..fed502f 100644 --- a/migrations/versions/33133bc3ce1d_.py +++ b/migrations/versions/56ec2b819bd8_.py @@ -1,8 +1,8 @@ """empty message -Revision ID: 33133bc3ce1d +Revision ID: 56ec2b819bd8 Revises: -Create Date: 2018-04-27 10:45:32.919141 +Create Date: 2018-04-27 16:05:14.796856 """ from alembic import op @@ -10,7 +10,7 @@ import sqlalchemy as sa from sqlalchemy.dialects import postgresql # revision identifiers, used by Alembic. -revision = '33133bc3ce1d' +revision = '56ec2b819bd8' down_revision = None branch_labels = None depends_on = None @@ -20,13 +20,15 @@ def upgrade(): # ### commands auto generated by Alembic - please adjust! ### op.create_table('recordings', sa.Column('id', sa.Integer(), nullable=False), - sa.Column('recorded_at', sa.DateTime(), nullable=False), - sa.Column('device_id', sa.Integer(), nullable=False), + sa.Column('recorded_at', sa.DateTime(), nullable=True), + 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_value', sa.String(), nullable=False), 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) # ### end Alembic commands ### @@ -34,5 +36,6 @@ def upgrade(): def downgrade(): # ### 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_received_at'), table_name='recordings') op.drop_table('recordings') # ### end Alembic commands ###