Commit cefb2042 by zCaesar

modify redis statement

parent a23d9730
var config = require('config');
// https://github.com/isaacs/node-lru-cache
var on_message_redis = require('./redis/on_message').on_message_redis
var LRU = require("lru-cache"),
cache = LRU({
max: 500,
......@@ -17,9 +19,6 @@ module.exports = function (options = {}) {
var topic = req.body.topic;
// save redis
require('./redis/on_message').on_message_redis(req.body.client_id, req.body.payload, topic).then(status => {
if (status) {
var authstatus;
var cachekey = 'pub:' + req.body.client_id + ':' + req.body.username + ':' + topic;
......@@ -34,7 +33,7 @@ module.exports = function (options = {}) {
if (decoded) {
if (decoded.role === 'realtimedb') response = { 'result': 'ok' }
else response = { result: {error: 'not allowed'} }
else response = { result: { error: 'not allowed' } }
res.send(response);
next();
}
......@@ -61,21 +60,22 @@ module.exports = function (options = {}) {
if (output.verb == 'get' || output.verb == 'read') {
response.modifiers.payload = Buffer.from(req.body.client_id).toString('base64');
}
// save on redis before send response
on_message_redis(req.body.client_id, req.body.payload, topic)
res.send(response);
next();
}
else {
res.send({ result: {error: 'not allowed'} });
res.send({ result: { error: 'not allowed' } });
next();
}
});
}
}
})
}
else {
res.send({ result: {error: 'not allowed'} });
res.send({ result: { error: 'not allowed' } });
next();
}
}
......
......@@ -3,7 +3,7 @@ var validator = require('./validator');
var config = require('config');
// var seneca = require('seneca')({log: 'silent'}).client({ port: config.get('device_registry_port'), host: config.get('device_registry_host') });
var seneca = require('seneca')({ log: 'silent' }).client({ port: config.get('token_registry_port'), host: config.get('token_registry_host') });
let on_register_redis = require('./redis/on_register').on_register_redis
var on_register_redis = require('./redis/on_register').on_register_redis
// https://github.com/isaacs/node-lru-cache
var LRU = require("lru-cache"),
......@@ -45,45 +45,6 @@ function authCheck(client_id, token, password, callback) {
callback(false);
}
});
// seneca.act('cmd:getAccessTokenInfo, tokencode:'+token, function(err,res) {
// if (!err && res) {
// var token_profile = (res&&res.result&&res.result[0])?res.result[0]:{};
// var mqttauth = {
// clientid : client_id,
// token : token,
// password : password
// };
// callback( validator.auth_connect(mqttauth, token_profile) );
// }
// else {
// callback(false);
// }
// });
// authclient.act({ role: 'auth', cmd: 'token', action: 'info', token: token }, function (err, res) { // auth client device by query from db
// if (debug) {
// console.log("res ------>\n");
// console.log(res);
// }
// if (res && res.data) {
// try {
// var jdata = JSON.parse(res.data);
// if (jdata && jdata.code == 200) {
// callback(true);
// }
// else callback(false);
// } catch (e) {
// callback(false);
// }
// callback(true);
// }
// else {
// callback(false);
// }
// });
}
}
......@@ -101,85 +62,15 @@ module.exports = function (options = {}) {
}
var cachekey = 'auth:' + req.body.client_id + ':' + req.body.username + ':' + req.body.password;
var authstatus = cache.get(cachekey);
<<<<<<< HEAD
// if (typeof (authstatus) == 'undefined') {
// // cache missed
// authCheck(req.body.client_id, req.body.username, req.body.password, function (result) {
// cache.set(cachekey, result);
// if (result) {
// require('./redis/on_register').on_register_redis(req.body.client_id).then(status => {
// console.log('redis:status: ' + status)
// if (status) {
// res.send({
// "result": "ok"
// });
// } else {
// res.send({
// result: { error: 'not allowed' }
// });
// }
// })
// } else {
// res.send({
// result: {error: 'not allowed'}
// });
// }
// // const util = require('util')
// // console.log(util.inspect(status, false, null, true))
// });
// }
// else {
// if (authstatus) {
// require('./redis/on_register').on_register_redis(req.body.client_id).then(status => {
// console.log('redis:status: ' + status)
// if (status) {
// res.send({
// "result": "ok"
// });
// } else {
// res.send({
// result: {error: 'not allowed'}
// });
// }
// })
// } else {
// res.send({
// result: {error: 'not allowed'}
// });
// }
// }
on_register_redis(req.body.client_id).then(status => {
console.log('redis:status: ' + status)
if (status) {
res.send({
"result": "ok"
});
} else {
res.send({
result: { error: 'not allowed' }
=======
if (typeof (authstatus) == 'undefined') {
// cache missed
authCheck(req.body.client_id, req.body.username, req.body.password, function (result) {
cache.set(cachekey, result);
if (result) {
require('./redis/on_register').on_register_redis(req.body.client_id).then(status => {
console.log('redis:status: ' + status)
if (status) {
res.send({
result: 'ok'
});
on_register_redis(req.body.client_id)
res.send({ result: 'ok' });
} else {
res.send({
result: {error: 'not allowed'}
});
}
})
} else {
res.send({
result: {error: 'not allowed'}
});
res.send({ result: { error: 'not allowed' } });
}
// const util = require('util')
// console.log(util.inspect(status, false, null, true))
......@@ -187,35 +78,16 @@ module.exports = function (options = {}) {
}
else {
if (authstatus) {
require('./redis/on_register').on_register_redis(req.body.client_id).then(status => {
console.log('redis:status: ' + status)
if (status) {
res.send({
result: 'ok'
});
on_register_redis(req.body.client_id)
res.send({ result: 'ok' });
} else {
res.send({
result: {error: 'not allowed'}
});
res.send({ result: { error: 'not allowed' } });
}
})
} else {
res.send({
result: {error: 'not allowed'}
>>>>>>> 9c233f79215090b0fd648f8b8b8061447ed746dc
});
}
})
next();
}
else {
res.send({
<<<<<<< HEAD
result: { error: 'not allowed' }
=======
result: 'no'
>>>>>>> 9c233f79215090b0fd648f8b8b8061447ed746dc
});
res.send({ result: { error: 'not allowed' } });
next();
}
}
......
......@@ -15,10 +15,10 @@ module.exports = function(options = {}) {
console.log(response);
res.send(response)
}
module.exports.on_deliver = on_deliver
// module.exports.on_deliver = on_deliver
function joinTopic(topics) {
var topic = topics.split('/!')[1]
return topics.split('/!')[0] + topic.substring(topic.indexOf('/'), topic.length)
}
// function joinTopic(topics) {
// var topic = topics.split('/!')[1]
// return topics.split('/!')[0] + topic.substring(topic.indexOf('/'), topic.length)
// }
}
var on_offline_redis = require('./redis/on_offline').on_offline_redis
module.exports = function (options = {}) {
debug = options.debug || false;
return function (req, res, next) {
console.log('auth_on_offline')
doRedis(req.body.client_id)
on_offline_redis(req.body.client_id)
res.status(200);
res.send('');
}
function doRedis(client_id) {
require('./redis/on_offline').on_offline_redis(client_id).then(status => {
console.log('redis:status: ' + status)
})
}
}
\ No newline at end of file
......@@ -2,10 +2,8 @@ var Redis = require('ioredis')
var redis = new Redis()
function on_message_redis(deviceid, payload, topic) { // first time to access on authhook auth_on_register
return new Promise((resolve, reject) => {
if (deviceid.indexOf('mqtt') === 0 || deviceid.length !== 36) {
if (deviceid.indexOf('mqtt') === 0) {
console.log(deviceid)
resolve(true)
}
else {
var information = setValue(deviceid, payload, topic)
......@@ -18,11 +16,8 @@ function on_message_redis(deviceid, payload, topic) { // first time to access on
redis.hincrby(setBrokerValue().keys, 'charged_message_count', information.charged_message_count)
redis.hincrby(setBrokerValue().keys, 'total_message_size', Math.floor(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
})
redis.hset(information.keys, 'last_publish', information.last_publish)
}
})
}
module.exports.on_message_redis = on_message_redis
......
......@@ -2,22 +2,13 @@ var Redis = require('ioredis')
var redis = new Redis()
function on_offline_redis(deviceid) { // first time to access on authhook auth_on_register
return new Promise((resolve, reject) => {
if (deviceid.indexOf('mqtt') === 0) {
// if (deviceid.indexOf('mqtt') === 0 || deviceid.length !== 36) {
resolve(true)
}
else {
var information = setValue(deviceid)
redis.hset(information.keys, 'status', information.status, 'offline_on', information.offline_on, function (err, res) {
console.log('res:', res)
if (res) {
redis.hset(information.keys, 'status', information.status, 'offline_on', information.offline_on)
deviceUsageCount()
resolve(true) // 0 is ok, 1 is no => if ok is 0 then not 0 = 1
} else resolve(false)
})
}
})
}
module.exports.on_offline_redis = on_offline_redis
......
......@@ -2,24 +2,15 @@ var Redis = require('ioredis')
var redis = new Redis()
function on_register_redis(deviceid) { // first time to access on authhook auth_on_register
return new Promise((resolve, reject) => {
// if (deviceid.indexOf('mqtt') === 0 || deviceid.length !== 36) {
if (deviceid.indexOf('mqtt') === 0) {
resolve(true)
}
else {
var information = setValue(deviceid)
redis.hdel(information.keys, 'offline_on')
console.log(information.keys)
redis.hset(information.keys, 'status', information.status, 'register_on', information.register_on, 'last_check', information.last_check, function (err, res) {
console.log('res: ',res)
// if (res) {
redis.hset(information.keys, 'status', information.status, 'register_on', information.register_on, 'last_check', information.last_check)
deviceUsageCount()
resolve(true) // 0 is ok, 1 is no => if ok is 0 then not 0 = 1
// } else resolve(false)
})
}
})
}
module.exports.on_register_redis = on_register_redis
......
var Redis = require('ioredis')
var redis = new Redis()
redis.subscribe('clientStatus')
\ No newline at end of file
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