-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreceiver.py
More file actions
executable file
·96 lines (77 loc) · 2.64 KB
/
Copy pathreceiver.py
File metadata and controls
executable file
·96 lines (77 loc) · 2.64 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/python
import os
import sys
import zmq
import mirrorvideo
import time
from datetime import datetime
from dotenv import load_dotenv
import logging as lg
lg.basicConfig(level=lg.INFO,
format='[%(levelname)-8s] %(asctime)s %(message)s')
IMAGE_TIMEOUT = 1 # Seconds
def connect(ctx, host):
socket = ctx.socket(zmq.SUB)
socket.setsockopt(zmq.SUBSCRIBE, '')
lg.info("Connecting to {}".format(host))
socket.connect('tcp://{}:1776'.format(host))
return socket
def receive(host):
while not mirrorvideo.blank_screen():
time.sleep(10)
# Open ZMQ context
ctx = zmq.Context()
lg.info("Receiving from {}".format(host))
class local:
"""Inner class to encapsulate nonlocal variables. Hack around
scoping.
"""
socket = None
poller = zmq.Poller()
last_update = datetime.min
while local.socket is None:
try:
local.socket = connect(ctx, host)
local.poller.register(local.socket, zmq.POLLIN)
except Exception as e:
lg.info(
"Failed to connect with exception {}, trying again.".format(e))
root = mirrorvideo.blank_screen()
def poll_events():
window = mirrorvideo.OverlayWindow.create()
now = datetime.now()
if (now - local.last_update).total_seconds() > IMAGE_TIMEOUT:
mirrorvideo.blank_screen()
local.last_update = now
if window:
window.after(0, poll_events)
try:
events = dict(local.poller.poll(1000))
if local.socket in events and \
events[local.socket] == zmq.POLLIN:
emo = local.socket.recv(zmq.NOBLOCK)
lg.info("received {}".format(emo))
mirrorvideo.play_emotion_video(int(emo))
except zmq.ZMQError as e:
if e.errno == zmq.EAGAIN:
pass
else:
lg.info("Failed with {}. Retrying.".format(e))
local.socket = connect(ctx, host)
root.after(0, poll_events)
root.mainloop()
def main():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-detector',
default='mirror1.local', action='store')
parser.add_argument('-env',
default='/home/pi/Desktop/config.txt',
action='store')
args = parser.parse_args()
load_dotenv(args.env)
global IMAGE_TIMEOUT
IMAGE_TIMEOUT = float(os.environ.get('IMAGE_TIMEOUT', 1))
receive(args.detector)
if __name__ == '__main__':
main()