Authored by Rock Zhang

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

@@ -8,7 +8,7 @@ @@ -8,7 +8,7 @@
8 * inventory_time: 盘点时间 8 * inventory_time: 盘点时间
9 */ 9 */
10 var readers = [ 10 var readers = [
11 - { 11 + /*{
12 id: 1, 12 id: 1,
13 readerId: 1, 13 readerId: 1,
14 readerIp: '172.16.13.5', 14 readerIp: '172.16.13.5',
@@ -106,7 +106,7 @@ var readers = [ @@ -106,7 +106,7 @@ var readers = [
106 frequency: 923.75, 106 frequency: 923.75,
107 stay_time: 100, 107 stay_time: 100,
108 inventory_time: 500 108 inventory_time: 500
109 - }, 109 + },*/
110 { 110 {
111 id: 12, 111 id: 12,
112 readerId: 12, 112 readerId: 12,
1 var net = require('net'), 1 var net = require('net'),
2 redis = require('redis'), 2 redis = require('redis'),
3 _ = require('lodash'), 3 _ = require('lodash'),
4 - index = require('../index.js'), 4 + async = require('async'),
  5 + index = require('../src/index.js'),
5 readerConfig = require('./readerConfig.js'); 6 readerConfig = require('./readerConfig.js');
6 7
7 var r = redis.createClient(); 8 var r = redis.createClient();
@@ -142,7 +143,15 @@ function connectAllReaders() @@ -142,7 +143,15 @@ function connectAllReaders()
142 freq: 10 143 freq: 10
143 }]; 144 }];
144 145
145 - connectReader(ip, 9761, filter, frequencys, reader.inventory_time, reader.ants, reader.group); 146 + // 初次连接最多连接5次
  147 + async.retry(5, function(cb, results){
  148 + connectReader(ip, 9761, filter, frequencys, reader.inventory_time, reader.ants, reader.group, cb);
  149 +
  150 + }, function(err, results){
  151 + if(err){
  152 + console.error(err);
  153 + }
  154 + });
146 } 155 }
147 } 156 }
148 157
@@ -156,8 +165,9 @@ function connectAllReaders() @@ -156,8 +165,9 @@ function connectAllReaders()
156 * @param inventoryTime 盘点时间 165 * @param inventoryTime 盘点时间
157 * @param power 各天线的功率 166 * @param power 各天线的功率
158 * @param group 读写器所在组 167 * @param group 读写器所在组
  168 + * @param callback 回调函数
159 */ 169 */
160 -function connectReader(host, port, filter, frequencys, inventoryTime, power, group) 170 +function connectReader(host, port, filter, frequencys, inventoryTime, power, group, callback)
161 { 171 {
162 index.start({ 172 index.start({
163 host: host, 173 host: host,
@@ -167,7 +177,7 @@ function connectReader(host, port, filter, frequencys, inventoryTime, power, gro @@ -167,7 +177,7 @@ function connectReader(host, port, filter, frequencys, inventoryTime, power, gro
167 inventoryTime: inventoryTime, 177 inventoryTime: inventoryTime,
168 power: power, 178 power: power,
169 group: group 179 group: group
170 - }, function (dat) { 180 + }, function (connected, dat) {
171 var onEpcs = [], 181 var onEpcs = [],
172 offEpcs = [], 182 offEpcs = [],
173 redisEpcs = []; 183 redisEpcs = [];
@@ -216,6 +226,12 @@ function connectReader(host, port, filter, frequencys, inventoryTime, power, gro @@ -216,6 +226,12 @@ function connectReader(host, port, filter, frequencys, inventoryTime, power, gro
216 r.hset(group, 'on', JSON.stringify(onEpcs.concat(redisEpcs)), function(){}); 226 r.hset(group, 'on', JSON.stringify(onEpcs.concat(redisEpcs)), function(){});
217 r.hset(group, 'off', JSON.stringify(offEpcs), function(){}); 227 r.hset(group, 'off', JSON.stringify(offEpcs), function(){});
218 228
  229 + if(connected){
  230 + callback(null, connected);
  231 + }else{
  232 + callback(new Error('连接读写器'+host+'错误'), connected);
  233 + }
  234 +
219 }); 235 });
220 } 236 }
221 237
1 { 1 {
2 - "name": "rfid-sdk",  
3 - "version": "1.0.0",  
4 - "description": "rfid uhf sdk",  
5 - "main": "index.js",  
6 - "dependencies": {  
7 - "babel": "~5.1.9",  
8 - "log4js": "0.6.x",  
9 - "lodash": "3.6.x",  
10 - "pm2": "~0.12.3",  
11 - "redis": "^0.12.1"  
12 - },  
13 - "scripts": {  
14 - "test": "echo \"Error: no test specified\" && exit 1"  
15 - },  
16 - "author": "",  
17 - "license": "ISC"  
18 -}  
  2 + "name": "rfid-sdk",
  3 + "version": "1.0.0",
  4 + "description": "rfid uhf sdk",
  5 + "main": "index.js",
  6 + "dependencies": {
  7 + "async": "^0.9.0",
  8 + "babel": "~5.1.9",
  9 + "lodash": "3.6.x",
  10 + "log4js": "0.6.x",
  11 + "pm2": "~0.12.3",
  12 + "redis": "^0.12.1"
  13 + },
  14 + "scripts": {
  15 + "test": "echo \"Error: no test specified\" && exit 1"
  16 + },
  17 + "author": "",
  18 + "license": "ISC"
  19 +}
@@ -37,6 +37,11 @@ exports.open = function (host, port, callback) { @@ -37,6 +37,11 @@ exports.open = function (host, port, callback) {
37 callback(connector); 37 callback(connector);
38 } 38 }
39 }); 39 });
  40 +
  41 + // 监听重连事件
  42 + connector.once('reconnect', function(){
  43 + setTimeout(function(){exports.open(host, port, callback);}, 5000);
  44 + });
