Commit 01463e11 by Chavee Issariyapat

refacttor and add coordinator

parent 1124bbbe
module.exports.create = create
const events = require('events');
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('client_unsubscribe', function(data){
console.log('client unsubscribe', data.deviceid, data.topic);
});
}
Coordinator.prototype = new events.EventEmitter;
Coordinator.prototype.start = function() {
this.remoteclient.connect();
this.localserver.start();
}
function create(param) {
return new Coordinator(param);
}
\ No newline at end of file
...@@ -7,9 +7,10 @@ console.log(localBrokerURI) ...@@ -7,9 +7,10 @@ console.log(localBrokerURI)
const MQTTClient = require('./mqttclient'); const MQTTClient = require('./mqttclient');
const MQTTServer = require('./mqttserver'); const MQTTServer = require('./mqttserver');
const Coordinatior = require('./coordinator');
let mqttclient = MQTTClient.create({ let remoteclient = MQTTClient.create({
host: config.get('config.remote_broker_uri'), host: config.get('config.remote_broker_uri'),
options: { options: {
clientId: config.get('config.flowagent_id'), clientId: config.get('config.flowagent_id'),
...@@ -19,12 +20,19 @@ let mqttclient = MQTTClient.create({ ...@@ -19,12 +20,19 @@ let mqttclient = MQTTClient.create({
} }
}); });
let localserver = MQTTServer.create({
let mqttserver = MQTTServer.create({
host : localBrokerURI.hostname || '0.0.0.0', host : localBrokerURI.hostname || '0.0.0.0',
port : parseInt(localBrokerURI.port) || 1883 port : parseInt(localBrokerURI.port) || 1883
}); });
mqttclient.connect(); // localserver.on('client_subscribe', function(data){
// console.log('client subscribe', data.deviceid, data.topic);
// });
// localserver.on('client_unsubscribe', function(data){
// console.log('client unsubscribe', data.deviceid, data.topic);
// });
let coordinatior = Coordinatior.create({remoteclient, localserver});
mqttserver.start(); coordinatior.start();
...@@ -25,6 +25,19 @@ MQTTServer.prototype.start = function() { ...@@ -25,6 +25,19 @@ MQTTServer.prototype.start = function() {
console.log('client connected', client.id); console.log('client connected', client.id);
}); });
this.mqttserver.on('subscribed', function(topic, client) {
that.emit('client_subscribe',{
deviceid: client.id,
topic: topic
});
});
this.mqttserver.on('unsubscribed', function(topic, client) {
that.emit('client_unsubscribe',{
deviceid: client.id,
topic: topic
});
});
} }
......
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