|
1
|
|
|
""" |
|
2
|
|
|
Endpoints: |
|
3
|
|
|
---------- |
|
4
|
|
|
|
|
5
|
|
|
- **/api/1.0/sensors/ - **GET** List all sensors |
|
6
|
|
|
- **/api/1.0/sensors/<id> - **GET** Detailed information about sensor |
|
7
|
|
|
- **/api/1.0/sensors/<id>/samples - **GET** Samples from sensor *id* |
|
8
|
|
|
""" |
|
9
|
|
|
from flask import jsonify, request, url_for, current_app |
|
10
|
|
|
|
|
11
|
|
|
from ..models import Sensor, Sample |
|
12
|
|
|
from .blueprint import api |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
@api.route('/sensors/') |
|
16
|
|
|
def get_sensors(): |
|
17
|
|
|
""" |
|
18
|
|
|
List all sensors |
|
19
|
|
|
|
|
20
|
|
|
Example response: :: |
|
21
|
|
|
|
|
22
|
|
|
{ "count": 17, |
|
23
|
|
|
"next": "http://riverflo.ws/api/1.0/sensors/?page=2", |
|
24
|
|
|
"prev": null, |
|
25
|
|
|
"sensors": [ |
|
26
|
|
|
{ "id": 2, |
|
27
|
|
|
"type": "voltage", |
|
28
|
|
|
"url": "http://riverflo.ws/api/1.0/sensors/2" |
|
29
|
|
|
}, |
|
30
|
|
|
{ "id": 3, |
|
31
|
|
|
"type": "amps", |
|
32
|
|
|
"url": "http://riverflo.ws/api/1.0/sensors/3" |
|
33
|
|
|
} |
|
34
|
|
|
] |
|
35
|
|
|
} |
|
36
|
|
|
""" |
|
37
|
|
|
page = request.args.get('page', 1, type=int) |
|
38
|
|
|
pagination = Sensor.query.paginate(page, |
|
39
|
|
|
per_page=current_app.config['API_GAGES_PER_PAGE'], # noqa |
|
40
|
|
|
error_out=False) |
|
41
|
|
|
sensors = pagination.items |
|
42
|
|
|
prev = None |
|
43
|
|
|
if pagination.has_prev: |
|
44
|
|
|
prev = url_for('.get_sensors', page=page-1, _external=True) |
|
45
|
|
|
next_p = None |
|
46
|
|
|
if pagination.has_next: |
|
47
|
|
|
next_p = url_for('.get_sensors', page=page+1, _external=True) |
|
48
|
|
|
return jsonify({ |
|
49
|
|
|
'sensors': [sensor.to_json() for sensor in sensors], |
|
50
|
|
|
'prev': prev, |
|
51
|
|
|
'next': next_p, |
|
52
|
|
|
'count': pagination.total |
|
53
|
|
|
}) |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
@api.route('/sensors/<int:sid>') |
|
57
|
|
|
def get_sensor(sid): |
|
58
|
|
|
""" |
|
59
|
|
|
Detailed information about sensor *id* |
|
60
|
|
|
|
|
61
|
|
|
Parameters: |
|
62
|
|
|
id (int): Primary id key of sensor |
|
63
|
|
|
|
|
64
|
|
|
Example response: :: |
|
65
|
|
|
|
|
66
|
|
|
{ "description": null, |
|
67
|
|
|
"ended": null, |
|
68
|
|
|
"gage": { |
|
69
|
|
|
"id": 2, |
|
70
|
|
|
"location": "Wild River near RT 2 in Gilead Maine", |
|
71
|
|
|
"name": "Wild River at Gilead", |
|
72
|
|
|
"url": "http://riverflo.ws/api/1.0/gages/2" |
|
73
|
|
|
}, |
|
74
|
|
|
"id": 5, |
|
75
|
|
|
"maximum": null, |
|
76
|
|
|
"minimum": null, |
|
77
|
|
|
"recent_sample": { |
|
78
|
|
|
"datetime": "Mon, 25 Aug 2014 13:11:35 GMT", |
|
79
|
|
|
"id": 1555, |
|
80
|
|
|
"url": "http://riverflo.ws/api/1.0/samples/1555", |
|
81
|
|
|
"value": 25.6666666666667 |
|
82
|
|
|
}, |
|
83
|
|
|
"started": null, |
|
84
|
|
|
"type": "level", |
|
85
|
|
|
"url": "http://riverflo.ws/api/1.0/sensors/5" |
|
86
|
|
|
} |
|
87
|
|
|
""" |
|
88
|
|
|
sensor = Sensor.query.get_or_404(sid) |
|
89
|
|
|
return jsonify(sensor.to_long_json()) |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
@api.route('/sensors/<int:sid>/samples') |
|
93
|
|
|
def get_sensor_samples(sid): |
|
94
|
|
|
""" |
|
95
|
|
|
List samples for sensor *id* |
|
96
|
|
|
|
|
97
|
|
|
Parameters: |
|
98
|
|
|
id (int): Primary id key of sensor |
|
99
|
|
|
|
|
100
|
|
|
Example response: :: |
|
101
|
|
|
|
|
102
|
|
|
{ "count": 487, |
|
103
|
|
|
"next": null, |
|
104
|
|
|
"prev": null, |
|
105
|
|
|
"samples": [ |
|
106
|
|
|
{ "datetime": "Thu, 05 Jun 2014 13:50:27 GMT", |
|
107
|
|
|
"id": 52, |
|
108
|
|
|
"url": "http://riverflo.ws/api/1.0/samples/52", |
|
109
|
|
|
"value": 24.0 |
|
110
|
|
|
}, |
|
111
|
|
|
{ "datetime": "Thu, 05 Jun 2014 13:50:42 GMT", |
|
112
|
|
|
"id": 55, |
|
113
|
|
|
"url": "http://riverflo.ws/api/1.0/samples/55", |
|
114
|
|
|
"value": 24.0 |
|
115
|
|
|
} |
|
116
|
|
|
], |
|
117
|
|
|
"sensor": { |
|
118
|
|
|
"id": 5, |
|
119
|
|
|
"type": "level", |
|
120
|
|
|
"url": "http://riverflo.ws/api/1.0/sensors/5" |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
""" |
|
124
|
|
|
sensor = Sensor.query.get_or_404(sid) |
|
125
|
|
|
sensor.recent() |
|
126
|
|
|
page = request.args.get('page', 1, type=int) |
|
127
|
|
|
pagination = Sample.query.filter_by(sensor_id=sid).paginate(page, |
|
128
|
|
|
per_page=current_app.config['API_GAGES_PER_PAGE'], # noqa |
|
129
|
|
|
error_out=False) |
|
130
|
|
|
samples = pagination.items |
|
131
|
|
|
prev = None |
|
132
|
|
|
if pagination.has_prev: |
|
133
|
|
|
prev = url_for('.get_sensor_samples', page=page-1, _external=True) |
|
134
|
|
|
next_p = None |
|
135
|
|
|
if pagination.has_next: |
|
136
|
|
|
next_p = url_for('.get_sensor_samples', page=page+1, _external=True) |
|
137
|
|
|
return jsonify({ |
|
138
|
|
|
'sensor': sensor.to_json(), |
|
139
|
|
|
'samples': [sample.to_sensor_json() for sample in samples], |
|
140
|
|
|
'prev': prev, |
|
141
|
|
|
'next': next_p, |
|
142
|
|
|
'count': pagination.total, |
|
143
|
|
|
}) |
|
144
|
|
|
|