Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
F
flowagent
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
node-red
flowagent
Commits
2448426a
Commit
2448426a
authored
Apr 16, 2020
by
Chavee Issariyapat
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix duplicate listeners on mqttclient.on('connect')
parent
7a965d29
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
39 additions
and
33 deletions
+39
-33
coordinator.js
+17
-15
flowagent.js
+8
-7
mqttclient.js
+14
-11
No files found.
coordinator.js
View file @
2448426a
...
...
@@ -7,22 +7,25 @@ const cache = require('./cache');
const
RESETDELAY
=
1000
const
Coordinator
=
function
(
param
=
{})
{
let
that
=
this
;
let
resettimer
=
0
;
this
.
x
=
0
;
this
.
started
=
false
;
this
.
events
=
new
events
.
EventEmitter
;
this
.
remoteclient
=
param
.
remote
client
;
this
.
mqttclient
=
param
.
mqtt
client
;
this
.
flowemitter
=
param
.
flowemitter
||
null
;
this
.
flowemitter
.
on
(
'newclient'
,
function
(
client
){
let
deviceid
=
client
.
deviceid
;
let
devicetoken
=
client
.
devicetoken
;
that
.
remote
client
.
subscribe
(
`@tap/shadow/updated/
${
deviceid
}
:
${
devicetoken
}
`
);
that
.
remote
client
.
subscribe
(
`@tap/device/changed/
${
deviceid
}
:
${
devicetoken
}
`
);
that
.
mqtt
client
.
subscribe
(
`@tap/shadow/updated/
${
deviceid
}
:
${
devicetoken
}
`
);
that
.
mqtt
client
.
subscribe
(
`@tap/device/changed/
${
deviceid
}
:
${
devicetoken
}
`
);
if
(
DEBUG_SUBLIST
)
{
console
.
log
(
`\nshadow client #
${
deviceid
}
connected -->`
)
...
...
@@ -38,24 +41,24 @@ const Coordinator = function(param={}) {
case
'@shadow/data/update'
:
outtopic
=
`@tap/shadow/update/
${
deviceid
}
:
${
devicetoken
}
`
;
outmsg
=
payload
.
toString
();
that
.
remote
client
.
publish
(
outtopic
,
outmsg
);
that
.
mqtt
client
.
publish
(
outtopic
,
outmsg
);
break
;
case
'@local/shadow/get'
:
outtopic
=
`@tap/shadow/get/
${
deviceid
}
:
${
devicetoken
}
`
;
outmsg
=
payload
.
toString
();
that
.
remote
client
.
publish
(
outtopic
,
outmsg
);
that
.
mqtt
client
.
publish
(
outtopic
,
outmsg
);
break
;
case
'@local/device/get'
:
outtopic
=
`@tap/device/get/
${
deviceid
}
:
${
devicetoken
}
`
;
outmsg
=
payload
.
toString
();
that
.
remote
client
.
publish
(
outtopic
,
outmsg
);
that
.
mqtt
client
.
publish
(
outtopic
,
outmsg
);
break
;
default
:
if
(
topic
.
startsWith
(
'@local/msgout/'
))
{
let
part
=
topic
.
substr
(
14
);
outtopic
=
`@tap/msg/topic/
${
deviceid
}
:
${
devicetoken
}
/
${
part
}
`
;
outmsg
=
payload
.
toString
();
that
.
remote
client
.
publish
(
outtopic
,
outmsg
);
that
.
mqtt
client
.
publish
(
outtopic
,
outmsg
);
}
}
});
...
...
@@ -68,7 +71,7 @@ const Coordinator = function(param={}) {
let
msgpart
=
topic
.
split
(
'/'
).
splice
(
4
).
join
(
'/'
);
outtopic
=
`@tap/msg/topic/
${
deviceid
}
:
${
devicetoken
}
/
${
msgpart
}
`
;
that
.
remote
client
.
subscribe
(
outtopic
);
that
.
mqtt
client
.
subscribe
(
outtopic
);
}
else
{
...
...
@@ -79,22 +82,21 @@ const Coordinator = function(param={}) {
if
(
!
resettimer
)
{
resettimer
=
setTimeout
(
function
()
{
console
.
log
(
' [info] MQTT resetting...'
);
that
.
remote
client
.
resetbroker
();
that
.
mqtt
client
.
resetbroker
();
},
RESETDELAY
);
}
});
this
.
remoteclient
.
on
(
'connect'
,
function
()
{
console
.
log
(
' [info] Connected to MQTT broker.'
)
that
.
remoteclient
.
subscribe
(
'@private/#'
);
this
.
mqttclient
.
on
(
'connect'
,
function
()
{
that
.
mqttclient
.
subscribe
(
'@private/#'
);
});
this
.
remote
client
.
on
(
'close'
,
function
()
{
this
.
mqtt
client
.
on
(
'close'
,
function
()
{
this
.
started
=
false
;
console
.
log
(
' [info] Disconnect from MQTT broker.'
)
});
this
.
remote
client
.
on
(
'message'
,
function
(
topic
,
payload
){
this
.
mqtt
client
.
on
(
'message'
,
function
(
topic
,
payload
){
let
jsonpayload
=
{};
try
{
jsonpayload
=
JSON
.
parse
(
payload
.
toString
());
...
...
@@ -148,7 +150,7 @@ Coordinator.prototype.emit = function(eventname, data1, data2, data3) {
Coordinator
.
prototype
.
start
=
function
()
{
if
(
!
this
.
started
)
{
this
.
started
=
true
;
this
.
remote
client
.
connect
();
this
.
mqtt
client
.
connect
();
this
.
flowemitter
.
start
();
return
true
;
}
...
...
flowagent.js
View file @
2448426a
...
...
@@ -3,16 +3,16 @@ module.exports.create = create
const
MQTTClient
=
require
(
'./mqttclient'
);
const
MQTTServer
=
require
(
'./mqttserver'
);
const
FlowEmitter
=
require
(
'./flowemitter'
);
const
Coordinatior
=
require
(
'./coordinator'
);
const
events
=
require
(
'events'
);
let
FlowAgent
=
function
(
option
=
{})
{
let
that
=
this
;
const
Coordinatior
=
require
(
'./coordinator'
)
;
this
.
started
=
false
;
this
.
events
=
new
events
.
EventEmitter
;
this
.
option
=
{
broker_uri
:
option
.
broker_uri
,
flowagentid
:
option
.
flowagentid
,
...
...
@@ -32,19 +32,19 @@ let FlowAgent = function(option = {}) {
keepalive
:
30
}
this
.
remote
client
=
MQTTClient
.
create
({
let
mqtt
client
=
MQTTClient
.
create
({
host
:
that
.
option
.
broker_uri
,
options
:
opt
});
this
.
flowemitter
=
FlowEmitter
.
create
({
let
flowemitter
=
FlowEmitter
.
create
({
red
:
this
.
option
.
red
,
namespace
:
this
.
option
.
namespace
});
this
.
coordinator
=
Coordinatior
.
create
({
remoteclient
:
that
.
remote
client
,
flowemitter
:
that
.
flowemitter
mqttclient
:
mqtt
client
,
flowemitter
:
flowemitter
});
}
...
...
@@ -57,7 +57,8 @@ FlowAgent.prototype.emit = function(eventname, data1, data2, data3) {
}
FlowAgent
.
prototype
.
start
=
function
()
{
if
(
this
.
coordinator
.
start
())
{
if
(
!
this
.
started
)
{
this
.
started
=
this
.
coordinator
.
start
();
console
.
log
(
'Staring FlowAgent...'
)
}
}
...
...
mqttclient.js
View file @
2448426a
module
.
exports
.
create
=
create
const
MQTT
=
require
(
'mqtt'
);
const
events
=
require
(
'events'
);
const
EventEmitter
=
require
(
'events'
);
const
MQTTClient
=
function
(
param
=
{})
{
this
.
client
=
null
;
...
...
@@ -12,19 +12,24 @@ const MQTTClient = function(param = {}) {
function
sleep
(
ms
)
{
return
new
Promise
(
resolve
=>
setTimeout
(
resolve
,
ms
));
}
MQTTClient
.
prototype
=
new
events
.
EventEmitter
;
//----
//const events = require('events');
//MQTTClient.prototype = new events.EventEmitter;
//MQTTClient.prototype = new EventEmitter();
//----
MQTTClient
.
prototype
.
__proto__
=
EventEmitter
.
prototype
;
MQTTClient
.
prototype
.
connect
=
function
()
{
var
that
=
this
;
this
.
subscription
=
[]
;
if
(
this
.
client
)
delete
this
.
client
;
this
.
subscription
=
[];
this
.
client
=
MQTT
.
connect
(
this
.
host
,
this
.
options
||
{});
this
.
client
.
on
(
'connect'
,
function
()
{
// for (let topic in that.subscription) {
// await sleep(100);
// }
that
.
emit
(
'connect'
);
});
...
...
@@ -61,11 +66,9 @@ MQTTClient.prototype.publish = function(topic, payload) {
}
MQTTClient
.
prototype
.
subscribe
=
function
(
topic
)
{
if
(
this
.
client
&&
this
.
client
.
connected
)
{
this
.
client
.
subscribe
(
topic
);
}
else
{
}
this
.
client
.
subscribe
(
topic
,
function
(
err
,
granted
){
//console.log({err, granted})
});
}
MQTTClient
.
prototype
.
unsubscribe
=
function
(
topic
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment