# Path: utils.py
# def filesize(filename):
#     return bytes2human(os.path.getsize(filename))
# 
# def send_array(socket, A, flags=0, copy=True, track=False):
#     """send a numpy array with metadata"""
#     md = dict(
#         dtype = str(A.dtype),
#         shape = A.shape,
#     )
#     socket.send_json(md, flags|zmq.SNDMORE)
#     return socket.send(A, flags, copy=copy, track=track)
# 
# def recv_array(socket, flags=0, copy=True, track=False):
#     """recv a numpy array"""
#     md = socket.recv_json(flags=flags)
#     msg = socket.recv(flags=flags, copy=copy, track=track)
#     buf = buffer(msg)
#     A = np.frombuffer(buf, dtype=md['dtype'])
#     return A.reshape(md['shape'])
# 
# Path: IO.py
# MIC = 5563
# 
# SPEAKER = 5564
# 
# CAMERA = 5561
# 
# PROJECTOR = 5562
# 
# STATE = 5565
# 
# SNAPSHOT= 5567
# 
# EVENT = 5568
# 
# EXTERNAL = 5566 # If you change this, you're out of luck.


import time
import multiprocessing as mp
import cPickle as pickle
import zmq
import numpy as np
    import Oger
from uuid import uuid1
from collections import deque
from sklearn import preprocessing as pp
from utils import filesize, send_array, recv_array
from IO import MIC, SPEAKER, CAMERA, PROJECTOR, STATE, SNAPSHOT, EVENT, EXTERNAL

    context = zmq.Context()
    mic = context.socket(zmq.SUB)
    mic.connect('tcp://{}:{}'.format(host, MIC))
    mic.setsockopt(zmq.SUBSCRIBE, b'')

    speaker = context.socket(zmq.PUSH)
    speaker.connect('tcp://{}:{}'.format(host, SPEAKER)) 

    camera = context.socket(zmq.SUB)
    camera.connect('tcp://{}:{}'.format(host, CAMERA))
    camera.setsockopt(zmq.SUBSCRIBE, b'')

    projector = context.socket(zmq.PUSH)
    projector.connect('tcp://{}:{}'.format(host, PROJECTOR)) 

    stateQ = context.socket(zmq.SUB)
    stateQ.connect('tcp://{}:{}'.format(host, STATE))
    stateQ.setsockopt(zmq.SUBSCRIBE, b'') 

    eventQ = context.socket(zmq.SUB)
    eventQ.connect('tcp://{}:{}'.format(host, EVENT))
    eventQ.setsockopt(zmq.SUBSCRIBE, b'') 

    snapshot = context.socket(zmq.REQ)
    snapshot.connect('tcp://{}:{}'.format(host, SNAPSHOT))
    snapshot.send(b'Send me the state, please')
    state = snapshot.recv_json()

    sender = context.socket(zmq.PUSH)

# Path: utils.py
# def filesize(filename):
#     return bytes2human(os.path.getsize(filename))
# 
# def send_array(socket, A, flags=0, copy=True, track=False):
#     """send a numpy array with metadata"""
#     md = dict(
#         dtype = str(A.dtype),
#         shape = A.shape,
#     )
#     socket.send_json(md, flags|zmq.SNDMORE)
#     return socket.send(A, flags, copy=copy, track=track)
# 
# def recv_array(socket, flags=0, copy=True, track=False):
#     """recv a numpy array"""
#     md = socket.recv_json(flags=flags)
#     msg = socket.recv(flags=flags, copy=copy, track=track)
#     buf = buffer(msg)
#     A = np.frombuffer(buf, dtype=md['dtype'])
#     return A.reshape(md['shape'])
# 
# Path: IO.py
# MIC = 5563
# 
# SPEAKER = 5564
# 
# CAMERA = 5561
# 
# PROJECTOR = 5562
# 
# STATE = 5565
# 
# SNAPSHOT= 5567
# 
# EVENT = 5568
# 
# EXTERNAL = 5566 # If you change this, you're out of luck.


# Path: AI.py
import time
import multiprocessing as mp
import cPickle as pickle
import zmq
import numpy as np
    import Oger
from uuid import uuid1
from collections import deque
from sklearn import preprocessing as pp
from utils import filesize, send_array, recv_array
from IO import MIC, SPEAKER, CAMERA, PROJECTOR, STATE, SNAPSHOT, EVENT, EXTERNAL


    context = zmq.Context()
    mic = context.socket(zmq.SUB)
    mic.connect('tcp://{}:{}'.format(host, MIC))
    mic.setsockopt(zmq.SUBSCRIBE, b'')

    speaker = context.socket(zmq.PUSH)
    speaker.connect('tcp://{}:{}'.format(host, SPEAKER)) 

    camera = context.socket(zmq.SUB)
    camera.connect('tcp://{}:{}'.format(host, CAMERA))
    camera.setsockopt(zmq.SUBSCRIBE, b'')

    projector = context.socket(zmq.PUSH)
    projector.connect('tcp://{}:{}'.format(host, PROJECTOR)) 

    stateQ = context.socket(zmq.SUB)
    stateQ.connect('tcp://{}:{}'.format(host, STATE))
    stateQ.setsockopt(zmq.SUBSCRIBE, b'') 

    eventQ = context.socket(zmq.SUB)
    eventQ.connect('tcp://{}:{}'.format(host, EVENT))
    eventQ.setsockopt(zmq.SUBSCRIBE, b'') 

    snapshot = context.socket(zmq.REQ)
    snapshot.connect('tcp://{}:{}'.format(host, SNAPSHOT))
    snapshot.send(b'Send me the state, please')
    state = snapshot.recv_json()

    sender = context.socket(zmq.PUSH)

    sender.connect('tcp://{}:{}'.format(host, EXTERNAL))