Commit 573dd9df by Chavee Issariyapat

update

parent 01463e11
......@@ -3,15 +3,23 @@ module.exports.create = create
const events = require('events');
const Coordinator = function(param={}) {
let that = this;
console.log(param)
this.remoteclient = param.remoteclient;
this.localserver = param.localserver;
this.localserver.on('client_subscribe', function(data){
console.log('client subscribe', data.deviceid, data.topic);
that.remoteclient.subscribe('@shadow/data/updated/+/+/99a7e50a-d9e3-49c4-a7f0-f09b670ebf96');
that.remoteclient.subscribe('@device/status/changed/+/+/99a7e50a-d9e3-49c4-a7f0-f09b670ebf96');
});
this.localserver.on('client_unsubscribe', function(data){
console.log('client unsubscribe', data.deviceid, data.topic);
that.remoteclient.unsubscribe('@shadow/data/updated/+/+/99a7e50a-d9e3-49c4-a7f0-f09b670ebf96');
that.remoteclient.unsubscribe('@device/status/changed/+/+/99a7e50a-d9e3-49c4-a7f0-f09b670ebf96');
});
}
......@@ -24,4 +32,4 @@ Coordinator.prototype.start = function() {
function create(param) {
return new Coordinator(param);
}
\ No newline at end of file
}
......@@ -3,16 +3,13 @@ require('dotenv').config();
const config = require('config');
const localBrokerURI = require('url').parse(config.get('config.local_broker_uri'));
console.log(localBrokerURI)
const MQTTClient = require('./mqttclient');
const MQTTServer = require('./mqttserver');
const Coordinatior = require('./coordinator');
let remoteclient = MQTTClient.create({
host: config.get('config.remote_broker_uri'),
options: {
options: {
clientId: config.get('config.flowagent_id'),
username: config.get('config.flowagent_username'),
password: config.get('config.flowagent_password'),
......@@ -25,14 +22,5 @@ let localserver = MQTTServer.create({
port : parseInt(localBrokerURI.port) || 1883
});
// 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});
coordinatior.start();
......@@ -33,7 +33,21 @@ MQTTClient.prototype.connect = function() {
}
MQTTClient.prototype.publish = function(topic, payload) {
this.client.publish(topic, payload);
if (this.client) {
this.client.publish(topic, payload);
}
else {
}
}
MQTTClient.prototype.subscribe = function(topic) {
if (this.client) {
this.client.subscribe(topic);
}
else {
}
}
......
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