Skip to content

Commit d956aa1

Browse files
committed
Merge pull request NARKOZ#93 from kashyapakshay/master
NodeJS for fucking_coffee to trigger using Yo.
2 parents bedd801 + ef18224 commit d956aa1

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

nodejs/fucking_coffee_yo_server.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env node
2+
3+
/* Before running:
4+
Setup Yo Callback URL and Yo username for coffee machine:
5+
http://docs.justyo.co/docs/receiving-a-yo-with-the-api
6+
*/
7+
8+
var exec = require('child_process').exec;
9+
var telnet = require('telnet-client');
10+
11+
var ME = 'my_username';
12+
var AUTHORIZED_YO_NAMES = [ME];
13+
var COFFEE_MACHINE_YO_NAME = 'coffeemachine';
14+
15+
// These should be same as what you set up in the Yo API
16+
var CALLBACK_URL = 'http://xxx.com';
17+
var CALLBACK_ENDPOINT = '/coffeemachine';
18+
19+
var PORT = '3000';
20+
21+
exec("who -q", function(error, stdout, stderr) {
22+
23+
var express = require('express');
24+
var coffeeApp = express();
25+
26+
// Exit if no sessions with my username are found
27+
if(stdout.indexOf(ME) == -1)
28+
process.exit(1);
29+
30+
// Got a Yo!
31+
coffeeApp.get(CALLBACK_ENDPOINT, function (req, res) {
32+
33+
if(req.query.username === undefined) {
34+
// Not a Yo, don't make coffee.
35+
res.sendStatus(401);
36+
}
37+
else if(AUTHORIZED_YO_NAMES.indexOf(req.query.username) == -1) {
38+
// If authorized users didn't Yo, don't make coffee.
39+
res.sendStatus(401);
40+
41+
console.log(req.query.username + ' YO\'d.')
42+
}
43+
else {
44+
res.sendStatus(200);
45+
46+
var coffee_machine_ip = 'xxx.xxx.xxx.xxx';
47+
var password = 'xxxx';
48+
var con = new telnet();
49+
50+
con.on('ready', function(prompt) {
51+
con.exec('Password: ' + password, function(error, res) {
52+
53+
// Brew Coffee!
54+
con.exec('sys brew', function(error, res) {
55+
56+
// Wait for 24s
57+
setTimeout(function() {
58+
59+
// Pour Coffee!
60+
con.exec('sys pour', function(error, res) {
61+
con.end();
62+
});
63+
}, 24000);
64+
});
65+
});
66+
});
67+
68+
con.connect({host: coffee_machine_ip});
69+
}
70+
});
71+
72+
// Not Callback endpoint
73+
coffeeApp.get('/*', function (req, res) {
74+
res.sendStatus(404);
75+
});
76+
77+
var coffeeServer = coffeeApp.listen(PORT, CALLBACK_URL, function() {
78+
console.log('Coffee Server listening at %s:%s',
79+
CALLBACK_URL, PORT);
80+
console.log('\nYo Callback URL: %s:%s/%s',
81+
CALLBACK_URL, PORT, CALLBACK_ENDPOINT);
82+
});
83+
});

0 commit comments

Comments
 (0)