-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
71 lines (64 loc) · 2.06 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
Hits the Fisheries shark api every 10 seconds and currently notifies a
specific beacon if there is a new sighting or detection with specific criteria.
"""
import time
import requests
import paho.mqtt.publish as publish
def get_latest(items):
"""
Returns the latest x number of records from the fisheries api as an
array of json objects.
"""
r = requests.get(
'http://api.fish.wa.gov.au/webapi/v1/RawData?pageNumber=1&pageSize={items}'.format(items=items)
)
return r.json()['PagedItems']
def get_warning_level(alert):
"""
Returns the appropriate warning level as an int for communication to mqtt
clients.
"""
# if alert['ReportDateTime']:
# # Default green for sightings that are not current
# return 0
if alert['InteractionValue'].lower() == 'detected':
# Default red for any detection as shark is <500m
return 2
if alert['DistanceUnit'] == 'm offshore':
# Sightings within 1km of shore are more likely to result in a beach closure.
if int(alert['Distance']) <= 1000:
return 2
else:
return 1
elif alert['DistanceUnit'] == 'km offshore':
if int(alert['Distance']) <= 1:
return 2
elif int(alert['Distance']) <= 2:
return 1
def main():
"""
Logic and looping for hitting api and communicating changes to mqtt clients
if there is something worth telling.
"""
current = get_latest(5)
while True:
latest = get_latest(5)
new = [x for x in latest if x not in current]
if new:
for x in new:
print('Hit! ' + str(x['RawDataId']))
msg = get_warning_level(x)
print('Warning level: ' + str(msg))
publish.single(
hostname="52.62.201.125",
topic="BEACON/1",
payload=msg,
qos=0,
)
else:
print('No hits!')
current = latest
time.sleep(10)
if __name__ == '__main__':
main()