Commit 166b1cc8 by Chavee Issariyapat

change topic structure

parent 9ac29370
module.exports.create = create
const events = require('events');
const cache = require('./cache');
const Coordinator = function(param={}) {
let that = this;
......@@ -12,17 +13,22 @@ const Coordinator = function(param={}) {
console.log('client connected', client.id);
let deviceid = client.id.split(':')[0];
client.server.ascoltatore.subscribe(`${deviceid}/+`, function(topic, payload, options) {
client.server.ascoltatore.subscribe(`${deviceid}/+/+`, function(topic, payload, options) {
client.forward(topic, payload, options, topic, 0);
});
if (that.localserver.sublist[deviceid] == undefined || that.localserver.sublist[deviceid] == 0) {
that.remoteclient.subscribe(`@tap/shadow/updated/${deviceid}:${client.token}`);
that.remoteclient.subscribe(`@tap/device/changed/${deviceid}:${client.token}`);
that.localserver.sublist[deviceid] = {
count : 1,
token : client.token
}
that.remoteclient.subscribe(`@tap/shadow/updated/${deviceid}:${client.token}`);
that.remoteclient.subscribe(`@tap/device/changed/${deviceid}:${client.token}`);
that.remoteclient.publish(`@tap/device/get/${deviceid}:${client.token}`);
that.remoteclient.publish(`@tap/shadow/get/${deviceid}:${client.token}`);
}
else{
that.localserver.sublist[deviceid].count++;
......@@ -37,6 +43,7 @@ const Coordinator = function(param={}) {
that.remoteclient.unsubscribe(`@tap/shadow/updated/${deviceid}:${client.token}`);
that.remoteclient.unsubscribe(`@tap/device/changed/${deviceid}:${client.token}`);
delete that.localserver.sublist[deviceid];
cache.clear(deviceid);
}
else {
that.localserver.sublist[deviceid].count--;
......@@ -48,21 +55,22 @@ const Coordinator = function(param={}) {
console.log('client published', client.id);
let tap_topic = `@tap/shadow/update/${deviceid}:${client.token}`;
let msg = packet.payload.toString();
that.remoteclient.publish(tap_topic, msg)
});
this.remoteclient.on('connect', function() {
console.log('Connected to NEXPIE message broker.')
that.remoteclient.subscribe('@private/#');
setTimeout(function() {
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/device/changed/${deviceid}:${that.localserver.sublist[deviceid].token}`);
}
}, 2000);
}, 1000);
});
this.remoteclient.on('message', function(topic, payload){
......@@ -74,13 +82,22 @@ const Coordinator = function(param={}) {
}
catch(e) {}
//console.log('incoming message ', topic, jsonpayload)
if (topic == '@shadow/data/updated') {
that.localserver.publish(`${jsonpayload.deviceid}/shadow`, payload.toString());
cache.setShadow(jsonpayload.deviceid, payload);
that.localserver.publish(`${jsonpayload.deviceid}/shadow/updated`, payload.toString());
}
else if (topic.startsWith('@device/status/changed')){
that.localserver.publish(`${jsonpayload.deviceid}/device`, payload.toString());
cache.setStatus(jsonpayload.deviceid, payload);
that.localserver.publish(`${jsonpayload.deviceid}/status/changed`, payload.toString());
}
else if (topic.startsWith('@private/shadow/data/get/response')){
cache.setShadow(jsonpayload.deviceid, payload);
that.localserver.publish(`${jsonpayload.deviceid}/shadow/get`, payload.toString());
}
else if (topic.startsWith('@private/device/status/get/response')){
cache.setStatus(jsonpayload.deviceid, payload);
that.localserver.publish(`${jsonpayload.deviceid}/status/get`, payload.toString());
}
});
}
......
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