Authored by Rock Zhang

添加初次连接时的重连指定次数的功能,和断开重新连接的功能

... ... @@ -8,7 +8,7 @@
* inventory_time: 盘点时间
*/
var readers = [
{
/*{
id: 1,
readerId: 1,
readerIp: '172.16.13.5',
... ... @@ -106,7 +106,7 @@ var readers = [
frequency: 923.75,
stay_time: 100,
inventory_time: 500
},
},*/
{
id: 12,
readerId: 12,
... ...
var net = require('net'),
redis = require('redis'),
_ = require('lodash'),
index = require('../index.js'),
async = require('async'),
index = require('../src/index.js'),
readerConfig = require('./readerConfig.js');
var r = redis.createClient();
... ... @@ -142,7 +143,15 @@ function connectAllReaders()
freq: 10
}];
connectReader(ip, 9761, filter, frequencys, reader.inventory_time, reader.ants, reader.group);
// 初次连接最多连接5次
async.retry(5, function(cb, results){
connectReader(ip, 9761, filter, frequencys, reader.inventory_time, reader.ants, reader.group, cb);
}, function(err, results){
if(err){
console.error(err);
}
});
}
}
... ... @@ -156,8 +165,9 @@ function connectAllReaders()
* @param inventoryTime 盘点时间
* @param power 各天线的功率
* @param group 读写器所在组
* @param callback 回调函数
*/
function connectReader(host, port, filter, frequencys, inventoryTime, power, group)
function connectReader(host, port, filter, frequencys, inventoryTime, power, group, callback)
{
index.start({
host: host,
... ... @@ -167,7 +177,7 @@ function connectReader(host, port, filter, frequencys, inventoryTime, power, gro
inventoryTime: inventoryTime,
power: power,
group: group
}, function (dat) {
}, function (connected, dat) {
var onEpcs = [],
offEpcs = [],
redisEpcs = [];
... ... @@ -216,6 +226,12 @@ function connectReader(host, port, filter, frequencys, inventoryTime, power, gro
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);
}
});
}
... ...
... ... @@ -4,9 +4,10 @@
"description": "rfid uhf sdk",
"main": "index.js",
"dependencies": {
"async": "^0.9.0",
"babel": "~5.1.9",
"log4js": "0.6.x",
"lodash": "3.6.x",
"log4js": "0.6.x",
"pm2": "~0.12.3",
"redis": "^0.12.1"
},
... ...
... ... @@ -37,6 +37,11 @@ exports.open = function (host, port, callback) {
callback(connector);
}
});
// 监听重连事件
connector.once('reconnect', function(){
setTimeout(function(){exports.open(host, port, callback);}, 5000);
});
}
/**
... ... @@ -128,7 +133,7 @@ exports.start = function (options, callback) {
} else if (data.code === 2) {
rets = procTagInfo(options.group, countMap, rets, data.data);
}
callback(rets);
callback(connector.connected, rets);
});
checkInventoryFlag = setInterval(function () { //检查是否在架
... ...
... ... @@ -29,6 +29,7 @@ class Connector extends events.EventEmitter {
*/
constructor(config, send, receive) {
super();
this.connected = false;
this.orderMq = [];
this.client = new net.Socket();
let client = this.client;
... ... @@ -36,6 +37,7 @@ class Connector extends events.EventEmitter {
//初始化
client.connect(config.port, config.host, function () {
that.connected = true;
log.info(config.host + ':' + config.port + ':读写器连接成功!');
send();
});
... ... @@ -61,12 +63,18 @@ class Connector extends events.EventEmitter {
//异常处理
client.on('error', function (error) {
log.error('错误:' + error.code);
// 读写器连接错误或者突然断开
this.connected = false;
client.destroy();
receive(client, null, error);
});
client.on('close', function () {
log.info('读写器连接断开');
// 触发重连事件
that.emit('reconnect');
});
}
send(sendOrder) {
... ...