-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.js
68 lines (60 loc) · 1.95 KB
/
server.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
/*jslint unparam: true, node: true, sloppy: true, nomen: true, maxerr: 50, indent: 2 */
var express = require('express'), app = express.createServer(), net = require('net'), twilioAPI = require('twilio-api');
var cli = new twilioAPI.Client(process.env.account_sid, process.env.auth_token);
app.use(cli.middleware());
app.listen(8002);
var arduinoTcp = null;
var curr_call = null;
var tcpServer = net.createServer(function (socket) {
console.log('tcp server running on port 1337');
});
function getDigits(call, input) {
console.log('pressed ' + input);
if (arduinoTcp === null) {
call.say("I can't do that for you, Dave. I'm offline.");
} else {
curr_call = call;
if (['2', '4', '5', '6', '8', '0'].indexOf(input) >= 0) {
arduinoTcp.write(input);
call.gather(getDigits, {numDigits: 1});
} else {
call.gather(getDigits, {numDigits: 1}).say("I can't do that for you, Dave. Invalid command.");
}
}
}
tcpServer.on('connection', function (socket) {
console.log('num of connections on port 1337: ' + tcpServer.connections);
arduinoTcp = socket;
socket.on('data', function (mydata) {
console.log('received on tcp socket:' + mydata);
curr_call.load(function (err, call) {
if (err) {
throw err;
}
if (!isNaN(mydata)) {
curr_call = call;
call.liveCb(function (err, subcall) {
if (err) {
throw err;
}
subcall.gather(getDigits, {numDigits: 1}).say(mydata + " inches");
});
}
});
});
});
tcpServer.listen(1337);
var twilio_app = cli.account.getApplication(process.env.app_sid, function (err, app) {
if (err) {
throw err;
}
app.register();
app.on('incomingCall', function (call) {
if (arduinoTcp === null) {
call.say("I can't do that for you, Dave. I'm offline.");
} else {
curr_call = call;
call.gather(getDigits, {numDigits: 1}).say("Use 2, 4, 6, and 8 to drive. Press 0 to stop, 5 for distance.");
}
});
});