2018-09-22 16:14:45 +00:00
|
|
|
import time
|
2018-04-27 08:47:24 +00:00
|
|
|
from datetime import datetime
|
2018-09-22 16:14:45 +00:00
|
|
|
import datetime as datetime_module
|
2018-09-20 20:05:03 +00:00
|
|
|
from app.core import db
|
2018-04-27 08:47:24 +00:00
|
|
|
from sqlalchemy.dialects.postgresql import JSON
|
2018-10-29 21:47:03 +00:00
|
|
|
from secrets import token_urlsafe
|
2018-04-27 08:47:24 +00:00
|
|
|
|
2018-04-27 14:06:16 +00:00
|
|
|
|
2018-05-03 13:28:57 +00:00
|
|
|
class Recording(db.Model):
|
2018-04-27 08:47:24 +00:00
|
|
|
__tablename__ = 'recordings'
|
|
|
|
|
2018-10-24 20:11:55 +00:00
|
|
|
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
2018-05-03 14:58:44 +00:00
|
|
|
recorded_at = db.Column(db.DateTime, index=True,
|
|
|
|
default=db.func.current_timestamp())
|
|
|
|
received_at = db.Column(db.DateTime, index=True,
|
|
|
|
default=db.func.current_timestamp())
|
2018-05-08 08:52:49 +00:00
|
|
|
device_id = db.Column(db.Integer, db.ForeignKey('devices.id'))
|
2018-05-03 14:58:44 +00:00
|
|
|
record_type = db.Column(db.Integer, nullable=False)
|
2018-10-30 19:16:27 +00:00
|
|
|
record_value = db.Column(db.Float, nullable=False)
|
2018-05-03 14:58:44 +00:00
|
|
|
raw_record = db.Column(JSON, nullable=True)
|
2018-04-27 08:47:24 +00:00
|
|
|
|
2018-04-27 14:06:16 +00:00
|
|
|
def __init__(self, device_id, record_type,
|
|
|
|
record_value, recorded_at, raw_json):
|
2018-05-02 19:05:30 +00:00
|
|
|
self.device_id = int(device_id)
|
|
|
|
self.record_type = int(record_type)
|
2018-10-30 19:16:27 +00:00
|
|
|
self.record_value = float(record_value)
|
2018-05-02 19:05:30 +00:00
|
|
|
self.recorded_at = datetime.fromtimestamp(int(recorded_at))
|
2018-04-27 14:06:16 +00:00
|
|
|
self.received_at = datetime.utcnow()
|
|
|
|
self.raw_record = raw_json
|
2018-04-27 08:47:24 +00:00
|
|
|
|
2018-05-03 14:58:44 +00:00
|
|
|
def save(self):
|
2018-05-08 10:58:14 +00:00
|
|
|
"""
|
|
|
|
Stores this recording to database
|
|
|
|
This may raise errors
|
|
|
|
"""
|
2018-05-03 14:58:44 +00:00
|
|
|
db.session.add(self)
|
|
|
|
db.session.commit()
|
|
|
|
|
2018-09-16 20:50:54 +00:00
|
|
|
def delete(self):
|
|
|
|
"""
|
|
|
|
Deletes this recording from database
|
|
|
|
"""
|
|
|
|
db.session.delete(self)
|
|
|
|
db.session.commit()
|
|
|
|
|
2018-05-03 14:58:44 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_all():
|
|
|
|
return Recording.query.all()
|
|
|
|
|
2018-05-08 10:58:14 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_many(**kwargs):
|
|
|
|
"""
|
|
|
|
Get many recording with given filters as a list
|
|
|
|
|
|
|
|
Available filters:
|
|
|
|
* id
|
|
|
|
* device_id
|
|
|
|
* record_type
|
|
|
|
* recorded_at
|
|
|
|
* received_at
|
|
|
|
* record_value (probably useless)
|
|
|
|
* raw_record (useless)
|
|
|
|
|
|
|
|
"""
|
|
|
|
return Recording.query.filter_by(**kwargs).all()
|
|
|
|
|
2018-09-22 16:14:45 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_many_filtered(device_id, record_type, date_start, date_end):
|
|
|
|
"""
|
|
|
|
Get many recording with given filters as a list
|
|
|
|
|
|
|
|
Available filters:
|
|
|
|
* record_type
|
|
|
|
* recorded_at (upper and lower limit)
|
|
|
|
"""
|
|
|
|
query = Recording.query.filter(Recording.device_id == device_id)
|
|
|
|
if record_type is not None:
|
|
|
|
query = query.filter(Recording.record_type == record_type)
|
|
|
|
if date_start is not None:
|
|
|
|
lower_limit = time.mktime(datetime.strptime(date_start,
|
|
|
|
"%d-%m-%Y").timetuple())
|
|
|
|
query = query.filter(Recording.recorded_at >
|
|
|
|
db.func.to_timestamp(lower_limit))
|
|
|
|
if date_end is not None:
|
|
|
|
upper_limit = time.mktime(
|
|
|
|
(datetime.strptime(
|
|
|
|
date_end,
|
|
|
|
"%d-%m-%Y"
|
|
|
|
)+datetime_module.timedelta(days=1)).timetuple())
|
|
|
|
query = query.filter(Recording.recorded_at <
|
|
|
|
db.func.to_timestamp(upper_limit))
|
|
|
|
return query.all()
|
|
|
|
|
2018-05-08 10:58:14 +00:00
|
|
|
@staticmethod
|
|
|
|
def get(**kwargs):
|
|
|
|
"""
|
|
|
|
Get recordings with given filters
|
|
|
|
|
|
|
|
Available filters:
|
|
|
|
* id
|
|
|
|
* device_id
|
|
|
|
* record_type
|
|
|
|
* recorded_at
|
|
|
|
* received_at
|
|
|
|
* record_value (probably useless)
|
|
|
|
* raw_record (useless)
|
|
|
|
|
|
|
|
"""
|
2018-09-19 20:51:57 +00:00
|
|
|
return Recording.query.filter_by(**kwargs).first_or_404()
|
2018-05-08 10:58:14 +00:00
|
|
|
|
2018-04-27 08:47:24 +00:00
|
|
|
def __repr__(self):
|
2018-05-02 19:05:30 +00:00
|
|
|
return '<Recording (value=%s, recorded_at=%s)>' % (
|
|
|
|
self.record_value, self.recorded_at)
|
2018-05-08 08:52:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Device(db.Model):
|
|
|
|
__tablename__ = 'devices'
|
|
|
|
|
2018-10-24 20:11:55 +00:00
|
|
|
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
2018-05-08 08:52:49 +00:00
|
|
|
created_at = db.Column(db.DateTime,
|
|
|
|
nullable=False,
|
|
|
|
default=db.func.current_timestamp())
|
|
|
|
modified_at = db.Column(db.DateTime,
|
|
|
|
nullable=False,
|
|
|
|
default=db.func.current_timestamp(),
|
|
|
|
onupdate=db.func.current_timestamp())
|
|
|
|
name = db.Column(db.String, nullable=False)
|
2018-05-08 10:58:14 +00:00
|
|
|
device_type_id = db.Column(db.Integer, db.ForeignKey('device_types.id'))
|
|
|
|
device_type = db.relationship("DeviceType", foreign_keys=[device_type_id])
|
2018-10-29 21:47:03 +00:00
|
|
|
device_secret = db.Column(db.String, nullable=False)
|
|
|
|
secret_algorithm = db.Column(db.String, nullable=False)
|
2018-05-08 08:52:49 +00:00
|
|
|
configuration = db.Column(JSON, nullable=True)
|
|
|
|
|
2018-09-16 20:50:54 +00:00
|
|
|
users = db.relationship("DeviceAssociation",
|
|
|
|
cascade="save-update, merge, delete")
|
|
|
|
recordings = db.relationship("Recording",
|
|
|
|
cascade="save-update, merge, delete")
|
2018-10-31 20:54:59 +00:00
|
|
|
widgets = db.relationship("DashboardWidget",
|
|
|
|
cascade="save-update, merge, delete")
|
2018-08-24 12:32:31 +00:00
|
|
|
|
2018-05-08 10:58:14 +00:00
|
|
|
def __init__(self, name, configuration=None, device_type=1):
|
|
|
|
self.name = name
|
|
|
|
self.configuration = configuration
|
|
|
|
self.device_type_id = device_type
|
2018-10-29 21:47:03 +00:00
|
|
|
self.secret_algorithm = 'sha512'
|
|
|
|
self.device_secret = token_urlsafe(32)
|
2018-05-08 10:58:14 +00:00
|
|
|
|
|
|
|
def save(self):
|
|
|
|
"""
|
|
|
|
Stores this device to database
|
|
|
|
This may raise errors
|
|
|
|
"""
|
|
|
|
db.session.add(self)
|
|
|
|
db.session.commit()
|
|
|
|
|
2018-09-16 20:50:54 +00:00
|
|
|
def delete(self):
|
|
|
|
"""
|
|
|
|
Deletes this recording from database
|
|
|
|
"""
|
|
|
|
db.session.delete(self)
|
|
|
|
db.session.commit()
|
|
|
|
|
2018-05-08 10:58:14 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_many(**kwargs):
|
|
|
|
"""
|
|
|
|
Get many devices with given filters as a list
|
|
|
|
|
|
|
|
Available filters:
|
|
|
|
* id
|
|
|
|
* name
|
|
|
|
* device_type_id
|
|
|
|
* created_at
|
|
|
|
* modified_at
|
|
|
|
* configuration (useless)
|
|
|
|
|
|
|
|
"""
|
2018-09-19 21:04:49 +00:00
|
|
|
return Device.query.filter_by(**kwargs).paginate(
|
|
|
|
None, None, False).items
|
2018-05-08 10:58:14 +00:00
|
|
|
|
2018-08-24 12:32:31 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_many_for_user(account_id):
|
|
|
|
"""
|
|
|
|
Get many devices which are associated to account
|
|
|
|
"""
|
2018-09-16 20:50:54 +00:00
|
|
|
return Device.query.filter(
|
|
|
|
Device.users.any(account_id=account_id)
|
2018-09-19 21:04:49 +00:00
|
|
|
).paginate(None, None, False).items
|
2018-08-24 12:32:31 +00:00
|
|
|
|
2018-05-08 10:58:14 +00:00
|
|
|
@staticmethod
|
|
|
|
def get(**kwargs):
|
|
|
|
"""
|
|
|
|
Get device with given filters
|
|
|
|
|
|
|
|
Available filters:
|
|
|
|
* id
|
|
|
|
* name
|
|
|
|
* device_type_id
|
|
|
|
* created_at
|
|
|
|
* modified_at
|
|
|
|
* configuration (useless)
|
|
|
|
|
|
|
|
"""
|
2018-09-19 20:51:57 +00:00
|
|
|
return Device.query.filter_by(**kwargs).first_or_404()
|
2018-05-08 10:58:14 +00:00
|
|
|
|
2018-05-08 14:45:09 +00:00
|
|
|
@staticmethod
|
|
|
|
def exists(**kwargs):
|
|
|
|
"""
|
|
|
|
Checks if device with all of the given arguments exists
|
|
|
|
"""
|
|
|
|
if Device.query.filter_by(**kwargs).first():
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2018-05-08 10:58:14 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return '<Device (name=%s, type=%s)>' % (
|
|
|
|
self.name, self.device_type_id)
|
|
|
|
|
2018-09-16 20:50:54 +00:00
|
|
|
|
2018-08-24 12:32:31 +00:00
|
|
|
class DeviceAssociation(db.Model):
|
|
|
|
__tablename__ = 'device_associations'
|
|
|
|
|
2018-09-16 20:50:54 +00:00
|
|
|
device_id = db.Column(db.Integer, db.ForeignKey('devices.id'),
|
|
|
|
primary_key=True)
|
|
|
|
account_id = db.Column(db.Integer, db.ForeignKey('accounts.id'),
|
|
|
|
primary_key=True)
|
2018-08-24 12:32:31 +00:00
|
|
|
access_level = db.Column(db.Integer, db.ForeignKey('access_levels.id'),
|
2018-09-16 20:50:54 +00:00
|
|
|
nullable=False)
|
2018-08-24 12:32:31 +00:00
|
|
|
|
|
|
|
def __init__(self, device_id, account_id, access_level=1):
|
|
|
|
self.device_id = device_id
|
|
|
|
self.account_id = account_id
|
|
|
|
self.access_level = access_level
|
|
|
|
|
|
|
|
def save(self):
|
|
|
|
"""
|
|
|
|
Stores this device association to database
|
|
|
|
This may raise errors
|
|
|
|
"""
|
|
|
|
db.session.add(self)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_many(**kwargs):
|
|
|
|
"""
|
|
|
|
Get many device associations with given filters as a list
|
|
|
|
|
|
|
|
Available filters:
|
|
|
|
* device_id
|
|
|
|
* account_id
|
|
|
|
"""
|
|
|
|
return DeviceAssociation.query.filter_by(**kwargs).all()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_for_user(account_id):
|
|
|
|
"""
|
|
|
|
Get many device associations for user with account id passed in
|
|
|
|
parameter
|
|
|
|
"""
|
2018-09-16 20:50:54 +00:00
|
|
|
return DeviceAssociation.get_many(account_id=account_id)
|
2018-08-24 12:32:31 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_for_device(device_id):
|
|
|
|
"""
|
|
|
|
Get many device associations for device with account id passed in
|
|
|
|
parameter
|
|
|
|
"""
|
2018-09-16 20:50:54 +00:00
|
|
|
return DeviceAssociation.get_many(device_id=device_id)
|
2018-08-24 12:32:31 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
2018-09-16 20:50:54 +00:00
|
|
|
return '<DeviceAssociation (device_id=%s, accoount_id=%s)>' % (
|
|
|
|
self.device_id, self.account_id)
|
2018-08-24 12:32:31 +00:00
|
|
|
|
2018-05-08 08:52:49 +00:00
|
|
|
|
|
|
|
class DeviceType(db.Model):
|
|
|
|
__tablename__ = 'device_types'
|
|
|
|
|
2018-10-24 20:11:55 +00:00
|
|
|
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
2018-05-08 08:52:49 +00:00
|
|
|
name = db.Column(db.String, nullable=False)
|
2018-05-08 10:58:14 +00:00
|
|
|
|
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
|
|
|
|
2018-09-17 22:51:26 +00:00
|
|
|
def save(self):
|
|
|
|
"""
|
|
|
|
Stores this device type to database
|
|
|
|
This may raise errors
|
|
|
|
"""
|
|
|
|
db.session.add(self)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_many(**kwargs):
|
|
|
|
"""
|
|
|
|
Get many devices types with given filters as a list
|
|
|
|
|
|
|
|
Available filters:
|
|
|
|
* id
|
|
|
|
* name
|
|
|
|
"""
|
2018-09-19 21:04:49 +00:00
|
|
|
return DeviceType.query.filter_by(**kwargs).paginate(
|
|
|
|
None, None, False).items
|
2018-09-17 22:51:26 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get(**kwargs):
|
|
|
|
"""
|
|
|
|
Get device type with given filters
|
|
|
|
|
|
|
|
Available filters:
|
|
|
|
* id
|
|
|
|
* name
|
|
|
|
"""
|
2018-09-19 20:51:57 +00:00
|
|
|
return DeviceType.query.filter_by(**kwargs).first_or_404()
|
2018-09-17 22:51:26 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def exists(**kwargs):
|
|
|
|
"""
|
|
|
|
Checks if device type with all of the given arguments exists
|
|
|
|
"""
|
|
|
|
if DeviceType.query.filter_by(**kwargs).first():
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2018-05-08 10:58:14 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return '<DeviceType (name %s)>' % self.name
|
2018-08-24 12:32:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AccessLevel(db.Model):
|
|
|
|
__tablename__ = 'access_levels'
|
|
|
|
|
2018-10-24 20:11:55 +00:00
|
|
|
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
2018-08-24 12:32:31 +00:00
|
|
|
name = db.Column(db.String, nullable=False)
|
|
|
|
|
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '<AccessLevel (name %s)>' % self.name
|