middleware/src/Communication/Server.cs
changeset 3 92f19af39024
parent 0 6fefd4afe506
child 5 d40f84d77db4
equal deleted inserted replaced
2:11234537653b 3:92f19af39024
     1 /*
     1 /*
     2  * Projet : KINECT PROJECTS
     2  * Projet : TraKERS
     3  * Module : MIDDLEWARE
     3  * Module : MIDDLEWARE
     4  * Sous-Module : Communication
     4  * Sous-Module : Communication
     5  * Classe : Server
     5  * Classe : Server
     6  * 
     6  * 
     7  * Auteur : alexandre.bastien@iri.centrepompidou.fr
     7  * Auteur : alexandre.bastien@iri.centrepompidou.fr
    24 using System.Windows;
    24 using System.Windows;
    25 using Microsoft.Kinect;
    25 using Microsoft.Kinect;
    26 using Trakers.Tracking;
    26 using Trakers.Tracking;
    27 using System.Windows.Media.Media3D;
    27 using System.Windows.Media.Media3D;
    28 using Trakers.Tracking.Events;
    28 using Trakers.Tracking.Events;
       
    29 using System.Timers;
    29 
    30 
    30 namespace Trakers.Communication
    31 namespace Trakers.Communication
    31 {
    32 {
    32     public class Server
    33     public class Server
    33     {
    34     {
    35         private TuioServer server;
    36         private TuioServer server;
    36 
    37 
    37         //Permet de savoir si un curseur pour la main gauche/droite a été créé.
    38         //Permet de savoir si un curseur pour la main gauche/droite a été créé.
    38         private bool leftHandCursorCreated;
    39         private bool leftHandCursorCreated;
    39         private bool rightHandCursorCreated;
    40         private bool rightHandCursorCreated;
       
    41         private bool messageCreated;
       
    42         private bool gestureLocked;
       
    43 
       
    44         private int timerElapsing;
       
    45 
       
    46         System.Timers.Timer _timer;
    40 
    47 
    41         /*
    48         /*
    42         * Constructeur : On initialise le serveur avec une adresse et un port, au début les curseurs
    49         * Constructeur : On initialise le serveur avec une adresse et un port, au début les curseurs
    43         * ne sont pas créés et on indique au ThreadPool une fonction de callback de manière à vérifier
    50         * ne sont pas créés et on indique au ThreadPool une fonction de callback de manière à vérifier
    44         * s'il reçoit des notifications.
    51         * s'il reçoit des notifications.
    45         */
    52         */
    46         public Server(String host, int port)
    53         public Server(String host, int port, int _timerElapsing)
    47         {
    54         {
       
    55             //On démarre le serveur TUIO.
    48             server = new TuioServer(host, port);
    56             server = new TuioServer(host, port);
       
    57             //On initialise le threadPool (appelé toutes les N ms).
       
    58             ThreadPool.QueueUserWorkItem(ThreadPoolCallback);
       
    59 
       
    60             //Au départ, aucune main n'est dans le champ de recherche et aucune gesture n'est détectée.
    49             leftHandCursorCreated = false;
    61             leftHandCursorCreated = false;
    50             rightHandCursorCreated = false;
    62             rightHandCursorCreated = false;
    51             ThreadPool.QueueUserWorkItem(ThreadPoolCallback);
    63             messageCreated = false;
       
    64             gestureLocked = false;
       
    65 
       
    66             timerElapsing = _timerElapsing;
       
    67 
       
    68             //On instancie le timer à N ms.
       
    69             _timer = new System.Timers.Timer(timerElapsing);
       
    70             //Dès que le timer est expiré, on appelle _timer_Elapsed.
       
    71             _timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
    52         }
    72         }
    53 
    73 
    54         /*
    74         /*
    55         * Getter du serveur.
    75         * Getter du serveur.
    56         */
    76         */
    58         {
    78         {
    59             return server;
    79             return server;
    60         }
    80         }
    61 
    81 
    62         /*
    82         /*
       
    83          * Méthode appelée à l'expiration du timer.
       
    84          */
       
    85         public void _timer_Elapsed(object sender, ElapsedEventArgs e)
       
    86         {
       
    87             //On débloque la détection de gesture.
       
    88             gestureLocked = false;
       
    89             //On arrête le timer.
       
    90             _timer.Stop();
       
    91         }
       
    92 
       
    93         /*
    63         * Méthode appelée lors d'une notification de type : main gauche entrée dans le champ.
    94         * Méthode appelée lors d'une notification de type : main gauche entrée dans le champ.
    64         */
    95         */
    65         public void LeftHandTracked(object sender, LeftHandTrackedEventArgs e)
    96         public void LeftHandTracked(object sender, LeftHandTrackedEventArgs e)
    66         {
    97         {
       
    98             return;
    67             //Si le curseur de la main gauche n'est pas créé, alors on le crée.
    99             //Si le curseur de la main gauche n'est pas créé, alors on le crée.
    68             if (!leftHandCursorCreated)
   100             if (!leftHandCursorCreated)
    69             {
   101             {
    70                 server.AddTuioCursor(0, SkeletonPointToPoint3D(e.handJoint.Position));
   102                 server.AddTuioCursor(0, SkeletonPointToPoint3D(e.handJoint.Position));
    71                 leftHandCursorCreated = true;
   103                 leftHandCursorCreated = true;
    80         /*
   112         /*
    81         * Méthode appelée lors d'une notification de type : main droite entrée dans le champ.
   113         * Méthode appelée lors d'une notification de type : main droite entrée dans le champ.
    82         */
   114         */
    83         public void RightHandTracked(object sender, RightHandTrackedEventArgs e)
   115         public void RightHandTracked(object sender, RightHandTrackedEventArgs e)
    84         {
   116         {
       
   117             return;
    85             //Si le curseur de la main droite n'est pas créé, alors on le crée.
   118             //Si le curseur de la main droite n'est pas créé, alors on le crée.
    86             if (!rightHandCursorCreated)
   119             if (!rightHandCursorCreated)
    87             {
   120             {
    88                 server.AddTuioCursor(1, SkeletonPointToPoint3D(e.handJoint.Position));
   121                 server.AddTuioCursor(1, SkeletonPointToPoint3D(e.handJoint.Position));
       
   122                 //server.AddTuioString(1, "BOO");
    89                 rightHandCursorCreated = true;
   123                 rightHandCursorCreated = true;
    90             }
   124             }
    91             //S'il existe, on le met simplement à jour.
   125             //S'il existe, on le met simplement à jour.
    92             else
   126             else
    93             {
   127             {
    94                 server.UpdateTuioCursor(1, SkeletonPointToPoint3D(e.handJoint.Position));
   128                 server.UpdateTuioCursor(1, SkeletonPointToPoint3D(e.handJoint.Position));
       
   129                 //server.UpdateTuioString(1, "BOO");
    95             }
   130             }
    96         }
   131         }
    97 
   132 
    98         /*
   133         /*
    99         * Méthode appelée lors d'une notification de type : main gauche sortie du champ.
   134         * Méthode appelée lors d'une notification de type : main gauche sortie du champ.
   100         */
   135         */
   101         public void LeftHandQuit(object sender, LeftHandQuitEventArgs e)
   136         public void LeftHandQuit(object sender, LeftHandQuitEventArgs e)
   102         {
   137         {
       
   138             return;
   103             //Si le curseur de la main gauche existe, alors on le supprime.
   139             //Si le curseur de la main gauche existe, alors on le supprime.
   104             if (leftHandCursorCreated)
   140             if (leftHandCursorCreated)
   105             {
   141             {
   106                 server.DeleteTuioCursor(0);
   142                 server.DeleteTuioCursor(0);
   107                 leftHandCursorCreated = false;
   143                 leftHandCursorCreated = false;
   111         /*
   147         /*
   112         * Méthode appelée lors d'une notification de type : main droite sortie du champ.
   148         * Méthode appelée lors d'une notification de type : main droite sortie du champ.
   113         */
   149         */
   114         public void RightHandQuit(object sender, RightHandQuitEventArgs e)
   150         public void RightHandQuit(object sender, RightHandQuitEventArgs e)
   115         {
   151         {
       
   152             return;
   116             //Si le curseur de la main droite existe, alors on le supprime.
   153             //Si le curseur de la main droite existe, alors on le supprime.
   117             if (rightHandCursorCreated)
   154             if (rightHandCursorCreated)
   118             {
   155             {
   119                 server.DeleteTuioCursor(1);
   156                 server.DeleteTuioCursor(1);
       
   157                 //server.DeleteTuioString(1);
   120                 rightHandCursorCreated = false;
   158                 rightHandCursorCreated = false;
       
   159             }
       
   160         }
       
   161 
       
   162         /*
       
   163         * Méthode appelée lors d'une notification de type : l'utilisateur fait un swipe.
       
   164         */
       
   165         public void Swipe(object sender, SwipeEventArgs e)
       
   166         {
       
   167             if (e.direction == Tracking.Gestures.SwipeDetector.Direction.LEFT)
       
   168                 GesturePerformed("SWIPE LEFT");
       
   169             else if (e.direction == Tracking.Gestures.SwipeDetector.Direction.RIGHT)
       
   170                 GesturePerformed("SWIPE RIGHT");
       
   171         }
       
   172 
       
   173         /*
       
   174         * Méthode appelée lors d'une notification de type : l'utilisateur fait un push/pull.
       
   175         */
       
   176         public void Pull(object sender, PushEventArgs e)
       
   177         {
       
   178             if(e.hand == Tracking.Gestures.PushDetector.Hand.NONE)
       
   179                 return;
       
   180 
       
   181             String pushPull = "", hand = "";
       
   182             if (e.direction == Tracking.Gestures.PushDetector.Direction.PUSH)
       
   183                 pushPull = "PUSH";
       
   184             else
       
   185                 pushPull = "PULL";
       
   186 
       
   187             if (e.hand == Tracking.Gestures.PushDetector.Hand.LEFT)
       
   188                 hand = "LEFT";
       
   189             else if (e.hand == Tracking.Gestures.PushDetector.Hand.RIGHT)
       
   190                 hand = "RIGHT";
       
   191             else
       
   192                 hand = "BOTH";
       
   193 
       
   194             Console.Out.WriteLine(pushPull + "-" + hand);
       
   195 
       
   196             GesturePerformed(pushPull + "-" + hand);
       
   197         }
       
   198 
       
   199         /*
       
   200         * Méthode appelée lorsqu'une gesture a été détectée et que l'événement approprié a été lancé.
       
   201         */
       
   202         public void GesturePerformed(String code)
       
   203         {
       
   204             //Si une gesture a été effectuée, on bloque un certain temps.
       
   205             if (!gestureLocked)
       
   206             {
       
   207                 gestureLocked = true;
       
   208                 
       
   209                 //On crée un message contenant le code à envoyer.
       
   210                 if (!messageCreated)
       
   211                 {
       
   212                     messageCreated = true;
       
   213                     server.AddTuioString(1, code);
       
   214                     //On démarre le timer.
       
   215                     _timer.Start();
       
   216                 }
   121             }
   217             }
   122         }
   218         }
   123 
   219 
   124         /*
   220         /*
   125         * Permet de convertir un point de position de noeud en Point3D.
   221         * Permet de convertir un point de position de noeud en Point3D.
   142                 server.InitFrame();
   238                 server.InitFrame();
   143                 //On l'envoie au client (au host et au port spécifiés dans le constructeur).
   239                 //On l'envoie au client (au host et au port spécifiés dans le constructeur).
   144                 server.CommitFrame();
   240                 server.CommitFrame();
   145                 //On attend 25 ms.
   241                 //On attend 25 ms.
   146                 Thread.Sleep(25);
   242                 Thread.Sleep(25);
       
   243 
       
   244                 //Si une gesture a été effectuée et que le délai d'attente est expiré.
       
   245                 if (messageCreated && !gestureLocked)
       
   246                 {
       
   247                     //On débloque la détection de gesture et on supprime l'objet envoyant les messages OSC de gesture.
       
   248                     messageCreated = false;
       
   249                     server.DeleteTuioString(1);
       
   250                 }
   147             }
   251             }
   148         }
   252         }
   149     }
   253     }
   150 }
   254 }