-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase_redis.js
More file actions
78 lines (73 loc) · 2.64 KB
/
database_redis.js
File metadata and controls
78 lines (73 loc) · 2.64 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
var redis = require('redis'),
client = redis.createClient();
var EventEmitter = require('events').EventEmitter;
var Proto = new EventEmitter;
function error(err) {
if (err) console.info('[redis]'.grey, 'error'.red);
else console.info('[redis]'.grey, 'OK'.green);
}
Proto.on('record', function() {
console.info('[redis]'.grey, 'record added');
});
Proto.addRecord = function(gps_msg) {
var timestamp = gps_msg.timestamp.toString();
var key = gps_msg.module_id + ':' + timestamp;
var timestamps = ['timestamps:' + gps_msg.module_id, timestamp, key];
//var modulesId = ['modules-id', gps_msg.module_id]; //is it necessary??
var waypoint = ['waypoints:' + key];
for(property in gps_msg) {
waypoint.push(property)
waypoint.push(gps_msg[property].toString())
}
client.multi()
.zadd(timestamps, error)
//.sadd(modulesId, error)
.hmset(waypoint, error)
.exec(error);
this.emit('record', true);
}
Proto.updateModuleList = function(changes) {
if(changes) {
console.log('[redis]'.grey, 'add', changes.add, 'remove', changes.remove);
if (changes.add.length > 0) {
client.sadd(['tracklist'].concat(changes.add), error);
}
if (changes.remove.length > 0) {
client.srem(['tracklist'].concat(changes.remove), error);
}
this.getList({'client': 'server'});
}
}
Proto.getList = function(request) {
var dst = request.client;
var client_id = request.socket_id;
console.log('[redis]'.grey, dst, 'requested tracking list');
client.smembers('tracklist', function (err, list) {
if (err) console.info('[redis]'.grey, 'error'.red);
console.info('[redis]'.grey, 'found', list == undefined ? '0' : list.length, 'in tracking list');
Proto.emit('tracklist-' + dst, {'socket_id': client_id, 'list': list});
});
}
Proto.query = function(request) {//must introduce query by id
var begin = request.begin;
var end = request.end;
var module_id = request.module_id;
var client_id = request.socket_id;
console.log('[redis]'.grey, 'Query', module_id, begin, '..', end);
client.zrangebyscore(['timestamps:' + module_id, begin, end], function (err, replies) {
if (undefined != replies) {
console.log('[redis]'.grey, 'found', replies.length);
replies.forEach(function (key, i) {
client.hgetall('waypoints:' + key, function(err, hash) {
//console.log('[redis]', 'hgetall got'.grey, Proto.result.length);
Proto.emit('result', {'socket_id': client_id, 'result': hash});
});
});
//console.log('[redis], 'zrangebyscore got'.grey, Proto.result.length);
//Proto.emit('result', Proto.result);
}
var count = replies.length == undefined ? 0 : replies.length;
Proto.emit('count', {'socket_id': client_id, 'count': count});
});
}
module.exports = Proto;