40 } 45 }
41 46
42 /** 47 /**
@@ -128,7 +133,7 @@ exports.start = function (options, callback) { @@ -128,7 +133,7 @@ exports.start = function (options, callback) {
128 } else if (data.code === 2) { 133 } else if (data.code === 2) {
129 rets = procTagInfo(options.group, countMap, rets, data.data); 134 rets = procTagInfo(options.group, countMap, rets, data.data);
130 } 135 }
131 - callback(rets); 136 + callback(connector.connected, rets);
132 }); 137 });
133 138
134 checkInventoryFlag = setInterval(function () { //检查是否在架 139 checkInventoryFlag = setInterval(function () { //检查是否在架
@@ -29,6 +29,7 @@ class Connector extends events.EventEmitter { @@ -29,6 +29,7 @@ class Connector extends events.EventEmitter {
29 */ 29 */
30 constructor(config, send, receive) { 30 constructor(config, send, receive) {
31 super(); 31 super();
  32 + this.connected = false;
32 this.orderMq = []; 33 this.orderMq = [];
33 this.client = new net.Socket(); 34 this.client = new net.Socket();
34 let client = this.client; 35 let client = this.client;
@@ -36,6 +37,7 @@ class Connector extends events.EventEmitter { @@ -36,6 +37,7 @@ class Connector extends events.EventEmitter {
36 37
37 //初始化 38 //初始化
38 client.connect(config.port, config.host, function () { 39 client.connect(config.port, config.host, function () {
  40 + that.connected = true;
39 log.info(config.host + ':' + config.port + ':读写器连接成功!'); 41 log.info(config.host + ':' + config.port + ':读写器连接成功!');
40 send(); 42 send();
41 }); 43 });
@@ -61,12 +63,18 @@ class Connector extends events.EventEmitter { @@ -61,12 +63,18 @@ class Connector extends events.EventEmitter {
61 //异常处理 63 //异常处理
62 client.on('error', function (error) { 64 client.on('error', function (error) {
63 log.error('错误:' + error.code); 65 log.error('错误:' + error.code);
  66 +
  67 + // 读写器连接错误或者突然断开
  68 + this.connected = false;
  69 +
64 client.destroy(); 70 client.destroy();
65 receive(client, null, error); 71 receive(client, null, error);
66 }); 72 });
67 73
68 client.on('close', function () { 74 client.on('close', function () {
69 log.info('读写器连接断开'); 75 log.info('读写器连接断开');
  76 + // 触发重连事件
  77 + that.emit('reconnect');
70 }); 78 });
71 } 79 }
72 send(sendOrder) { 80 send(sendOrder) {