|
0
|
1 |
/* |
|
8
|
2 |
* This file is part of the TraKERS\Middleware package. |
|
|
3 |
* |
|
|
4 |
* (c) IRI <http://www.iri.centrepompidou.fr/> |
|
|
5 |
* |
|
|
6 |
* For the full copyright and license information, please view the LICENSE_MIDDLEWARE |
|
|
7 |
* file that was distributed with this source code. |
|
|
8 |
*/ |
|
|
9 |
|
|
|
10 |
/* |
|
3
|
11 |
* Projet : TraKERS |
|
0
|
12 |
* Module : MIDDLEWARE |
|
|
13 |
* Sous-Module : Communication |
|
|
14 |
* Classe : Server |
|
|
15 |
* |
|
|
16 |
* Auteur : alexandre.bastien@iri.centrepompidou.fr |
|
|
17 |
* |
|
|
18 |
* Fonctionnalités : Reçoit des notifications du module sous-module Tracking. |
|
|
19 |
* Traduit les notifications sous forme de messages OSC et les envoie au Front Atelier. |
|
|
20 |
* Forme des messages : |
|
|
21 |
* - Notification de main dans le champ de recherche : Point3D indiquant la position de la main dans l'espace. |
|
|
22 |
*/ |
|
|
23 |
|
|
|
24 |
using System; |
|
|
25 |
using System.Collections.Generic; |
|
|
26 |
using System.Linq; |
|
|
27 |
using System.Text; |
|
|
28 |
using System.IO; |
|
|
29 |
using System.Net; |
|
|
30 |
using System.Net.Sockets; |
|
|
31 |
using System.Threading; |
|
|
32 |
using Tuio; |
|
|
33 |
using System.Windows; |
|
|
34 |
using Microsoft.Kinect; |
|
|
35 |
using Trakers.Tracking; |
|
|
36 |
using System.Windows.Media.Media3D; |
|
|
37 |
using Trakers.Tracking.Events; |
|
3
|
38 |
using System.Timers; |
|
5
|
39 |
using Trakers.Debug; |
|
|
40 |
using System.Resources; |
|
|
41 |
using System.Reflection; |
|
0
|
42 |
|
|
|
43 |
namespace Trakers.Communication |
|
|
44 |
{ |
|
|
45 |
public class Server |
|
|
46 |
{ |
|
|
47 |
//Serveur TUIO, provenant de la DLL TuioServer créé par Bespoke. |
|
|
48 |
private TuioServer server; |
|
5
|
49 |
//Affichage de debug. |
|
|
50 |
private DebugWindow debug; |
|
0
|
51 |
|
|
|
52 |
//Permet de savoir si un curseur pour la main gauche/droite a été créé. |
|
|
53 |
private bool leftHandCursorCreated; |
|
|
54 |
private bool rightHandCursorCreated; |
|
3
|
55 |
private bool messageCreated; |
|
|
56 |
private bool gestureLocked; |
|
5
|
57 |
//Intervalle minimum entre les gestures. |
|
3
|
58 |
private int timerElapsing; |
|
5
|
59 |
//Timer. |
|
|
60 |
private System.Timers.Timer _timer; |
|
|
61 |
//Gestionnaire de ressources. |
|
|
62 |
private ResourceManager rm; |
|
0
|
63 |
|
|
|
64 |
/* |
|
|
65 |
* Constructeur : On initialise le serveur avec une adresse et un port, au début les curseurs |
|
|
66 |
* ne sont pas créés et on indique au ThreadPool une fonction de callback de manière à vérifier |
|
|
67 |
* s'il reçoit des notifications. |
|
|
68 |
*/ |
|
5
|
69 |
public Server(String host, int port, int _timerElapsing, DebugWindow _debug) |
|
0
|
70 |
{ |
|
5
|
71 |
debug = _debug; |
|
|
72 |
rm = new ResourceManager("Trakers.Properties.resources", Assembly.GetExecutingAssembly()); |
|
3
|
73 |
//Au départ, aucune main n'est dans le champ de recherche et aucune gesture n'est détectée. |
|
0
|
74 |
leftHandCursorCreated = false; |
|
|
75 |
rightHandCursorCreated = false; |
|
3
|
76 |
messageCreated = false; |
|
|
77 |
gestureLocked = false; |
|
|
78 |
|
|
|
79 |
timerElapsing = _timerElapsing; |
|
|
80 |
|
|
5
|
81 |
try |
|
|
82 |
{ |
|
|
83 |
//On démarre le serveur TUIO. |
|
|
84 |
server = new TuioServer(host, port); |
|
|
85 |
//On initialise le threadPool (appelé toutes les N ms). |
|
|
86 |
ThreadPool.QueueUserWorkItem(ThreadPoolCallback); |
|
|
87 |
|
|
|
88 |
//On instancie le timer à N ms. |
|
|
89 |
_timer = new System.Timers.Timer(timerElapsing); |
|
|
90 |
//Dès que le timer est expiré, on appelle _timer_Elapsed. |
|
|
91 |
_timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed); |
|
|
92 |
} |
|
|
93 |
catch (Exception) |
|
|
94 |
{ |
|
|
95 |
debug.ExceptionLbl.Content = rm.GetString("serverCantStart"); |
|
|
96 |
} |
|
0
|
97 |
} |
|
|
98 |
|
|
|
99 |
/* |
|
|
100 |
* Getter du serveur. |
|
|
101 |
*/ |
|
|
102 |
public TuioServer getServer() |
|
|
103 |
{ |
|
|
104 |
return server; |
|
|
105 |
} |
|
|
106 |
|
|
|
107 |
/* |
|
3
|
108 |
* Méthode appelée à l'expiration du timer. |
|
|
109 |
*/ |
|
|
110 |
public void _timer_Elapsed(object sender, ElapsedEventArgs e) |
|
|
111 |
{ |
|
|
112 |
//On débloque la détection de gesture. |
|
|
113 |
gestureLocked = false; |
|
|
114 |
//On arrête le timer. |
|
|
115 |
_timer.Stop(); |
|
|
116 |
} |
|
|
117 |
|
|
|
118 |
/* |
|
0
|
119 |
* Méthode appelée lors d'une notification de type : main gauche entrée dans le champ. |
|
|
120 |
*/ |
|
|
121 |
public void LeftHandTracked(object sender, LeftHandTrackedEventArgs e) |
|
|
122 |
{ |
|
|
123 |
//Si le curseur de la main gauche n'est pas créé, alors on le crée. |
|
|
124 |
if (!leftHandCursorCreated) |
|
|
125 |
{ |
|
|
126 |
server.AddTuioCursor(0, SkeletonPointToPoint3D(e.handJoint.Position)); |
|
|
127 |
leftHandCursorCreated = true; |
|
|
128 |
} |
|
|
129 |
//S'il existe, on le met simplement à jour. |
|
|
130 |
else |
|
|
131 |
{ |
|
|
132 |
server.UpdateTuioCursor(0, SkeletonPointToPoint3D(e.handJoint.Position)); |
|
|
133 |
} |
|
|
134 |
} |
|
|
135 |
|
|
|
136 |
/* |
|
|
137 |
* Méthode appelée lors d'une notification de type : main droite entrée dans le champ. |
|
|
138 |
*/ |
|
|
139 |
public void RightHandTracked(object sender, RightHandTrackedEventArgs e) |
|
|
140 |
{ |
|
|
141 |
//Si le curseur de la main droite n'est pas créé, alors on le crée. |
|
|
142 |
if (!rightHandCursorCreated) |
|
|
143 |
{ |
|
|
144 |
server.AddTuioCursor(1, SkeletonPointToPoint3D(e.handJoint.Position)); |
|
|
145 |
rightHandCursorCreated = true; |
|
|
146 |
} |
|
|
147 |
//S'il existe, on le met simplement à jour. |
|
|
148 |
else |
|
|
149 |
{ |
|
|
150 |
server.UpdateTuioCursor(1, SkeletonPointToPoint3D(e.handJoint.Position)); |
|
|
151 |
} |
|
|
152 |
} |
|
|
153 |
|
|
|
154 |
/* |
|
|
155 |
* Méthode appelée lors d'une notification de type : main gauche sortie du champ. |
|
|
156 |
*/ |
|
|
157 |
public void LeftHandQuit(object sender, LeftHandQuitEventArgs e) |
|
|
158 |
{ |
|
|
159 |
//Si le curseur de la main gauche existe, alors on le supprime. |
|
|
160 |
if (leftHandCursorCreated) |
|
|
161 |
{ |
|
|
162 |
server.DeleteTuioCursor(0); |
|
|
163 |
leftHandCursorCreated = false; |
|
|
164 |
} |
|
|
165 |
} |
|
|
166 |
|
|
|
167 |
/* |
|
|
168 |
* Méthode appelée lors d'une notification de type : main droite sortie du champ. |
|
|
169 |
*/ |
|
|
170 |
public void RightHandQuit(object sender, RightHandQuitEventArgs e) |
|
|
171 |
{ |
|
|
172 |
//Si le curseur de la main droite existe, alors on le supprime. |
|
|
173 |
if (rightHandCursorCreated) |
|
|
174 |
{ |
|
|
175 |
server.DeleteTuioCursor(1); |
|
|
176 |
rightHandCursorCreated = false; |
|
|
177 |
} |
|
|
178 |
} |
|
|
179 |
|
|
|
180 |
/* |
|
3
|
181 |
* Méthode appelée lors d'une notification de type : l'utilisateur fait un swipe. |
|
|
182 |
*/ |
|
|
183 |
public void Swipe(object sender, SwipeEventArgs e) |
|
|
184 |
{ |
|
|
185 |
if (e.direction == Tracking.Gestures.SwipeDetector.Direction.LEFT) |
|
|
186 |
GesturePerformed("SWIPE LEFT"); |
|
|
187 |
else if (e.direction == Tracking.Gestures.SwipeDetector.Direction.RIGHT) |
|
|
188 |
GesturePerformed("SWIPE RIGHT"); |
|
|
189 |
} |
|
|
190 |
|
|
|
191 |
/* |
|
|
192 |
* Méthode appelée lors d'une notification de type : l'utilisateur fait un push/pull. |
|
|
193 |
*/ |
|
|
194 |
public void Pull(object sender, PushEventArgs e) |
|
|
195 |
{ |
|
|
196 |
if(e.hand == Tracking.Gestures.PushDetector.Hand.NONE) |
|
|
197 |
return; |
|
|
198 |
|
|
|
199 |
String pushPull = "", hand = ""; |
|
|
200 |
if (e.direction == Tracking.Gestures.PushDetector.Direction.PUSH) |
|
|
201 |
pushPull = "PUSH"; |
|
|
202 |
else |
|
|
203 |
pushPull = "PULL"; |
|
|
204 |
|
|
|
205 |
if (e.hand == Tracking.Gestures.PushDetector.Hand.LEFT) |
|
|
206 |
hand = "LEFT"; |
|
|
207 |
else if (e.hand == Tracking.Gestures.PushDetector.Hand.RIGHT) |
|
|
208 |
hand = "RIGHT"; |
|
|
209 |
else |
|
|
210 |
hand = "BOTH"; |
|
|
211 |
|
|
|
212 |
GesturePerformed(pushPull + "-" + hand); |
|
|
213 |
} |
|
|
214 |
|
|
|
215 |
/* |
|
|
216 |
* Méthode appelée lorsqu'une gesture a été détectée et que l'événement approprié a été lancé. |
|
|
217 |
*/ |
|
|
218 |
public void GesturePerformed(String code) |
|
|
219 |
{ |
|
|
220 |
//Si une gesture a été effectuée, on bloque un certain temps. |
|
|
221 |
if (!gestureLocked) |
|
|
222 |
{ |
|
|
223 |
gestureLocked = true; |
|
|
224 |
|
|
|
225 |
//On crée un message contenant le code à envoyer. |
|
|
226 |
if (!messageCreated) |
|
|
227 |
{ |
|
|
228 |
messageCreated = true; |
|
5
|
229 |
server.AddTuioString(2, code); |
|
3
|
230 |
//On démarre le timer. |
|
|
231 |
_timer.Start(); |
|
|
232 |
} |
|
|
233 |
} |
|
|
234 |
} |
|
|
235 |
|
|
|
236 |
/* |
|
0
|
237 |
* Permet de convertir un point de position de noeud en Point3D. |
|
|
238 |
*/ |
|
|
239 |
private Point3D SkeletonPointToPoint3D(SkeletonPoint p) |
|
|
240 |
{ |
|
|
241 |
return new Point3D((double)p.X, (double)p.Y, (double)p.Z); |
|
|
242 |
} |
|
|
243 |
|
|
|
244 |
/* |
|
|
245 |
* Méthode de callback vérifiant toutes les 25 ms les nouvelles notifications. |
|
|
246 |
* Il est à noter que si le temps de rafraîchissement des trop rapide, les messages n'ont pas |
|
|
247 |
* le temps d'être envoyés. |
|
|
248 |
*/ |
|
|
249 |
private void ThreadPoolCallback(Object threadContext) |
|
|
250 |
{ |
|
|
251 |
while (true) |
|
|
252 |
{ |
|
|
253 |
//On initialise le message OSC. |
|
|
254 |
server.InitFrame(); |
|
|
255 |
//On l'envoie au client (au host et au port spécifiés dans le constructeur). |
|
|
256 |
server.CommitFrame(); |
|
|
257 |
//On attend 25 ms. |
|
|
258 |
Thread.Sleep(25); |
|
3
|
259 |
|
|
|
260 |
//Si une gesture a été effectuée et que le délai d'attente est expiré. |
|
|
261 |
if (messageCreated && !gestureLocked) |
|
|
262 |
{ |
|
|
263 |
//On débloque la détection de gesture et on supprime l'objet envoyant les messages OSC de gesture. |
|
|
264 |
messageCreated = false; |
|
5
|
265 |
server.DeleteTuioString(2); |
|
3
|
266 |
} |
|
0
|
267 |
} |
|
|
268 |
} |
|
|
269 |
} |
|
|
270 |
} |