2010-12-04 22:04:58 +01:00
|
|
|
var Connect = require('connect');
|
2011-10-14 02:56:16 +02:00
|
|
|
var wss = require('websocket').server;
|
|
|
|
var wsc = require('websocket').client;
|
2010-12-10 22:04:17 +01:00
|
|
|
var irc = require('irc-js');
|
2010-12-04 22:04:58 +01:00
|
|
|
|
|
|
|
var frontend;
|
|
|
|
|
2010-12-10 22:04:17 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Nedap backend connection
|
|
|
|
*/
|
|
|
|
|
2010-12-04 22:04:58 +01:00
|
|
|
/* TODO: url */
|
2010-12-06 19:19:52 +01:00
|
|
|
var nedap;
|
|
|
|
function connectNedap() {
|
2011-10-14 02:56:16 +02:00
|
|
|
nedap = new wsc();
|
|
|
|
nedap.on('connect', function() {
|
2010-12-06 19:19:52 +01:00
|
|
|
console.log('NEDAP opened');
|
2011-10-14 02:56:16 +02:00
|
|
|
nedap.sendUTF('nedap-kneemFothbedchoadHietEnobKavLub1');
|
|
|
|
});
|
|
|
|
nedap.on('close', function() {
|
2010-12-06 19:19:52 +01:00
|
|
|
console.log('NEDAP closed');
|
|
|
|
connectNedap();
|
2011-10-14 02:56:16 +02:00
|
|
|
});
|
|
|
|
nedap.on('error', function(e) {
|
2010-12-06 19:19:52 +01:00
|
|
|
console.log('NEDAP error: ' + e.message);
|
|
|
|
connectNedap();
|
2011-10-14 02:56:16 +02:00
|
|
|
});
|
|
|
|
nedap.on('message', function(data) {
|
2010-12-06 19:19:52 +01:00
|
|
|
try {
|
|
|
|
var msg = JSON.parse(data);
|
|
|
|
console.log({ fromNedap: msg });
|
2010-12-10 22:04:17 +01:00
|
|
|
sendToFrontend({ nedap: msg });
|
2010-12-06 19:19:52 +01:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e.stack);
|
|
|
|
}
|
2011-10-14 02:56:16 +02:00
|
|
|
});
|
|
|
|
nedap.connect('ws://localhost/', 'quiz-nedap');
|
2010-12-06 19:19:52 +01:00
|
|
|
}
|
|
|
|
connectNedap();
|
2010-12-04 22:04:58 +01:00
|
|
|
|
2010-12-10 22:04:17 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* IRC client
|
|
|
|
*/
|
|
|
|
|
2011-04-22 23:15:02 +02:00
|
|
|
var IRC_SERVER = 'irc.hackint.eu';
|
2010-12-10 22:04:17 +01:00
|
|
|
var IRC_CHAN = '#pentanews';
|
|
|
|
var chat = new irc({ server: IRC_SERVER,
|
|
|
|
encoding: 'utf-8',
|
|
|
|
nick: '[Ceiling]Cat'
|
|
|
|
});
|
|
|
|
function connectChat() {
|
|
|
|
chat.connect();
|
|
|
|
}
|
|
|
|
connectChat();
|
|
|
|
chat.addListener('376', function() {
|
|
|
|
chat.join(IRC_CHAN);
|
|
|
|
});
|
|
|
|
chat.addListener('366', function(msg) {
|
|
|
|
if (msg.params[1] === IRC_CHAN) {
|
|
|
|
console.log('Successfully joined ' + IRC_CHAN);
|
|
|
|
pushIrcInfo();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
chat.addListener('privmsg', function(msg) {
|
|
|
|
console.log({PRIVMSG:msg});
|
|
|
|
var nick = msg.person.nick;
|
|
|
|
var channel = msg.params[0];
|
|
|
|
var text = msg.params[1];
|
2010-12-27 15:57:30 +01:00
|
|
|
|
|
|
|
var sText = "", i;
|
|
|
|
for(i = 0; i < text.length; i++) {
|
|
|
|
if (text.charCodeAt(i) >= 32)
|
|
|
|
sText += text.charAt(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nick && channel === IRC_CHAN && sText && frontend) {
|
2010-12-10 22:04:17 +01:00
|
|
|
sendToFrontend({ irc: { nick: nick,
|
2010-12-27 15:57:30 +01:00
|
|
|
text: sText
|
2010-12-10 22:04:17 +01:00
|
|
|
} });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
chat.addListener('disconnected', function() {
|
|
|
|
console.error('Chat disconnected!');
|
|
|
|
process.nextTick(connectChat);
|
|
|
|
});
|
|
|
|
|
|
|
|
function pushIrcInfo() {
|
|
|
|
sendToFrontend({ irc: { server: IRC_SERVER,
|
|
|
|
channel: IRC_CHAN } });
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-22 21:30:20 +02:00
|
|
|
/*
|
|
|
|
* Buttons
|
|
|
|
*/
|
|
|
|
var buzz = new (require('./buzz_iface/node_lib/buzz').Buzz)('/dev/ttyUSB0');
|
|
|
|
buzz.on('button', function(key) {
|
2011-04-22 23:14:56 +02:00
|
|
|
console.log({button:key});
|
2011-04-22 21:30:20 +02:00
|
|
|
sendToFrontend({ buzzer: key });
|
|
|
|
});
|
|
|
|
|
2010-12-10 22:04:17 +01:00
|
|
|
/*
|
|
|
|
* Web server
|
|
|
|
*/
|
2010-12-21 22:47:57 +01:00
|
|
|
function noCache(req, res, next) {
|
|
|
|
var writeHead = res.writeHead;
|
|
|
|
res.writeHead = function(status, headers) {
|
|
|
|
headers['Cache-Control'] = 'no-cache';
|
|
|
|
writeHead.call(this, status, headers);
|
|
|
|
};
|
|
|
|
next();
|
|
|
|
}
|
2010-12-10 22:04:17 +01:00
|
|
|
|
2010-12-04 22:04:58 +01:00
|
|
|
var server = Connect.createServer(
|
|
|
|
Connect.logger(),
|
2010-12-21 22:47:57 +01:00
|
|
|
noCache,
|
2010-12-04 22:04:58 +01:00
|
|
|
Connect.bodyDecoder(),
|
2010-12-21 22:47:57 +01:00
|
|
|
Connect.staticProvider({ root: __dirname, maxAge: 1000 }),
|
2010-12-04 22:04:58 +01:00
|
|
|
Connect.errorHandler({ dumpExceptions: true, showStack: true })
|
|
|
|
);
|
|
|
|
|
2010-12-10 22:04:17 +01:00
|
|
|
/*
|
|
|
|
* WebSocket server
|
|
|
|
*/
|
|
|
|
|
2011-10-14 02:56:16 +02:00
|
|
|
new wss({ httpServer: server }).on('request', function(req) {
|
|
|
|
var conn = req.accept(null, req.origin);
|
2010-12-06 19:10:44 +01:00
|
|
|
frontend = conn;
|
2010-12-04 22:04:58 +01:00
|
|
|
|
2011-10-14 02:56:16 +02:00
|
|
|
conn.on('message', function(wsmsg) {
|
2010-12-06 19:10:44 +01:00
|
|
|
try {
|
2011-10-14 02:56:16 +02:00
|
|
|
var msg = JSON.parse(wsmsg.utf8Data);
|
2010-12-04 23:08:05 +01:00
|
|
|
if (msg.nedap) {
|
|
|
|
console.log({ toNedap: msg.nedap });
|
2011-10-14 02:56:16 +02:00
|
|
|
if (nedap)
|
|
|
|
nedap.sendUTF(JSON.stringify(msg.nedap));
|
2011-04-22 21:30:20 +02:00
|
|
|
} else if (msg.irc === "activate") {
|
2010-12-10 22:04:17 +01:00
|
|
|
pushIrcInfo();
|
2011-04-22 21:30:20 +02:00
|
|
|
} else if (msg.buzzerLED) {
|
|
|
|
buzz.set_led(msg.buzzerLED[0], msg.buzzerLED[1]);
|
2010-12-10 22:04:17 +01:00
|
|
|
}
|
2010-12-06 19:10:44 +01:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e.stack);
|
|
|
|
}
|
|
|
|
});
|
2010-12-04 22:04:58 +01:00
|
|
|
|
2010-12-06 19:10:44 +01:00
|
|
|
var reset = function() {
|
|
|
|
frontend = null;
|
|
|
|
};
|
|
|
|
conn.on('close', reset);
|
|
|
|
conn.on('error', reset);
|
2010-12-04 22:04:58 +01:00
|
|
|
});
|
2010-12-06 18:01:48 +01:00
|
|
|
|
2010-12-10 22:04:17 +01:00
|
|
|
function sendToFrontend(obj) {
|
|
|
|
if (!frontend)
|
|
|
|
return;
|
|
|
|
|
2011-10-14 02:56:16 +02:00
|
|
|
frontend.sendUTF(JSON.stringify(obj));
|
2010-12-10 22:04:17 +01:00
|
|
|
}
|
|
|
|
|
2010-12-06 18:01:48 +01:00
|
|
|
server.listen(8081);
|