Commit d176edea by Chavee Issariyapat

use different topic on @msg pub and sub

parent 9c029cc7
...@@ -7,7 +7,7 @@ const cache = require('./cache'); ...@@ -7,7 +7,7 @@ const cache = require('./cache');
const Coordinator = function(param={}) { const Coordinator = function(param={}) {
let that = this; let that = this;
//console.log(param)
this.remoteclient = param.remoteclient; this.remoteclient = param.remoteclient;
this.localserver = param.localserver; this.localserver = param.localserver;
...@@ -59,8 +59,14 @@ const Coordinator = function(param={}) { ...@@ -59,8 +59,14 @@ const Coordinator = function(param={}) {
this.localserver.on('clientSubscribed', function(packet, client) { this.localserver.on('clientSubscribed', function(packet, client) {
let deviceid = client.id.split(':')[0]; let deviceid = client.id.split(':')[0];
// if client try to subscribe @msg/xxx/yyy --> flowagent subscribe @tap on a remote broker // if client try to subscribe @msg/xxx/yyy --> flowagent subscribe @tap on a remote broker
if (packet.topic.startsWith('@msg/')) {
let msgpart = packet.topic.split('/').splice(3).join('/'); // if (packet.topic.startsWith('@msg/')) {
// let msgpart = packet.topic.split('/').splice(3).join('/');
// outtopic = `@tap/msg/topic/${deviceid}:${client.token}/${msgpart}`;
// that.remoteclient.subscribe(outtopic);
// }
if (packet.topic.startsWith('@local/msgin/')) {
let msgpart = packet.topic.split('/').splice(4).join('/');
outtopic = `@tap/msg/topic/${deviceid}:${client.token}/${msgpart}`; outtopic = `@tap/msg/topic/${deviceid}:${client.token}/${msgpart}`;
that.remoteclient.subscribe(outtopic); that.remoteclient.subscribe(outtopic);
} }
...@@ -92,8 +98,14 @@ const Coordinator = function(param={}) { ...@@ -92,8 +98,14 @@ const Coordinator = function(param={}) {
that.remoteclient.publish(outtopic, outmsg); that.remoteclient.publish(outtopic, outmsg);
break; break;
default: default:
if (packet.topic.startsWith('@msg/')) { // if (packet.topic.startsWith('@msg/')) {
let part = packet.topic.substr(5); // let part = packet.topic.substr(5);
// outtopic = `@tap/msg/topic/${deviceid}:${client.token}/${part}`;
// outmsg = packet.payload.toString();
// that.remoteclient.publish(outtopic, outmsg);
// }
if (packet.topic.startsWith('@local/msgout/')) {
let part = packet.topic.substr(14);
outtopic = `@tap/msg/topic/${deviceid}:${client.token}/${part}`; outtopic = `@tap/msg/topic/${deviceid}:${client.token}/${part}`;
outmsg = packet.payload.toString(); outmsg = packet.payload.toString();
that.remoteclient.publish(outtopic, outmsg); that.remoteclient.publish(outtopic, outmsg);
...@@ -108,7 +120,6 @@ const Coordinator = function(param={}) { ...@@ -108,7 +120,6 @@ const Coordinator = function(param={}) {
setTimeout(function() { setTimeout(function() {
for (let deviceid in that.localserver.sublist) { for (let deviceid in that.localserver.sublist) {
//console.log(deviceid, that.localserver.sublist[deviceid])
that.remoteclient.subscribe(`@tap/shadow/updated/${deviceid}:${that.localserver.sublist[deviceid].token}`); that.remoteclient.subscribe(`@tap/shadow/updated/${deviceid}:${that.localserver.sublist[deviceid].token}`);
that.remoteclient.subscribe(`@tap/device/changed/${deviceid}:${that.localserver.sublist[deviceid].token}`); that.remoteclient.subscribe(`@tap/device/changed/${deviceid}:${that.localserver.sublist[deviceid].token}`);
...@@ -144,10 +155,9 @@ const Coordinator = function(param={}) { ...@@ -144,10 +155,9 @@ const Coordinator = function(param={}) {
that.localserver.publish(`${jsonpayload.deviceid}/device/get`, JSON.stringify(jsonpayload)); that.localserver.publish(`${jsonpayload.deviceid}/device/get`, JSON.stringify(jsonpayload));
} }
else if (topic.startsWith('@msg/')) { else if (topic.startsWith('@msg/')) {
console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>') let part = topic.split('/').splice(1).join('/');
console.log({topic, payload}) let localtopic = `@local/msgin/${part}`;
that.localserver.publish(localtopic, payload);
that.localserver.publish(topic, payload);
} }
}); });
} }
......
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