Skip to content
This repository was archived by the owner on Oct 24, 2022. It is now read-only.

Commit fc8e281

Browse files
committed
Rewritten for Waldo.
1 parent db91c33 commit fc8e281

File tree

3 files changed

+64
-96
lines changed

3 files changed

+64
-96
lines changed

Procfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
web: gunicorn lifeguard:app --log-file=-
1+
web: gunicorn lifeguard:app --timeout 10 --preload

lifeguard.py

Lines changed: 60 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -6,131 +6,99 @@
66
import pytz
77

88

9-
class PersonMap:
10-
def __init__(self, name):
11-
self.name = name
12-
self.latitude = 0.0
13-
self.longitude = 0.0
14-
self.map_id = name + '_map'
15-
self.map = Map(identifier=self.map_id, lat=self.latitude,
16-
lng=self.longitude, markers=[(self.latitude, self.longitude)])
17-
self.last_update = None
18-
19-
def update_location(self, lat, lon, dt=None):
20-
self.latitude = lat
21-
self.longitude = lon
22-
self.map = Map(identifier=self.map_id, lat=self.latitude,
23-
lng=self.longitude, markers=[(self.latitude, self.longitude)])
24-
self.last_update = dt
25-
26-
27-
device_owners = {
28-
'0529CF2B-AEBD-4E81-8DD0-A99DDCC4EA60': 'Greg',
29-
30-
}
31-
32-
owner_maps = {
33-
'Greg': PersonMap('Greg L.'),
34-
}
35-
36-
379
app = Flask(__name__)
3810
GoogleMaps(app)
3911
tz = pytz.timezone('America/New_York')
4012

4113

14+
# Waldo APIs
15+
devices = {}
16+
17+
4218
@app.route('/')
4319
def home():
44-
return render_template('home.html', owner_maps=owner_maps)
20+
return render_template('home.html', devices=devices)
4521

4622

4723
@app.route('/about')
4824
def about():
4925
return render_template('about.html')
5026

5127

52-
@app.route('/location', methods=['POST', 'GET'])
53-
def location():
28+
class UserMap:
29+
def __init__(self, name):
30+
self.name = name
31+
self.latitude = 42.4343779
32+
self.longitude = -83.9891627
33+
self.map_id = name + '_map'
34+
self.map = Map(identifier=self.map_id, lat=self.latitude,
35+
lng=self.longitude, markers=[(self.latitude, self.longitude)])
36+
self.last_update = None
37+
38+
def update_location(self, lat, lon, dt=None):
39+
self.latitude = lat
40+
self.longitude = lon
41+
self.map = Map(identifier=self.map_id, lat=self.latitude,
42+
lng=self.longitude, markers=[(self.latitude, self.longitude)])
43+
self.last_update = dt
44+
5445

55-
device = request.args.get('p')
56-
# get time in tz
57-
posix_timestamp = request.args.get('t')
58-
dt = datetime.fromtimestamp(float(posix_timestamp), tz)
59-
latitude = request.args.get('lat')
60-
longitude = request.args.get('lng')
46+
class Device:
47+
def __init__(self, device_id, user_id, name):
48+
self.device_id = device_id
49+
self.name = name
50+
self.user_id = user_id
51+
self.map = UserMap(name)
6152

62-
if device in device_owners:
63-
owner = device_owners[device]
64-
owner_map = owner_maps[owner]
65-
owner_map.update_location(latitude, longitude, dt)
66-
else:
67-
owner = 'unknown (device: ' + device + ')'
53+
def update_device(self, user_id, name):
54+
self.user_id = user_id
55+
self.name = name
6856

69-
result = 'Lifeguard location updated at ' + dt.strftime('%Y-%m-%d %I:%M:%S %p') + ' for ' + owner + ': ' + latitude + ',' + longitude
70-
print(result)
71-
return result
7257

58+
@app.route('/devicecheckin', methods=['POST'])
59+
def device_check_in():
60+
checkin_data = request.get_json()
7361

74-
@app.route('/register', methods=['POST', 'GET'])
75-
def register_user():
62+
device_id = checkin_data['id']
63+
lat = checkin_data['lat']
64+
lng = checkin_data['lng']
65+
time = checkin_data['time']
66+
#mode = checkin_data['mode']
67+
dt = datetime.fromtimestamp(float(time), tz)
7668

