1
|
|
|
# Copyright 2013 Netherlands eScience Center |
2
|
|
|
# |
3
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); |
4
|
|
|
# you may not use this file except in compliance with the License. |
5
|
|
|
# You may obtain a copy of the License at |
6
|
|
|
# |
7
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
8
|
|
|
# |
9
|
|
|
# Unless required by applicable law or agreed to in writing, software |
10
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
11
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12
|
|
|
# See the License for the specific language governing permissions and |
13
|
|
|
# limitations under the License. |
14
|
|
|
|
15
|
1 |
|
import logging |
16
|
1 |
|
from iso8601 import parse_date |
17
|
1 |
|
from pyramid.view import view_config |
18
|
|
|
|
19
|
1 |
|
logger = logging.getLogger(__package__) |
|
|
|
|
20
|
|
|
|
21
|
|
|
|
22
|
1 |
|
@view_config(route_name="home", renderer="home.mako") |
23
|
|
|
def home(request): |
24
|
1 |
|
return {} |
25
|
|
|
|
26
|
|
|
|
27
|
1 |
|
@view_config(route_name='trackers', renderer='json') |
28
|
|
|
def trackers(request): |
29
|
|
|
"""Returns a list of tracker identifiers the user has access to""" |
30
|
1 |
|
cur = request.db.cursor() |
31
|
1 |
|
return {'trackers': fetch_trackers(cur)} |
32
|
|
|
|
33
|
|
|
|
34
|
1 |
|
def fetch_trackers(cur): |
35
|
1 |
|
cur.execute(""" |
36
|
|
|
SELECT DISTINCT device_info_serial as id |
37
|
|
|
FROM gps.ee_tracker_limited |
38
|
|
|
JOIN gps.ee_track_session_limited USING (device_info_serial) |
39
|
|
|
ORDER BY device_info_serial |
40
|
|
|
""") |
41
|
1 |
|
return list(cur) |
42
|
|
|
|
43
|
|
|
|
44
|
1 |
|
def fetch_track(cur, tracker_id, start, end): |
45
|
|
|
# TODO accelartion freq is hardcoded |
46
|
1 |
|
freq = 20.0 |
47
|
|
|
|
48
|
1 |
|
sql2 = """ |
49
|
|
|
SELECT |
50
|
|
|
timezone('zulu', date_time) date_time |
51
|
|
|
, round(s.latitude::numeric, 5) lat |
52
|
|
|
, round(s.longitude::numeric, 5) lon |
53
|
|
|
, s.altitude |
54
|
|
|
, s.altitude altitude_asl |
55
|
|
|
, s.altitude - s.altitude_agl AS ground_elevation |
56
|
|
|
, s.temperature |
57
|
|
|
, round(s.speed_2d::numeric, 5) AS speed |
58
|
|
|
, round(( |
59
|
|
|
ST_Length_Spheroid(ST_MakeLine(location, lag(location) over (order by device_info_serial, date_time)), 'SPHEROID["WGS 84",6378137,298.257223563]') |
60
|
|
|
/ |
61
|
|
|
EXTRACT(EPOCH FROM (date_time - lag(date_time) over (order by device_info_serial, date_time))) |
62
|
|
|
)::numeric, 5) as tspeed |
63
|
|
|
, round(s.direction, 2) AS idirection |
64
|
|
|
, round(degrees(ST_Azimuth(lag(location) over (order by device_info_serial, date_time), location))::numeric, 2) tdirection |
65
|
|
|
, round(mod(s.direction - lag(s.direction) over (order by device_info_serial, date_time), 180.0), 2) AS delta_idirection |
66
|
|
|
, round(degrees( |
67
|
|
|
ST_Azimuth(location, lead(location) over (order by device_info_serial, date_time)) - |
68
|
|
|
ST_Azimuth(lag(location) over (order by device_info_serial, date_time), location) |
69
|
|
|
)::numeric %% 180.0, 2) AS delta_tdirection |
70
|
|
|
, aa.time_acceleration |
71
|
|
|
, aa.x_acceleration, aa.y_acceleration, aa.z_acceleration |
72
|
|
|
FROM |
73
|
|
|
gps.ee_tracking_speed_limited s |
74
|
|
|
LEFT JOIN |
75
|
|
|
( |
76
|
|
|
SELECT device_info_serial, date_time |
77
|
|
|
, array_agg(round(a.index/%s, 4) ORDER BY date_time, index) time_acceleration |
78
|
|
|
, array_agg(round(((x_acceleration-x_o)/x_s)::numeric, 4) ORDER BY date_time, index) x_acceleration |
79
|
|
|
, array_agg(round(((y_acceleration-y_o)/y_s)::numeric, 4) ORDER BY date_time, index) y_acceleration |
80
|
|
|
, array_agg(round(((z_acceleration-z_o)/z_s)::numeric, 4) ORDER BY date_time, index) z_acceleration |
81
|
|
|
FROM gps.ee_acceleration_limited a |
82
|
|
|
JOIN ( |
83
|
|
|
SELECT |
84
|
|
|
DISTINCT device_info_serial |
85
|
|
|
, x_o, x_s |
86
|
|
|
, y_o, y_s |
87
|
|
|
, z_o, z_s |
88
|
|
|
FROM gps.ee_tracker_limited d |
89
|
|
|
) tu USING (device_info_serial) |
90
|
|
|
WHERE |
91
|
|
|
device_info_serial = %s AND date_time BETWEEN %s AND %s |
92
|
|
|
GROUP BY device_info_serial, date_time |
93
|
|
|
) aa USING (device_info_serial, date_time) |
94
|
|
|
WHERE |
95
|
|
|
device_info_serial = %s AND date_time BETWEEN %s AND %s |
96
|
|
|
AND userflag != 1 AND longitude IS NOT NULL |
97
|
|
|
ORDER BY date_time |
98
|
|
|
""" |
99
|
|
|
|
100
|
1 |
|
logger.debug('Fetching track data for id:{0}, start:{1}, end:{2}'.format(tracker_id, start, end)) |
|
|
|
|
101
|
1 |
|
cur.execute(sql2, (freq, tracker_id, start, end, tracker_id, start, end)) |
102
|
1 |
|
return cur |
103
|
|
|
|
104
|
|
|
|
105
|
1 |
|
@view_config(route_name='tracker', renderer='json') |
106
|
|
|
def tracker(request): |
107
|
|
|
"""Returns gps+accel data of tracker in a certain time range""" |
108
|
1 |
|
cur = request.db.cursor() |
109
|
1 |
|
tracker_id = int(request.matchdict['id']) |
110
|
1 |
|
start = parse_date(request.matchdict['start']).isoformat() |
111
|
1 |
|
end = parse_date(request.matchdict['end']).isoformat() |
112
|
|
|
return fetch_track(cur, tracker_id, start, end) |
113
|
|
|
|
This check looks for invalid names for a range of different identifiers.
You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.
If your project includes a Pylint configuration file, the settings contained in that file take precedence.
To find out more about Pylint, please refer to their site.