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

31 lines
1.1 KiB
Python
Raw Normal View History

2018-04-27 08:47:24 +00:00
from datetime import datetime
from . import db
from sqlalchemy.dialects.postgresql import JSON
2018-04-27 08:47:24 +00:00
class Recording(db.Model):
__tablename__ = 'recordings'
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)
2018-04-27 08:47:24 +00:00
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, 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
def __repr__(self):
return '<Recording (value=%s, recorded_at=%s)>' % (
self.record_value, self.recorded_at)