import imagezmq
import sudoku_reader as sr

'''
This "main" program is in effect a server in a client/server
relationship with the Pi. It listens for an image from the Pi.
Once received, it processes the image to find the digits in
a Sudoku puzzle. It then sends a binary string representing
the puzzle back to the Pi. 
'''
image_hub = imagezmq.ImageHub()

# get an instance of the puzzle reader
reader = sr.SudokuReader()

while True:
    print("Waiting on image")
    text, image = image_hub.recv_image()
    y, x, z = image.shape

    '''
    Take action based on request from Pi
    '''
    if  text == 'test':
        # ignore request
        will_send = "ok".encode('utf-8')
        image_hub.send_reply(will_send)
    elif text == 'end':
        # end server
        will_send = "ok".encode('utf-8')
        image_hub.send_reply(will_send)
        break
    else: # have an image
        puzzle = reader.find_puzzle(image)
        will_send = puzzle.encode('utf-8')
        image_hub.send_reply(will_send)
        print("Digits sent")

print("Receiver complete")