...
|
...
|
@@ -179,9 +179,11 @@ exports.stopInventory = function (connector) { |
|
|
var isStop = false,
|
|
|
checkInventoryFlag,
|
|
|
checkSpeedFlag;
|
|
|
|
|
|
exports.start = function (options, callback) {
|
|
|
var rets = [];
|
|
|
var countMap = {};
|
|
|
var lastRets = [],
|
|
|
rets = [],
|
|
|
goneRets = [];
|
|
|
exports.open(options.host, options.port, function (connector) {
|
|
|
|
|
|
if (options.filter) { //开启灵敏度过滤
|
...
|
...
|
@@ -213,32 +215,23 @@ exports.start = function (options, callback) { |
|
|
|
|
|
connector.on('sartInventory', function (data) {
|
|
|
if (data.code !== 2 && !isStop) {
|
|
|
if(lastRets.length === 0){
|
|
|
lastRets = rets;
|
|
|
}else{
|
|
|
var compResults = compTags(lastRets, rets, goneRets);
|
|
|
lastRets = compResults.last;
|
|
|
goneRets = compResults.gone;
|
|
|
}
|
|
|
|
|
|
callback(connector.connected, rets, goneRets);
|
|
|
|
|
|
rets = [];
|
|
|
goneRets = [];
|
|
|
exports.sartInventory(connector);
|
|
|
} else if (data.code === 2) {
|
|
|
rets = procTagInfo(countMap, rets, data.data);
|
|
|
rets = procTagInfo(rets, data.data, options.group, options.host);
|
|
|
}
|
|
|
callback(connector.connected, rets);
|
|
|
});
|
|
|
|
|
|
checkInventoryFlag = setInterval(function () { //检查是否在架
|
|
|
for (var i = 0; i < rets.length; i++) {
|
|
|
var offTime = 1 * new Date() - rets[i].updateTime;
|
|
|
if (offTime > appConfig.offInventory) {
|
|
|
rets[i].state = 'off';
|
|
|
rets[i].speed = 0;
|
|
|
}
|
|
|
}
|
|
|
}, appConfig.checkInventory);
|
|
|
|
|
|
checkSpeedFlag = setInterval(function () { //检查读取速度(times/sec)
|
|
|
for (var i = 0; i < rets.length; i++) {
|
|
|
if (countMap[rets[i].epc]) {
|
|
|
var prevCount = Number(countMap[rets[i].epc]);
|
|
|
rets[i].speed = prevCount / (appConfig.checkCount / 1000);
|
|
|
countMap[rets[i].epc] = 0;
|
|
|
}
|
|
|
}
|
|
|
}, appConfig.checkCount);
|
|
|
});
|
|
|
};
|
|
|
|
...
|
...
|
@@ -255,37 +248,71 @@ exports.stop = function (connector) { |
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 当前盘点到的标签与上次标签对比函数
|
|
|
* @param {Array} lastRets 上次盘点结果集
|
|
|
* @param {Array} rets 盘点结果集
|
|
|
* @param {Array} goneRets 离架标签结果集
|
|
|
* @return {Object} result 包含上次盘点结果集和离架标签结果集的对象
|
|
|
*/
|
|
|
function compTags(lastRets, rets, goneRets){
|
|
|
var result = {
|
|
|
last: [],
|
|
|
gone: []
|
|
|
};
|
|
|
|
|
|
for(var i = 0; i < lastRets.length; i++){
|
|
|
var item = lastRets[i];
|
|
|
var checkedItem = _.findWhere(rets, {epc: item.epc});
|
|
|
if(checkedItem === undefined){ // 上次盘点数据不包含在此次数据中
|
|
|
if(item.speed > 1)
|
|
|
goneRets.push(item);
|
|
|
}else{
|
|
|
lastRets[i] = checkedItem;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
for(var j = 0; j < rets.length; j++){
|
|
|
var ele = rets[j];
|
|
|
if(!_.findWhere(lastRets, {epc: ele.epc})){
|
|
|
lastRets.push(ele);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
result.last = lastRets;
|
|
|
result.gone = goneRets;
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 标签处理
|
|
|
* @method procTagInfo
|
|
|
* @param {Object} countMap 标签读取的次数
|
|
|
* @param {Array} rets 盘点结果集
|
|
|
* @param {Object} data 读取一次的内容
|
|
|
* @param {Number} group 标签所属的群组
|
|
|
* @param {String} host 读写器IP
|
|
|
*/
|
|
|
function procTagInfo(countMap, rets, data) {
|
|
|
var isExist = false,
|
|
|
now = 1 * new Date();
|
|
|
for (var i = 0; i < rets.length; i++) {
|
|
|
for(var j = 0; j < data.length; j++) {
|
|
|
if (rets[i].epc === data[j].epc) {
|
|
|
rets[i].rssi = data[j].rssi;
|
|
|
rets[i].updateTime = now;
|
|
|
rets[i].state = 'on';
|
|
|
rets[i].ant = data[j].ant;
|
|
|
countMap[rets[i].epc] ++;
|
|
|
isExist = true;
|
|
|
}
|
|
|
function procTagInfo(rets, data, group, host) {
|
|
|
var now = 1 * new Date();
|
|
|
|
|
|
for(var j = 0; j < data.length; j++) {
|
|
|
var item = data[j];
|
|
|
var checkedItem = _.findWhere(rets, {epc: item.epc});
|
|
|
if(checkedItem === undefined){
|
|
|
rets.push({
|
|
|
epc: item.epc,
|
|
|
rssi: item.rssi,
|
|
|
ant: item.ant,
|
|
|
group: group,
|
|
|
host: host,
|
|
|
speed: 1,
|
|
|
updateTime: now
|
|
|
});
|
|
|
}else{
|
|
|
checkedItem.speed++;
|
|
|
}
|
|
|
}
|
|
|
if (!isExist) {
|
|
|
_.map(data, function(item){
|
|
|
item.updateTime = now;
|
|
|
item.state = 'on';
|
|
|
item.speed = 0;
|
|
|
countMap[item.epc] = 1;
|
|
|
});
|
|
|
|
|
|
rets = rets.concat(data);
|
|
|
}
|
|
|
return rets;
|
|
|
}
|
|
|
|
...
|
...
|
|