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
166b1cc8
Commit
166b1cc8
authored
Nov 18, 2019
by
Chavee Issariyapat
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
change topic structure
parent
9ac29370
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
9 deletions
+26
-9
coordinator.js
+26
-9
No files found.
coordinator.js
View file @
166b1cc8
module
.
exports
.
create
=
create
const
events
=
require
(
'events'
);
const
cache
=
require
(
'./cache'
);
const
Coordinator
=
function
(
param
=
{})
{
let
that
=
this
;
...
...
@@ -12,17 +13,22 @@ const Coordinator = function(param={}) {
console
.
log
(
'client connected'
,
client
.
id
);
let
deviceid
=
client
.
id
.
split
(
':'
)[
0
];
client
.
server
.
ascoltatore
.
subscribe
(
`
${
deviceid
}
/+`
,
function
(
topic
,
payload
,
options
)
{
client
.
server
.
ascoltatore
.
subscribe
(
`
${
deviceid
}
/+
/+
`
,
function
(
topic
,
payload
,
options
)
{
client
.
forward
(
topic
,
payload
,
options
,
topic
,
0
);
});
if
(
that
.
localserver
.
sublist
[
deviceid
]
==
undefined
||
that
.
localserver
.
sublist
[
deviceid
]
==
0
)
{
that
.
remoteclient
.
subscribe
(
`@tap/shadow/updated/
${
deviceid
}
:
${
client
.
token
}
`
);
that
.
remoteclient
.
subscribe
(
`@tap/device/changed/
${
deviceid
}
:
${
client
.
token
}
`
);
that
.
localserver
.
sublist
[
deviceid
]
=
{
count
:
1
,
token
:
client
.
token
}
that
.
remoteclient
.
subscribe
(
`@tap/shadow/updated/
${
deviceid
}
:
${
client
.
token
}
`
);
that
.
remoteclient
.
subscribe
(
`@tap/device/changed/
${
deviceid
}
:
${
client
.
token
}
`
);
that
.
remoteclient
.
publish
(
`@tap/device/get/
${
deviceid
}
:
${
client
.
token
}
`
);
that
.
remoteclient
.
publish
(
`@tap/shadow/get/
${
deviceid
}
:
${
client
.
token
}
`
);
}
else
{
that
.
localserver
.
sublist
[
deviceid
].
count
++
;
...
...
@@ -37,6 +43,7 @@ const Coordinator = function(param={}) {
that
.
remoteclient
.
unsubscribe
(
`@tap/shadow/updated/
${
deviceid
}
:
${
client
.
token
}
`
);
that
.
remoteclient
.
unsubscribe
(
`@tap/device/changed/
${
deviceid
}
:
${
client
.
token
}
`
);
delete
that
.
localserver
.
sublist
[
deviceid
];
cache
.
clear
(
deviceid
);
}
else
{
that
.
localserver
.
sublist
[
deviceid
].
count
--
;
...
...
@@ -48,21 +55,22 @@ const Coordinator = function(param={}) {
console
.
log
(
'client published'
,
client
.
id
);
let
tap_topic
=
`@tap/shadow/update/
${
deviceid
}
:
${
client
.
token
}
`
;
let
msg
=
packet
.
payload
.
toString
();
that
.
remoteclient
.
publish
(
tap_topic
,
msg
)
});
this
.
remoteclient
.
on
(
'connect'
,
function
()
{
console
.
log
(
'Connected to NEXPIE message broker.'
)
that
.
remoteclient
.
subscribe
(
'@private/#'
);
setTimeout
(
function
()
{
for
(
let
deviceid
in
that
.
localserver
.
sublist
)
{
//console.log(deviceid, that.localserver.sublist[deviceid])
that
.
remoteclient
.
subscribe
(
`@tap/shadow/updated/
${
deviceid
}
:
${
that
.
localserver
.
sublist
[
deviceid
].
token
}
`
);
that
.
remoteclient
.
subscribe
(
`@tap/device/changed/
${
deviceid
}
:
${
that
.
localserver
.
sublist
[
deviceid
].
token
}
`
);
}
},
2
000
);
},
1
000
);
});
this
.
remoteclient
.
on
(
'message'
,
function
(
topic
,
payload
){
...
...
@@ -74,13 +82,22 @@ const Coordinator = function(param={}) {
}
catch
(
e
)
{}
//console.log('incoming message ', topic, jsonpayload)
if
(
topic
==
'@shadow/data/updated'
)
{
that
.
localserver
.
publish
(
`
${
jsonpayload
.
deviceid
}
/shadow`
,
payload
.
toString
());
cache
.
setShadow
(
jsonpayload
.
deviceid
,
payload
);
that
.
localserver
.
publish
(
`
${
jsonpayload
.
deviceid
}
/shadow/updated`
,
payload
.
toString
());
}
else
if
(
topic
.
startsWith
(
'@device/status/changed'
)){
that
.
localserver
.
publish
(
`
${
jsonpayload
.
deviceid
}
/device`
,
payload
.
toString
());
cache
.
setStatus
(
jsonpayload
.
deviceid
,
payload
);
that
.
localserver
.
publish
(
`
${
jsonpayload
.
deviceid
}
/status/changed`
,
payload
.
toString
());
}
else
if
(
topic
.
startsWith
(
'@private/shadow/data/get/response'
)){
cache
.
setShadow
(
jsonpayload
.
deviceid
,
payload
);
that
.
localserver
.
publish
(
`
${
jsonpayload
.
deviceid
}
/shadow/get`
,
payload
.
toString
());
}
else
if
(
topic
.
startsWith
(
'@private/device/status/get/response'
)){
cache
.
setStatus
(
jsonpayload
.
deviceid
,
payload
);
that
.
localserver
.
publish
(
`
${
jsonpayload
.
deviceid
}
/status/get`
,
payload
.
toString
());
}
});
}
...
...
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