#!/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 not local.socket in events:
                    return
                emo = events[local.socket]
                lg.info("received {}".format(emo))
                mirrorvideo.play_emotion_video(int(emo))
            except zmq.ZMQError as e:
                if e.errno == zmq.EAGAIN:
                    return
                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))
    mirrorvideo.MediaRepository.init()
    receive(args.detector)


if __name__ == '__main__':
    main()

@@ -1620,20 +1620,16 @@
      if 
-not 
 local.so
@@ -1642,20 +1642,23 @@
 n events
-:%0A  
+ and %5C%0A
         
@@ -1659,39 +1659,73 @@
                 
-return%0A
+ events%5Blocal.socket%5D == zmq.POLLIN:%0A    
                 
@@ -1730,23 +1730,16 @@
   emo = 
-events%5B
 local.so
@@ -1746,10 +1746,31 @@
 cket
-%5D%0A
+.recv(zmq.NOBLOCK)%0A    
     
@@ -1808,32 +1808,36 @@
 %7D%22.format(emo))%0A
+    
                 

Fix receiver.