-
Notifications
You must be signed in to change notification settings - Fork 94
/
bots.js
49 lines (44 loc) · 1.1 KB
/
bots.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
/*jslint node: true */
"use strict";
var db = require('./db.js');
var objectHash = require('./object_hash.js');
var device = require('./device.js');
var async = require('async');
var bots_cache = [];
function getBotByID(id, cb) {
for (var i in bots_cache) {
var bot = bots_cache[i];
if (bot.id == id) {
return setPairingStatus(bot, cb);
}
}
}
function load(cb) {
device.requestFromHub("hub/get_bots", false, function(err, bots){
if (err != null) {
return cb(err, null);
}
async.eachSeries(bots,
function(bot, cb) {
setPairingStatus(bot, function(handled_bot){
bot.isPaired = handled_bot.isPaired;
cb();
})
},
function(){
bots_cache = bots;
cb(err, bots);
}
);
})
}
function setPairingStatus(bot, cb) {
var pubkey = bot.pairing_code.substr(0, bot.pairing_code.indexOf('@'));
bot.device_address = objectHash.getDeviceAddress(pubkey);
db.query("SELECT 1 FROM correspondent_devices WHERE device_address = ?", [bot.device_address], function(rows){
bot.isPaired = (rows.length == 1);
cb(bot);
});
}
exports.load = load;
exports.getBotByID = getBotByID;