Authored by 周奇琪

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

Conflicts:
	src/index.js
@@ -334,14 +334,14 @@ var ants = [ @@ -334,14 +334,14 @@ var ants = [
334 readerId: 11, 334 readerId: 11,
335 groupId: 8, 335 groupId: 8,
336 nPower: 15 336 nPower: 15
337 - }/*, 337 + },
338 { 338 {
339 id: 31, 339 id: 31,
340 antId: 0, 340 antId: 0,
341 readerId: 12, 341 readerId: 12,
342 groupId: 12, 342 groupId: 12,
343 nPower: 15 343 nPower: 15
344 - }*/ 344 + }
345 ]; 345 ];
346 346
347 // 获取读写器配置 347 // 获取读写器配置
@@ -35,7 +35,7 @@ r.on('error', function(err) @@ -35,7 +35,7 @@ r.on('error', function(err)
35 function main() 35 function main()
36 { 36 {
37 // 读取配置文件 37 // 读取配置文件
38 - var host = '172.16.13.59', 38 + var host = '172.16.13.95',
39 port = 12345; 39 port = 12345;
40 40
41 41
@@ -167,11 +167,10 @@ function connectReader(host, port, filter, frequency, inventoryTime, antStayTime @@ -167,11 +167,10 @@ function connectReader(host, port, filter, frequency, inventoryTime, antStayTime
167 antStayTime: antStayTime, 167 antStayTime: antStayTime,
168 power: power, 168 power: power,
169 group: group 169 group: group
170 - }, function (connected, dat) {  
171 - //console.log(dat); 170 + }, function (connected, onTags, goneTags) {
  171 +
172 var onEpcs = [], 172 var onEpcs = [],
173 - offEpcs = [],  
174 - redisEpcs = []; 173 + offEpcs = [];
175 174
176 // 获取redis中指定组在架标签数据 175 // 获取redis中指定组在架标签数据
177 r.hget(group, 'on', function(err, epcs) 176 r.hget(group, 'on', function(err, epcs)
@@ -184,7 +183,45 @@ function connectReader(host, port, filter, frequency, inventoryTime, antStayTime @@ -184,7 +183,45 @@ function connectReader(host, port, filter, frequency, inventoryTime, antStayTime
184 { 183 {
185 try 184 try
186 { 185 {
187 - redisEpcs = JSON.parse(epcs); 186 + var redisEpcs = JSON.parse(epcs);
  187 +
  188 + // 处理在架标签
  189 + _.forEach(redisEpcs, function(item){
  190 + if(item.speed > 2 && item.host != host && _.findWhere(onTags, {epc: item.epc}) === undefined){
  191 + onEpcs.push({
  192 + GroupID: item.group,
  193 + EPC: item.epc,
  194 + updateTime: item.updateTime
  195 + });
  196 + }
  197 + });
  198 + _.forEach(onTags, function(item){
  199 + onEpcs.push({
  200 + GroupID: item.group,
  201 + EPC: item.epc,
  202 + updateTime: item.updateTime
  203 + });
  204 + });
  205 +
  206 + // 处理离架标签
  207 + _.forEach(goneTags, function(goneEpc){
  208 + if(!goneEpc || _.findWhere(redisEpcs, {epc: goneEpc.epc}) === undefined){
  209 + offEpcs.push({
  210 + GroupID: goneEpc.group,
  211 + EPC: goneEpc.epc,
  212 + updateTime: goneEpc.updateTime
  213 + });
  214 + }
  215 + });
  216 +
  217 + r.hset(group, 'on', JSON.stringify(onEpcs), function(){});
  218 + r.hset(group, 'off', JSON.stringify(offEpcs), function(){});
  219 +
  220 + if(connected){
  221 + callback(null, connected);
  222 + }else{
  223 + callback(new Error('连接读写器'+host+'错误'), connected);
  224 + }
188 } 225 }
189 catch(er) 226 catch(er)
190 { 227 {
@@ -193,36 +230,6 @@ function connectReader(host, port, filter, frequency, inventoryTime, antStayTime @@ -193,36 +230,6 @@ function connectReader(host, port, filter, frequency, inventoryTime, antStayTime
193 } 230 }
194 }); 231 });
195 232
196 - _.forEach(dat, function(item){  
197 -  
198 - if(item.state == 'on' && item.speed > 2 && !_.findWhere(redisEpcs, {EPC: item.epc}))  
199 - {  
200 - onEpcs.push({  
201 - EPC: item.epc,  
202 - GroupID: group,  
203 - updateTime: item.updateTime  
204 - });  
205 - }  
206 -  
207 - if(item.state == 'off' && !_.findWhere(redisEpcs, {EPC: item.epc}))  
208 - {  
209 - offEpcs.push({  
210 - EPC: item.epc,  
211 - GroupID: group,  
212 - updateTime: item.updateTime  
213 - });  
214 - }  
215 - });  
216 -  
217 - r.hset(group, 'on', JSON.stringify(onEpcs.concat(redisEpcs)), function(){});  
218 - r.hset(group, 'off', JSON.stringify(offEpcs), function(){});  
219 -  
220 - if(connected){  
221 - callback(null, connected);  
222 - }else{  
223 - callback(new Error('连接读写器'+host+'错误'), connected);  
224 - }  
225 -  
226 }); 233 });
227 } 234 }
228 235
@@ -185,14 +185,14 @@ export let stopInventory = function(connector) { @@ -185,14 +185,14 @@ export let stopInventory = function(connector) {
185 let isStop = false; 185 let isStop = false;
186 186
187 export let start = function(options, callback) { 187 export let start = function(options, callback) {
188 - var rets = [];  
189 - var countMap = {}; 188 + var lastRets = [],
  189 + rets = [],
  190 + goneRets = [];
190 open(options.host, options.port, function(err, connector) { 191 open(options.host, options.port, function(err, connector) {
191 if (!options.filter || !options.frequencys || !options.inventoryTime || !options.power || !options.antStayTime) { 192 if (!options.filter || !options.frequencys || !options.inventoryTime || !options.power || !options.antStayTime) {
192 let error = new Error('Error:灵敏度,定频,盘点时长,功率必须要传!'); 193 let error = new Error('Error:灵敏度,定频,盘点时长,功率必须要传!');
193 callback(error); 194 callback(error);
194 } 195 }
195 -  
196 //开启灵敏度过滤 196 //开启灵敏度过滤
197 setFilter(connector, options.filter); 197 setFilter(connector, options.filter);
198 //设置天线驻留时长 198 //设置天线驻留时长
@@ -215,11 +215,23 @@ export let start = function(options, callback) { @@ -215,11 +215,23 @@ export let start = function(options, callback) {
215 215
216 connector.on('startInventory', function(data) { 216 connector.on('startInventory', function(data) {
217 if (data.code !== 2 && !isStop) { 217 if (data.code !== 2 && !isStop) {
  218 +
  219 + if (lastRets.length === 0) {
  220 + lastRets = rets;
  221 + } else {
  222 + var compResults = compTags(lastRets, rets, goneRets);
  223 + lastRets = compResults.last;
  224 + goneRets = compResults.gone;
  225 + }
  226 +
  227 + callback(connector.connected, rets, goneRets);
  228 +
  229 + rets = [];
  230 + goneRets = [];
218 startInventory(connector); 231 startInventory(connector);
219 } else if (data.code === 2) { 232 } else if (data.code === 2) {
220 - rets = procTagInfo(countMap, rets, data.data); 233 + rets = procTagInfo(rets, data.data, options.group, options.host);
221 } 234 }
222 - callback(null, rets);  
223 }); 235 });
224 }); 236 });
225 }; 237 };
@@ -234,37 +246,77 @@ export let stop = function() { @@ -234,37 +246,77 @@ export let stop = function() {
234 }; 246 };
235 247
236 /** 248 /**
  249 + * 当前盘点到的标签与上次标签对比函数
  250 + * @param {Array} lastRets 上次盘点结果集
  251 + * @param {Array} rets 盘点结果集
  252 + * @param {Array} goneRets 离架标签结果集
  253 + * @return {Object} result 包含上次盘点结果集和离架标签结果集的对象
  254 + */
  255 +function compTags(lastRets, rets, goneRets) {
  256 + var result = {
  257 + last: [],
  258 + gone: []
  259 + };
  260 +
  261 + for (var i = 0; i < lastRets.length; i++) {
  262 + var item = lastRets[i];
  263 + var checkedItem = _.findWhere(rets, {
  264 + epc: item.epc
  265 + });
  266 + if (checkedItem === undefined) { // 上次盘点数据不包含在此次数据中
  267 + if (item.speed > 1)
  268 + goneRets.push(item);
  269 + } else {
  270 + lastRets[i] = checkedItem;
  271 + }
  272 + }
  273 +
  274 + for (var j = 0; j < rets.length; j++) {
  275 + var ele = rets[j];
  276 + if (!_.findWhere(lastRets, {
  277 + epc: ele.epc
  278 + })) {
  279 + lastRets.push(ele);
  280 + }
  281 + }
  282 +
  283 + result.last = lastRets;
  284 + result.gone = goneRets;
  285 +
  286 + return result;
  287 +}
  288 +
  289 +/**
237 * 标签处理 290 * 标签处理
238 * @method procTagInfo 291 * @method procTagInfo
239 - * @param {Object} countMap 标签读取的次数  
240 * @param {Array} rets 盘点结果集 292 * @param {Array} rets 盘点结果集
241 * @param {Object} data 读取一次的内容 293 * @param {Object} data 读取一次的内容
  294 + * @param {Number} group 标签所属的群组
  295 + * @param {String} host 读写器IP
242 */ 296 */
243 -function procTagInfo(countMap, rets, data) {  
244 - let isExist = false,  
245 - now = 1 * new Date();  
246 - for (var i = 0; i < rets.length; i++) {  
247 - for (var j = 0; j < data.length; j++) {  
248 - if (rets[i].epc === data[j].epc) {  
249 - rets[i].rssi = data[j].rssi;  
250 - rets[i].updateTime = now;  
251 - rets[i].state = 'on';  
252 - rets[i].ant = data[j].ant;  
253 - countMap[rets[i].epc] ++;  
254 - isExist = true;  
255 - } 297 +function procTagInfo(rets, data, group, host) {
  298 + var now = 1 * new Date();
  299 +
  300 + for (var j = 0; j < data.length; j++) {
  301 + var item = data[j];
  302 + var checkedItem = _.findWhere(rets, {
  303 + epc: item.epc
  304 + });
  305 + if (checkedItem === undefined) {
  306 + rets.push({
  307 + epc: item.epc,
  308 + rssi: item.rssi,
  309 + ant: item.ant,
  310 + group: group,
  311 + host: host,
  312 + speed: 1,
  313 + updateTime: now
  314 + });
  315 + } else {
  316 + checkedItem.speed++;
256 } 317 }
257 } 318 }
258 - if (!isExist) {  
259 - _.map(data, function(item) {  
260 - item.updateTime = now;  
261 - item.state = 'on';  
262 - item.speed = 0;  
263 - countMap[item.epc] = 1;  
264 - });  
265 319
266 - rets = rets.concat(data);  
267 - }  
268 return rets; 320 return rets;
269 } 321 }
270 322
@@ -75,6 +75,7 @@ export let returnProc = function (data, ip, output) { @@ -75,6 +75,7 @@ export let returnProc = function (data, ip, output) {
75 } 75 }
76 76
77 let ret = data.join(''); 77 let ret = data.join('');
  78 + console.log(ret);
78 if (!output[ret]) { 79 if (!output[ret]) {
79 return false; 80 return false;
80 } 81 }