|
1
|
|
|
""" |
|
2
|
|
|
Model for sensor |
|
3
|
|
|
""" |
|
4
|
|
|
import datetime |
|
5
|
|
|
from flask import url_for, current_app |
|
6
|
|
|
from sqlalchemy.dialects.postgresql import JSON |
|
7
|
|
|
|
|
8
|
|
|
from app.database import db |
|
9
|
|
|
from .sample import Sample |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
class Sensor(db.Model): |
|
13
|
|
|
""" |
|
14
|
|
|
A single sensor value for a Gage |
|
15
|
|
|
|
|
16
|
|
|
Arguments: |
|
17
|
|
|
id (int): Primary key for Sensor |
|
18
|
|
|
gage_id (int): Foreign ``Gage``.id for the Gage that this Sensor is a part of |
|
19
|
|
|
gage: ``Gage`` object for associated Gage |
|
20
|
|
|
stype (str): Type of sensor as reported by sending station (gage). |
|
21
|
|
|
name (str): Nice display name for sensor |
|
22
|
|
|
slug (str): slug for url |
|
23
|
|
|
prefix (str): Prefix for value display |
|
24
|
|
|
suffix (str): Suffix for value display |
|
25
|
|
|
local (boolean): True if the station(gage) sends this sensor's data to the server. |
|
26
|
|
|
remote_type (str): Type of remote sensor. Currently only ``'usgs'`` is valid. |
|
27
|
|
|
remote_id (int): String element that should be used to query remote sensor. |
|
28
|
|
|
remote_parameter (str): Parameter that is required to query remote sensor. |
|
29
|
|
|
last (datetime): Last time data was received or retrieved. |
|
30
|
|
|
title (str): Title to display on plots. |
|
31
|
|
|
xlabel (str): x-axis label to display on plots. |
|
32
|
|
|
ylabel (str): y-axis label to display on plots. |
|
33
|
|
|
info (JSON): JSON with more detail about sensor for possible future use. |
|
34
|
|
|
description (text): Long description of sensor that can contain HTML or Markdown within reason. |
|
35
|
|
|
backend_notes (text): Backend info for admins. |
|
36
|
|
|
minimum (float): Lowest measurement of sensor. Used for plot formatting. |
|
37
|
|
|
maximum (float): Highest measurement of sensor. Used for plot formatting. |
|
38
|
|
|
started (datetime): Datetime that sample collection started. |
|
39
|
|
|
ended (datetime): Datetime that sample collection ended. |
|
40
|
|
|
samples: List of ``Sample`` objects from Sensor. |
|
41
|
|
|
""" |
|
42
|
|
|
__tablename__ = 'sensors' |
|
43
|
|
|
|
|
44
|
|
|
id = db.Column(db.Integer, primary_key=True) |
|
45
|
|
|
|
|
46
|
|
|
gage_id = db.Column(db.Integer, db.ForeignKey('gages.id'), index=True) |
|
47
|
|
|
gage = db.relationship('Gage', |
|
48
|
|
|
backref=db.backref('sensors', lazy='dynamic')) |
|
49
|
|
|
|
|
50
|
|
|
stype = db.Column(db.String(80)) |
|
51
|
|
|
name = db.Column(db.String(80)) |
|
52
|
|
|
slug = db.Column(db.String(80)) |
|
53
|
|
|
prefix = db.Column(db.String(10)) |
|
54
|
|
|
suffix = db.Column(db.String(10)) |
|
55
|
|
|
local = db.Column(db.Boolean) |
|
56
|
|
|
remote_type = db.Column(db.String) |
|
57
|
|
|
remote_id = db.Column(db.String) |
|
58
|
|
|
remote_parameter = db.Column(db.String) |
|
59
|
|
|
last = db.Column(db.DateTime) |
|
60
|
|
|
title = db.Column(db.String) |
|
61
|
|
|
xlabel = db.Column(db.String) |
|
62
|
|
|
ylabel = db.Column(db.String) |
|
63
|
|
|
info = db.Column(JSON) |
|
64
|
|
|
description = db.Column(db.Text) |
|
65
|
|
|
backend_notes = db.Column(db.Text) |
|
66
|
|
|
minimum = db.Column(db.Float) |
|
67
|
|
|
maximum = db.Column(db.Float) |
|
68
|
|
|
started = db.Column(db.DateTime) |
|
69
|
|
|
ended = db.Column(db.DateTime) |
|
70
|
|
|
|
|
71
|
|
|
def timediff(self, dateTime): |
|
72
|
|
|
delta = datetime.datetime.now()-datetime.timedelta(minutes=60) |
|
73
|
|
|
return (self.stype, |
|
74
|
|
|
str(dateTime), |
|
75
|
|
|
str(datetime.datetime.now()), |
|
76
|
|
|
str(dateTime > delta), |
|
77
|
|
|
str(datetime.datetime.now() - dateTime)) |
|
78
|
|
|
|
|
79
|
|
|
def recent(self): |
|
80
|
|
|
""" |
|
81
|
|
|
Return recent sample value. |
|
82
|
|
|
""" |
|
83
|
|
|
current_app.logger.debug('Recent for sensor {name}'.format(name=self.name)) |
|
84
|
|
|
sample = Sample.query.filter_by(sensor_id=self.id).order_by(Sample.datetime.desc()).first() |
|
85
|
|
|
current_app.logger.debug('Retrieved sample: {sample}'.format(sample=sample)) |
|
86
|
|
|
return sample |
|
87
|
|
|
|
|
88
|
|
|
def to_json(self): |
|
89
|
|
|
""" |
|
90
|
|
|
Creates a JSON object from sensor. Used where multiple sensors may be |
|
91
|
|
|
displayed at once. |
|
92
|
|
|
""" |
|
93
|
|
|
json_post = { |
|
94
|
|
|
'id': self.id, |
|
95
|
|
|
'type': self.stype, |
|
96
|
|
|
'url': url_for('api.get_sensor', sid=self.id, _external=True) |
|
97
|
|
|
} |
|
98
|
|
|
return json_post |
|
99
|
|
|
|
|
100
|
|
|
def to_long_json(self): |
|
101
|
|
|
""" |
|
102
|
|
|
Creates a JSON object from sensor. Used where a single sensor will be |
|
103
|
|
|
displayed. |
|
104
|
|
|
""" |
|
105
|
|
|
sample = self.recent() |
|
106
|
|
|
json_post = { |
|
107
|
|
|
'id': self.id, |
|
108
|
|
|
'type': self.stype, |
|
109
|
|
|
'description': self.description, |
|
110
|
|
|
'minimum': self.minimum, |
|
111
|
|
|
'maximum': self.maximum, |
|
112
|
|
|
'started': self.started, |
|
113
|
|
|
'ended': self.ended, |
|
114
|
|
|
'url': url_for('api.get_sensor', sid=self.id, _external=True), |
|
115
|
|
|
'gage': self.gage.to_json() |
|
116
|
|
|
} |
|
117
|
|
|
if sample is not None: |
|
118
|
|
|
json_post['recent_sample'] = sample.to_sensor_json() |
|
119
|
|
|
return json_post |
|
120
|
|
|
|
|
121
|
|
|
def to_gage_json(self): |
|
122
|
|
|
""" |
|
123
|
|
|
Creates a JSON object from sensor. Displayed with gage JSON and includes |
|
124
|
|
|
most recent sample. |
|
125
|
|
|
""" |
|
126
|
|
|
sample = self.recent() |
|
127
|
|
|
json_post = { |
|
128
|
|
|
'id': self.id, |
|
129
|
|
|
'type': self.stype, |
|
130
|
|
|
'url': url_for('api.get_sensor', sid=self.id, _external=True) |
|
131
|
|
|
} |
|
132
|
|
|
if sample is not None: |
|
133
|
|
|
json_post['recent_sample'] = sample.to_sensor_json() |
|
134
|
|
|
return json_post |
|
135
|
|
|
|
|
136
|
|
|
def to_sample_json(self): |
|
137
|
|
|
""" |
|
138
|
|
|
Creates a JSON object from sensor. Displayed with sample JSON and |
|
139
|
|
|
includes gage information. |
|
140
|
|
|
""" |
|
141
|
|
|
json_sensor = { |
|
142
|
|
|
'id': self.id, |
|
143
|
|
|
'type': self.stype, |
|
144
|
|
|
'gage': self.gage.to_json(), |
|
145
|
|
|
'url': url_for('api.get_sensor', sid=self.id, _external=True) |
|
146
|
|
|
} |
|
147
|
|
|
return json_sensor |
|
148
|
|
|
|
|
149
|
|
|
def __repr__(self): |
|
150
|
|
|
return '<Sensor {0} - {1}>'.format(self.gage.name, self.stype) |
|
151
|
|
|
|
|
152
|
|
|
def __str__(self): |
|
153
|
|
|
return self.gage.name + ' - ' + self.nice_name() |
|
154
|
|
|
|
|
155
|
|
|
def nice_name(self): |
|
156
|
|
|
if self.name: |
|
157
|
|
|
return self.name |
|
158
|
|
|
return self.stype |