Commit 742e2089 by zCaesar

add handler error from redis

parent cefb2042
...@@ -61,8 +61,13 @@ module.exports = function (options = {}) { ...@@ -61,8 +61,13 @@ module.exports = function (options = {}) {
response.modifiers.payload = Buffer.from(req.body.client_id).toString('base64'); response.modifiers.payload = Buffer.from(req.body.client_id).toString('base64');
} }
// save on redis before send response // save on redis before send response
on_message_redis(req.body.client_id, req.body.payload, topic) try {
res.send(response); on_message_redis(req.body.client_id, req.body.payload, topic)
res.send(response);
}
catch (e) {
res.send({ result: { error: 'not allowed' } });
}
next(); next();
} }
else { else {
......
...@@ -62,28 +62,33 @@ module.exports = function (options = {}) { ...@@ -62,28 +62,33 @@ module.exports = function (options = {}) {
} }
var cachekey = 'auth:' + req.body.client_id + ':' + req.body.username + ':' + req.body.password; var cachekey = 'auth:' + req.body.client_id + ':' + req.body.username + ':' + req.body.password;
var authstatus = cache.get(cachekey); var authstatus = cache.get(cachekey);
if (typeof (authstatus) == 'undefined') { // if (typeof (authstatus) == 'undefined') {
// cache missed // cache missed
authCheck(req.body.client_id, req.body.username, req.body.password, function (result) { // authCheck(req.body.client_id, req.body.username, req.body.password, function (result) {
cache.set(cachekey, result); // cache.set(cachekey, result);
if (result) { // if (result) {
on_register_redis(req.body.client_id) try {
res.send({ result: 'ok' }); on_register_redis(req.body.client_id)
} else { res.send({ result: 'ok' });
res.send({ result: { error: 'not allowed' } }); }
} catch (e) {
res.send({ result: { error: 'not allowed' } });
}
// } else {
// res.send({ result: { error: 'not allowed' } });
// }
// const util = require('util') // const util = require('util')
// console.log(util.inspect(status, false, null, true)) // console.log(util.inspect(status, false, null, true))
}); // });
} // }
else { // else {
if (authstatus) { // if (authstatus) {
on_register_redis(req.body.client_id) // on_register_redis(req.body.client_id)
res.send({ result: 'ok' }); // res.send({ result: 'ok' });
} else { // } else {
res.send({ result: { error: 'not allowed' } }); // res.send({ result: { error: 'not allowed' } });
} // }
} // }
next(); next();
} }
else { else {
......
...@@ -21,6 +21,18 @@ function on_message_redis(deviceid, payload, topic) { // first time to access on ...@@ -21,6 +21,18 @@ function on_message_redis(deviceid, payload, topic) { // first time to access on
} }
module.exports.on_message_redis = on_message_redis module.exports.on_message_redis = on_message_redis
redis.on("error", (error) => {
console.log("Redis connection error", error);
});
redis.on('reconnecting', function reconnecting() {
console.log('Connection reestablished');
});
redis.on('connect', function connect() {
console.log('connecting');
});
function setValue(deviceid, payload, topic) { function setValue(deviceid, payload, topic) {
var dateNow = Math.floor(Date.now() / 1000) var dateNow = Math.floor(Date.now() / 1000)
var keys = 'deviceid:' + deviceid var keys = 'deviceid:' + deviceid
......
...@@ -12,6 +12,18 @@ function on_offline_redis(deviceid) { // first time to access on authhook auth_o ...@@ -12,6 +12,18 @@ function on_offline_redis(deviceid) { // first time to access on authhook auth_o
} }
module.exports.on_offline_redis = on_offline_redis module.exports.on_offline_redis = on_offline_redis
redis.on("error", (error) => {
console.log("Redis connection error", error);
});
redis.on('reconnecting', function reconnecting() {
console.log('Connection reestablished');
});
redis.on('connect', function connect() {
console.log('connecting');
});
function setValue(deviceid) { function setValue(deviceid) {
var dateNow = Math.floor(Date.now() / 1000) var dateNow = Math.floor(Date.now() / 1000)
var keys = 'deviceid:' + deviceid var keys = 'deviceid:' + deviceid
......
...@@ -14,6 +14,18 @@ function on_register_redis(deviceid) { // first time to access on authhook auth_ ...@@ -14,6 +14,18 @@ function on_register_redis(deviceid) { // first time to access on authhook auth_
} }
module.exports.on_register_redis = on_register_redis module.exports.on_register_redis = on_register_redis
redis.on("error", (error) => {
console.log("Redis connection error", error);
});
redis.on('reconnecting', function reconnecting() {
console.log('Connection reestablished');
});
redis.on('connect', function connect() {
console.log('connecting');
});
function setValue(deviceid) { function setValue(deviceid) {
var dateNow = Math.floor(Date.now() / 1000) var dateNow = Math.floor(Date.now() / 1000)
var keys = 'deviceid:' + deviceid var keys = 'deviceid:' + deviceid
......
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