Commit 689cf194 by Chavee Issariyapat

update but still not working

parent 20e8460a
...@@ -9,8 +9,10 @@ const Coordinator = function(param={}) { ...@@ -9,8 +9,10 @@ const Coordinator = function(param={}) {
let that = this; let that = this;
this.remoteclient = param.remoteclient; this.remoteclient = param.remoteclient;
this.localserver = param.localserver; // this.localserver = param.localserver || null;
this.flowemitter = param.flowemitter;
/*
this.localserver.on('clientConnected', function(client){ this.localserver.on('clientConnected', function(client){
console.log('client connected', client.id); console.log('client connected', client.id);
let deviceid = client.id.split(':')[0]; let deviceid = client.id.split(':')[0];
...@@ -147,12 +149,14 @@ const Coordinator = function(param={}) { ...@@ -147,12 +149,14 @@ const Coordinator = function(param={}) {
that.localserver.publish(localtopic, payload); that.localserver.publish(localtopic, payload);
} }
}); });
*/
} }
Coordinator.prototype = new events.EventEmitter; Coordinator.prototype = new events.EventEmitter;
Coordinator.prototype.start = function() { Coordinator.prototype.start = function() {
this.remoteclient.connect(); this.remoteclient.connect();
this.localserver.start(); //this.localserver.start();
this.flowemitter.start();
} }
function create(param) { function create(param) {
......
module.exports.create = create module.exports.create = create
const MQTTClient = require('./mqttclient'); const MQTTClient = require('./mqttclient');
const MQTTServer = require('./mqttserver'); const FlowEmitter = require('./flowemitter');
const Coordinatior = require('./coordinator'); const Coordinatior = require('./coordinator');
const events = require('events'); const events = require('events');
let FlowAgent = function(options) { const FlowAgent = function(option = {}) {
let that = this; let that = this;
this.option = {
broker_uri : option.broker_uri,
flowagentid : option.flowagentid,
flowagentsecret : option.flowagentsecret
}
this.context = option.context;
this.remoteclient = MQTTClient.create({ this.remoteclient = MQTTClient.create({
host: options.remote_broker_uri, host: that.option.broker_uri,
options: { options: {
clientId: options.flowagent_id, clientId: Date.now()+'-'+that.option.flowagentid,
username: options.flowagent_username, username: that.option.flowagentid,
password: options.flowagent_password, password: that.option.flowagentsecret,
keepalive: 30 keepalive: 30
} }
}); });
this.flowemitter = FlowEmitter.create();
this.localserver = MQTTServer.create({ this.coordinatior = Coordinatior.create({
host : options.localBrokerURI.hostname || '0.0.0.0', remoteclient: that.remoteclient,
port : parseInt(options.localBrokerURI.port) || 1883 flowemitter: that.flowemitter
}); });
this.coordinatior = Coordinatior.create({remoteclient: that.remoteclient, localserver: that.localserver});
} }
FlowAgent.prototype = new events.EventEmitter; FlowAgent.prototype = new events.EventEmitter;
FlowAgent.prototype.start = function() { FlowAgent.prototype.start = function() {
this.coordinatior.start(); this.coordinatior.start();
} }
function create(options) { function create() {
return FlowAgent(options); return new FlowAgent();
} }
module.exports.create = create
const events = require('events');
const FlowEmitter = function(param = {}) {
this.sublist = {};
this.globalcontext = param.globalcontext;
}
FlowEmitter.prototype = new events.EventEmitter;
FlowEmitter.prototype.start = function() {
let that = this;
}
// a method for ndoe-red block to register
FlowEmitter.prototype.register = function(deviceid, devicetoken) {
let that = this;
}
FlowEmitter.prototype.deregister = function(deviceid) {
let that = this;
}
function create() {
return new FlowEmitter();
}
require('dotenv').config();
const config = require('config'); /*
const localBrokerURI = require('url').parse(config.get('config.local_broker_uri')); module.exports.create = create
const MQTTClient = require('./mqttclient'); const MQTTClient = require('./mqttclient');
const MQTTServer = require('./mqttserver'); const FlowEmitter = require('./flowemitter');
const Coordinatior = require('./coordinator'); const Coordinatior = require('./coordinator');
let remoteclient = MQTTClient.create({ const events = require('events');
host: config.get('config.remote_broker_uri'),
const FlowAgent = function(option = {}) {
let that = this;
let this.option = {
broker_uri : option.broker_uri,
flowagentid : option.flowagentid,
flowagentsecret : option.flowagentsecret
}
this.context = option.context;
this.remoteclient = MQTTClient.create({
host: that.option.broker_uri,
options: { options: {
clientId: config.get('config.flowagent_id'), clientId: Date.now()+'-'+that.option.flowagentid,
username: config.get('config.flowagent_username'), username: that.option.flowagentid,
password: config.get('config.flowagent_password'), password: that.option.flowagentsecret,
keepalive: 30 keepalive: 30
} }
}); });
this.flowemitter = FlowEmitter.create();
this.coordinatior = Coordinatior.create({
remoteclient: that.remoteclient,
flowemitter: that.flowemitter
});
}
FlowAgent.prototype = new events.EventEmitter;
FlowAgent.prototype.start = function() {
this.coordinatior.start();
let localserver = MQTTServer.create({ }
host : localBrokerURI.hostname || '0.0.0.0',
port : parseInt(localBrokerURI.port) || 1883
});
let coordinatior = Coordinatior.create({remoteclient, localserver}); function create() {
coordinatior.start(); return new FlowAgent();
}
*/
\ No newline at end of file
...@@ -2,9 +2,9 @@ ...@@ -2,9 +2,9 @@
"name": "flowagent", "name": "flowagent",
"version": "1.0.0", "version": "1.0.0",
"description": "", "description": "",
"main": "index.js", "main": "flowagent.js",
"scripts": { "scripts": {
"start": "node index.js" "start": "node run.js"
}, },
"author": "NEXPIE Company Limited (https://nexpie.com)", "author": "NEXPIE Company Limited (https://nexpie.com)",
"license": "ISC", "license": "ISC",
......
require('dotenv').config();
const config = require('config');
const localBrokerURI = require('url').parse(config.get('config.local_broker_uri'));
const flowagent = require('./FlowAgent').create({
broker_uri : config.get('config.remote_broker_uri'),
flowagentid: config.get('config.flowagent_username'),
flowagentsecret: config.get('config.flowagent_password'),
mqttenabled : true
});
flowagent.start();
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