# Path: lib/devices.py
# RABBITMQ_ADDRESS = 'localhost'
# 
# Path: lib/utils.py
# def BufferToMatrix(jsonDump, output_type=None):
#     """
#     After retrieving the encoded json from the message queue buffer, we need to translate the 3 element json
#     back into its original form.
#     The 3 elements are:
#     0: use the 0th element to cast correct type
#     1: the base64 encoded data just needs to be base64 decoded
#     2: use the 2nd element to set correct dimensions with reshape()
# 
#     borrowed from: http://stackoverflow.com/questions/13461945/dumping-2d-python-array-with-json
#     :param jsonDump:
#     :return:
#     """
#     # if incoming json has not yet been json decoded, do it
#     if isinstance(jsonDump, str):
#         jsonDump = json.loads(jsonDump)
# 
#     # handle type
#     dtype_string = jsonDump[0]
#     if "DTYPE_" in dtype_string:
#         # if the type
#         dtype = np.dtype(eval(dtype_string))
#     else:
#         dtype = np.dtype(dtype_string)
# 
#     # reconstitute the data, using cast to decoded type from above
#     matrix_array = np.frombuffer(base64.decodestring(jsonDump[1]),dtype)
#     if len(jsonDump) > 2:
#         matrix_array = matrix_array.reshape(jsonDump[2])
# 
#     if output_type is 'list':
#         matrix_list = []
#         vector = []
#         [rows,cols] = matrix_array.shape
#         if dtype_string == "DTYPE_COORD":
#             for row in np.arange(rows):
#                 for column in np.arange(cols):
#                     point = [matrix_array[row][column]["x"],matrix_array[row][column]["y"]]
#                     vector.append(point)
#                 matrix_list.append(vector)
#                 vector = []
#         else:
#             for row in np.arange(rows):
#                 for column in np.arange(cols):
#                     vector.append(matrix_array[row][column])
#                 matrix_list.append(vector)
#                 vector = []
# 
#         return matrix_list
# 
#     return matrix_array
# 
# def ListConfOutputMetrics(conf, prefix=None):
#     """
#     Loop through all of the available output metrics, among all of the modules defined in the conf .yml file.
#     If an option prefix is specified, this will limit response sent to the UI to only include those metrics w/ the prefix
#     """
#     metrics = []
#     print conf['modules']
#     for module in conf['modules']:
#         if 'outputs' in module and 'data' in module['outputs']:
#             if 'message_queues' in module['outputs']['data']:
#                 if type(module['outputs']['data']['message_queues']) == str:
#                     if prefix is None or module['outputs']['data']['message_queues'].startswith(prefix):
#                         metrics.append(module['outputs']['data']['message_queues'])
#                 elif type(module['outputs']['data']['message_queues']) == list:
#                     for metric in module['outputs']['data']['message_queues']:
#                         if prefix is None or metric.startswith(prefix):
#                             metrics.append(metric)
# 
#     return metrics


import json
import logging
from sockjs.tornado.conn import SockJSConnection
from lib.devices import RABBITMQ_ADDRESS
from TornadoSubscriber import TornadoSubscriber
from lib.utils import BufferToMatrix, ListConfOutputMetrics
from lib.constants import *
        def send_probe(body):
            #logging.debug("GOT [" + metric_name + "]: " + body)
            #print "GOT [" + metric_name + "]: [" + data_type + "]"
            if data_type == MESSAGE_TYPE_MATRIX:
                """
                MESSAGE_TYPE_MATRIX output is a base64 encoded blob which needs to be decoded
                """
                buffer_content = json.loads(body)
                for record in buffer_content:
                    message = BufferToMatrix(record, output_type="list")
                    self.send(message,True)
            else:
                """
                MESSAGE_TYPE_TIME_SAMPLE output is a dict object, with one element for each channel + one for timestamp
                gotcha: Pika tends to make all keys in the dict utf8
                """

                buffer_content = json.loads(body)
                for record in buffer_content:
                    record["metric"] = metric_name
                    self.send(json.dumps(record))

        return send_probe


    def on_open(self, info):
        logging.info("Got a new connection...")
        # debug
        print "[Tornado Server: ConnectionPlot] opened websocket connection"
        self.clients.add(self)

