-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
73 lines (60 loc) · 1.64 KB
/
client.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
72
73
#!/usr/bin/env python3
from gpiozero import LED
import socketio
import json
import time
from modules.camera import get_squirrel_confidence
alert_led = LED(4) # green
fire_led = LED(24) # red
sio = socketio.Client()
syncedState = {}
@sio.event
def connect():
print('connection established')
sio.start_background_task(task_find_squirrel)
sio.start_background_task(task_fire)
@sio.event
def syncedState_update(data):
data = json.loads(data)
syncedState.update(data)
# print('syncedState_update:', syncedState)
@sio.event
def disconnect():
print('disconnected from server')
def alert_server(header, data):
data_str = json.dumps(data)
sio.emit('syncedState_change', data_str)
def look_for_squirrel():
confidence = get_squirrel_confidence()
alert_server(
'syncedState_change',
{'squirrelConfidence': confidence},
)
if confidence > 60:
print('IT\'S A SQUIRREL!')
alert_led.blink()
alert_server(
'syncedState_change',
{'squirrelAlert': time.time()},
)
else:
print('Nope.')
alert_led.off()
alert_server(
'syncedState_change',
{'squirrelAlert': False},
)
def task_fire():
while True:
if syncedState.get('buttonSquirt'):
fire_led.on()
else:
fire_led.off()
def task_find_squirrel():
while True:
print('Looking for squirrel...')
look_for_squirrel()
time.sleep(1)
if __name__ == '__main__':
sio.connect('http://138.197.142.143')
sio.wait()