Commit 6914f081 by Chavee Issariyapat

update

parent 166b1cc8
module.exports = { getStatus, getShadow, setStatus, setShadow, clear, clearAll } module.exports = { getStatus, getShadow, setStatus, setShadow, mergeShadow, clear, clearAll }
const merge = require('lodash.merge');
const LRU = require("lru-cache"); const LRU = require("lru-cache");
let cache = new LRU({ let cache = new LRU({
max: 500 max: 500
...@@ -21,6 +22,21 @@ function setShadow(deviceid, data) { ...@@ -21,6 +22,21 @@ function setShadow(deviceid, data) {
return cache.set(`${deviceid}:shadow`, data); return cache.set(`${deviceid}:shadow`, data);
} }
function mergeShadow(deviceid, newdata) {
let data = cache.get(`${deviceid}:shadow`) || {};
//let newdata = {...olddata, ...data};
merge(data, newdata);
cache.set(`${deviceid}:shadow`, data);
console.log('---just send ')
console.log(newdata)
console.log('---merge old new ')
console.log(data)
return data;
}
function clear(deviceid) { function clear(deviceid) {
cache.del(`${deviceid}:status`); cache.del(`${deviceid}:status`);
cache.del(`${deviceid}:shadow`); cache.del(`${deviceid}:shadow`);
......
...@@ -69,6 +69,9 @@ const Coordinator = function(param={}) { ...@@ -69,6 +69,9 @@ const Coordinator = function(param={}) {
//console.log(deviceid, that.localserver.sublist[deviceid]) //console.log(deviceid, that.localserver.sublist[deviceid])
that.remoteclient.subscribe(`@tap/shadow/updated/${deviceid}:${that.localserver.sublist[deviceid].token}`); that.remoteclient.subscribe(`@tap/shadow/updated/${deviceid}:${that.localserver.sublist[deviceid].token}`);
that.remoteclient.subscribe(`@tap/device/changed/${deviceid}:${that.localserver.sublist[deviceid].token}`); that.remoteclient.subscribe(`@tap/device/changed/${deviceid}:${that.localserver.sublist[deviceid].token}`);
that.remoteclient.publish(`@tap/device/get/${deviceid}:${that.localserver.sublist[deviceid].token}`);
that.remoteclient.publish(`@tap/shadow/get/${deviceid}:${that.localserver.sublist[deviceid].token}`);
} }
}, 1000); }, 1000);
}); });
...@@ -83,20 +86,21 @@ const Coordinator = function(param={}) { ...@@ -83,20 +86,21 @@ const Coordinator = function(param={}) {
catch(e) {} catch(e) {}
if (topic == '@shadow/data/updated') { if (topic == '@shadow/data/updated') {
cache.setShadow(jsonpayload.deviceid, payload); let newpayload = cache.mergeShadow(jsonpayload.deviceid, jsonpayload);
that.localserver.publish(`${jsonpayload.deviceid}/shadow/updated`, payload.toString()); that.localserver.publish(`${jsonpayload.deviceid}/shadow/merged`, JSON.stringify(newpayload) );
that.localserver.publish(`${jsonpayload.deviceid}/shadow/updated`, JSON.stringify(jsonpayload) );
} }
else if (topic.startsWith('@device/status/changed')){ else if (topic.startsWith('@device/status/changed')){
cache.setStatus(jsonpayload.deviceid, payload); cache.setStatus(jsonpayload.deviceid, jsonpayload);
that.localserver.publish(`${jsonpayload.deviceid}/status/changed`, payload.toString()); that.localserver.publish(`${jsonpayload.deviceid}/status/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, payload); cache.setShadow(jsonpayload.deviceid, jsonpayload);
that.localserver.publish(`${jsonpayload.deviceid}/shadow/get`, payload.toString()); that.localserver.publish(`${jsonpayload.deviceid}/shadow/get`, JSON.stringify(jsonpayload));
} }
else if (topic.startsWith('@private/device/status/get/response')){ else if (topic.startsWith('@private/device/status/get/response')){
cache.setStatus(jsonpayload.deviceid, payload); cache.setStatus(jsonpayload.deviceid, jsonpayload);
that.localserver.publish(`${jsonpayload.deviceid}/status/get`, payload.toString()); that.localserver.publish(`${jsonpayload.deviceid}/status/get`, JSON.stringify(jsonpayload));
} }
}); });
} }
......
...@@ -1852,6 +1852,11 @@ ...@@ -1852,6 +1852,11 @@
"integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==", "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==",
"optional": true "optional": true
}, },
"lodash.merge": {
"version": "4.6.2",
"resolved": "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.2.tgz",
"integrity": "sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ=="
},
"lodash.pad": { "lodash.pad": {
"version": "4.5.1", "version": "4.5.1",
"resolved": "https://registry.npmjs.org/lodash.pad/-/lodash.pad-4.5.1.tgz", "resolved": "https://registry.npmjs.org/lodash.pad/-/lodash.pad-4.5.1.tgz",
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
"dependencies": { "dependencies": {
"config": "^3.2.4", "config": "^3.2.4",
"dotenv": "^8.2.0", "dotenv": "^8.2.0",
"lodash.merge": "^4.6.2",
"mosca": "^2.8.3", "mosca": "^2.8.3",
"mqtt": "^3.0.0" "mqtt": "^3.0.0"
} }
......
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