Authored by Rock Zhang

限定读写器自动重连次数,并增加手动重连方式

... ... @@ -16,28 +16,40 @@ function Daemon()
{
events.EventEmitter.call(this);
this.readers = [];
// 监听重连事件
this.on('reconnect', function(host){
tagService.connectedReaders = _.filter(tagService.connectedReaders, function(reader){
return reader !== host;
});
var reader = readerConfig[host];
var filter = {
'on': '1',
rssi: reader.rssi
};
// 尝试重连5次
async.retry(5, function(cb, results){
setTimeout(function(){
tagService.connectReader(host, 9761, filter, reader.frequency, reader.inventory_time, reader.stay_time, reader.ants, reader.group, cb);
}, 2000);
}, function(err, results){
if(err){
console.error(err);
tagService.connectedReaders = _.forEach(tagService.connectedReaders, function(reader){
if(reader.name === host)
{
reader.status = false;
reader.restartTimes++;
}
});
var checkedReader = _.findWhere(tagService.connectedReaders, {name: host});
if(checkedReader !== undefined && checkedReader.restartTimes === 1) // 只自动重连一次
{
var reader = readerConfig[host];
var filter = {
'on': '1',
rssi: reader.rssi
};
// 尝试重连5次
async.retry(5, function(cb, results){
console.log(results);
setTimeout(function(){
tagService.connectReader(host, 9761, filter, reader.frequency, reader.inventory_time, reader.stay_time, reader.ants, reader.group, cb);
}, 3000);
}, function(err, results){
if(err){
console.error(err);
}
});
}
});
}
... ...
var net = require('net'),
_ = require('lodash'),
tagService = require('./tagService.js'),
readerConfig = require('./readerConfig.js').getReaderConfig(),
async = require('async'),
WebSocketServer = require('ws').Server;
var r = require('./rfidRedis.js');
... ... @@ -62,6 +64,26 @@ function main()
ws.send(JSON.stringify(infos));
}
else if(params.commandType === 'restart') // 重启读写器
{
var reader = readerConfig[params.reader];
console.log(reader);
var filter = {
'on': '1',
rssi: reader.rssi
};
// 尝试重连5次
async.retry(5, function(cb, results){
setTimeout(function(){
tagService.connectReader(params.reader, 9761, filter, reader.frequency, reader.inventory_time, reader.stay_time, reader.ants, reader.group, cb);
}, 3000);
}, function(err, results){
if(err){
console.error(err);
}
});
}
else
{
terminals.push({
... ... @@ -130,7 +152,7 @@ function getMillTimeDiffNow(val)
/**
* 读取命令结果
* @param String data 传入的数据
* @param {String} data 传入的数据
* @returns {Object} 解析出来的数据对象
*/
function getCommand(data)
... ... @@ -152,13 +174,13 @@ function getCommand(data)
}
params.commandType = data.CommandType;
if(params.commandType !== 'daemon')
if('GroupID' in data)
{
if('GroupID' in data)
{
params.groupId = data.GroupID;
}
params.groupId = data.GroupID;
}
if('state' in data)
{
var state = data.State;
if(state == 'ShelfOn' || state == 'ShelfOnAndNear')
{
... ... @@ -170,6 +192,10 @@ function getCommand(data)
}
params.state = state;
}
if('reader' in data)
{
params.reader = data.reader;
}
return params;
}
... ...
... ... @@ -107,8 +107,19 @@ function connectReader(host, port, filter, frequency, inventoryTime, antStayTime
r.hset(group, 'off', JSON.stringify(offEpcs), function(){});
if(connected){
exports.connectedReaders.push(host);
exports.connectedReaders = _.uniq(exports.connectedReaders);
var reader = _.findWhere(exports.connectedReaders, {name: host});
if(reader !== undefined)
{
reader.status = true;
}
else
{
exports.connectedReaders.push({
name: host,
status: true,
restartTimes: 0
});
}
cb(null, connected);
}else{
cb(new Error('连接读写器'+host+'错误'), connected);
... ...
... ... @@ -246,11 +246,11 @@ export let start = function(options, callback) {
callback(error);
return;
}
//开启灵敏度过滤
setFilter(connector, options.filter);
//设置天线驻留时长
connector.on('setFilter', function(data) {
console.log('goood');
setAntStayTime(connector, options.antStayTime);
});
//设置频率区域
... ...