Commit daabfd9d by zCaesar

add usage

parent 1b439aca
...@@ -15,57 +15,63 @@ module.exports = function (options = {}) { ...@@ -15,57 +15,63 @@ module.exports = function (options = {}) {
console.log(req.body); console.log(req.body);
} }
var authstatus; // save redis
var topic = req.body.topic; require('./redis/on_message').on_message_redis(req.body.client_id, req.body.payload).then(status => {
if (status) {
var authstatus;
var topic = req.body.topic;
var cachekey = 'pub:' + req.body.client_id + ':' + req.body.username + ':' + topic; var cachekey = 'pub:' + req.body.client_id + ':' + req.body.username + ':' + topic;
var authstatus = cache.get(cachekey); var authstatus = cache.get(cachekey);
if (typeof (authstatus) === 'undefined') { if (typeof (authstatus) === 'undefined') {
cache.set(cachekey, true); // cache missed cache.set(cachekey, true); // cache missed
} }
var response
var decoded = require('jwt-verify').verify(req.body.username).res
if (decoded) {
if (decoded.role === 'realtimedb') response = { 'result': 'ok' }
else response = { 'result': 'no' }
res.send(response);
next();
}
else {
var GGID = require('./utils/getGroupID');
var output = {}; var response
GGID.getGroupID(req.body.username, req.body.client_id, function (group) {
if (group) {
var _ftopic = require('./utils/router').rewriteTopic(topic, 'pub', group, req.body.client_id, output); // get topic where concat with groupID
response = {
'result': 'ok',
'modifiers': {
'topic': _ftopic,
'qos': 0,
'retain': false
}
}
if (options.debug) { var decoded = require('jwt-verify').verify(req.body.username).res
console.log(response);
}
if (output.verb == 'get' || output.verb == 'read') { if (decoded) {
response.modifiers.payload = Buffer.from(req.body.client_id).toString('base64'); if (decoded.role === 'realtimedb') response = { 'result': 'ok' }
} else response = { 'result': 'no' }
res.send(response); res.send(response);
next(); next();
} }
else { else {
res.send({ "result": "no" }); var GGID = require('./utils/getGroupID');
next();
var output = {};
GGID.getGroupID(req.body.username, req.body.client_id, function (group) {
if (group) {
var _ftopic = require('./utils/router').rewriteTopic(topic, 'pub', group, req.body.client_id, output); // get topic where concat with groupID
response = {
'result': 'ok',
'modifiers': {
'topic': _ftopic,
'qos': 0,
'retain': false
}
}
if (options.debug) {
console.log(response);
}
if (output.verb == 'get' || output.verb == 'read') {
response.modifiers.payload = Buffer.from(req.body.client_id).toString('base64');
}
res.send(response);
next();
}
else {
res.send({ "result": "no" });
next();
}
});
} }
}); }
} })
} }
else { else {
res.send({ "result": "no" }); res.send({ "result": "no" });
......
> 5k : 2msg
online_duration
last_checked
msg_count_real
msg_count_per_bit
msg_size
last_publish
\ No newline at end of file
var Redis = require('ioredis')
var redis = new Redis()
function on_message_redis(deviceid, payload) { // first time to access on authhook auth_on_register
return new Promise((resolve, reject) => {
var information = setValue(deviceid, payload)
redis.hincrby(information.keys, 'actual_message_count', 1)
redis.hincrby(information.keys, 'charged_message_count', information.charged_message_count)
redis.hincrby(information.keys, 'total_message_size', information.total_message_size)
redis.hset(information.keys, 'last_publish', information.last_publish, function (err, res) {
resolve(!res) // 0 is ok, 1 is no => if ok is 0 then not 0 = 1
})
})
}
module.exports.on_message_redis = on_message_redis
function setValue(deviceid, payload) {
var dateNow = Math.floor(Date.now() / 1000)
var keys = 'deviceid:' + deviceid
var last_publish = dateNow
var msg_count_size = payload.length * 3 / 4
var charged_message_count = 1
while (msg_count_size > 5000) {
charged_message_count += 1
msg_count_size = (payload.length * 3 / 4) - 5000
}
var total_message_size = payload.length * 3 / 4
return {
keys: keys,
last_publish: last_publish,
charged_message_count: charged_message_count,
total_message_size: total_message_size
}
}
\ No newline at end of file
...@@ -4,6 +4,7 @@ var redis = new Redis() ...@@ -4,6 +4,7 @@ var redis = new Redis()
function on_register_redis(deviceid) { // first time to access on authhook auth_on_register function on_register_redis(deviceid) { // first time to access on authhook auth_on_register
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
var information = setValue(deviceid) var information = setValue(deviceid)
redis.hdel(information.keys, 'offline_on')
redis.hset(information.keys, 'status', information.status, 'register_on', information.register_on, 'last_check', information.last_check, function (err, res) { redis.hset(information.keys, 'status', information.status, 'register_on', information.register_on, 'last_check', information.last_check, function (err, res) {
resolve(!res) // 0 is ok, 1 is no => if ok is 0 then not 0 = 1 resolve(!res) // 0 is ok, 1 is no => if ok is 0 then not 0 = 1
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment