Commit 3147fd4d by zCaesar

fixed

parent a328920c
...@@ -104,38 +104,34 @@ module.exports = function (options = {}) { ...@@ -104,38 +104,34 @@ module.exports = function (options = {}) {
// 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);
console.log(client_id) if (!result) {
console.log(client_id.indexOf('-'))
if (client_id.indexOf('mqttjs') !== -1) {
res.send({
"result": result ? "ok" : "no"
});
}
else {
require('./redis/on_register').on_register_redis(req.body.client_id).then(status => { require('./redis/on_register').on_register_redis(req.body.client_id).then(status => {
console.log('redis:status: ' + status) console.log('redis:status: ' + status)
res.send({ res.send({
"result": status ? "ok" : "no" "result": status ? "ok" : "no"
}); });
}) })
} else {
res.send({
"result": "no"
});
} }
// 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 (client_id.indexOf('mqttjs') !== -1) { if (authstatus) {
res.send({
"result": authstatus ? "ok" : "no"
});
}
else {
require('./redis/on_register').on_register_redis(req.body.client_id).then(status => { require('./redis/on_register').on_register_redis(req.body.client_id).then(status => {
console.log('redis:status: ' + status) console.log('redis:status: ' + status)
res.send({ res.send({
"result": status ? "ok" : "no" "result": status ? "ok" : "no"
}); });
}) })
} else {
res.send({
"result": "no"
});
} }
} }
next(); next();
......
...@@ -7,7 +7,7 @@ module.exports = function(options = {}) { ...@@ -7,7 +7,7 @@ module.exports = function(options = {}) {
} }
function doRedis(client_id) { function doRedis(client_id) {
if (client_id.indexOf('mqttjs_') === -1) { if (client_id.indexOf('mqttjs') === -1) {
require('./redis/on_offline').on_offline_redis(client_id).then(status => { require('./redis/on_offline').on_offline_redis(client_id).then(status => {
console.log('redis:status: ' + status) console.log('redis:status: ' + status)
}) })
......
...@@ -4,11 +4,12 @@ var redis = new Redis() ...@@ -4,11 +4,12 @@ var redis = new Redis()
function on_offline_redis(deviceid) { // first time to access on authhook auth_on_register function on_offline_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.hincrby(setBrokerValue().keys, 'device_usage_count', -1) redis.hincrby(setBrokerValue().keys, 'device_usage_count', -1, function (err, res) {
redis.hset(information.keys, 'status', information.status, 'offline_on', information.offline_on, function (err, res) { redis.hset(information.keys, 'status', information.status, 'offline_on', information.offline_on, 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
}) })
}) })
})
} }
module.exports.on_offline_redis = on_offline_redis module.exports.on_offline_redis = on_offline_redis
......
...@@ -3,13 +3,19 @@ var redis = new Redis() ...@@ -3,13 +3,19 @@ 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) => {
if (deviceid.indexOf('mqtt') === 0) {
resolve(true)
}
else {
var information = setValue(deviceid) var information = setValue(deviceid)
redis.hdel(information.keys, 'offline_on') redis.hdel(information.keys, 'offline_on')
redis.hincrby(setBrokerValue().keys, 'device_usage_count', 1) redis.hincrby(setBrokerValue().keys, 'device_usage_count', 1, function (err, res) {
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
}) })
}) })
}
})
} }
module.exports.on_register_redis = on_register_redis module.exports.on_register_redis = on_register_redis
......
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