import os, time, threading

import tornado.ioloop
import tornado.web
import tornado.httpserver
import redis

# include tornadio for socketio capabilities
from tornadio2 import SocketConnection, TornadioRouter, event

from dl.communicator import DottedLandscapeCommunicator
from dl.analytics import Analytics
from dl.animationstorage import AnimationStorage
from dl.animationplayer import AnimationPlayer

DL_COMMUNICATOR = DottedLandscapeCommunicator()
# we define the panel here
DL_COMMUNICATOR.define_panel(8, 8, 3)
WEB_CLIENTS = []
SOCKET_IO_CONNECTIONS = []
ANALYTICS = Analytics()
ANIMATION_STORAGE = AnimationStorage()
ANIMATION_PLAYER = AnimationPlayer(DL_COMMUNICATOR)


# Helper functions
def create_user_fingerprint(req):
    pass


# Template handlers

    def send_partial(self, x, y, r, g, b):
        liblo.send(self.__target, "/dl/plot", x, y, r, g, b)


if __name__ == '__main__':
    # foo = 'a'
    # while foo != 'q':
    #     foo = raw_input('> ')
    #     sc.write([int(foo) for i in xrange(0, 8)])
    # sc.close()


    osc = OscRouter()
    dlc = DottedLandscapeCommunicator()
    dlc.connect('127.0.0.1', 2323, accept_partial_frames=True)

    done = False
    while not done:

        # get any incoming data from the DL server
        data = dlc.check_for_data()
        if not data:
            continue
        headers, payload = data

        if headers[0] == dlc.dl_MAGIC_FRAME_PARTIAL:
            print "Osc got a partial frame!", len(payload)
            osc.send_partial(*payload)
import time, sys
import pygame

from dl.communicator import DottedLandscapeCommunicator

led_panel_initialized = False
clock, screen = None, None
leds_x, leds_y, amount_of_channels = 0, 0, 0
circle_radius = 6
circle_spacing = 10
window_margins = (30, 30)

print "Panel: waiting for connections to determine panel size" 
dlc = DottedLandscapeCommunicator()
dlc.connect('127.0.0.1', 2323)


def on_receive(data):
    global led_panel_initialized, leds_x, leds_y, dlc
    h, w = dlc.panel_height, dlc.panel_width
    if not led_panel_initialized or leds_x != w or leds_y != h:
        global screen, clock, amount_of_channels
        width, height = w, h
        amount_of_channels = dlc.channels
        print "Panel: received new width, height", w, h, "on %s channels" % amount_of_channels
        
        pygame.init()
        # get the panel size and set the window width accordingly
        leds_x, leds_y = w, h
        width, height = w * circle_radius + w * circle_spacing + 2 * window_margins[0], \
                        h * circle_radius + h * circle_spacing + 2 * window_margins[1]
def select_random_visualization():
    visualization = random.choice([knight_rider, random_colors, game_of_life,
                                   random_colors, game_of_life, game_of_life,
                                   message, message])
    print "picked random viz:", visualization.__name__
    vars = {'frame_duration': 0.35, 'frame_counter': 0}
    vars['color'] = get_random_color()
    if visualization == random_colors: #  and sum(vars['color']) > 255:
        vars['color'] = None

    return visualization, vars


if __name__ == '__main__':

    dlc = DottedLandscapeCommunicator()
    dlc.connect('127.0.0.1', 2323)
    last_packet_received = 0
    done = False
    idle_time = 15
    last_frame = None
    now = time.time()
    vars = {'frame_duration': 0.5, 'frame_counter': 0} # will be updated by the visualization algorithms
    visualization, vars = select_random_visualization()

    while not done:
        # get any incoming data from the DL server
        headers, payload = dlc.check_for_data()

        # panel is up, but there is nothing happening
        if not payload and dlc.panel_width: