university-final-iot-backend/app/devices/models.py

39 lines
1.3 KiB
Python
Raw Normal View History

2018-04-27 08:47:24 +00:00
from datetime import datetime
2018-05-03 13:28:57 +00:00
from app import db
2018-04-27 08:47:24 +00:00
from sqlalchemy.dialects.postgresql import JSON
2018-05-03 13:28:57 +00:00
class Recording(db.Model):
2018-04-27 08:47:24 +00:00
__tablename__ = 'recordings'
2018-05-03 14:58:44 +00:00
id = db.Column(db.Integer, primary_key=True)
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())
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)
2018-04-27 08:47:24 +00:00
def __init__(self, device_id, record_type,
record_value, recorded_at, raw_json):
self.device_id = int(device_id)
self.record_type = int(record_type)
self.record_value = str(record_value)
self.recorded_at = datetime.fromtimestamp(int(recorded_at))
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):
db.session.add(self)
db.session.commit()
@staticmethod
def get_all():
return Recording.query.all()
2018-04-27 08:47:24 +00:00
def __repr__(self):
return '<Recording (value=%s, recorded_at=%s)>' % (
self.record_value, self.recorded_at)