Commit 6075f13f by Chavee Issariyapat

add remote subscription when client connects

parent 573dd9df
......@@ -8,19 +8,27 @@ const Coordinator = function(param={}) {
this.remoteclient = param.remoteclient;
this.localserver = param.localserver;
this.localserver.on('client_subscribe', function(data){
console.log('client subscribe', data.deviceid, data.topic);
this.localserver.on('clientConnected', function(client){
console.log('client connected', client.id);
let deviceid = client.id.split(':')[0];
that.remoteclient.subscribe('@shadow/data/updated/+/+/99a7e50a-d9e3-49c4-a7f0-f09b670ebf96');
that.remoteclient.subscribe('@device/status/changed/+/+/99a7e50a-d9e3-49c4-a7f0-f09b670ebf96');
client.server.ascoltatore.subscribe(`${deviceid}/+`, function(topic, payload, options) {
client.forward(topic, payload, options, topic, 0);
});
this.localserver.on('client_unsubscribe', function(data){
console.log('client unsubscribe', data.deviceid, data.topic);
that.remoteclient.subscribe(`@shadow/data/updated/+/+/${deviceid}`);
that.remoteclient.subscribe(`@device/status/changed/+/+/${deviceid}`);
});
that.remoteclient.unsubscribe('@shadow/data/updated/+/+/99a7e50a-d9e3-49c4-a7f0-f09b670ebf96');
that.remoteclient.unsubscribe('@device/status/changed/+/+/99a7e50a-d9e3-49c4-a7f0-f09b670ebf96');
this.remoteclient.on('message', function(topic, payload){
let jsonpayload = {};
try {
jsonpayload = JSON.parse(payload.toString());
}
catch(e) {}
that.localserver.publish(`${jsonpayload.deviceid}/devicestatus`, payload.toString());
});
}
Coordinator.prototype = new events.EventEmitter;
......
......@@ -22,18 +22,18 @@ MQTTServer.prototype.start = function() {
});
this.mqttserver.on('clientConnected', function(client) {
console.log('client connected', client.id);
that.emit('clientConnected',client);
});
this.mqttserver.on('subscribed', function(topic, client) {
that.emit('client_subscribe',{
that.emit('clientSubscribed',{
deviceid: client.id,
topic: topic
});
});
this.mqttserver.on('unsubscribed', function(topic, client) {
that.emit('client_unsubscribe',{
that.emit('clientUnsubscribed',{
deviceid: client.id,
topic: topic
});
......@@ -42,10 +42,22 @@ MQTTServer.prototype.start = function() {
}
// MQTTServer.prototype.xxx = function(topic, payload) {
// }
MQTTServer.prototype.publish = function(topic, payload) {
let msgobj = {
topic : topic,
payload : payload,
qos: 0,
retain: false
}
this.mqttserver.publish(msgobj, function(){
console.log({topic, payload})
});
}
function create(param) {
return new MQTTServer(param);
......
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