server.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. const express = require('express');
  2. const dgram = require('dgram');
  3. const oscParser = require('./osc-parser');
  4. const bodyParser = require('body-parser');
  5. const { Bundle, Client } = require('node-osc');
  6. const path = require('path');
  7. const config = require('../frontend/assets/config.json');
  8. const app = express();
  9. app.use(bodyParser.json());
  10. app.use(bodyParser.urlencoded({extended:true}));
  11. app.use(express.static('./frontend/assets'));
  12. app.get('/', function (req, res) {
  13. res.sendFile('./frontend/index.html', {
  14. root: path.resolve(__dirname + '/..')
  15. });
  16. });
  17. const httpServer = require('http').Server(app);
  18. /* --------
  19. * RECEIVER
  20. * --------
  21. */
  22. const receiverIo = require('socket.io')(httpServer, {
  23. transports: ['websocket']
  24. });
  25. const onSocketListening = function() {
  26. const address = receiverUdpSocket.address();
  27. console.log('Serveur TUIO en écoute sur : ' + address.address + ':' + address.port);
  28. };
  29. const onSocketConnection = function(socket) {
  30. receiverUdpSocket.on('message', function(msg) {
  31. socket.emit('osc', oscParser.decode(msg));
  32. });
  33. };
  34. const receiverUdpSocket = dgram.createSocket('udp4');
  35. receiverUdpSocket.on('listening', onSocketListening);
  36. receiverUdpSocket.bind(config.app.oscUdpPort, '127.0.0.1');
  37. app.get('/receiver/json', function (req, res) {
  38. res.status(200).send();
  39. });
  40. receiverIo.sockets.on('connection', (socket) =>{
  41. console.log(`Connecté au client ${socket.id}`);
  42. const dgramCallback = function (buf) {
  43. if (config.app.debug && config.app.debugLog.backend.receiver.oscDatagram) {
  44. console.log(oscParser.decode(buf));
  45. }
  46. socket.emit('osc', oscParser.decode(buf));
  47. };
  48. // forward UDP packets via socket.io
  49. receiverUdpSocket.on('message', dgramCallback);
  50. // prevent memory leak on disconnect
  51. socket.on('disconnect', function (socket) {
  52. receiverUdpSocket.removeListener('message', dgramCallback);
  53. });
  54. });
  55. /* -------
  56. * EMITTER
  57. * -------
  58. */
  59. const emitterOscClient = new Client('127.0.0.1', config.app.oscUdpPort);
  60. let alive = [];
  61. let fseq;
  62. let objectsAlive = [] ;
  63. let lastDots = [];
  64. function getHypotenuse(touch1, touch2) {
  65. const x = Math.abs(touch1.x - touch2.x);
  66. const y = Math.abs(touch1.y - touch2.y);
  67. return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
  68. }
  69. function getTop(dotTrio) {
  70. const dist01 = getHypotenuse(dotTrio[0], dotTrio[1]);
  71. const dist02 = getHypotenuse(dotTrio[0], dotTrio[2]);
  72. const dist12 = getHypotenuse(dotTrio[1], dotTrio[2]);
  73. const diff01m02 = Math.abs(dist01 - dist02);
  74. const diff01m12 = Math.abs(dist01 - dist12);
  75. const diff02m12 = Math.abs(dist02 - dist12);
  76. if (diff01m02 < diff02m12 && diff01m02 < diff01m12) {
  77. return 0;
  78. }
  79. else if (diff01m12 < diff01m02 && diff01m12 < diff02m12) {
  80. return 1;
  81. }
  82. else if (diff02m12 < diff01m02 && diff02m12 < diff01m12) {
  83. return 2;
  84. }
  85. }
  86. function getAngleApex(dotTrio, topIndex) {
  87. let dotA;
  88. let dotB;
  89. let dotC;
  90. dotB = dotTrio[topIndex];
  91. if (topIndex == 0) {
  92. dotA = dotTrio[1];
  93. dotC = dotTrio[2];
  94. }
  95. else if (topIndex == 1) {
  96. dotA = dotTrio[0];
  97. dotC = dotTrio[2];
  98. }
  99. else if (topIndex == 2) {
  100. dotA = dotTrio[0];
  101. dotC = dotTrio[1];
  102. }
  103. const AB = [dotB.x - dotA.x, dotB.y - dotA.y];
  104. const CB = [dotB.x - dotC.x, dotB.y - dotC.y];
  105. const dotProd = (AB[0] * CB[0] + AB[1] * CB[1]);
  106. const crossProd = (AB[0] * CB[1] - AB[1] * CB[0]);
  107. const alpha = Math.atan2(crossProd, dotProd);
  108. //return alpha ;
  109. return Math.abs(Math.floor(alpha * 180. / Math.PI + 0.5)) ;
  110. }
  111. function getOrientation(dotTrio, topIndex) {
  112. let dotA;
  113. let dotB;
  114. let dotC;
  115. dotB = dotTrio[topIndex];
  116. if (topIndex == 0) {
  117. dotA = dotTrio[1];
  118. dotC = dotTrio[2];
  119. }
  120. else if (topIndex == 1) {
  121. dotA = dotTrio[0];
  122. dotC = dotTrio[2];
  123. }
  124. else if (topIndex == 2) {
  125. dotA = dotTrio[0];
  126. dotC = dotTrio[1];
  127. }
  128. const middlePt = [(dotA.x + dotC.x) / 2, (dotA.y + dotC.y) / 2 ] ;
  129. let diff = [dotB.x - middlePt[0], dotB.y - middlePt[1]] ;
  130. //const length = Math.sqrt(Math.pow(diff[0], 2) + Math.pow(diff[1], 2) ) ;
  131. //normalize diff
  132. //diff = [diff[0] / length, diff[1] / length];
  133. const rad = Math.atan2(diff[1], diff[0]) ;
  134. return Math.floor(rad * 180 / Math.PI) ;
  135. //return length ;
  136. }
  137. function objectGarbageCollector(){
  138. //si un point dans last dots est detecté dans un des triangle alors on ne réduit pas sa duration
  139. for(const triangle of objectsAlive){
  140. if(triangle.dots.some(dot => lastDots.some(lastDot => lastDot.target == dot.target))){
  141. } else {
  142. triangle.remainingDuration -= 1;
  143. }
  144. };
  145. objectsAlive = objectsAlive.filter(triangle => triangle.remainingDuration > 0);
  146. if (config.app.debug && config.app.debugLog.backend.emitter.dots) {
  147. console.log("--garbageCollection--", lastDots);
  148. objectsAlive.forEach(tr => console.log(tr));
  149. }
  150. createBundle();
  151. }
  152. setInterval(objectGarbageCollector, config.app.garbageCollectorInterval);
  153. let currentOscBundle = null;
  154. let hasPending = false;
  155. function createBundle(){
  156. currentOscBundle = new Bundle ;
  157. currentOscBundle.append([ '/tuio/2Dobj', 'alive'].concat(objectsAlive.map(t => t.matchingObject.apexAngle) ));
  158. for(const triangle of objectsAlive){
  159. currentOscBundle.append([
  160. '/tuio/2Dobj',
  161. 'set',
  162. triangle.matchingObject.apexAngle,
  163. triangle.matchingObject.apexAngle,
  164. triangle.target, //we use x as target identifier
  165. triangle.center[1],
  166. triangle.orientation,
  167. 0.0,
  168. 0.0,
  169. 0.0,
  170. 0.0,
  171. 0.0
  172. ]);
  173. }
  174. currentOscBundle.append(['/tuio/2Dobj', 'fseq', fseq]);
  175. hasPending = true;
  176. }
  177. function listDots(touches){
  178. const dots = [];
  179. for(const touch of touches){
  180. dots.push({
  181. id: touch.identifier,
  182. x: touch.clientX,
  183. y: touch.clientY,
  184. target: touch.target
  185. });
  186. };
  187. if (config.app.debug && config.app.debugLog.backend.emitter.dots) {
  188. console.log('-- dots --', dots);
  189. }
  190. return dots
  191. }
  192. function listSegments(dots){
  193. const segments = [];
  194. if (dots.length > 2) {
  195. for (var i = 0; i < dots.length; i++) {
  196. for (var j = 0; j < dots.length; j++) {
  197. if (j !== i) {
  198. /* on vérifie que le segment n'est pas déjà listé */
  199. const alreadyExists = segments.find(segment => {
  200. return segment.identifiers.includes(i) && segment.identifiers.includes(j);
  201. });
  202. /* on calcule la taille du segment (l'hypoténuse) */
  203. var hyp = getHypotenuse(dots[i], dots[j]);
  204. /* on garde uniquement les segments inférieurs à 750px (valeur par défaut)
  205. * cette valeur est la variable de configuration "maxDistanceBetweenPoints" */
  206. if (!alreadyExists && hyp <= config.app.maxDistanceBetweenPoints) {
  207. segments.push({
  208. identifiers: [i, j],
  209. x1: dots[i].x,
  210. x2: dots[j].x,
  211. y1: dots[i].y,
  212. y2: dots[j].y,
  213. target: dots[i].target,
  214. hyp
  215. });
  216. }
  217. }
  218. }
  219. }
  220. }
  221. if (config.app.debug && config.app.debugLog.backend.emitter.segments) {
  222. console.log('-- segments --', segments);
  223. }
  224. return segments;
  225. }
  226. function listTriangles(segments){
  227. const triangles = [];
  228. /* on boucle sur les segments */
  229. for(const segment of segments) {
  230. const dot1 = segment.identifiers[0];
  231. const dot2 = segment.identifiers[1];
  232. /* on vérifie que le triangle n'est pas déjà listé */
  233. const alreadyExists = triangles.find(triangle => {
  234. return triangle.includes(dot1) && triangle.includes(dot2);
  235. });
  236. if (!alreadyExists) {
  237. /* on cherche les segments qui contiennent un des 2 points du segment actuel
  238. * ex: si le segment actuel est AB, on cherche un segment contenant A (pour AC) et un autre contenant B (pour BC) */
  239. const found1 = segments.findIndex(seg => {
  240. return (seg.identifiers.includes(dot1) && !seg.identifiers.includes(dot2));
  241. });
  242. const found2 = segments.findIndex(seg => {
  243. return (seg.identifiers.includes(dot2) && !seg.identifiers.includes(dot1));
  244. });
  245. /* si on trouve bien les 2 segments (AC et BC), on peut créer un triangle */
  246. if (found1 !== -1 && found2 !== -1) {
  247. /* on devine quel est le 3ème point du triangle par rapport au segment actuel (le point C par rapport au segment AB) */
  248. const dot3 = segments[found1].identifiers.find(identifier => {
  249. return identifier !== dot1 && identifier !== dot2;
  250. });
  251. triangles.push([dot1, dot2, dot3]);
  252. }
  253. }
  254. };
  255. if (config.app.debug && config.app.debugLog.backend.emitter.triangles) {
  256. console.log('-- triangles --', triangles);
  257. }
  258. return triangles
  259. }
  260. function filterTriangles(dots, triangles){
  261. const filteredTriangles = [];
  262. /* Définition de l'apex, de la position du centre et de l'orientation */
  263. for(const triangle of triangles){
  264. const objTriangle = {} ;
  265. objTriangle.dots = [];
  266. objTriangle.dots[0] = dots[triangle[0]];
  267. objTriangle.dots[1] = dots[triangle[1]];
  268. objTriangle.dots[2] = dots[triangle[2]];
  269. objTriangle.target = objTriangle.dots[0].target
  270. objTriangle.apex = getTop(objTriangle.dots);
  271. objTriangle.center = [
  272. (objTriangle.dots[0].x + objTriangle.dots[1].x + objTriangle.dots[2].x) / 3,
  273. (objTriangle.dots[0].y + objTriangle.dots[1].y + objTriangle.dots[2].y) / 3
  274. ];
  275. objTriangle.angleApex = getAngleApex(objTriangle.dots, objTriangle.apex);
  276. objTriangle.orientation = getOrientation(objTriangle.dots, objTriangle.apex);
  277. if (config.app.debug && config.app.debugLog.backend.emitter.apex) {
  278. console.log('-- apex --', objTriangle.apex);
  279. console.log('angle: ', objTriangle.angleApex);
  280. console.log('centerPos: ' + objTriangle.center);
  281. console.log('orientation: ' + objTriangle.orientation);
  282. }
  283. //verify if triangle has a corresponding triangle in config
  284. const matchingObject = config.objects.find(item => {
  285. return objTriangle.angleApex > item.apexAngle - config.app.matchingTolerance && objTriangle.angleApex < item.apexAngle + config.app.matchingTolerance;
  286. });
  287. if (matchingObject) {
  288. objTriangle.matchingObject = matchingObject;
  289. filteredTriangles.push(objTriangle);
  290. if (config.app.debug && config.app.debugLog.backend.emitter.matchingObject) {
  291. console.log('-- matchingObject --', objTriangle);
  292. }
  293. }
  294. };
  295. return filteredTriangles;
  296. }
  297. function updateAliveTriangles(filteredTriangles) {
  298. for (const triangle of filteredTriangles) {
  299. let idx = objectsAlive.findIndex(item => {
  300. return triangle.matchingObject.name == item.matchingObject.name;
  301. });
  302. if (idx == -1) {
  303. triangle.remainingDuration = config.app.remainingDuration;
  304. if(objectsAlive.some(obj => obj.target == triangle.target)){
  305. console.log("already a triangle in this box");
  306. } else {
  307. objectsAlive.push(triangle);
  308. }
  309. } else {
  310. triangle.remainingDuration = config.app.remainingDuration;
  311. objectsAlive[idx] = triangle;
  312. }
  313. }
  314. if (config.app.debug && config.app.debugLog.backend.emitter.aliveTriangles) {
  315. console.log('-- aliveTriangles --', objectsAlive);
  316. }
  317. }
  318. const sendBundle = () => {
  319. if (hasPending) {
  320. emitterOscClient.send(currentOscBundle, () => {
  321. hasPending = false;
  322. });
  323. }
  324. setTimeout(() => {
  325. sendBundle();
  326. }, config.app.timerRefresh);
  327. };
  328. sendBundle();
  329. app.post('/emitter/json', function (req, res) {
  330. if (config.app.debug && config.app.debugLog.backend.emitter.httpRequest) {
  331. console.log('### Emitter POST request ###');
  332. }
  333. let oscBundle;
  334. // if (req.body.type === 'touchend') {
  335. // fseq = fseq ? fseq + 1 : 1;
  336. // const aliveMessage = [ '/tuio/2Dcur', 'alive' ].concat(alive);
  337. // currentOscBundle = new Bundle(
  338. // [ '/tuio/2Dcur', 'source', `tangibles${req.body.section.toString()}@127.0.0.1` ],
  339. // aliveMessage,
  340. // [ '/tuio/2Dcur', 'fseq', fseq ],
  341. // [
  342. // '/tuio/2Dcur',
  343. // 'del',
  344. // req.body.touches[0].identifier
  345. // ]
  346. // );
  347. // emitterOscClient.send(currentOscBundle, () => {
  348. // const index = alive.indexOf(req.body.touches[0].identifier);
  349. // alive.splice(index, 1);
  350. // if (alive.length === 0) {
  351. // currentOscBundle = new Bundle(
  352. // [ '/tuio/2Dcur', 'source', `tangibles${req.body.section.toString()}@127.0.0.1` ],
  353. // [ '/tuio/2Dcur', 'alive' ],
  354. // [ '/tuio/2Dcur', 'fseq', fseq ]
  355. // );
  356. // emitterOscClient.send(currentOscBundle, () => {
  357. // res.status(200).send();
  358. // fseq = 0;
  359. // hasPending = false;
  360. // });
  361. // } else {
  362. // res.status(200).send();
  363. // }
  364. // });
  365. // } else {
  366. if (req.body.touches && req.body.touches.length && req.body.touches.length > 0) {
  367. fseq = fseq ? fseq + 1 : 1;
  368. const touches = Object.keys(req.body.touches);
  369. // const aliveMessage = [ '/tuio/2Dcur', 'alive' ].concat(alive);
  370. // touches.forEach(touch => {
  371. // const id = req.body.touches[touch].identifier;
  372. // if (!alive.includes(id)) {
  373. // alive.push(id);
  374. // aliveMessage.push(id);
  375. // }
  376. // });
  377. /* Listage de tous les points */
  378. const dots = listDots(req.body.touches);
  379. lastDots = dots;
  380. const segments = listSegments(dots);
  381. const triangles = listTriangles(segments);
  382. const filteredTriangles = filterTriangles(dots, triangles);
  383. updateAliveTriangles(filteredTriangles);
  384. createBundle();
  385. res.status(200).send();
  386. } else {
  387. lastDots = [];
  388. res.status(200).send();
  389. }
  390. });
  391. httpServer.listen(config.app.httpPort, function () {
  392. console.log(`Votre app est disponible sur http://localhost:${config.app.httpPort} !`)
  393. });