Skip to content

Commit

Permalink
First Test to maybe fix undefined (zigbeer#22)
Browse files Browse the repository at this point in the history
* First Test to maybe fix undefined

* Updates

* Updates
  • Loading branch information
tb-killa authored and Koenkk committed Apr 16, 2019
1 parent c726e88 commit a295cb8
Showing 1 changed file with 82 additions and 7 deletions.
89 changes: 82 additions & 7 deletions lib/components/event_handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ handlers.devIncoming = function (devInfo, resolve) {
return dev;
} else {
dev = new Device(devInfo);
dev.update({ status: 'online' });
dev.update({ status: 'online', joinTime: Math.floor(Date.now()/1000) });
syncEndpoints(dev);
return self._registerDev(dev).then(function () {
return dev;
Expand All @@ -122,12 +122,9 @@ handlers.devIncoming = function (devInfo, resolve) {
// Try genBasic interview, not certain if it works for all devices
try {
var attrMap = {
3: 'hwVersion',
4: 'manufName',
5: 'modelId',
6: 'dateCode',
7: 'powerSource',
16384: 'swBuildId'
};

var powerSourceMap = {
Expand Down Expand Up @@ -174,9 +171,89 @@ handlers.devIncoming = function (devInfo, resolve) {

// Update dev
dev.update(data);

debug.shepherd('Identified Device: { manufacturer: %s, product: %s }', data.manufName, data.modelId);

// Save device
return Q.ninvoke(self._devbox, 'sync', dev._getId()).then(function () {
return dev;
});
}).catch(function(){
// Get manufName, modelId and powerSource information a second one
const attrIds = Object.keys(attrMap).map((a) => {return {attrId: a}});
return self.af.zclFoundation(basicEpInst, basicEpInst, 0, 'read', attrIds ).then(function (readStatusRecsRsp) {
var data = {};
if (readStatusRecsRsp && _.isArray(readStatusRecsRsp.payload)) {
readStatusRecsRsp.payload.forEach(function(item){ // { attrId, status, dataType, attrData }
if (item && item.hasOwnProperty('attrId') && item.hasOwnProperty('attrData')) {
if (item.attrId === 7)
data[attrMap[item.attrId]] = powerSourceMap[item.attrData];
else
data[attrMap[item.attrId]] = item.attrData;
}
});
}

// Update dev
dev.update(data);
debug.shepherd('Identified Device: { manufacturer: %s, product: %s }', data.manufName, data.modelId);

// Save device
return Q.ninvoke(self._devbox, 'sync', dev._getId()).then(function () {
return dev;
});
}).catch(function(){
return dev;
});
});
} catch (err) {
return dev;
}
}).then(function(dev) {
if (!dev || !dev.hasOwnProperty('endpoints')) return dev;

// Try genBasic interview, not certain if it works for all devices
try {
var attrMap = {
0: 'zclVersion',
1: 'appVersion',
2: 'stackVersion',
3: 'hwVersion',
6: 'dateCode',
16384: 'swBuildId'
};

// Loop all endpoints to find genBasic cluster, and get basic endpoint if possible
var basicEpInst;

for (var i in dev.endpoints) {
var ep = dev.getEndpoint(i),
clusterList = ep.getClusterList();

if (_.isArray(clusterList) && clusterList.indexOf(0) > -1) {
// genBasic found
basicEpInst = ep;
break;
}
}

if (!basicEpInst || basicEpInst instanceof Error) return dev;

// Get other attrMap informations
const attrIds = Object.keys(attrMap).map((a) => {return {attrId: a}});
return self.af.zclFoundation(basicEpInst, basicEpInst, 0, 'read', attrIds).then(function (readStatusRecsRsp) {
var data = {};
debug.shepherd(`zclFoundation(): attrIds: ${JSON.stringify(attrIds)}, readStatusRecsRsp: ${JSON.stringify(readStatusRecsRsp)}`);
if (readStatusRecsRsp && _.isArray(readStatusRecsRsp.payload)) {
readStatusRecsRsp.payload.forEach(function(item){ // { attrId, status, dataType, attrData }
if (item && item.hasOwnProperty('attrId') && item.hasOwnProperty('attrData')) {
data[attrMap[item.attrId]] = item.attrData;
}
});
}

// Update dev
dev.update(data);

// Save device
return Q.ninvoke(self._devbox, 'sync', dev._getId()).then(function () {
return dev;
Expand Down Expand Up @@ -255,7 +332,6 @@ handlers.endDeviceAnnceInd = function (msg) {
// { srcaddr, extaddr, request, removechildren, rejoin }
debug.shepherd('Device: %s endAnnounce.', msg.ieeeaddr);
var dev = this._findDevByAddr(msg.ieeeaddr);

if (dev) {
this.emit('ind:enddeviceannce', dev);
}
Expand All @@ -274,7 +350,6 @@ handlers.leaveInd = function (msg) {
this._unregisterDev(dev);
else // indication
this._devbox.remove(dev._getId(), function () {});

debug.shepherd('Device: %s leave the network.', ieeeAddr);
this.emit('ind:leaving', epList, ieeeAddr);
}
Expand Down

0 comments on commit a295cb8

Please sign in to comment.