-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathENOCOFFEE.js
More file actions
438 lines (380 loc) · 13.4 KB
/
ENOCOFFEE.js
File metadata and controls
438 lines (380 loc) · 13.4 KB
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
var serialPort = require("serialport"); //npm install serialport
var sf = require("sf"); //npm install sf
var irc = require("irc"); //npm install node-irc
var os = require("os");
var repl = require("repl");
var net = require("net");
var crypto = require("crypto");
// Configs & Globals
var arduinoSerialPath = null;
var serial = null;
var sensorValueBuffer = [800,800,800,800,800];
var sensorValueAvg = 0;
var latestUpdate = null;
var coffeeAmountSamples = [800, 800, 800, 800, 800]; // to observe when the coffee is added.
var coffeeLimits = [[35, "no coffee", 0],
[36, "practically no coffee", 5],
[60, "less than a quarter of a packet", 90],
[80, "quarter of a packet", 125],
[130, "half a packet", 250],
[190, "one packet", 500],
[280, "one and half packets", 750],
[360, "two packets", 1000],
[460, "two and half packets", 1250],
[540, "three packets", 1500],
[640, "three and half packets", 1750],
[720, "four packets", 2000],
[810, "four and half packets", 2250],
[800, "five packets or more \\:D/", 2500]];
var ircChannel = "#test-coffeebot";
var ircServer = "irc.cc.tut.fi";
var ircPort = 6667;
var botNick = "CoffeeBot";
var ircUseSecure = false;
var serialPathTO = null;
var initSerialTO = null;
var args = process.argv.splice(2);
for(var i=0;i<args.length;i++) {
if(args[i].indexOf("=") == -1) {
console.log(args[i] + " not valid. Should be key=value.");
continue;
}
var key = args[i].split("=")[0];
var value = args[i].split("=")[1];
switch (key) {
case "botNick":
botNick = value;
break;
case "ircServer":
ircServer = value;
break;
case "port":
ircPort = parseInt(value);
break;
case "secure":
if(value === "true") {
ircUseSecure = true;
} else {
ircUseSecure = false;
}
break;
case "ircChannel":
ircChannel = value;
break;
default:
console.log("Unknown: " + key + " " + value);
}
}
var ircConfiguration = {
userName: 'coffeebot',
realName: 'Your friendly coffee servant',
port: ircPort,
debug: true,
showErrors: true,
autoRejoin: true,
autoConnect: true,
channels: [ircChannel],
secure: ircUseSecure,
selfSigned: true,
certExpired: true,
floodProtection: false,
floodProtectionDelay: 1000,
messageSplit: 512
};
var eventArray = [{id:"500warn", type:"below", limit:190, active:true, timelimit:10*60, timeoutId:null,
action:function()
{
ircnotice("ELOWCOFFEE: ~500 g remaining!");
deactivateEvent("500warn");
activateEvent("activatewarns");
}},
{id:"250warn", type:"below", limit:120, active:true, timelimit:10*60, timeoutId:null,
action:function()
{
ircnotice("EVERYLOWCOFFEE: ~250 g remaining!");
deactivateEvent("250warn");
activateEvent("activatewarns");
}},
{id:"100warn", type:"below", limit:60, active:true, timelimit:10*60, timeoutId:null,
action:function()
{
ircnotice("ECOFFEECRITICAL: We're running on fumes!");
deactivateEvent("250warn");
activateEvent("activatewarns");
}},
{id:"activatewarns", type:"over", limit:191, active:false, timelimit:5*60, timeoutId:null,
action:function()
{
activateEvent("500warn");
activateEvent("250warn");
activateEvent("100warn");
//ircnotice("Coffee++ -- coffee now at " + getCoffeeEstimateStr() + ".");
}}
];
function determineIfCoffeeAdded() {
if ((coffeeAmountSamples[0]+coffeeAmountSamples[1]+coffeeAmountSamples[2]+coffeeAmountSamples[3]+coffeeAmountSamples[4])/5+40 < sensorValueAvg) {
setTimeout(function() {ircnotice("Coffee++ -- coffee now at " + getCoffeeEstimateStr() + ".");}, 5000); // wait a bit to let the average settle.
coffeeAmountSamples[0] = sensorValueAvg;
coffeeAmountSamples[1] = sensorValueAvg;
coffeeAmountSamples[2] = sensorValueAvg;
coffeeAmountSamples[3] = sensorValueAvg;
coffeeAmountSamples[4] = sensorValueAvg;
}
coffeeAmountSamples.push(sensorValueAvg);
coffeeAmountSamples.shift();
}
var bot = null;
// Startup
net.createServer(function (socket) {
connections += 1;
repl.start({
prompt: "node via Unix socket> ",
input: socket,
output: socket
}).on('exit', function() {
socket.end();
})
}).listen("/tmp/node-repl-sock-" + crypto.randomBytes(4).readUInt32LE(0));
determineArduinoSerialPath();
initializeIRC();
initializeSerial();
setInterval(function() { if (((new Date() - latestUpdate) / 1000) > 30) { console.log("Serial unresponsive, re-initializing it...");
determineArduinoSerialPath();
initializeSerial(); }}, 30*1000);
setInterval(determineIfCoffeeAdded, 2*60*1000);
// Arduino stuff
var pre = "sensor:",
post = "-units"; // Arduino should put out something like "Sensor:150-units\r\n"
var serialBuffer = "";
function setInitSerialTimeout(timeout) {
try {
clearTimeout(initSerialTO);
} catch(err) {
}
initSerialTO = setTimeout(initializeSerial, timeout);
}
function setSerialPathSearchTimeout(timeout) {
try {
clearTimeout(serialPathTO);
} catch(err) {
}
serialPathTO = setTimeout(determineArduinoSerialPath, timeout);
}
function determineArduinoSerialPath() {
// Finds arduino serial port and appends it to "arduinoserial" global variable. Only works properly if there is only
// one existing tty where manufacturer string contains "Arduino".
var arduinoSerials = [];
serialPort.list(function (err, results) {
if (err) {
throw err;
}
var found = false;
for (var i = 0; i < results.length; i++) {
if (typeof results[i]["manufacturer"] !== "undefined") {
if (results[i]["manufacturer"].indexOf("Arduino") !== -1) {
found = true;
arduinoSerials.push(results[i]["comName"]);
//console.log("Arduino serial found: " + arduinoSerialPath);
}
} else if (results[i]["pnpId"].indexOf("Arduino") !== -1) {
found = true;
arduinoSerials.push(results[i]["comName"]);
//console.log("Arduino serial found: " + arduinoSerialPath);
}
}
if (!found) {
console.log("Could not find arduino serial path. Trying again in one second...");
setSerialPathSearchTimeout(1000);
} else {
arduinoSerialPath = arduinoSerials[Math.floor(Math.random() * arduinoSerials.length)]; // Select random serial.
console.log("Found arduino serial(s). Selected: " + arduinoSerialPath + " of " + arduinoSerials.toString());
}
});
}
function initializeSerial() {
var serialFailed = false;
if (!arduinoSerialPath) {
console.log("No arduino serial path set, aborting serial initialization. Trying again in one second...");
setInitSerialTimeout(1000);
return;
}
try {
serial.close();
} catch(err) {
console.log("Serial.close err: ");
console.log(err);
// We are merely doing best effort to clean up old serial instances...
}
serial = new serialPort.SerialPort(arduinoSerialPath, {baudrate : 9200}, true, function (err) {
if (err) {
console.log("Failed to open Arduino Port: " + err);
serialFailed = true;
}
});
if(serialFailed) {
setSerialPathSearchTimeout(5000);
setInitSerialTimeout(7500);
return;
}
console.log("Arduino serial opened: " + arduinoSerialPath);
serial.on("data", handleSerialData);
serial.on("error", handleSerialErr);
}
function handleSerialErr(msg) {
console.log("Serial port error: " + msg);
setSerialPathSearchTimeout(2000);
setInitSerialTimeout(10000);
try {
serial.close();
}
catch(err) {}
serial = null;
}
function handleSerialData(chunk) {
serialBuffer += chunk;
serialBuffer = serialBuffer.replace(/ /g,'').replace(/(\r\n|\n|\r)/gm,""); //remove whitespaces etc.
console.log("Got buf, length:" + serialBuffer.length);
if (serialBuffer.indexOf(pre) !== -1 && serialBuffer.indexOf(post) !== -1) {
var parseSnippet = serialBuffer.substring(serialBuffer.indexOf(pre)+pre.length,serialBuffer.indexOf(post));
var parseResult = parseInt(parseSnippet);
sensorValueBuffer.push(parseResult);
sensorValueBuffer.shift();
latestUpdate = new Date();
//console.log(parseResult);
var sum = 0;
for(var i=0; i < sensorValueBuffer.length; i++) {
sum += sensorValueBuffer[i];
}
sensorValueAvg = sum / sensorValueBuffer.length;
}
serialBuffer = "";
handleEvents();
}
//{id:"500warn", type:"below", limit:200, active:true, timelimit:10*60, timeoutId:null,
// action:function(){ircmsg("ELOWCOFFEE: <500 g remaining!");deactivateEvent("500warn");}}
function handleEvents() {
for(var i=0;i<eventArray.length;i++) {
var e = eventArray[i];
if (!e.active) {
continue;
}
if (e.type === "below") {
if (e.timeoutId === null && e.limit > sensorValueAvg) {
//Set timer
e.timeoutId = setTimeout(e.action, e.timelimit*1000);
} else if (e.timeoutId !== null && e.limit < sensorValueAvg) {
clearTimeout(e.timeoutId);
e.timeoutId = null;
}
} else if (e.type === "over") {
if (e.timeoutId === null && e.limit < sensorValueAvg) {
//Set timer
e.timeoutId = setTimeout(e.action, e.timelimit*1000);
} else if (e.timeoutId !== null && e.limit > sensorValueAvg) {
//Remove timer
clearTimeout(e.timeoutId);
e.timeoutId = null;
}
}
}
}
// IRC stuff
function initializeIRC() {
console.log("Initializing IRC connection...");
bot = new irc.Client(ircServer, botNick, ircConfiguration);
// bot.addListener("raw", function(message) {console.log("IRC activity: %j", message); } );
bot.addListener("message", onIrcMessage);
bot.addListener('error', function(message) {
console.log('IRC error: ', message);
});
bot.addListener('netError', function(error) {
console.log('IRC netError: ' + error);
});
bot.addListener('names', function(chan, nicks) {
console.log("Connected to channel " + ircChannel);
});
}
function ircmsg(msg) {
try {
bot.say(ircChannel, msg);
} catch (err) {
determineArduinoSerialPath();
initializeIRC();
initializeSerial();
setTimeout(5000, ircmsg(msg));
}
}
function ircnotice(msg) {
try {
bot.notice(ircChannel, msg);
} catch (err) {
determineArduinoSerialPath();
initializeIRC();
initializeSerial();
setTimeout(5000, ircnotice(msg));
}
}
function ircop(to_be_opped) {
try {
bot.send("MODE", ircChannel, "+o", to_be_opped);
} catch (err) {
console.log("Error !ircopping: " + err.toString());
}
}
function onIrcMessage(from, to, message) {
if (message.substring(0,7) === "!coffee") {
var msg = "Coffee currently at " + getCoffeeEstimateStr() + ". (Latest update " + ((new Date() - latestUpdate) / 1000).toString() + " seconds ago)";
if (to === botNick) {
bot.say(from, msg);
} else {
ircmsg(msg);
}
} else if (message.substring(0, 6) === "!getip") {
if (to === botNick) {
var ifaces = os.networkInterfaces();
for (var dev in ifaces) {
ifaces[dev].forEach(function(details) {
if (details.family=="IPv4") {
bot.say(from, details.address.toString());
}
});
}
}
} else if (message.substring(0,5) === "!arvo") {
if(message.indexOf(" ") !== -1) {
var values = message.split(" ");
values.shift();
var result = values[Math.floor(Math.random() * values.length)];
ircmsg(result);
}
} else if (message.substring(0,3) === "!op") {
if (to === botNick) {
ircop("Mutjake");
}
}
}
// Helper functions
function deactivateEvent(id) {
for(var i=0;i<eventArray.length;i++) {
if (eventArray[i].id == id) {
eventArray[i].active = false;
}
}
}
function activateEvent(id) {
for(var i=0;i<eventArray.length;i++) {
if (eventArray[i].id == id) {
eventArray[i].active = true;
}
}
}
function getCoffeeEstimateStr() {
for(var i=1;i<coffeeLimits.length;i++) {
if (coffeeLimits[i][0] > sensorValueAvg) {
var returnVal = coffeeLimits[i-1][1];
//console.log(returnVal);
return returnVal;
}
}
return coffeeLimits[coffeeLimits.length-1][1];
}