|
1
|
|
|
""" |
|
2
|
|
|
Model for sample |
|
3
|
|
|
""" |
|
4
|
|
|
from flask import url_for |
|
5
|
|
|
|
|
6
|
|
|
from app.database import db |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
class Sample(db.Model): |
|
10
|
|
|
""" |
|
11
|
|
|
Sample model |
|
12
|
|
|
|
|
13
|
|
|
Arguments: |
|
14
|
|
|
id (int): Primary Sample key |
|
15
|
|
|
sensor_id (int): Foreign ``Sensor``.id key |
|
16
|
|
|
sensor: ``Sensor`` object related to this sample |
|
17
|
|
|
datetime (datetime): ``datetime`` object of this sample (should in UTC) |
|
18
|
|
|
value (float): Value of sample |
|
19
|
|
|
""" |
|
20
|
|
|
__tablename__ = 'samples' |
|
21
|
|
|
|
|
22
|
|
|
id = db.Column(db.Integer, primary_key=True) |
|
23
|
|
|
|
|
24
|
|
|
sensor_id = db.Column(db.Integer, db.ForeignKey('sensors.id'), index=True) |
|
25
|
|
|
sensor = db.relationship('Sensor', backref=db.backref('samples')) |
|
26
|
|
|
|
|
27
|
|
|
datetime = db.Column(db.DateTime, index=True) |
|
28
|
|
|
value = db.Column(db.Float) |
|
29
|
|
|
|
|
30
|
|
|
def to_json(self): |
|
31
|
|
|
""" |
|
32
|
|
|
Creates a JSON object from Sample. Used where multiple samples will be |
|
33
|
|
|
displayed at once. |
|
34
|
|
|
""" |
|
35
|
|
|
json_sample = { |
|
36
|
|
|
'id': self.id, |
|
37
|
|
|
'sensor': self.sensor.to_sample_json(), |
|
38
|
|
|
'value': self.value, |
|
39
|
|
|
'datetime': self.datetime, |
|
40
|
|
|
'url': url_for('api.get_sample', sid=self.id, _external=True) |
|
41
|
|
|
} |
|
42
|
|
|
return json_sample |
|
43
|
|
|
|
|
44
|
|
|
def to_sensor_json(self): |
|
45
|
|
|
""" |
|
46
|
|
|
Creates a JSON object from Sample for used with Sensor JSON. |
|
47
|
|
|
""" |
|
48
|
|
|
json_sample = { |
|
49
|
|
|
'id': self.id, |
|
50
|
|
|
'value': self.value, |
|
51
|
|
|
'datetime': str(self.datetime), |
|
52
|
|
|
'url': url_for('api.get_sample', sid=self.id, _external=True) |
|
53
|
|
|
} |
|
54
|
|
|
return json_sample |
|
55
|
|
|
|
|
56
|
|
|
def __repr__(self): |
|
57
|
|
|
return '<Sample %r>' % self.id |
|
58
|
|
|
|