Authored by 周奇琪

Merge branch 'develop'

@@ -39,7 +39,7 @@ class Connector extends events.EventEmitter { @@ -39,7 +39,7 @@ class Connector extends events.EventEmitter {
39 client.connect(config.port, config.host, function() { 39 client.connect(config.port, config.host, function() {
40 that.connected = true; 40 that.connected = true;
41 log.info(config.host + ':' + config.port + ':读写器连接成功!'); 41 log.info(config.host + ':' + config.port + ':读写器连接成功!');
42 - send(); 42 + send(that);
43 }); 43 });
44 44
45 45
@@ -26,7 +26,7 @@ export let log = loger; @@ -26,7 +26,7 @@ export let log = loger;
26 * @param {Number} number 十进制数 26 * @param {Number} number 十进制数
27 * @return {Number} 16进制数字 27 * @return {Number} 16进制数字
28 */ 28 */
29 -export let tenToHex = function (number) { 29 +export let tenToHex = function(number) {
30 return (parseInt(number, 10) >>> 0).toString(16); 30 return (parseInt(number, 10) >>> 0).toString(16);
31 }; 31 };
32 32
@@ -37,7 +37,7 @@ export let tenToHex = function (number) { @@ -37,7 +37,7 @@ export let tenToHex = function (number) {
37 * @param {Boolean} minus true表示负数,false表示正数 37 * @param {Boolean} minus true表示负数,false表示正数
38 * @return {Number} 十进制数字 38 * @return {Number} 十进制数字
39 */ 39 */
40 -export let hexToTen = function (number, minus) { 40 +export let hexToTen = function(number, minus) {
41 if (minus) { 41 if (minus) {
42 return parseInt('FFFF' + number, 16) >> 0; 42 return parseInt('FFFF' + number, 16) >> 0;
43 } else { 43 } else {
@@ -52,7 +52,7 @@ export let hexToTen = function (number, minus) { @@ -52,7 +52,7 @@ export let hexToTen = function (number, minus) {
52 * @param {Buff} data 返回数据流 52 * @param {Buff} data 返回数据流
53 * @return {Array} 转成16进制的数组 53 * @return {Array} 转成16进制的数组
54 */ 54 */
55 -export let toHex = function (data) { 55 +export let toHex = function(data) {
56 let arr = []; 56 let arr = [];
57 for (let i = 0; i < data.length; i++) { 57 for (let i = 0; i < data.length; i++) {
58 arr[i] = tenToHex(data.readUIntLE(i)); 58 arr[i] = tenToHex(data.readUIntLE(i));
@@ -69,7 +69,7 @@ export let toHex = function (data) { @@ -69,7 +69,7 @@ export let toHex = function (data) {
69 * @param {Object} output 标准输出的规格对象 69 * @param {Object} output 标准输出的规格对象
70 * @return {object} 标准输出 70 * @return {object} 标准输出
71 */ 71 */
72 -export let returnProc = function (data, ip, output) { 72 +export let returnProc = function(data, ip, output) {
73 if (!_.isArray(data) || _.size(data) < 1) { 73 if (!_.isArray(data) || _.size(data) < 1) {
74 return false; 74 return false;
75 } 75 }
@@ -79,7 +79,7 @@ export let returnProc = function (data, ip, output) { @@ -79,7 +79,7 @@ export let returnProc = function (data, ip, output) {
79 if (!output[ret]) { 79 if (!output[ret]) {
80 return false; 80 return false;
81 } 81 }
82 - //log.info("读写器(" + ip + ")返回:" + output[ret].message + " 返回码:" + ret); 82 + log.info("读写器(" + ip + ")返回:" + output[ret].message + " 返回码:" + ret);
83 output[ret].hex = data; 83 output[ret].hex = data;
84 return output[ret]; 84 return output[ret];
85 }; 85 };
@@ -91,7 +91,7 @@ export let returnProc = function (data, ip, output) { @@ -91,7 +91,7 @@ export let returnProc = function (data, ip, output) {
91 * @param {String} data 得到的指令数据结果集 91 * @param {String} data 得到的指令数据结果集
92 * @return {Object} 返回拆分结果和拆分依据的指令头 92 * @return {Object} 返回拆分结果和拆分依据的指令头
93 */ 93 */
94 -export let spliteRet = function (mq, data) { 94 +export let spliteRet = function(mq, data) {
95 let indexes = [], 95 let indexes = [],
96 rets = [], 96 rets = [],
97 i, 97 i,
@@ -121,7 +121,7 @@ export let spliteRet = function (mq, data) { @@ -121,7 +121,7 @@ export let spliteRet = function (mq, data) {
121 121
122 let allRets = []; 122 let allRets = [];
123 for (j = 0; j < rets.length; j++) { 123 for (j = 0; j < rets.length; j++) {
124 - if (rets[j][0] == 'f1' || rets[j][1] != '2') { //对于盘点一次指令,多次返回,盘点特殊处理 124 + if (rets[j][0] == 'f1' && rets[j][1] != '2') { //对于盘点一次指令,多次返回,盘点特殊处理
125 125
126 let length = hexToTen(rets[j][1]); 126 let length = hexToTen(rets[j][1]);
127 let allLength = rets[j].length; 127 let allLength = rets[j].length;
@@ -148,15 +148,17 @@ export let spliteRet = function (mq, data) { @@ -148,15 +148,17 @@ export let spliteRet = function (mq, data) {
148 * @param {Array} data 返回的结果集 eg. ['f0','2','0','0'] 148 * @param {Array} data 返回的结果集 eg. ['f0','2','0','0']
149 * @return {Object} 返回定位到的指令 149 * @return {Object} 返回定位到的指令
150 */ 150 */
151 -export let locateOrder = function (orders, data) { 151 +export let locateOrder = function(orders, data) {
152 let orderRet = {}; 152 let orderRet = {};
153 - _.forIn(orders, function (val, key) {  
154 - var order = val.input.toLowerCase();  
155 - var prefix = data[0].toLowerCase();  
156 -  
157 - if (_.startsWith(order, prefix)) {  
158 - orderRet.name = key;  
159 - orderRet.val = val; 153 + _.forIn(orders, function(val, key) {
  154 + if (val && val.input) {
  155 + var order = val.input.toLowerCase();
  156 + var prefix = data[0].toLowerCase();
  157 +
  158 + if (_.startsWith(order, prefix)) {
  159 + orderRet.name = key;
  160 + orderRet.val = val;
  161 + }
160 } 162 }
161 }); 163 });
162 return orderRet; 164 return orderRet;
  1 +import expect from 'expect.js';
  2 +import muk from 'muk';
  3 +describe('base.js', function() {
  4 + describe('init', function() {
  5 + it();
  6 + });
  7 + describe('send', function() {
  8 + it();
  9 + });
  10 + describe('receive', function() {
  11 + it();
  12 + });
  13 + describe('procFunc', function() {
  14 + it();
  15 + });
  16 +
  17 +});
  1 +import expect from 'expect.js';
  2 +import muk from 'muk';
  3 +describe('config.js', function() {
  4 + describe('init', function() {
  5 + it();
  6 + });
  7 + describe('prePower', function() {
  8 + it();
  9 + });
  10 + describe('preFilter', function() {
  11 + it();
  12 + });
  13 + describe('preStayTime', function() {
  14 + it();
  15 + });
  16 + describe('preFrequency', function() {
  17 + it();
  18 + });
  19 +
  20 +});
  1 +import expect from 'expect.js';
  2 +import muk from 'muk';
  3 +import net from '../mock/fakeNet';
  4 +
  5 +describe('connector.js', function() {
  6 +
  7 + let C;
  8 + before(function() {
  9 + C = muk('../../src/lib/connector', {
  10 + 'net': net
  11 + });
  12 +
  13 + C.__set__('log', {
  14 + info: function() {},
  15 + error: function() {}
  16 + });
  17 + })
  18 +
  19 + describe('init', function() {
  20 + it('new connector init ok', function(done) {
  21 + var connect = new C({
  22 + host: '1.1.1.1',
  23 + port: '123'
  24 + }, function(con) {
  25 + expect(con.connected).to.be(true);
  26 + done();
  27 + });
  28 + });
  29 +
  30 + it('new connector error', function(done) {
  31 + var connect = new C({
  32 + host: '1.1.1.1',
  33 + port: '123'
  34 + }, function(con) {}, function(client, data, error) {
  35 + expect(error.message).to.be('mock error');
  36 + done();
  37 + });
  38 +
  39 + connect.client.addEmit('error', {
  40 + message: 'mock error'
  41 + });
  42 + });
  43 +
  44 + it('new connect after error then reconnect', function(done) {
  45 + var connect = new C({
  46 + host: '1.1.1.1',
  47 + port: '123'
  48 + }, function(con) {}, function() {});
  49 + connect.on('reconnect', function() {
  50 + done();
  51 + });
  52 + connect.client.addEmit('error', {
  53 + message: 'mock error'
  54 + });
  55 + });
  56 + });
  57 +
  58 + describe('connector write then get data', function() {
  59 + it('on data expect ok', function(done) {
  60 + C.__set__('toHex', function() {});
  61 + C.__set__('spliteRet', function() {
  62 + return {
  63 + rets: ['mock']
  64 + }
  65 + });
  66 + C.__set__('locateOrder', function() {
  67 + return {
  68 + name: 'mock'
  69 + }
  70 + });
  71 +
  72 + var connect = new C({
  73 + host: '1.1.1.1',
  74 + port: '123'
  75 + }, function(con) {}, function() {
  76 + return 'ok';
  77 + });
  78 +
  79 + connect.on('mock', function(data) {
  80 + expect(data).to.be('ok');
  81 + done();
  82 + });
  83 +
  84 + connect.client.write('test');
  85 + });
  86 + });
  87 +
  88 + describe('sendOrder', function() {
  89 + it('send expect same back', function(done) {
  90 + var connect = new C({
  91 + host: '1.1.1.1',
  92 + port: '123'
  93 + }, function(con) {}, function() {});
  94 + C.__set__('spliteRet', function(mq) {
  95 + return {
  96 + rets: [],
  97 + mq: mq
  98 + };
  99 + });
  100 + let call = new Buffer('Mock', 'hex');
  101 + connect.client.on('data', function(data) {
  102 + expect(data).to.eql(call);
  103 + expect(connect.orderMq[0]).to.be('mo');
  104 + done();
  105 + });
  106 + connect.send('Mock');
  107 +
  108 + });
  109 + })
  110 +
  111 + after(function() {
  112 + muk.restore();
  113 + });
  114 +});
  1 +import expect from 'expect.js';
  2 +import muk from 'muk';
  3 +describe('inventory.js', function() {
  4 + describe('init', function() {
  5 + it();
  6 + });
  7 + describe('preSetInventoryTime', function() {
  8 + it();
  9 + });
  10 + describe('start', function() {
  11 + it();
  12 + });
  13 +});
  1 +import expect from 'expect.js';
  2 +import muk from 'muk';
  3 +describe('uhf.js', function() {
  4 + describe('init', function() {
  5 + it();
  6 + });
  7 + describe('getConfig', function() {
  8 + it();
  9 + });
  10 + describe('getVersion', function() {
  11 + it();
  12 + });
  13 +});
  1 +import expect from 'expect.js';
  2 +import muk from 'muk';
  3 +import {
  4 + tenToHex,
  5 + hexToTen,
  6 + toHex,
  7 + log,
  8 + returnProc,
  9 + spliteRet,
  10 + locateOrder
  11 +}
  12 +from '../../src/lib/util';
  13 +import interfaces from '../../src/lib/interface';
  14 +
  15 +describe('util.js', function() {
  16 + let util;
  17 + before(function() {
  18 + util = muk('../../src/lib/util', {
  19 + 'log4js': {
  20 + loadAppender: function() {},
  21 + addAppender: function() {},
  22 + appenders: {
  23 + file: function() {}
  24 + },
  25 + getLogger: function() {
  26 + return {
  27 + error: function() {},
  28 + info: function() {},
  29 + setLevel: function() {}
  30 + }
  31 + }
  32 + }
  33 + });
  34 + });
  35 +
  36 + describe('tenToHex', function() {
  37 + it('tenToHex input 12 expect c', function() {
  38 + expect(tenToHex(12)).to.be('c');
  39 + });
  40 +
  41 + it('tenToHex input -12 expect fffffff4', function() {
  42 + expect(tenToHex(-12)).to.be('fffffff4');
  43 + });
  44 +
  45 + it('tenToHex input 0 expect 0', function() {
  46 + expect(tenToHex(0)).to.be('0');
  47 + });
  48 + });
  49 +
  50 + describe('hexToTen', function() {
  51 + it('hexToTen input FFD3 ,true expect -45', function() {
  52 + expect(hexToTen('FFD3', true)).to.be(-45);
  53 + });
  54 + it('hexToTen input FFD3 expect 65491', function() {
  55 + expect(hexToTen('FFD3')).to.be(65491);
  56 + })
  57 + });
  58 +
  59 + describe('toHex', function() {
  60 + it('toHex input new Buffer("0C0D","hex") expect ["c","d"]', function() {
  61 + let input = new Buffer('0C0D', 'hex');
  62 + expect(toHex(input)).to.eql(['c', 'd']);
  63 + });
  64 + });
  65 +
  66 + describe('returnProc', function() {
  67 + it('returnProc input null expect false', function() {
  68 + expect(returnProc()).to.be(false);
  69 + });
  70 +
  71 + it('returnProc input [1,2] 1.1.1.1 {"1":1} ', function() {
  72 + expect(returnProc([1, 2], "1.1.1.1", {
  73 + "1": 1
  74 + })).to.be(false);
  75 + });
  76 +
  77 + it('returnProc input ["0", "1"], "1.1.1.1", {"01": {"message": "test","hex": "01"}}', function() {
  78 + let ret = util.returnProc(['0', '1'], '1.1.1.1', {
  79 + '01': {
  80 + 'message': 'test'
  81 + }
  82 + });
  83 +
  84 + expect(ret).to.eql({
  85 + 'message': 'test',
  86 + 'hex': '01'
  87 + });
  88 + });
  89 + });
  90 +
  91 + describe('spliteRet', function() {
  92 + let mq = ['f5', 'f2', 'f3'];
  93 + let data = ['f5', '1', '3', 'f2', '2', '3', '4', 'f3', '0', '12', '2'];
  94 + it("expect ['f5','1','3'] ['f2','2','3','4'],['f3','0','12','2']", function() {
  95 + expect(spliteRet(mq, data)).to.eql({
  96 + rets: [
  97 + ['f5', '1', '3'],
  98 + ['f2', '2', '3', '4'],
  99 + ['f3', '0', '12', '2']
  100 + ],
  101 + mq: []
  102 + });
  103 + });
  104 +
  105 + let mq2 = ['f1'];
  106 + let data2 = ['f1', '1', '2', 'f1', '1', '4'];
  107 + it("expect ['f1', '1', '2' ],['f1', '1', '4']", function() {
  108 + expect(spliteRet(mq2, data2)).to.eql({
  109 + rets: [
  110 + ['f1', '1', '2'],
  111 + ['f1', '1', '4']
  112 + ],
  113 + mq: []
  114 + });
  115 + })
  116 + });
  117 +
  118 + describe('locateOrder', function() {
  119 + it('input ["a0", "00", "00"] expect {key:"openUhf",val:interfaces.openUhf}', function() {
  120 + expect(locateOrder(interfaces, ['a0', '00', '00']).name).to.be('openUhf');
  121 + })
  122 + })
  123 +});
  1 +import events from 'events';
  2 +
  3 +class FakeSocket extends events.EventEmitter {
  4 + constructor() {
  5 + super();
  6 + this.data = {};
  7 + }
  8 +
  9 + connect(port, host, callback) {
  10 + callback();
  11 + }
  12 +
  13 + write(data) {
  14 + this.addEmit('data', data);
  15 + }
  16 +
  17 + addEmit(ev, data) {
  18 + let that = this;
  19 + process.nextTick(function() {
  20 + that.emit(ev, data);
  21 + });
  22 + }
  23 + destroy() {
  24 + let that = this;
  25 + process.nextTick(function() {
  26 + that.addEmit('close', that.data);
  27 + });
  28 + }
  29 +}
  30 +
  31 +export default {
  32 + Socket: FakeSocket
  33 +};
@@ -2,4 +2,10 @@ require("babel/register")({ @@ -2,4 +2,10 @@ require("babel/register")({
2 plugins: ["babel-plugin-rewire"] 2 plugins: ["babel-plugin-rewire"]
3 }); //es6 polyfill 3 }); //es6 polyfill
4 4
5 -require('./lib/index.test');  
  5 +require('./lib/index.test');
  6 +require('./lib/connector.test');
  7 +require('./lib/util.test');
  8 +require('./lib/base.test');
  9 +require('./lib/uhf.test');
  10 +require('./lib/config.test');
  11 +require('./lib/inventory.test');