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
f6206ec0
Commit
f6206ec0
authored
Feb 01, 2020
by
Chavee Issariyapat
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
redesign structure of subscribed topics
parent
2159a50c
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
65 additions
and
45 deletions
+65
-45
coordinator.js
+36
-31
flowemitter.js
+24
-14
mqttclient.js
+5
-0
No files found.
coordinator.js
View file @
f6206ec0
...
...
@@ -16,22 +16,20 @@ const Coordinator = function(param={}) {
this
.
flowemitter
.
on
(
'newclient'
,
function
(
client
){
let
deviceid
=
client
.
deviceid
;
let
devicetoken
=
client
.
devicetoken
;
if
(
that
.
flowemitter
.
sublist
[
deviceid
]
==
undefined
||
that
.
flowemitter
.
sublist
[
deviceid
]
==
0
)
{
that
.
flowemitter
.
sublist
[
deviceid
]
=
{
count
:
1
,
token
:
devicetoken
}
let
nodeList
=
that
.
flowemitter
.
getDeviceList
();
if
(
nodeList
[
client
.
nodeid
]
&&
!
nodeList
[
client
.
nodeid
].
subbed
)
{
nodeList
[
client
.
nodeid
].
subbed
=
true
;
that
.
remoteclient
.
subscribe
(
`@tap/shadow/updated/
${
deviceid
}
:
${
client
.
token
}
`
)
;
that
.
remoteclient
.
subscribe
(
`@tap/device/changed/
${
deviceid
}
:
${
client
.
token
}
`
)
;
let
deviceid
=
nodeList
[
client
.
nodeid
].
deviceid
;
let
devicetoken
=
nodeList
[
client
.
nodeid
].
devicetoken
;
that
.
remoteclient
.
publish
(
`@tap/device/get/
${
deviceid
}
:
${
client
.
token
}
`
);
that
.
remoteclient
.
publish
(
`@tap/shadow/get/
${
deviceid
}
:
${
client
.
token
}
`
);
}
else
{
that
.
flowemitter
.
sublist
[
deviceid
].
count
++
;
that
.
remoteclient
.
subscribe
(
`@tap/shadow/updated/
${
deviceid
}
:
${
device
token
}
`
);
that
.
remoteclient
.
subscribe
(
`@tap/device/changed/
${
deviceid
}
:
${
device
token
}
`
);
that
.
remoteclient
.
publish
(
`@tap/device/get/
${
deviceid
}
:
${
devicetoken
}
`
);
that
.
remoteclient
.
publish
(
`@tap/shadow/get/
${
deviceid
}
:
${
devicetoken
}
`
);
}
if
(
DEBUG_SUBLIST
)
{
...
...
@@ -42,27 +40,28 @@ const Coordinator = function(param={}) {
this
.
flowemitter
.
on
(
'flowpub'
,
function
(
topic
,
payload
,
client
){
let
outtopic
,
outmsg
;
let
deviceid
=
client
.
id
;
let
deviceid
=
client
.
deviceid
;
let
devicetoken
=
client
.
devicetoken
;
switch
(
topic
)
{
case
'@shadow/data/update'
:
outtopic
=
`@tap/shadow/update/
${
deviceid
}
:
${
client
.
token
}
`
;
outtopic
=
`@tap/shadow/update/
${
deviceid
}
:
${
device
token
}
`
;
outmsg
=
payload
.
toString
();
that
.
remoteclient
.
publish
(
outtopic
,
outmsg
);
break
;
case
'@local/shadow/get'
:
outtopic
=
`@tap/shadow/get/
${
deviceid
}
:
${
client
.
token
}
`
;
outtopic
=
`@tap/shadow/get/
${
deviceid
}
:
${
device
token
}
`
;
outmsg
=
payload
.
toString
();
that
.
remoteclient
.
publish
(
outtopic
,
outmsg
);
break
;
case
'@local/device/get'
:
outtopic
=
`@tap/device/get/
${
deviceid
}
:
${
client
.
token
}
`
;
outtopic
=
`@tap/device/get/
${
deviceid
}
:
${
device
token
}
`
;
outmsg
=
payload
.
toString
();
that
.
remoteclient
.
publish
(
outtopic
,
outmsg
);
break
;
default
:
if
(
topic
.
startsWith
(
'@local/msgout/'
))
{
let
part
=
topic
.
substr
(
14
);
outtopic
=
`@tap/msg/topic/
${
deviceid
}
:
${
client
.
token
}
/
${
part
}
`
;
outtopic
=
`@tap/msg/topic/
${
deviceid
}
:
${
device
token
}
/
${
part
}
`
;
outmsg
=
payload
.
toString
();
that
.
remoteclient
.
publish
(
outtopic
,
outmsg
);
}
...
...
@@ -72,11 +71,12 @@ const Coordinator = function(param={}) {
this
.
flowemitter
.
on
(
'flowsub'
,
function
(
topic
,
client
)
{
let
deviceid
=
client
.
id
;
let
deviceid
=
client
.
deviceid
;
let
devicetoken
=
client
.
devicetoken
;
// if client try to subscribe @msg/xxx/yyy --> flowagent subscribe @tap on a remote broker
if
(
topic
.
startsWith
(
'@local/msgin/'
))
{
let
msgpart
=
topic
.
split
(
'/'
).
splice
(
4
).
join
(
'/'
);
outtopic
=
`@tap/msg/topic/
${
deviceid
}
:
${
client
.
token
}
/
${
msgpart
}
`
;
outtopic
=
`@tap/msg/topic/
${
deviceid
}
:
${
device
token
}
/
${
msgpart
}
`
;
that
.
remoteclient
.
subscribe
(
outtopic
);
}
else
{
...
...
@@ -84,19 +84,24 @@ const Coordinator = function(param={}) {
});
this
.
remoteclient
.
on
(
'connect'
,
function
()
{
console
.
log
(
' [info] Connected to
NEXPIE message
broker.'
)
console
.
log
(
' [info] Connected to
MQTT
broker.'
)
that
.
remoteclient
.
subscribe
(
'@private/#'
);
that
.
emit
(
'mqttconnected'
);
// setTimeout(function() {
// for (let deviceid in that.flowemitter.sublist) {
// that.remoteclient.subscribe(`@tap/shadow/updated/${deviceid}:${that.flowemitter.sublist[deviceid].token}`);
// that.remoteclient.subscribe(`@tap/device/changed/${deviceid}:${that.flowemitter.sublist[deviceid].token}`);
// that.remoteclient.publish(`@tap/device/get/${deviceid}:${that.flowemitter.sublist[deviceid].token}`);
// that.remoteclient.publish(`@tap/shadow/get/${deviceid}:${that.flowemitter.sublist[deviceid].token}`);
// }
// }, 1000);
});
setTimeout
(
function
()
{
for
(
let
deviceid
in
that
.
flowemitter
.
sublist
)
{
that
.
remoteclient
.
subscribe
(
`@tap/shadow/updated/
${
deviceid
}
:
${
that
.
flowemitter
.
sublist
[
deviceid
].
token
}
`
);
that
.
remoteclient
.
subscribe
(
`@tap/device/changed/
${
deviceid
}
:
${
that
.
flowemitter
.
sublist
[
deviceid
].
token
}
`
);
that
.
remoteclient
.
publish
(
`@tap/device/get/
${
deviceid
}
:
${
that
.
flowemitter
.
sublist
[
deviceid
].
token
}
`
);
that
.
remoteclient
.
publish
(
`@tap/shadow/get/
${
deviceid
}
:
${
that
.
flowemitter
.
sublist
[
deviceid
].
token
}
`
);
}
},
1000
);
this
.
remoteclient
.
on
(
'close'
,
function
()
{
console
.
log
(
' [info] Disconnect from MQTT broker.'
)
that
.
emit
(
'mqttdisconnected'
);
});
this
.
remoteclient
.
on
(
'message'
,
function
(
topic
,
payload
){
...
...
flowemitter.js
View file @
f6206ec0
...
...
@@ -5,38 +5,44 @@ const events = require('events');
const
FlowEmitter
=
function
(
param
=
{})
{
this
.
red
=
param
.
red
;
// node-red object
this
.
devicelist
=
{};
this
.
sublist
=
{};
this
.
globalcontext
=
param
.
globalcontext
;
this
.
events
=
new
events
.
EventEmitter
;
this
.
events
=
new
events
.
EventEmitter
;
}
FlowEmitter
.
prototype
.
start
=
function
()
{
let
that
=
this
;
this
.
red
.
events
.
on
(
'init'
,
function
(
client
)
{
that
.
emit
(
'newclient'
,
{
deviceid
:
client
.
deviceid
,
devicetoken
:
client
.
devicetoken
});
this
.
red
.
events
.
on
(
'register'
,
function
(
client
)
{
that
.
devicelist
[
client
.
nodeid
]
=
{
deviceid
:
client
.
deviceid
,
devicetoken
:
client
.
devicetoken
,
msgtopic
:
[]
}
that
.
emit
(
'newclient'
,
client
);
});
this
.
red
.
events
.
on
(
'deregister'
,
function
(
client
)
{
delete
that
.
devicelist
[
client
.
nodeid
];
});
this
.
red
.
events
.
on
(
'flowsub'
,
function
(
topic
,
client
)
{
that
.
emit
(
'flowsub'
,
topic
,
{
id
:
client
.
deviceid
,
token
:
client
.
devicetoken
});
that
.
emit
(
'flowsub'
,
topic
,
client
);
});
this
.
red
.
events
.
on
(
'flowpub'
,
function
(
topic
,
payload
,
client
)
{
that
.
emit
(
'flowpub'
,
topic
,
payload
,
{
id
:
client
.
deviceid
,
token
:
client
.
devicetoken
});
that
.
emit
(
'flowpub'
,
topic
,
payload
,
client
);
});
}
FlowEmitter
.
prototype
.
getDeviceList
=
function
()
{
return
this
.
devicelist
;
}
FlowEmitter
.
prototype
.
pub
=
function
(
topic
,
payload
)
{
this
.
red
.
events
.
emit
(
topic
,
payload
);
}
...
...
@@ -54,6 +60,10 @@ FlowEmitter.prototype.emit = function(eventname, data1, data2, data3) {
this
.
events
.
emit
(
eventname
,
data1
,
data2
,
data3
);
}
FlowEmitter
.
prototype
.
redemit
=
function
(
eventname
,
data1
,
data2
,
data3
)
{
this
.
red
.
events
.
emit
(
eventname
,
data1
,
data2
,
data3
);
}
function
create
(
param
)
{
return
new
FlowEmitter
(
param
);
}
mqttclient.js
View file @
f6206ec0
...
...
@@ -23,6 +23,10 @@ MQTTClient.prototype.connect = function() {
that
.
emit
(
'disconnect'
);
});
this
.
client
.
on
(
'close'
,
function
()
{
that
.
emit
(
'close'
);
});
this
.
client
.
on
(
'message'
,
async
function
(
topic
,
payload
)
{
that
.
emit
(
'message'
,
topic
,
payload
);
});
...
...
@@ -36,6 +40,7 @@ MQTTClient.prototype.connect = function() {
MQTTClient
.
prototype
.
publish
=
function
(
topic
,
payload
)
{
if
(
this
.
client
)
{
console
.
log
(
' mqtt pub --> '
+
topic
);
this
.
client
.
publish
(
topic
,
payload
);
}
else
{
...
...
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