Commit daabfd9d by zCaesar

add usage

parent 1b439aca
...@@ -15,6 +15,9 @@ module.exports = function (options = {}) { ...@@ -15,6 +15,9 @@ module.exports = function (options = {}) {
console.log(req.body); console.log(req.body);
} }
// save redis
require('./redis/on_message').on_message_redis(req.body.client_id, req.body.payload).then(status => {
if (status) {
var authstatus; var authstatus;
var topic = req.body.topic; var topic = req.body.topic;
...@@ -67,6 +70,9 @@ module.exports = function (options = {}) { ...@@ -67,6 +70,9 @@ module.exports = function (options = {}) {
}); });
} }
} }
})
}
else { else {
res.send({ "result": "no" }); res.send({ "result": "no" });
next(); next();
......
> 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