# Path: lib/devices.py
# RABBITMQ_ADDRESS = 'localhost'
# 
# Path: lib/utils.py
# def BufferToMatrix(jsonDump, output_type=None):
#     """
#     After retrieving the encoded json from the message queue buffer, we need to translate the 3 element json
#     back into its original form.
#     The 3 elements are:
#     0: use the 0th element to cast correct type
#     1: the base64 encoded data just needs to be base64 decoded
#     2: use the 2nd element to set correct dimensions with reshape()
# 
#     borrowed from: http://stackoverflow.com/questions/13461945/dumping-2d-python-array-with-json
#     :param jsonDump:
#     :return:
#     """
#     # if incoming json has not yet been json decoded, do it
#     if isinstance(jsonDump, str):
#         jsonDump = json.loads(jsonDump)
# 
#     # handle type
#     dtype_string = jsonDump[0]
#     if "DTYPE_" in dtype_string:
#         # if the type
#         dtype = np.dtype(eval(dtype_string))
#     else:
#         dtype = np.dtype(dtype_string)
# 
#     # reconstitute the data, using cast to decoded type from above
#     matrix_array = np.frombuffer(base64.decodestring(jsonDump[1]),dtype)
#     if len(jsonDump) > 2:
#         matrix_array = matrix_array.reshape(jsonDump[2])
# 
#     if output_type is 'list':
#         matrix_list = []
#         vector = []
#         [rows,cols] = matrix_array.shape
#         if dtype_string == "DTYPE_COORD":
#             for row in np.arange(rows):
#                 for column in np.arange(cols):
#                     point = [matrix_array[row][column]["x"],matrix_array[row][column]["y"]]
#                     vector.append(point)
#                 matrix_list.append(vector)
#                 vector = []
#         else:
#             for row in np.arange(rows):
#                 for column in np.arange(cols):
#                     vector.append(matrix_array[row][column])
#                 matrix_list.append(vector)
#                 vector = []
# 
#         return matrix_list
# 
#     return matrix_array
# 
# def ListConfOutputMetrics(conf, prefix=None):
#     """
#     Loop through all of the available output metrics, among all of the modules defined in the conf .yml file.
#     If an option prefix is specified, this will limit response sent to the UI to only include those metrics w/ the prefix
#     """
#     metrics = []
#     print conf['modules']
#     for module in conf['modules']:
#         if 'outputs' in module and 'data' in module['outputs']:
#             if 'message_queues' in module['outputs']['data']:
#                 if type(module['outputs']['data']['message_queues']) == str:
#                     if prefix is None or module['outputs']['data']['message_queues'].startswith(prefix):
#                         metrics.append(module['outputs']['data']['message_queues'])
#                 elif type(module['outputs']['data']['message_queues']) == list:
#                     for metric in module['outputs']['data']['message_queues']:
#                         if prefix is None or metric.startswith(prefix):
#                             metrics.append(metric)
# 
#     return metrics


# Path: Viz/Server/Connections.py
import json
import logging
from sockjs.tornado.conn import SockJSConnection
from lib.devices import RABBITMQ_ADDRESS
from TornadoSubscriber import TornadoSubscriber
from lib.utils import BufferToMatrix, ListConfOutputMetrics
from lib.constants import *

        def send_probe(body):
            #logging.debug("GOT [" + metric_name + "]: " + body)
            #print "GOT [" + metric_name + "]: [" + data_type + "]"
            if data_type == MESSAGE_TYPE_MATRIX:
                """
                MESSAGE_TYPE_MATRIX output is a base64 encoded blob which needs to be decoded
                """
                buffer_content = json.loads(body)
                for record in buffer_content:
                    message = BufferToMatrix(record, output_type="list")
                    self.send(message,True)
            else:
                """
                MESSAGE_TYPE_TIME_SAMPLE output is a dict object, with one element for each channel + one for timestamp
                gotcha: Pika tends to make all keys in the dict utf8
                """

                buffer_content = json.loads(body)
                for record in buffer_content:
                    record["metric"] = metric_name
                    self.send(json.dumps(record))

        return send_probe


    def on_open(self, info):
        logging.info("Got a new connection...")
        # debug
        print "[Tornado Server: ConnectionPlot] opened websocket connection"
        self.clients.add(self)

        metrics = ListConfOutputMetrics(self.conf, prefix="viz_")