Commit beb2058d by Chavee Issariyapat

redesign event

parent 936fad75
...@@ -12,6 +12,8 @@ const Coordinator = function(param={}) { ...@@ -12,6 +12,8 @@ const Coordinator = function(param={}) {
let that = this; let that = this;
let resettimer = 0; let resettimer = 0;
this.topicstore = require('./topicstore').create();
this.x = 0; this.x = 0;
this.started = false; this.started = false;
...@@ -20,19 +22,34 @@ const Coordinator = function(param={}) { ...@@ -20,19 +22,34 @@ const Coordinator = function(param={}) {
this.mqttclient = param.mqttclient; this.mqttclient = param.mqttclient;
this.flowemitter = param.flowemitter || null; this.flowemitter = param.flowemitter || null;
this.flowemitter.on('newclient', function(client){ this.flowemitter.on('newshadowclient', function(client){
let deviceid = client.deviceid; let deviceid = client.deviceid;
let devicetoken = client.devicetoken; let devicetoken = client.devicetoken;
console.log
that.mqttclient.subscribe(`@tap/shadow/updated/${deviceid}:${devicetoken}`); that.mqttclient.subscribe(`@tap/shadow/updated/${deviceid}:${devicetoken}`);
that.mqttclient.subscribe(`@tap/device/changed/${deviceid}:${devicetoken}`); that.mqttclient.subscribe(`@tap/device/changed/${deviceid}:${devicetoken}`);
if (DEBUG_SUBLIST) { if (DEBUG_SUBLIST) {
console.log(`\nshadow client #${deviceid} connected -->`) // console.log(`\nshadow client #${deviceid} connected -->`)
console.log(that.flowemitter.sublist); // console.log(that.flowemitter.sublist);
} }
}); });
this.flowemitter.on('newmsgclient', function(client){
let deviceid = client.deviceid;
let devicetoken = client.devicetoken;
that.mqttclient.subscribe(`@tap/device/changed/${deviceid}:${devicetoken}`);
if (DEBUG_SUBLIST) {
// console.log(`\nshadow client #${deviceid} connected -->`)
// console.log(that.flowemitter.sublist);
}
});
this.flowemitter.on('flowpub', function(topic, payload, client){ this.flowemitter.on('flowpub', function(topic, payload, client){
let outtopic, outmsg; let outtopic, outmsg;
let deviceid = client.deviceid; let deviceid = client.deviceid;
...@@ -65,14 +82,34 @@ const Coordinator = function(param={}) { ...@@ -65,14 +82,34 @@ const Coordinator = function(param={}) {
this.flowemitter.on('flowsub', function(topic, client) { this.flowemitter.on('flowsub', function(topic, client) {
console.log(client.nodeid + ' flowsub --> '+topic)
let deviceid = client.deviceid; let deviceid = client.deviceid;
let devicetoken = client.devicetoken; let devicetoken = client.devicetoken;
if (topic.startsWith('@local/msgin/')) { if (topic.startsWith('@local/msgin/')) {
let msgpart = topic.split('/').splice(4).join('/'); let msgpart = topic.split('/').splice(4).join('/');
outtopic = `@tap/msg/topic/${deviceid}:${devicetoken}/${msgpart}`; outtopic = `@tap/msg/topic/${deviceid}:${devicetoken}/${msgpart}`;
if (that.topicstore.addTopic(outtopic, client.nodeid)) {
that.mqttclient.subscribe(outtopic); that.mqttclient.subscribe(outtopic);
}
}
else {
}
});
this.flowemitter.on('flowunsub', function(topic, client) {
console.log(client.nodeid + ' flowunsub --> '+topic)
let deviceid = client.deviceid;
let devicetoken = client.devicetoken;
if (topic.startsWith('@local/msgin/')) {
let msgpart = topic.split('/').splice(4).join('/');
outtopic = `@tap/msg/topic/${deviceid}:${devicetoken}/${msgpart}`;
if (that.topicstore.delTopic(outtopic, client.nodeid)) {
//console.log('----- unsubscribe : '+outtopic)
that.mqttclient.unsubscribe(outtopic);
}
} }
else { else {
} }
......
...@@ -12,11 +12,11 @@ const FlowEmitter = function(param = {}) { ...@@ -12,11 +12,11 @@ const FlowEmitter = function(param = {}) {
FlowEmitter.prototype.start = function() { FlowEmitter.prototype.start = function() {
let that = this; let that = this;
this.red.events.on(`${that.namespace}-register`, function(client) { this.red.events.on(`${that.namespace}-registershadowlistener`, function(client) {
that.emit('newclient', client); that.emit('newshadowclient', client);
}); });
this.red.events.on(`${that.namespace}-deregister`, function(client) { this.red.events.on(`${that.namespace}-deregistershadowlistener`, function(client) {
that.emit('startcountdownreset'); that.emit('startcountdownreset');
}); });
...@@ -25,6 +25,10 @@ FlowEmitter.prototype.start = function() { ...@@ -25,6 +25,10 @@ FlowEmitter.prototype.start = function() {
that.emit('flowsub', topic, client); that.emit('flowsub', topic, client);
}); });
this.red.events.on(`${that.namespace}-flowunsub`, function(topic, client) {
that.emit('flowunsub', topic, client);
});
this.red.events.on(`${that.namespace}-flowpub`, function(topic, payload, client) { this.red.events.on(`${that.namespace}-flowpub`, function(topic, payload, client) {
that.emit('flowpub', topic, payload, client); that.emit('flowpub', topic, payload, client);
}); });
......
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