-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
162 lines (134 loc) · 3.95 KB
/
index.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
/*
Created by David FAIVRE-MAÇON
https://www.davidfm.me
Serveur NODEJS pour nabaztag pi
Connexion homedoudou
https://www.homedoudou.fr
*/
const version = "0.5";
const helpers = require('./helpers.js');
const ddp = require('./ddp.js');
const choreography = require('./choreography.js');
const os = require('os');
// Connexion homedoudou.fr en websocket
// const host = '127.0.0.1:3000';
const host = 'hard.homedoudou.fr';
ddp.connect('ws://' + host + '/websocket', {headers: {"User-Agent": "Nabaztag/" + version}});
// Serveur NODEJS
const express = require('express');
const exphbs = require('express-handlebars');
const Handlebars = require('handlebars');
const bodyParser = require("body-parser");
const fs = require('fs');
const app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static('public'));
app.engine('.hbs', exphbs({
layoutsDir: './views/layouts',
defaultLayout: 'layout',
partialsDir: './views/layouts/partials/',
extname: '.hbs',
helpers: {
version: function() {
return version;
},
},
}));
app.set('view engine', '.hbs');
const port = 8076;
const server = app.listen(port, () => console.log(`server listen on port : ${port}`));
const io = require('socket.io').listen(server);
let chor = [];
// helpers.gestalt = {};
app.get('/', (req, res) => {
const data = {
name: "home",
os: {
platform: os.platform(),
arch: os.arch(),
type: os.type(),
release: os.release(),
cpus: os.cpus(),
freemem: helpers.bytesToSize(os.freemem()),
totalmem: helpers.bytesToSize(os.totalmem()),
},
};
res.render('home', data);
});
app.get('/nabd', (req, res) => {
const data = {
name: "nabd",
opCode: choreography.opCode,
};
res.render('nabd', data);
});
app.post('/nabd', (req, res) => {
const form = req.body;
if(form.data) helpers.sendToRabbit(form.data);
if(form.chor) helpers.sendChoreographyToRabbit(form.chor);
res.redirect(req.headers.referer);
});
app.get('/chorgenerator', (req, res) => {
const data = {
name: "chorgenerator",
chor: chor,
options: function() {
const options = [];
for(let i = 0; i <= 255; i++) {
if(i === 16)
options.push({value: i, text: i, isSelected: true});
else
options.push({value: i, text: i});
}
return options;
},
};
res.render('chorgenerator', data);
});
app.post('/chorgenerator', (req, res) => {
const form = req.body;
console.log('form.chorValue', form.chorValue);
if(form.reset === 'reset') chor = [];
else if(form.save === 'save') {
const base64ch = Buffer.from(String.fromCharCode.apply(null, chor)).toString('base64');
console.log('base64ch', base64ch);
helpers.sendToRabbit(JSON.stringify({"type": "command", "sequence": [{"choreography": "data:application/x-nabaztag-mtl-choreography;base64," + base64ch}]}));
fs.writeFile('./public/chorFiles/generated_' + helpers.getFormattedTime() + '.chor', base64ch, function(err) {
if(err) return console.log(err);
});
}
else chor = chor.concat(form.chorValue);
console.log(chor);
res.redirect('/chorgenerator');
});
app.get('/chorFiles', (req, res) => {
const fs = require('fs');
const filenames = fs.readdirSync('./public/chorFiles/');
const data = {
name: "chorFiles",
chor: chor,
filenames: filenames,
};
res.render('chorFiles', data);
});
app.use(function(req, res, next) {
res.status(404).render('404');
});
io.sockets.on('connect', function(socket) {
console.log("client connect");
socket.on('getGestalt', function() {
helpers.sendToRabbit('{"type": "gestalt"}', function(gestalt) {
if(gestalt) {
socket.emit('gestalt', gestalt.toString('ascii'));
}
else
socket.emit('gestalt', '{}');
});
});
});
Handlebars.registerHelper("stringify", function(s) {
return JSON.stringify(s, null, 4);
});
Handlebars.registerHelper("isActive", function(url, name) {
return url === name ? "active" : "";
});