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
7a8fd03f
Commit
7a8fd03f
authored
Nov 25, 2019
by
Chavee Issariyapat
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
change device status topic, handle local request from client
parent
6914f081
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
15 deletions
+29
-15
cache.js
+0
-6
coordinator.js
+29
-9
No files found.
cache.js
View file @
7a8fd03f
...
@@ -24,16 +24,10 @@ function setShadow(deviceid, data) {
...
@@ -24,16 +24,10 @@ function setShadow(deviceid, data) {
function
mergeShadow
(
deviceid
,
newdata
)
{
function
mergeShadow
(
deviceid
,
newdata
)
{
let
data
=
cache
.
get
(
`
${
deviceid
}
:shadow`
)
||
{};
let
data
=
cache
.
get
(
`
${
deviceid
}
:shadow`
)
||
{};
//let newdata = {...olddata, ...data};
merge
(
data
,
newdata
);
merge
(
data
,
newdata
);
cache
.
set
(
`
${
deviceid
}
:shadow`
,
data
);
cache
.
set
(
`
${
deviceid
}
:shadow`
,
data
);
console
.
log
(
'---just send '
)
console
.
log
(
newdata
)
console
.
log
(
'---merge old new '
)
console
.
log
(
data
)
return
data
;
return
data
;
}
}
...
...
coordinator.js
View file @
7a8fd03f
module
.
exports
.
create
=
create
module
.
exports
.
create
=
create
const
DEBUG_SUBLIST
=
false
;
const
events
=
require
(
'events'
);
const
events
=
require
(
'events'
);
const
cache
=
require
(
'./cache'
);
const
cache
=
require
(
'./cache'
);
...
@@ -23,7 +25,6 @@ const Coordinator = function(param={}) {
...
@@ -23,7 +25,6 @@ const Coordinator = function(param={}) {
count
:
1
,
count
:
1
,
token
:
client
.
token
token
:
client
.
token
}
}
that
.
remoteclient
.
subscribe
(
`@tap/shadow/updated/
${
deviceid
}
:
${
client
.
token
}
`
);
that
.
remoteclient
.
subscribe
(
`@tap/shadow/updated/
${
deviceid
}
:
${
client
.
token
}
`
);
that
.
remoteclient
.
subscribe
(
`@tap/device/changed/
${
deviceid
}
:
${
client
.
token
}
`
);
that
.
remoteclient
.
subscribe
(
`@tap/device/changed/
${
deviceid
}
:
${
client
.
token
}
`
);
...
@@ -33,6 +34,11 @@ const Coordinator = function(param={}) {
...
@@ -33,6 +34,11 @@ const Coordinator = function(param={}) {
else
{
else
{
that
.
localserver
.
sublist
[
deviceid
].
count
++
;
that
.
localserver
.
sublist
[
deviceid
].
count
++
;
}
}
if
(
DEBUG_SUBLIST
)
{
console
.
log
(
`\nshadow client #
${
deviceid
}
connected -->`
)
console
.
log
(
that
.
localserver
.
sublist
);
}
});
});
this
.
localserver
.
on
(
'clientDisconnected'
,
function
(
client
){
this
.
localserver
.
on
(
'clientDisconnected'
,
function
(
client
){
...
@@ -51,13 +57,29 @@ const Coordinator = function(param={}) {
...
@@ -51,13 +57,29 @@ const Coordinator = function(param={}) {
});
});
this
.
localserver
.
on
(
'clientPublished'
,
function
(
packet
,
client
){
this
.
localserver
.
on
(
'clientPublished'
,
function
(
packet
,
client
){
console
.
log
(
'client published'
,
client
.
id
);
let
outtopic
,
outmsg
;
let
deviceid
=
client
.
id
.
split
(
':'
)[
0
];
let
deviceid
=
client
.
id
.
split
(
':'
)[
0
];
console
.
log
(
'client published'
,
client
.
id
);
switch
(
packet
.
topic
)
{
let
tap_topic
=
`@tap/shadow/update/
${
deviceid
}
:
${
client
.
token
}
`
;
case
'@shadow/data/update'
:
let
msg
=
packet
.
payload
.
toString
();
outtopic
=
`@tap/shadow/update/
${
deviceid
}
:
${
client
.
token
}
`
;
outmsg
=
packet
.
payload
.
toString
();
that
.
remoteclient
.
publish
(
outtopic
,
outmsg
);
break
;
case
'@local/shadow/get'
:
outtopic
=
`@tap/shadow/get/
${
deviceid
}
:
${
client
.
token
}
`
;
outmsg
=
packet
.
payload
.
toString
();
that
.
remoteclient
.
publish
(
outtopic
,
outmsg
);
break
;
case
'@local/device/get'
:
outtopic
=
`@tap/device/get/
${
deviceid
}
:
${
client
.
token
}
`
;
outmsg
=
packet
.
payload
.
toString
();
that
.
remoteclient
.
publish
(
outtopic
,
outmsg
);
break
;
that
.
remoteclient
.
publish
(
tap_topic
,
msg
)
}
});
});
this
.
remoteclient
.
on
(
'connect'
,
function
()
{
this
.
remoteclient
.
on
(
'connect'
,
function
()
{
...
@@ -77,8 +99,6 @@ const Coordinator = function(param={}) {
...
@@ -77,8 +99,6 @@ const Coordinator = function(param={}) {
});
});
this
.
remoteclient
.
on
(
'message'
,
function
(
topic
,
payload
){
this
.
remoteclient
.
on
(
'message'
,
function
(
topic
,
payload
){
console
.
log
(
that
.
localserver
.
sublist
);
let
jsonpayload
=
{};
let
jsonpayload
=
{};
try
{
try
{
jsonpayload
=
JSON
.
parse
(
payload
.
toString
());
jsonpayload
=
JSON
.
parse
(
payload
.
toString
());
...
@@ -92,7 +112,7 @@ const Coordinator = function(param={}) {
...
@@ -92,7 +112,7 @@ const Coordinator = function(param={}) {
}
}
else
if
(
topic
.
startsWith
(
'@device/status/changed'
)){
else
if
(
topic
.
startsWith
(
'@device/status/changed'
)){
cache
.
setStatus
(
jsonpayload
.
deviceid
,
jsonpayload
);
cache
.
setStatus
(
jsonpayload
.
deviceid
,
jsonpayload
);
that
.
localserver
.
publish
(
`
${
jsonpayload
.
deviceid
}
/
status
/changed`
,
JSON
.
stringify
(
jsonpayload
));
that
.
localserver
.
publish
(
`
${
jsonpayload
.
deviceid
}
/
device
/changed`
,
JSON
.
stringify
(
jsonpayload
));
}
}
else
if
(
topic
.
startsWith
(
'@private/shadow/data/get/response'
)){
else
if
(
topic
.
startsWith
(
'@private/shadow/data/get/response'
)){
cache
.
setShadow
(
jsonpayload
.
deviceid
,
jsonpayload
);
cache
.
setShadow
(
jsonpayload
.
deviceid
,
jsonpayload
);
...
@@ -100,7 +120,7 @@ const Coordinator = function(param={}) {
...
@@ -100,7 +120,7 @@ const Coordinator = function(param={}) {
}
}
else
if
(
topic
.
startsWith
(
'@private/device/status/get/response'
)){
else
if
(
topic
.
startsWith
(
'@private/device/status/get/response'
)){
cache
.
setStatus
(
jsonpayload
.
deviceid
,
jsonpayload
);
cache
.
setStatus
(
jsonpayload
.
deviceid
,
jsonpayload
);
that
.
localserver
.
publish
(
`
${
jsonpayload
.
deviceid
}
/
status
/get`
,
JSON
.
stringify
(
jsonpayload
));
that
.
localserver
.
publish
(
`
${
jsonpayload
.
deviceid
}
/
device
/get`
,
JSON
.
stringify
(
jsonpayload
));
}
}
});
});
}
}
...
...
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