Clean up imports and bad code
parent
9615a07e49
commit
e188141a2a
|
@ -8,12 +8,15 @@ app.config.from_pyfile('config.py')
|
|||
db = SQLAlchemy(app)
|
||||
|
||||
|
||||
# Blueprints setup
|
||||
from app.mod_devices import devices
|
||||
from app.mod_accounts import accounts
|
||||
def setup_blueprints(app):
|
||||
from .mod_devices import devices
|
||||
from .mod_accounts import accounts
|
||||
|
||||
app.register_blueprint(devices, url_prefix='/devices')
|
||||
app.register_blueprint(accounts, url_prefix='/accounts')
|
||||
app.register_blueprint(devices, url_prefix='/devices')
|
||||
app.register_blueprint(accounts, url_prefix='/accounts')
|
||||
|
||||
|
||||
setup_blueprints(app)
|
||||
|
||||
|
||||
@app.route("/")
|
||||
|
|
|
@ -1,12 +1,7 @@
|
|||
from flask import Blueprint
|
||||
from .. import db
|
||||
|
||||
accounts = Blueprint('accounts', __name__)
|
||||
|
||||
# Models
|
||||
from .models import Account
|
||||
from .models import Role
|
||||
|
||||
|
||||
# Routes
|
||||
@accounts.route("/")
|
||||
|
|
|
@ -1,14 +1,15 @@
|
|||
from . import db
|
||||
from sqlalchemy import (Model, Column, String,
|
||||
Integer, ForeignKey, relationship)
|
||||
|
||||
|
||||
class Account(db.Model):
|
||||
class Account(Model):
|
||||
__tablename__ = 'accounts'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
username = db.Column(db.String)
|
||||
password = db.Column(db.String)
|
||||
role_id = db.Column(db.Integer, db.ForeignKey("roles.id"))
|
||||
role = db.relationship("Role", foreign_keys=[role_id])
|
||||
id = Column(Integer, primary_key=True)
|
||||
username = Column(String)
|
||||
password = Column(String)
|
||||
role_id = Column(Integer, ForeignKey("roles.id"))
|
||||
role = relationship("Role", foreign_keys=[role_id])
|
||||
|
||||
def __init__(self, username, password, role):
|
||||
self.username = str(username)
|
||||
|
@ -22,11 +23,11 @@ class Account(db.Model):
|
|||
return '<Account (name=%s, role=%s)>' % self.username, self.role
|
||||
|
||||
|
||||
class Role(db.Model):
|
||||
class Role(Model):
|
||||
__tablename__ = 'roles'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
display_name = db.Column(db.String)
|
||||
id = Column(Integer, primary_key=True)
|
||||
display_name = Column(String)
|
||||
|
||||
def __init__(self, name):
|
||||
self.display_name = str(name)
|
||||
|
|
|
@ -1,15 +1,9 @@
|
|||
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
|
||||
|
||||
# Mqtt
|
||||
from .mqtt_client import tear_down_mqtt, setup_mqtt
|
||||
|
||||
|
||||
# When app dies, stop mqtt connection
|
||||
def on_stop():
|
||||
|
|
|
@ -1,20 +1,20 @@
|
|||
from datetime import datetime
|
||||
from . import db
|
||||
from sqlalchemy import Model, DateTime, String, Column, Integer
|
||||
from sqlalchemy.dialects.postgresql import JSON
|
||||
|
||||
|
||||
class Recording(db.Model):
|
||||
class Recording(Model):
|
||||
__tablename__ = 'recordings'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
recorded_at = db.Column(db.DateTime, index=True,
|
||||
id = Column(Integer, primary_key=True)
|
||||
recorded_at = Column(DateTime, index=True,
|
||||
default=datetime.utcnow())
|
||||
received_at = db.Column(db.DateTime, index=True,
|
||||
received_at = Column(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)
|
||||
device_id = Column(Integer)
|
||||
record_type = Column(Integer, nullable=False)
|
||||
record_value = Column(String, nullable=False)
|
||||
raw_record = Column(JSON, nullable=True)
|
||||
|
||||
def __init__(self, device_id, record_type,
|
||||
record_value, recorded_at, raw_json):
|
||||
|
|
|
@ -2,7 +2,6 @@ import sys
|
|||
import json
|
||||
from flask_mqtt import Mqtt
|
||||
from .models import Recording
|
||||
from . import db
|
||||
|
||||
mqtt = Mqtt()
|
||||
|
||||
|
@ -38,6 +37,7 @@ def handle_subscribe(client, userdata, mid, granted_qos):
|
|||
|
||||
|
||||
def handle_mqtt_message(client, userdata, message):
|
||||
from .. import db
|
||||
print("Received message!")
|
||||
print("Topic: " + message.topic)
|
||||
print("Payload: " + message.payload.decode())
|
||||
|
|
Loading…
Reference in New Issue