Commit 7a8fd03f by Chavee Issariyapat

change device status topic, handle local request from client

parent 6914f081
......@@ -24,16 +24,10 @@ function setShadow(deviceid, 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;
}
......
module.exports.create = create
const DEBUG_SUBLIST = false;
const events = require('events');
const cache = require('./cache');
......@@ -23,7 +25,6 @@ const Coordinator = function(param={}) {
count : 1,
token : client.token
}
that.remoteclient.subscribe(`@tap/shadow/updated/${deviceid}:${client.token}`);
that.remoteclient.subscribe(`@tap/device/changed/${deviceid}:${client.token}`);
......@@ -33,6 +34,11 @@ const Coordinator = function(param={}) {
else{
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){
......@@ -51,13 +57,29 @@ const Coordinator = function(param={}) {
});
this.localserver.on('clientPublished', function(packet, client){
console.log('client published', client.id);
let outtopic, outmsg;
let deviceid = client.id.split(':')[0];
console.log('client published', client.id);
let tap_topic = `@tap/shadow/update/${deviceid}:${client.token}`;
let msg = packet.payload.toString();
switch (packet.topic) {
case '@shadow/data/update' :
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() {
......@@ -77,8 +99,6 @@ const Coordinator = function(param={}) {
});
this.remoteclient.on('message', function(topic, payload){
console.log(that.localserver.sublist);
let jsonpayload = {};
try {
jsonpayload = JSON.parse(payload.toString());
......@@ -92,7 +112,7 @@ const Coordinator = function(param={}) {
}
else if (topic.startsWith('@device/status/changed')){
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')){
cache.setShadow(jsonpayload.deviceid, jsonpayload);
......@@ -100,7 +120,7 @@ const Coordinator = function(param={}) {
}
else if (topic.startsWith('@private/device/status/get/response')){
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));
}
});
}
......
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