Authored by 周奇琪

Merge branch 'master' of git.dev.yoho.cn:web/rfid-sdk

Conflicts:
	src/index.js
... ... @@ -334,14 +334,14 @@ var ants = [
readerId: 11,
groupId: 8,
nPower: 15
}/*,
},
{
id: 31,
antId: 0,
readerId: 12,
groupId: 12,
nPower: 15
}*/
}
];
// 获取读写器配置
... ...
... ... @@ -35,7 +35,7 @@ r.on('error', function(err)
function main()
{
// 读取配置文件
var host = '172.16.13.59',
var host = '172.16.13.95',
port = 12345;
... ... @@ -167,11 +167,10 @@ function connectReader(host, port, filter, frequency, inventoryTime, antStayTime
antStayTime: antStayTime,
power: power,
group: group
}, function (connected, dat) {
//console.log(dat);
}, function (connected, onTags, goneTags) {
var onEpcs = [],
offEpcs = [],
redisEpcs = [];
offEpcs = [];
// 获取redis中指定组在架标签数据
r.hget(group, 'on', function(err, epcs)
... ... @@ -184,7 +183,45 @@ function connectReader(host, port, filter, frequency, inventoryTime, antStayTime
{
try
{
redisEpcs = JSON.parse(epcs);
var redisEpcs = JSON.parse(epcs);
// 处理在架标签
_.forEach(redisEpcs, function(item){
if(item.speed > 2 && item.host != host && _.findWhere(onTags, {epc: item.epc}) === undefined){
onEpcs.push({
GroupID: item.group,
EPC: item.epc,
updateTime: item.updateTime
});
}
});
_.forEach(onTags, function(item){
onEpcs.push({
GroupID: item.group,
EPC: item.epc,
updateTime: item.updateTime
});
});
// 处理离架标签
_.forEach(goneTags, function(goneEpc){
if(!goneEpc || _.findWhere(redisEpcs, {epc: goneEpc.epc}) === undefined){
offEpcs.push({
GroupID: goneEpc.group,
EPC: goneEpc.epc,
updateTime: goneEpc.updateTime
});
}
});
r.hset(group, 'on', JSON.stringify(onEpcs), function(){});
r.hset(group, 'off', JSON.stringify(offEpcs), function(){});
if(connected){
callback(null, connected);
}else{
callback(new Error('连接读写器'+host+'错误'), connected);
}
}
catch(er)
{
... ... @@ -193,36 +230,6 @@ function connectReader(host, port, filter, frequency, inventoryTime, antStayTime
}
});
_.forEach(dat, function(item){
if(item.state == 'on' && item.speed > 2 && !_.findWhere(redisEpcs, {EPC: item.epc}))
{
onEpcs.push({
EPC: item.epc,
GroupID: group,
updateTime: item.updateTime
});
}
if(item.state == 'off' && !_.findWhere(redisEpcs, {EPC: item.epc}))
{
offEpcs.push({
EPC: item.epc,
GroupID: group,
updateTime: item.updateTime
});
}
});
r.hset(group, 'on', JSON.stringify(onEpcs.concat(redisEpcs)), function(){});
r.hset(group, 'off', JSON.stringify(offEpcs), function(){});
if(connected){
callback(null, connected);
}else{
callback(new Error('连接读写器'+host+'错误'), connected);
}
});
}
... ...
... ... @@ -185,14 +185,14 @@ export let stopInventory = function(connector) {
let isStop = false;
export let start = function(options, callback) {
var rets = [];
var countMap = {};
var lastRets = [],
rets = [],
goneRets = [];
open(options.host, options.port, function(err, connector) {
if (!options.filter || !options.frequencys || !options.inventoryTime || !options.power || !options.antStayTime) {
let error = new Error('Error:灵敏度,定频,盘点时长,功率必须要传!');
callback(error);
}
//开启灵敏度过滤
setFilter(connector, options.filter);
//设置天线驻留时长
... ... @@ -215,11 +215,23 @@ export let start = function(options, callback) {
connector.on('startInventory', 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 = [];
startInventory(connector);
} else if (data.code === 2) {
rets = procTagInfo(countMap, rets, data.data);
rets = procTagInfo(rets, data.data, options.group, options.host);
}
callback(null, rets);
});
});
};
... ... @@ -234,37 +246,77 @@ export let stop = function() {
};
/**
* 当前盘点到的标签与上次标签对比函数
* @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) {
let 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;
}
... ...
... ... @@ -75,6 +75,7 @@ export let returnProc = function (data, ip, output) {
}
let ret = data.join('');
console.log(ret);
if (!output[ret]) {
return false;
}
... ...