77-
device = request.args.get('p')
78-
# get time in tz
79-
posix_timestamp = request.args.get('t')
80-
dt = datetime.fromtimestamp(float(posix_timestamp), tz)
81-
user_id = request.args.get('uid')
82-
latitude = request.args.get('lat')
83-
longitude = request.args.get('lng')
69+
result = "Waldo device check in API result: "
8470

85-
if device in device_owners:
86-
owner_maps[device_owners[device]].update_location(latitude, longitude, dt)
71+
if device_id in devices:
72+
device = devices[device_id]
73+
map = device.map
74+
map.update_location(lat, lng, dt)
75+
result = result + "updating device {} with location (lat,ln) ({},{}) at {}.".format(device_id, lat, lng, dt)
8776
else:
88-
owner_maps[user_id] = PersonMap(user_id)
89-
owner_maps[user_id].update_location(latitude, longitude, dt)
90-
device_owners[device] = user_id
91-
92-
result = 'Lifeguard user registered with user name ' + user_id
93-
print(result)
77+
result = result + "error due to unknown device."
9478
return result
9579

9680

97-
@app.route('/visit', methods=['POST', 'GET'])
98-
def visit():
81+
@app.route('/registration', methods=['POST'])
82+
def device_registration():
83+
registration_data = request.get_json()
9984

100-
user = request.args.get('user')
101-
# get time in tz
102-
timestamp = request.args.get('t')
85+
device_id = registration_data['deviceId']
86+
user_id = registration_data['userId']
87+
name = registration_data['name']
10388

104-
latitude = request.args.get('lat')
105-
longitude = request.args.get('lng')
106-
arrive = request.args.get('arrive')
107-
depart = request.args.get('depart')
89+
result = "Waldo user registration API result: "
10890

109-
if user in owner_maps:
110-
owner_map = owner_maps[user]
111-
owner_map.update_location(latitude, longitude)
91+
if device_id in devices:
92+
devices[device_id].update_device(user_id, name)
93+
result = result + "updating device {} with user name {} with user ID of {}.".format(device_id, name, user_id)
11294
else:
113-
user = 'Unknown user: ' + user
114-
115-
result = 'Lifeguard visit update at ' + timestamp + ' for ' + user + ': ' + ' arrived at ' + arrive + \
116-
', departed at ' + depart + ', at location ' + latitude + ',' + longitude
95+
device = Device(device_id, user_id, name)
96+
devices[device_id] = device
97+
result = result + "adding device {} for user name {} with user ID of {}.".format(device_id, name, user_id)
11798

118-
print(result)
11999
return result
120100

121101

122-
# Waldo APIs
123-
@app.route('/devicecheckin', methods=['POST'])
124-
def device_check_in():
125-
checkin_data = request.get_json()
126-
127-
device_id = checkin_data['id']
128-
lat = checkin_data['lat']
129-
lng = checkin_data['lng']
130-
time = checkin_data['time']
131-
mode = checkin_data['mode']
132-
133-
134102
if __name__ == '__main__':
135103
app.debug = True
136104
app.run()

templates/home.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
<div class="starter-template">
44

55
<h1>Testing Team</h1>
6-
{% for owner, owner_map in owner_maps.items() %}
6+
{% for device_id, device in devices.items() %}
77
<br/>
88
<p class="lead">
9-
<a href="http://maps.google.com/maps?q={{ owner_map.latitude }},{{ owner_map.longitude }}+(My+Point)&z=15&ll={{ owner_map.latitude }},{{ owner_map.longitude }}">{{owner}}'s Location</a>
10-
- last updated at {{ owner_map.last_update }}
9+
<a href="http://maps.google.com/maps?q={{ device.map.latitude }},{{ device.map.longitude }}+(My+Point)&z=15&ll={{ device.map.latitude }},{{ device.map.longitude }}">{{device.name}}'s Location</a>
10+
- last updated at {{ device.map.last_update }}
1111
</p>
1212
<br/>
1313
{% endfor %}

0 commit comments

Comments
 (0)