forked from loisirs-numeriques/desertbus-intranet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
548 lines (500 loc) · 14.1 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
const electron = require('electron');
const {
app,
BrowserWindow
} = electron;
const websocket = require('websocket');
const WebSocketServer = websocket.server;
const http = require('http');
const fs = require('fs');
const url = require('url');
const request = require('request');
const WStools = require('./wsTools.js');
const Don = require('./class/don.js');
const DonCtrl = require('./ctrl/don.ctrl.js');
const Timer = require('./class/timer.js');
const TimerCtrl = require('./ctrl/timer.ctrl.js');
const Credentials = require('./class/credentials.js')
let donCtrl = new DonCtrl();
let timerCtrl = new TimerCtrl();
let twitchDesertbus = 0;
let twitchJvtv = 0;
const port = 8080;
var credentials = new Credentials('credentials.json');
let server = http.createServer(function (request, response) {
//console.log((new Date()) + ' [HTTP] Demande d\'une page');
let page = url.parse(request.url).pathname;
console.log((new Date()) + ' [HTTP] Demande de ' + page);
if (page == "/" || page == "/index.html") {
fs.readFile('./www/index.html', 'utf-8', function (error, content) {
response.writeHead(200, {
"Content-Type": "text/html"
});
response.end(content);
});
} else if (page == "/compteur.html") {
fs.readFile('./www/compteur.html', 'utf-8', function (error, content) {
response.writeHead(200, {
"Content-Type": "text/html"
});
response.end(content);
});
} else if (page == "/index.js") {
fs.readFile('./www/index.js', 'binary', function (error, content) {
response.writeHead(200, {
"Content-Type": "application/javascript"
});
response.end(content, 'binary');
});
} else if (page == "/wsTools.js") {
fs.readFile('./www/wsTools.js', 'binary', function (error, content) {
response.writeHead(200, {
"Content-Type": "application/javascript"
});
response.end(content, 'binary');
});
} else if (page == "/timer.html") {
fs.readFile('./www/timer.html', 'utf-8', function (error, content) {
response.writeHead(200, {
"Content-Type": "text/html"
});
response.end(content);
});
} else if (page == "/timer.js") {
fs.readFile('./www/timer.js', 'utf-8', function (error, content) {
response.writeHead(200, {
"Content-Type": "application/javascript"
});
response.end(content);
});
} else if (page == "/desertbus.css") {
fs.readFile('./www/desertbus.css', 'utf-8', function (error, content) {
response.writeHead(200, {
"Content-Type": "text/css"
});
response.end(content);
});
} else if (page == "/logoDefinitif.png") {
fs.readFile('./www/logoDefinitif.png', function (error, content) {
response.writeHead(200, {
"Content-Type": "image/png"
});
response.end(content);
});
} else {
response.writeHead(404);
response.end();
}
});
server.listen(port, function () {
console.log((new Date()) + ' Serveur à l\'écoute sur le port ' + port);
});
let webserver = new WebSocketServer({
httpServer: server
//, autoAcceptConnections: false // Protection à mettre en place pour la prod ?
});
function sauvegarderSysteme() {
let donnees = {
dons: donCtrl.dons,
donId: donCtrl.id,
donsId: donCtrl.donsId,
donsCleExterne: donCtrl.cleExterne,
timer: timerCtrl.timers,
timerId: timerCtrl.id
};
fs.writeFileSync('sauvegarde.json', JSON.stringify(donnees));
}
function restaurerSysteme() {
fs.readFile('sauvegarde.json', 'utf-8', function (err, data) {
if (!err) {
let obj = JSON.parse(data);
for (let heure in obj.dons) {
// console.log(obj.dons);
for (let donId in obj.dons[heure].dons) {
let item = obj.dons[heure].dons[donId];
// console.log(item);
let don = new Don(new Date(item.date), item.montant, item.infos);
donCtrl.addDon(don);
}
}
/*donCtrl.dons = obj.dons;
donCtrl.id = obj.donId;
donCtrl.cleExterne = obj.donsCleExterne;
donCtrl.donsId = obj.donsId;*/
timerCtrl.timers = obj.timer;
timerCtrl.id = obj.timerId;
}
});
}
function getObjMajDon() {
return {
totalDon: donCtrl.totalDon,
nbDon: donCtrl.nbDons,
totalDonHeure: donCtrl.heureCourante.totalDons,
nbDonHeure: donCtrl.heureCourante.nbDons,
compteurBloque: donCtrl.bloque,
compteurVraieValeur: donCtrl.totalDonReel
};
}
function majDon() {
console.log("Total don : " + donCtrl.totalDon);
allConnexion
.filter(function (c) {
return c.connected;
})
.forEach(
function (c) {
WStools.envoiStatic(c, "majDon", getObjMajDon());
}
);
sauvegarderSysteme();
}
function majTwitch() {
console.log("Total viewers : " + (twitchJvtv + twitchDesertbus));
allConnexion
.filter(function (c) {
return c.connected;
})
.forEach(
function (c) {
WStools.envoiStatic(c, "majTwitch", {
desertbus: twitchDesertbus,
jvtv: twitchJvtv
});
}
);
}
function majTimer() {
console.log("Envoi de la mise à jour des timers");
allConnexion
.filter(function (c) {
return c.connected;
})
.forEach(
function (c) {
WStools.envoiStatic(c, "listeTimers", timerCtrl.timers);
}
);
}
let allConnexion = [];
//On prépare déjà le reset des dons de l'heure courante
let now = new Date();
setTimeout(
function () {
setInterval(function () {
donCtrl.resetDonHeureCourante();
majDon();
majTwitch()
}, 3600000);
donCtrl.resetDonHeureCourante();
majDon();
majTwitch()
},
(59 - now.getMinutes()) * 60000 + (59 - now.getSeconds()) * 1000 + (1000 - now.getMilliseconds())
);
function decodeUTF16LE(binaryStr) {
var cp = [];
for (var i = 0; i < binaryStr.length; i += 2) {
cp.push(
binaryStr.charCodeAt(i) |
(binaryStr.charCodeAt(i + 1) << 8)
);
}
return String.fromCharCode.apply(String, cp);
}
function recupererTwitch() {
request({
url: "https://api.twitch.tv/kraken/streams/desertbusfr",
headers: {
"Client-ID": "4wjei4iwb43xcysa0r70x9i4n8sxy8z"
}
},
function (error, response, body) {
if (body == null || body[0] != '{') {
console.log("body", body);
return;
} else
var data = JSON.parse(body)
if (data.stream != null)
twitchDesertbus = data.stream.viewers
majTwitch()
}
)
request({
url: "https://api.twitch.tv/kraken/streams/jvtv",
headers: {
"Client-ID": "4wjei4iwb43xcysa0r70x9i4n8sxy8z"
}
},
function (error, response, body) {
if (body == null || body[0] != '{') {
console.log("body", body);
return;
} else
var data = JSON.parse(body)
if (data.stream != null)
twitchJvtv = data.stream.viewers
majTwitch()
}
)
}
// function requestMaxPage() {
// let auth = "Basic " + new Buffer(credentials.username + ":" + credentials.password).toString("base64");
// request({
// url: "https://api.helloasso.com/v3/campaigns/"+campaigns_id+"/actions.json?page=1&results_per_page=100",
// headers: {
// "Authorization": auth
// }
// },
// function (error, response, body) {
// if (body == "You do not have permission to view this directory or page.") {
// console.log("body", body);
// requestMaxPage()
// } else {
// var data = JSON.parse(body);
// return data.pagination.max_page
// }
// })
// return 20;
// }
//Récupération des dons hello asso
function recupererDonsHelloAsso() {
let auth = "Basic " + new Buffer(credentials.username + ":" + credentials.password).toString("base64");
let campaigns_id = credentials.campaigns_id
request({
url: "https://api.helloasso.com/v3/campaigns/" + campaigns_id + "/actions.json?page=1&results_per_page=100",
headers: {
"Authorization": auth
}
},
function (error, response, body) {
if (body == null || body[0] != '{') {
console.log("body", body);
return;
} else {
var data = JSON.parse(body);
traiterHelloAsso(data);
}
for (let i = 2; i <= data.pagination.max_page; i++) {
request({
url: "https://api.helloasso.com/v3/campaigns/" + campaigns_id + "/actions.json?page=" + i + "&results_per_page=100",
headers: {
"Authorization": auth
}
},
function (error, response, body) {
if (body == null || body[0] != '{') {
console.log("body", body);
i--
} else {
let data = JSON.parse(body);
traiterHelloAsso(data);
}
}
);
setTimeout(() => {}, 200);
}
}
);
}
function traiterHelloAsso(contenu) {
// console.log('contenu', contenu)
if (contenu.resources != null) {
contenu.resources.map((el) => {
let infos = {};
infos.date = new Date(el.date);
infos.montant = el.amount
infos.infos = {}
infos.infos.cle = el.id
infos.infos.donOnly = false
infos.infos.prenom = el.first_name
infos.infos.nom = el.last_name
infos.infos.adresse = el.address
infos.infos.cp = el.zip_code
infos.infos.ville = el.city
infos.infos.pays = el.country
infos.infos.courriel = el.email
el.custom_infos.map((lot) => {
if (lot.label == 'Je souhaite participer au tirage au sort de la loterie') {
infos.infos.donOnly = (lot.value === 'Oui')
}
})
let don = new Don(infos.date, infos.montant, infos.infos);
donCtrl.addDon(don);
})
}
// for (let el of contenu.resources) {
// let infos = {};
// infos.date = new Date(el.date);
// infos.montant = el.amount
// infos.infos = {}
// infos.infos.cle = el.id
// infos.infos.donOnly = false
// infos.infos.prenom = el.first_name
// infos.infos.nom = el.last_name
// infos.infos.adresse = el.address
// infos.infos.cp = el.zip_code
// infos.infos.ville = el.city
// infos.infos.pays = el.country
// infos.infos.courriel = el.email
// for (let lot of el.custom_infos) {
// if (lot.label == 'Je souhaite participer au tirage au sort de la loterie') {
// infos.infos.donOnly = (lot.value === 'Oui')
// }
// }
// let don = new Don(infos.date, infos.montant, infos.infos);
// donCtrl.addDon(don);
// }
// console.log("contenu.ressources[0]", contenu.ressources[0])
majDon();
}
restaurerSysteme();
recupererDonsHelloAsso();
setInterval(recupererTwitch, 10000);
setInterval(recupererDonsHelloAsso, 10000);
webserver.on('request', function (request) {
let connexion = request.accept('echo-protocol', request.origin);
allConnexion.push(connexion);
let wsTools = new WStools(connexion);
console.log((new Date()) + ' Connexion de ' + connexion.remoteAddress);
connexion.on("message", function (event) {
// console.log(event);
let data = JSON.parse(event.utf8Data);
console.log(data);
if (!!data.tag) {
let tag = data.tag;
console.log("Tag : " + tag);
let obj = data.donnees;
if (obj == undefined)
obj = {};
// console.log(obj);
if (tag == "ajoutDon") {
let date, montant, infos;
if (!!obj.date)
date = new Date(obj.date);
else
date = new Date();
if (!!obj.montant)
montant = obj.montant;
else
montant = 0;
infos = obj.infos || {};
if (!!obj.id && parseInt(obj.id) > 0) {
let don = donCtrl.getDon(parseInt(obj.id));
let diff = montant - don.montant;
let index = don.indexHeure;
don.montant = montant;
don.date = date;
for (let cle in don.infos) {
if (infos[cle] !== undefined)
don.infos[cle] = infos[cle];
}
//don.infos = infos;
donCtrl.updateHeure(index, diff);
//donCtrl.modifDon(parseInt(obj.id), don);
} else {
let don = new Don(date, montant, infos);
donCtrl.addDon(don);
}
majDon();
} else if (tag == "majDon") {
wsTools.envoi("majDon", getObjMajDon());
} else if (tag == "majTwitch") {
wsTools.envoi("majTwitch", {
desertbus: twitchDesertbus,
jvtv: twitchJvtv
});
} else if (tag == "listeDons") {
let index;
if (!!obj.index)
index = obj.index;
else
index = donCtrl.indexHeureCourante;
wsTools.envoi("listeDons", donCtrl.getHeureIndex(index));
} else if (tag == "supprimerDon") {
donCtrl.removeDon(obj.id);
majDon();
} else if (tag == "ajoutTimer") {
let debut;
if (!!obj.debut)
debut = new Date(obj.debut);
else
debut = new Date();
let nom = obj.nom;
let active = obj.active;
if (!!obj.id && parseInt(obj.id) > 0) {
let id = parseInt(obj.id);
console.log("Mise à jour du timer #" + id);
let timer = timerCtrl.getTimer(id);
let valeur = obj.valeur;
timer.nom = nom;
timer.debut = debut;
timer.active = active;
timer.valeur = valeur;
timerCtrl.modifTimer(id, timer);
} else {
console.log("Ajout d'un nouveau timer");
let timer = new Timer(nom, debut, active);
timerCtrl.addTimer(timer);
}
majTimer();
} else if (tag == "supprimerTimer") {
timerCtrl.removeTimer(obj.id);
majTimer();
} else if (tag == "listeTimers") {
wsTools.envoi("listeTimers", timerCtrl.timers);
} else if (tag == "etatCompteur") {
donCtrl.bloque = obj.bloque;
donCtrl.valeurBloque = obj.valeur;
majDon();
majTwitch()
}
}
});
connexion.on('close', function (reasonCode, description) {
console.log((new Date()) + ' Deconnexion de ' + connexion.remoteAddress);
});
});
app.on('ready', () => {
let fenetre = new BrowserWindow({
width: 800,
height: 600,
autoHideMenuBar: true
});
fenetre.loadURL('file://' + __dirname + '/index.html');
let webContents = fenetre.webContents;
webContents.session.on('will-download', function (event, item, webContents) {
//event.stopPropagation();
// Set the save path, making Electron not to prompt a save dialog.
item.setSavePath('/tmp/save.csv');
console.log("will - Download");
console.log(item.getSavePath());
item.on('updated', (event, state) => {
if (state === 'interrupted') {
console.log('Download is interrupted but can be resumed')
} else if (state === 'progressing') {
if (item.isPaused()) {
console.log('Download is paused')
} else {
console.log(`Received bytes: ${item.getReceivedBytes()}`)
}
}
})
item.once('done', (event, state) => {
if (state === 'completed') {
console.log('Download successfully');
fs.readFile('/tmp/save.csv', function read(err, data) {
if (err) {
throw err;
}
var content = data;
// Invoke the next step here however you like
console.log(String.fromCharCode.apply(null, new Uint16Array(content))); // Put all of the code here (not the best solution)
});
} else {
console.log(`Download failed: ${state}`)
}
})
});
fenetre.webContents.openDevTools();
});