from plugin import Plugin
import socket
import os
from pyglui import ui

# This file has to be copied to pupil/capture_settings/plugins/

class Udp_Server(Plugin):
    """
    Udp Server broadcasts the gaze position
    over the network
    """

    def __init__(self, g_pool):
        super(Udp_Server, self).__init__(g_pool)
        self.order = .9
        self.ip = '192.168.0.2' #change to unity IP
        self.port = 5007        #change to unity port
        self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    def init_gui(self):
        if self.g_pool.app == 'capture':
            self.menu = ui.Growing_Menu("UDP Broadcast Server")
            self.g_pool.sidebar.append(self.menu)

        self.menu.append(ui.Button('Close', self.close))
        help_str = "UDP Message server"
        self.menu.append(ui.Info_Text(help_str))
    #self.menu.append(ui.Text_Input('address', self, setter=self.set_server, label='Address'))


    def deinit_gui(self):
        if self.menu and self.g_pool.app == 'capture':
            self.g_pool.sidebar.remove(self.menu)
            self.cleanup()
        self.menu = None

    def close(self): self.alive = False


    def get_init_dict(self): return {'address': self.address}


    def cleanup(self):
        """gets called when the plugin get terminated.
        This happens either voluntarily or forced.
        """
        self.deinit_gui()
        self.s.close() #close socket


    def update(self,frame,events):
        # pf = open('pupil_pos', 'a')
        # for p in events.get('pupil_positions',[]):
        #   try:
        #       pf.write(p + '\n')
        #   except:
        #       print "no pupil position"
        # msg = "Pupil\n"
        # for key,value in p.iteritems():
        #     if key not in self.exclude_list:
        #         msg +=key+":"+str(value)+'\n'
        # self.socket.send( msg )
        # gf = open('pupil_pos', 'a')
        for g in events.get('surface',[]):
            #self.conn.send(str(g['norm_pos']))
            #print g
            try:
                print g['gaze_on_srf']
                for x,y in g['gaze_on_srf']:
                    print x,y

                    lo,hi = 0,1
                    x = lo if x <= lo else hi if x>=hi else x
                    y = lo if y <= lo else hi if y>=hi else y

                    print "(" + str(x) + "," + str(y) + ")"
                    self.s.sendto((" + str(x) + "," + str(y) + "), (self.ip, self.port))
                    # gf.write(g + '\n')
            except Exception, e:
                print "not on surface:" + str(e)
    #for key, value in g.iteritems():
    #self.conn.send( key+":"+str(value)+'\n' )

    #for g in events.get('gaze_positions',[]):
    #   self.conn.send( g['norm_gaze'] )

    #self.conn.send("Hello, World")
    #conn.close()


@@ -2051,24 +2051,26 @@
             
+ #
  print g%5B'ga
@@ -2081,16 +2081,49 @@
 n_srf'%5D%0A
+                gaze_on_srf = %5B%5D%0A
         
@@ -2134,19 +2134,17 @@
     for 
-x,y
+i
  in g%5B'g
@@ -2181,55 +2181,850 @@
     
-print x,y%0A%0A                    lo,hi = 0,1%0A    
+gaze_on_srf.append(i%5B'norm_pos'%5D) %0A                %0A                # taking average of points in gaze_on_srf on a given event%0A                # TODO research incremental averaging to avoid computation speed issues (yet functional lambda it should be fast enough)%0A                x,y = tuple(map(lambda y: sum(y) / float(len(y)), zip(*gaze_on_srf)))%0A                %0A                # use the for loop if every single coordinate should be used%0A                # for x,y in g%5B'gaze_on_srf'%5D:%0A                print x,y%0A                %0A                # bound the x,y between 0 and 1 to get coordinates on the screen%0A                # TODO fix the %22Show Calibration%22 to understand the uncertainty areas more.%0A                # TODO 2 If you managed to fix %22Show Calibration%22, make a Pull Request to Pupil Labs -_-%22 %0A                lo,hi = 0,1%0A
     
@@ -3093,20 +3093,16 @@
         
-   
- 
 y = lo i
@@ -3148,28 +3148,24 @@
             
-    
 print %22(%22 + 
@@ -3204,28 +3204,24 @@
             
-  
-  
 self.s.sendt
@@ -3277,20 +3277,16 @@
 .port))%0A
-    
         

Update udpServer to accomm. json changes