...
|
...
|
@@ -2,54 +2,57 @@ |
|
|
* @fileOverview 调用入口
|
|
|
* @author qiqi.zhou@yoho.cn
|
|
|
*/
|
|
|
//require("babel/register"); //es6 polyfill
|
|
|
|
|
|
/**
|
|
|
* 连接器
|
|
|
* @type {Connector}
|
|
|
*/
|
|
|
var Connector = require('./lib/connector');
|
|
|
import Connector from './lib/connector';
|
|
|
/**
|
|
|
* order config
|
|
|
* @type {Object}
|
|
|
*/
|
|
|
var orders = require('./lib/interface');
|
|
|
import orders from './lib/interface';
|
|
|
/**
|
|
|
* uhf dirctive module
|
|
|
* @type {Uhf}
|
|
|
*/
|
|
|
var Uhf = require('./lib/directive/uhf');
|
|
|
import Uhf from './lib/directive/uhf';
|
|
|
/**
|
|
|
* config dirctive module
|
|
|
* @type {[Config]}
|
|
|
*/
|
|
|
var Config = require('./lib/directive/config');
|
|
|
import Config from './lib/directive/config';
|
|
|
/**
|
|
|
* inventory dirctive module
|
|
|
* @type {Inventory}
|
|
|
*/
|
|
|
var Inventory = require('./lib/directive/inventory');
|
|
|
import Inventory from './lib/directive/inventory';
|
|
|
|
|
|
var _ = require('lodash');
|
|
|
var util = require('./lib/util');
|
|
|
import _ from 'lodash';
|
|
|
|
|
|
var appConfig = require('./config');
|
|
|
import {
|
|
|
locateOrder
|
|
|
}
|
|
|
from './lib/util';
|
|
|
|
|
|
import appConfig from './config';
|
|
|
|
|
|
/**
|
|
|
* 读写器初始化模块实例
|
|
|
* @type {Uhf}
|
|
|
*/
|
|
|
var uhf = new Uhf();
|
|
|
let uhf = new Uhf();
|
|
|
/**
|
|
|
* 配置读写器实例
|
|
|
* @type {Config}
|
|
|
*/
|
|
|
var config = new Config();
|
|
|
let config = new Config();
|
|
|
/**
|
|
|
* 读写器盘点实例
|
|
|
* @type {Inventory}
|
|
|
*/
|
|
|
var inventory = new Inventory();
|
|
|
let inventory = new Inventory();
|
|
|
|
|
|
/**
|
|
|
* 打开读写器
|
...
|
...
|
@@ -59,22 +62,25 @@ var inventory = new Inventory(); |
|
|
* @param {Number} port 端口
|
|
|
* @param {Function} callback 打开成功回调
|
|
|
*/
|
|
|
exports.open = function (host, port, callback) {
|
|
|
if (!host || !port) {
|
|
|
return;
|
|
|
export let open = function(host, port, callback) {
|
|
|
if (!host || !port || !callback) {
|
|
|
throw new Error("错误:ip,端口,回调必须要传");
|
|
|
}
|
|
|
//连接一个读写器
|
|
|
var connector = new Connector({
|
|
|
host: host,
|
|
|
port: port
|
|
|
}, function () {
|
|
|
}, function() {
|
|
|
uhf.send(connector, orders.openUhf);
|
|
|
}, receive);
|
|
|
|
|
|
//当打开读写器模块
|
|
|
connector.on('openUhf', function (data) {
|
|
|
if ((data.code === 1 || data.code === 2) && callback) {
|
|
|
callback(connector);
|
|
|
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);
|
|
|
}
|
|
|
});
|
|
|
};
|
...
|
...
|
@@ -86,7 +92,7 @@ exports.open = function (host, port, callback) { |
|
|
* @param {Connector} connector 连接器
|
|
|
* @param {Array} 传参,依次是4个天线的功率 eg: [15,15,15,0]
|
|
|
*/
|
|
|
exports.setPower = function (connector, param) {
|
|
|
export let setPower = function(connector, param) {
|
|
|
config.send(connector, orders.setPower, param);
|
|
|
};
|
|
|
|
...
|
...
|
@@ -97,7 +103,7 @@ exports.setPower = function (connector, param) { |
|
|
* @param {Connector} connect 连接器
|
|
|
* param {Object} 是否开启灵敏度和灵敏度值 eg:{on:'1',rssi:-60}
|
|
|
*/
|
|
|
exports.setFilter = function (connector, param) {
|
|
|
export let setFilter = function(connector, param) {
|
|
|
config.send(connector, orders.setFilter, param);
|
|
|
};
|
|
|
|
...
|
...
|
@@ -108,7 +114,7 @@ exports.setFilter = function (connector, param) { |
|
|
* @param {Connector} connector 连接器
|
|
|
* @param {Object} param 天线端口和定频的值 eg: {ant: 0, freq: 10 }
|
|
|
*/
|
|
|
exports.setFrequency = function (connector, param) {
|
|
|
export let setFrequency = function(connector, param) {
|
|
|
config.send(connector, orders.setFrequency, param);
|
|
|
};
|
|
|
|
...
|
...
|
@@ -119,18 +125,18 @@ exports.setFrequency = function (connector, param) { |
|
|
* @param {Connector} connect 连接器
|
|
|
* @param {Number} param 时长,单位毫秒 eg: 400
|
|
|
*/
|
|
|
exports.setInventoryTime = function (connector, param) {
|
|
|
export let setInventoryTime = function(connector, param) {
|
|
|
inventory.send(connector, orders.setInventoryTime, param);
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 开始盘点
|
|
|
* @method sartInventory
|
|
|
* @exports index/sartInventory
|
|
|
* @method startInventory
|
|
|
* @exports index/startInventory
|
|
|
* @param {Connector} connect 连接器
|
|
|
*/
|
|
|
exports.sartInventory = function (connector) {
|
|
|
inventory.send(connector, orders.sartInventory);
|
|
|
export let startInventory = function(connector) {
|
|
|
inventory.send(connector, orders.startInventory);
|
|
|
};
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -139,7 +145,7 @@ exports.sartInventory = function (connector) { |
|
|
* @exports index/stopInventory
|
|
|
* @param {Connector} connector 连接器
|
|
|
*/
|
|
|
exports.stopInventory = function (connector) {
|
|
|
export let stopInventory = function(connector) {
|
|
|
inventory.send(connector, orders.stopInventory);
|
|
|
};
|
|
|
|
...
|
...
|
@@ -172,72 +178,49 @@ exports.stopInventory = function (connector) { |
|
|
}],
|
|
|
inventoryTime: 400,//设置盘点时长
|
|
|
power: [15, 0, 0, 0]//功率设置
|
|
|
}, function (data) {
|
|
|
}, function (error,data) {
|
|
|
//console.log(data);
|
|
|
});
|
|
|
*/
|
|
|
var isStop = false,
|
|
|
checkInventoryFlag,
|
|
|
checkSpeedFlag;
|
|
|
exports.start = function (options, callback) {
|
|
|
let isStop = false;
|
|
|
|
|
|
export let start = function(options, callback) {
|
|
|
var rets = [];
|
|
|
var countMap = {};
|
|
|
exports.open(options.host, options.port, function (connector) {
|
|
|
|
|
|
if (options.filter) { //开启灵敏度过滤
|
|
|
exports.setFilter(connector, options.filter);
|
|
|
}
|
|
|
|
|
|
if (options.frequencys) { //开启固定频率
|
|
|
_.forIn(options.frequencys, function (val, key) {
|
|
|
exports.setFrequency(connector, val);
|
|
|
});
|
|
|
open(options.host, options.port, function(err, connector) {
|
|
|
if (!options.filter || !options.frequencys || !options.inventoryTime || !options.power) {
|
|
|
let error = new Error('Error:灵敏度,定频,盘点时长,功率必须要传!');
|
|
|
callback(error);
|
|
|
}
|
|
|
//开启灵敏度过滤
|
|
|
setFilter(connector, options.filter);
|
|
|
|
|
|
if (options.inventoryTime) { //设置盘点时长
|
|
|
exports.setInventoryTime(connector, options.inventoryTime);
|
|
|
}
|
|
|
//开启固定频率
|
|
|
_.forIn(options.frequencys, function(val, key) {
|
|
|
setFrequency(connector, val);
|
|
|
});
|
|
|
|
|
|
if (options.power) { //设置功率
|
|
|
connector.on('setInventoryTime', function (data) {
|
|
|
exports.setPower(connector, options.power);
|
|
|
});
|
|
|
//设置盘点时长
|
|
|
setInventoryTime(connector, options.inventoryTime);
|
|
|
|
|
|
}
|
|
|
//设置功率
|
|
|
connector.on('setInventoryTime', function(data) {
|
|
|
setPower(connector, options.power);
|
|
|
});
|
|
|
|
|
|
connector.on('setPower', function (data) {
|
|
|
exports.sartInventory(connector); //开始盘点
|
|
|
//开始盘点
|
|
|
connector.on('setPower', function(data) {
|
|
|
startInventory(connector);
|
|
|
});
|
|
|
|
|
|
connector.on('sartInventory', function (data) {
|
|
|
//console.log(data.code);
|
|
|
connector.on('startInventory', function(data) {
|
|
|
if (data.code !== 2 && !isStop) {
|
|
|
exports.sartInventory(connector);
|
|
|
startInventory(connector);
|
|
|
} else if (data.code === 2) {
|
|
|
rets = procTagInfo(countMap, rets, data.data);
|
|
|
}
|
|
|
callback(rets);
|
|
|
callback(null, 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);
|
|
|
});
|
|
|
};
|
|
|
|
...
|
...
|
@@ -245,12 +228,9 @@ exports.start = function (options, callback) { |
|
|
* 停止间歇盘点
|
|
|
* @method stop
|
|
|
* @exports index/stop
|
|
|
* @param {Connector} connect 连接器
|
|
|
*/
|
|
|
exports.stop = function (connector) {
|
|
|
export let stop = function() {
|
|
|
isStop = true;
|
|
|
clearInterval(checkInventoryFlag);
|
|
|
clearInterval(checkSpeedFlag);
|
|
|
};
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -261,7 +241,7 @@ exports.stop = function (connector) { |
|
|
* @param {Object} data 读取一次的内容
|
|
|
*/
|
|
|
function procTagInfo(countMap, rets, data) {
|
|
|
var isExist = false,
|
|
|
let isExist = false,
|
|
|
now = 1 * new Date();
|
|
|
for (var i = 0; i < rets.length; i++) {
|
|
|
if (rets[i].epc === data.epc) {
|
...
|
...
|
@@ -291,26 +271,26 @@ function procTagInfo(countMap, rets, data) { |
|
|
* @param {Object} data 接收到的数据
|
|
|
*/
|
|
|
function receive(client, data, err) {
|
|
|
var className = null,
|
|
|
let className = null,
|
|
|
ret = null;
|
|
|
if (!data) {
|
|
|
return;
|
|
|
return null;
|
|
|
}
|
|
|
if (err) {
|
|
|
console.log(err);
|
|
|
throw err;
|
|
|
}
|
|
|
className = util.locateOrder(orders, data).val.directive;
|
|
|
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;
|
|
|
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;
|
|
|
} |
|
|
\ No newline at end of file |
...
|
...
|
|