Commit 61bbce57 by Chavee Issariyapat

update topic protocol

parent 8b9e2a85
......@@ -27,15 +27,17 @@ module.exports = function (options = {}) {
var authstatus = cache.get(cachekey);
// checkRole
var checkRealDB = require('./checkClientRole').checkRealDB
var checkRealDB = require('./checkClientRole').checkRealDB;
checkRealDB(req.body.username, (err, decoded) => {
if (err) {
// Set Response
var isShadow = require('./utils/getTopic').isShadow
if (isShadow(topic)) {
var output = {};
var getGroupID = require('./utils/getGroupID').getGroupID
getGroupID(req.body.username, req.body.client_id, (_id) => { // get groupID
var _ftopic = require('./utils/getTopic').rewriteTopic(topic, 'pub', _id, req.body.client_id) // get topic where concat with groupID
var _ftopic = require('./utils/getTopic').rewriteTopic(topic, 'pub', _id, req.body.client_id, output); // get topic where concat with groupID
var response = {
'result': 'ok',
'modifiers': {
......@@ -44,7 +46,12 @@ module.exports = function (options = {}) {
'retain': false
}
}
console.log(response)
if (output.verb == 'get' || output.verb == 'read') {
response.modifiers.payload = Buffer.from(req.body.client_id).toString('base64');
}
console.log(response);
if (typeof (authstatus) == 'undefined') {
// cache missed
......
......@@ -97,9 +97,7 @@ module.exports = function (options = {}) {
}
}
else {
console.log('>>>>>>>>>>>>>>>>>>>')
console.log(decoded)
console.log('>>>>>>>>>>>>>>>>>>>')
if (decoded.role === 'realtimedb') {
var response = {
'result': 'ok'
......
......@@ -21,18 +21,22 @@
var modify = require('./modify');
function rewriteTopic(topic, op, groupid, clientid) {
var chunk = topic.split('/', 2);
function rewriteTopic(topic, op, groupid, clientid, output) {
var chunk = topic.split('/');
switch (chunk[0]) {
case '@msg' :
return modify.insertWordIntoTopic(topic, '!'+clientid, 1);
case '@shadow' :
if (chunk[1] == 'write')
if (chunk[1] == 'write' || chunk[1] == 'read') {
output.verb = chunk[1];
return modify.insertWordIntoTopic(topic, ''+clientid, 2);
else if (chunk[1] == 'set')
return modify.insertWordIntoTopic(topic, ''+groupid, 2);
}
else if (chunk[1] == 'set' || chunk[1] == 'get') {
output.verb = chunk[1];
return modify.insertWordIntoTopic(topic, groupid, 2);
}
else if (chunk[1] == 'changed') {
if (op == 'pub') {
......
module.exports.insertWordIntoTopic = insertWordIntoTopic
module.exports.removeTempWordInTopic = removeTempWordInTopic
module.exports.replaceWords = replaceWords
module.exports.cleanUpTopic = cleanUpTopic
/*
Ex: insertWordIntoTopic('@shadow/home/temp', '!GROUP', 1)
......@@ -31,7 +33,20 @@ function removeTempWordInTopic(topic) {
return a.join('/');
}
function replaceWords(topic, word, from, to) {
var a = topic.split('/');
a.splice(from,to-from+1);
if (word != null) a.splice(from, 0, word);
return a.join('/');
}
// Sample
// var str = insertWordIntoTopic('@shadow/home/temp', '!GROUP', 0);
// console.log(str);
// console.log(removeTempWordInTopic(str));
//console.log(replaceWords('@shadow/set/home/bob/bedroom/temp', 'CLIENTID-XXXXX', 2, 3));
function cleanUpTopic(topic) {
topic = topic.trim();
if (topic.substr(-1)=='/') return topic.slice(-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