Authored by 周奇琪

Merge branch 'develop'

... ... @@ -39,7 +39,7 @@ class Connector extends events.EventEmitter {
client.connect(config.port, config.host, function() {
that.connected = true;
log.info(config.host + ':' + config.port + ':读写器连接成功!');
send();
send(that);
});
... ...
... ... @@ -26,7 +26,7 @@ export let log = loger;
* @param {Number} number 十进制数
* @return {Number} 16进制数字
*/
export let tenToHex = function (number) {
export let tenToHex = function(number) {
return (parseInt(number, 10) >>> 0).toString(16);
};
... ... @@ -37,7 +37,7 @@ export let tenToHex = function (number) {
* @param {Boolean} minus true表示负数,false表示正数
* @return {Number} 十进制数字
*/
export let hexToTen = function (number, minus) {
export let hexToTen = function(number, minus) {
if (minus) {
return parseInt('FFFF' + number, 16) >> 0;
} else {
... ... @@ -52,7 +52,7 @@ export let hexToTen = function (number, minus) {
* @param {Buff} data 返回数据流
* @return {Array} 转成16进制的数组
*/
export let toHex = function (data) {
export let toHex = function(data) {
let arr = [];
for (let i = 0; i < data.length; i++) {
arr[i] = tenToHex(data.readUIntLE(i));
... ... @@ -69,7 +69,7 @@ export let toHex = function (data) {
* @param {Object} output 标准输出的规格对象
* @return {object} 标准输出
*/
export let returnProc = function (data, ip, output) {
export let returnProc = function(data, ip, output) {
if (!_.isArray(data) || _.size(data) < 1) {
return false;
}
... ... @@ -79,7 +79,7 @@ export let returnProc = function (data, ip, output) {
if (!output[ret]) {
return false;
}
//log.info("读写器(" + ip + ")返回:" + output[ret].message + " 返回码:" + ret);
log.info("读写器(" + ip + ")返回:" + output[ret].message + " 返回码:" + ret);
output[ret].hex = data;
return output[ret];
};
... ... @@ -91,7 +91,7 @@ export let returnProc = function (data, ip, output) {
* @param {String} data 得到的指令数据结果集
* @return {Object} 返回拆分结果和拆分依据的指令头
*/
export let spliteRet = function (mq, data) {
export let spliteRet = function(mq, data) {
let indexes = [],
rets = [],
i,
... ... @@ -121,7 +121,7 @@ export let spliteRet = function (mq, data) {
let allRets = [];
for (j = 0; j < rets.length; j++) {
if (rets[j][0] == 'f1' || rets[j][1] != '2') { //对于盘点一次指令,多次返回,盘点特殊处理
if (rets[j][0] == 'f1' && rets[j][1] != '2') { //对于盘点一次指令,多次返回,盘点特殊处理
let length = hexToTen(rets[j][1]);
let allLength = rets[j].length;
... ... @@ -148,15 +148,17 @@ export let spliteRet = function (mq, data) {
* @param {Array} data 返回的结果集 eg. ['f0','2','0','0']
* @return {Object} 返回定位到的指令
*/
export let locateOrder = function (orders, data) {
export let locateOrder = function(orders, data) {
let orderRet = {};
_.forIn(orders, function (val, key) {
var order = val.input.toLowerCase();
var prefix = data[0].toLowerCase();
if (_.startsWith(order, prefix)) {
orderRet.name = key;
orderRet.val = val;
_.forIn(orders, function(val, key) {
if (val && val.input) {
var order = val.input.toLowerCase();
var prefix = data[0].toLowerCase();
if (_.startsWith(order, prefix)) {
orderRet.name = key;
orderRet.val = val;
}
}
});
return orderRet;
... ...
import expect from 'expect.js';
import muk from 'muk';
describe('base.js', function() {
describe('init', function() {
it();
});
describe('send', function() {
it();
});
describe('receive', function() {
it();
});
describe('procFunc', function() {
it();
});
});
\ No newline at end of file
... ...
import expect from 'expect.js';
import muk from 'muk';
describe('config.js', function() {
describe('init', function() {
it();
});
describe('prePower', function() {
it();
});
describe('preFilter', function() {
it();
});
describe('preStayTime', function() {
it();
});
describe('preFrequency', function() {
it();
});
});
\ No newline at end of file
... ...
import expect from 'expect.js';
import muk from 'muk';
import net from '../mock/fakeNet';
describe('connector.js', function() {
let C;
before(function() {
C = muk('../../src/lib/connector', {
'net': net
});
C.__set__('log', {
info: function() {},
error: function() {}
});
})
describe('init', function() {
it('new connector init ok', function(done) {
var connect = new C({
host: '1.1.1.1',
port: '123'
}, function(con) {
expect(con.connected).to.be(true);
done();
});
});
it('new connector error', function(done) {
var connect = new C({
host: '1.1.1.1',
port: '123'
}, function(con) {}, function(client, data, error) {
expect(error.message).to.be('mock error');
done();
});
connect.client.addEmit('error', {
message: 'mock error'
});
});
it('new connect after error then reconnect', function(done) {
var connect = new C({
host: '1.1.1.1',
port: '123'
}, function(con) {}, function() {});
connect.on('reconnect', function() {
done();
});
connect.client.addEmit('error', {
message: 'mock error'
});
});
});
describe('connector write then get data', function() {
it('on data expect ok', function(done) {
C.__set__('toHex', function() {});
C.__set__('spliteRet', function() {
return {
rets: ['mock']
}
});
C.__set__('locateOrder', function() {
return {
name: 'mock'
}
});
var connect = new C({
host: '1.1.1.1',
port: '123'
}, function(con) {}, function() {
return 'ok';
});
connect.on('mock', function(data) {
expect(data).to.be('ok');
done();
});
connect.client.write('test');
});
});
describe('sendOrder', function() {
it('send expect same back', function(done) {
var connect = new C({
host: '1.1.1.1',
port: '123'
}, function(con) {}, function() {});
C.__set__('spliteRet', function(mq) {
return {
rets: [],
mq: mq
};
});
let call = new Buffer('Mock', 'hex');
connect.client.on('data', function(data) {
expect(data).to.eql(call);
expect(connect.orderMq[0]).to.be('mo');
done();
});
connect.send('Mock');
});
})
after(function() {
muk.restore();
});
});
\ No newline at end of file
... ...
import expect from 'expect.js';
import muk from 'muk';
describe('inventory.js', function() {
describe('init', function() {
it();
});
describe('preSetInventoryTime', function() {
it();
});
describe('start', function() {
it();
});
});
\ No newline at end of file
... ...
import expect from 'expect.js';
import muk from 'muk';
describe('uhf.js', function() {
describe('init', function() {
it();
});
describe('getConfig', function() {
it();
});
describe('getVersion', function() {
it();
});
});
\ No newline at end of file
... ...
import expect from 'expect.js';
import muk from 'muk';
import {
tenToHex,
hexToTen,
toHex,
log,
returnProc,
spliteRet,
locateOrder
}
from '../../src/lib/util';
import interfaces from '../../src/lib/interface';
describe('util.js', function() {
let util;
before(function() {
util = muk('../../src/lib/util', {
'log4js': {
loadAppender: function() {},
addAppender: function() {},
appenders: {
file: function() {}
},
getLogger: function() {
return {
error: function() {},
info: function() {},
setLevel: function() {}
}
}
}
});
});
describe('tenToHex', function() {
it('tenToHex input 12 expect c', function() {
expect(tenToHex(12)).to.be('c');
});
it('tenToHex input -12 expect fffffff4', function() {
expect(tenToHex(-12)).to.be('fffffff4');
});
it('tenToHex input 0 expect 0', function() {
expect(tenToHex(0)).to.be('0');
});
});
describe('hexToTen', function() {
it('hexToTen input FFD3 ,true expect -45', function() {
expect(hexToTen('FFD3', true)).to.be(-45);
});
it('hexToTen input FFD3 expect 65491', function() {
expect(hexToTen('FFD3')).to.be(65491);
})
});
describe('toHex', function() {
it('toHex input new Buffer("0C0D","hex") expect ["c","d"]', function() {
let input = new Buffer('0C0D', 'hex');
expect(toHex(input)).to.eql(['c', 'd']);
});
});
describe('returnProc', function() {
it('returnProc input null expect false', function() {
expect(returnProc()).to.be(false);
});
it('returnProc input [1,2] 1.1.1.1 {"1":1} ', function() {
expect(returnProc([1, 2], "1.1.1.1", {
"1": 1
})).to.be(false);
});
it('returnProc input ["0", "1"], "1.1.1.1", {"01": {"message": "test","hex": "01"}}', function() {
let ret = util.returnProc(['0', '1'], '1.1.1.1', {
'01': {
'message': 'test'
}
});
expect(ret).to.eql({
'message': 'test',
'hex': '01'
});
});
});
describe('spliteRet', function() {
let mq = ['f5', 'f2', 'f3'];
let data = ['f5', '1', '3', 'f2', '2', '3', '4', 'f3', '0', '12', '2'];
it("expect ['f5','1','3'] ['f2','2','3','4'],['f3','0','12','2']", function() {
expect(spliteRet(mq, data)).to.eql({
rets: [
['f5', '1', '3'],
['f2', '2', '3', '4'],
['f3', '0', '12', '2']
],
mq: []
});
});
let mq2 = ['f1'];
let data2 = ['f1', '1', '2', 'f1', '1', '4'];
it("expect ['f1', '1', '2' ],['f1', '1', '4']", function() {
expect(spliteRet(mq2, data2)).to.eql({
rets: [
['f1', '1', '2'],
['f1', '1', '4']
],
mq: []
});
})
});
describe('locateOrder', function() {
it('input ["a0", "00", "00"] expect {key:"openUhf",val:interfaces.openUhf}', function() {
expect(locateOrder(interfaces, ['a0', '00', '00']).name).to.be('openUhf');
})
})
});
\ No newline at end of file
... ...
import events from 'events';
class FakeSocket extends events.EventEmitter {
constructor() {
super();
this.data = {};
}
connect(port, host, callback) {
callback();
}
write(data) {
this.addEmit('data', data);
}
addEmit(ev, data) {
let that = this;
process.nextTick(function() {
that.emit(ev, data);
});
}
destroy() {
let that = this;
process.nextTick(function() {
that.addEmit('close', that.data);
});
}
}
export default {
Socket: FakeSocket
};
\ No newline at end of file
... ...
... ... @@ -2,4 +2,10 @@ require("babel/register")({
plugins: ["babel-plugin-rewire"]
}); //es6 polyfill
require('./lib/index.test');
\ No newline at end of file
require('./lib/index.test');
require('./lib/connector.test');
require('./lib/util.test');
require('./lib/base.test');
require('./lib/uhf.test');
require('./lib/config.test');
require('./lib/inventory.test');
\ No newline at end of file
... ...