index.js
11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/**
* @fileOverview 调用入口
* @author qiqi.zhou@yoho.cn
*/
/**
* 连接器
* @type {Connector}
*/
import Connector from './lib/connector';
/**
* order config
* @type {Object}
*/
import orders from './lib/interface';
/**
* uhf dirctive module
* @type {Uhf}
*/
import Uhf from './lib/directive/uhf';
/**
* config dirctive module
* @type {[Config]}
*/
import Config from './lib/directive/config';
/**
* inventory dirctive module
* @type {Inventory}
*/
import Inventory from './lib/directive/inventory';
import _ from 'lodash';
import async from 'async';
//import r from '../extra/rfidRedis.js';// 引入redis
import {
locateOrder
}
from './lib/util';
import appConfig from './config';
/**
* 读写器初始化模块实例
* @type {Uhf}
*/
let uhf = new Uhf();
/**
* 配置读写器实例
* @type {Config}
*/
let config = new Config();
/**
* 读写器盘点实例
* @type {Inventory}
*/
let inventory = new Inventory();
import log4js from 'log4js';
let loger = log4js.getLogger('log');
loger.setLevel(config.logLevel); //配置日志
/**
* @mixes 日志记录对象
* @exports util/log
*/
export let log = loger;
/**
* 打开读写器
* @method open
* @exports index/open
* @param {String} host 读写器IP
* @param {Number} port 端口
* @param {Function} callback 打开成功回调
*/
export let open = function(host, port, callback) {
if (!host || !port || !callback) {
throw new Error("错误:ip,端口,回调必须要传");
}
//连接一个读写器
var connector = new Connector({
host: host,
port: port
}, function() {
uhf.send(connector, orders.openUhf);
}, receive);
//当打开读写器模块
connector.on('openUhf', function(data) {
if (data.code === 1 || data.code === 2) {
callback(null, connector);
} else if (data) {
var error = new Error(data.message);
error.code = data.code;
callback(error);
}
});
};
/**
* 设置功率
* @method setPower
* @exports index/setPower
* @param {Connector} connector 连接器
* @param {Array} 传参,依次是4个天线的功率 eg: [15,15,15,0]
*/
export let setPower = function(connector, param) {
config.send(connector, orders.setPower, param);
};
/**
* 设置灵敏度筛选
* @method setFilter
* @exports index/setFilter
* @param {Connector} connect 连接器
* param {Object} 是否开启灵敏度和灵敏度值 eg:{on:'1',rssi:-60}
*/
export let setFilter = function(connector, param) {
config.send(connector, orders.setFilter, param);
};
/**
* 设置天线驻留时长
* @method setAntStayTime
* @exports index/setAntStayTime
* @param {Connector} connect 连接器
* @param {Number} param 时长,单位毫秒 eg: 100
*/
export let setAntStayTime = function(connector, param) {
config.send(connector, orders.setAntStayTime, param);
};
/**
* 设置固定频率
* @method setFrequency
* @exports index/setFrequency
* @param {Connector} connector 连接器
* @param {Object} param 天线端口和定频的值 eg: 10
*/
export let setFrequency = function(connector, param) {
config.send(connector, orders.setFrequency, param);
};
/**
* 设置频率区域
* @method setFrequencyRegion
* @exports index/setFrequencyRegion
* @param {Connector} connector 连接器
* @param {Object} param 频率区域的值 eg: 0:FCC,1:CCC,2:NCC,3:JPN
*/
export let setFrequencyRegion = function(connector, param) {
config.send(connector, orders.setFrequencyRegion, param);
};
/**
* 设置速度模式
* @method setSpeedMode
* @exports index/setSpeedMode
* @param {Connector} connector 连接器
* @param {Object} param 速度模式的值 eg: 0:高速,1:正常速度,2:省电模式
*/
export let setSpeedMode = function(connector, param) {
config.send(connector, orders.setSpeedMode, param);
};
/**
* 设置标签组
* @method setTagGroup
* @exports index/setTagGroup
* @param {Connector} connector 连接器
* @param {Object} param 标签组的值 eg: {'session':0,'target':0,'search_mode':1}
* 其中session:0代表S0,1代表S1,2代表S2,3代表S3
* target:0代表A,1代表B
* search_mode:0代表DUAL_TARGET,1代表SINGLE_TARGET,2代表SINGLE_TARGET_WITH_SUPPRESSION
*/
export let setTagGroup = function(connector, param) {
config.send(connector, orders.setTagGroup, param);
};
/**
* 设置盘点时长
* @method setInventoryTime
* @exports index/setInventoryTime
* @param {Connector} connect 连接器
* @param {Number} param 时长,单位毫秒 eg: 400
*/
export let setInventoryTime = function(connector, param) {
inventory.send(connector, orders.setInventoryTime, param);
};
/**
* 开始盘点
* @method startInventory
* @exports index/startInventory
* @param {Connector} connect 连接器
*/
export let startInventory = function(connector) {
inventory.send(connector, orders.startInventory);
};
/**
* 结束盘点
* @method stopInventory
* @exports index/stopInventory
* @param {Connector} connector 连接器
*/
export let stopInventory = function(connector) {
inventory.send(connector, orders.stopInventory);
};
/**
* 开始间歇盘点封装
* @method start
* @exports index/start
* @param {Object} options 传入的配置参数
* @param {Function} callback 获取数据的回调
* @example:
exports.start({
host: '172.16.13.6',//IP
port: '9761',//端口
filter: {//设置灵敏度的过滤
'on': '1',
rssi: -60
},
frequencys: 10, //天线的定频
inventoryTime: 400,//设置盘点时长
antStayTime: 100,//设置天线驻留时长
power: [15, 0, 0, 0]//功率设置
}, function (error,data) {
//console.log(data);
});
*/
let isStop = false;
export let start = function(options, callback) {
var lastRets = [],
rets = [],
goneRets = [];
open(options.host, options.port, function(err, connector) {
if (!options.filter || !options.frequency || !options.inventoryTime || !options.power || !options.antStayTime) {
let error = new Error('Error:灵敏度,定频,盘点时长,功率必须要传!');
callback(error);
return;
}
//开启灵敏度过滤
setFilter(connector, options.filter);
//设置天线驻留时长
connector.on('setFilter', function(data) {
console.log('goood');
setAntStayTime(connector, options.antStayTime);
});
//设置频率区域
connector.on('setAntStayTime', function(data) {
setFrequencyRegion(connector, 1); // 设置频率区域为CCC
});
//设置速度模式
connector.on('setFrequencyRegion', function(data) {
setSpeedMode(connector, 0); // 设置速度模式为高速
});
//设置标签组
connector.on('setSpeedMode', function(data) {
setTagGroup(connector, {'session':0,'target':0,'search_mode':1}); // 设置标签组:session:S0,target:A,search_mode:单目标
});
//开启固定频率
connector.on('setTagGroup', function(data) {
setFrequency(connector, options.frequency);
});
//设置盘点时长
connector.on('setFrequency', function(data) {
setInventoryTime(connector, options.inventoryTime);
});
//设置功率
connector.on('setInventoryTime', function(data) {
setPower(connector, options.power);
});
//开始盘点
connector.on('setPower', function(data) {
startInventory(connector);
});
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(null, connector.connected, rets, goneRets);
rets = [];
goneRets = [];
startInventory(connector);
} else if (data.code === 2) {
rets = procTagInfo(rets, data.data, options.group, options.host);
}
});
});
};
/**
* 停止间歇盘点
* @method stop
* @exports index/stop
*/
export let stop = function() {
isStop = true;
};
/**
* 当前盘点到的标签与上次标签对比函数
* @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 > 2 || item.rssi > -60)
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 {Array} rets 盘点结果集
* @param {Object} data 读取一次的内容
* @param {Number} group 标签所属的群组
* @param {String} host 读写器IP
*/
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++;
checkedItem.rssi = item.rssi;
checkedItem.ant = item.ant;
checkedItem.updateTime = now;
}
}
return rets;
}
/**
* 接收数据处理
* @method receive
* @param {Socket} client socket客户端
* @param {Object} data 接收到的数据
*/
function receive(client, data, err) {
let className = null,
ret = null;
if (!data) {
return null;
}
if (err) {
throw err;
}
className = locateOrder(orders, data).val.directive;
switch (className) {
case 'uhf':
ret = uhf.receive(client, data);
break;
case 'config':
ret = config.receive(client, data);
break;
case 'inventory':
ret = inventory.receive(client, data);
break;
}
return ret;
}