1.添加单元测试框架,引入了mock和代码注入
2.加了makefile和travis ci脚本 3.调整index.js的部分写法,修改成ES6的语法格式 4.把依赖babel的方法移到入口index.js 5.把usage.js放到example目录 6.修改package.json引入了babel新版本(5.4.2) 7.为了mock测试支持es6,引入了babel-plugin-rewire插件
Showing
17 changed files
with
532 additions
and
124 deletions
.travis.yml
0 → 100644
Makefile
0 → 100644
1 | -读写器的SDK | ||
2 | ------------- | 1 | +title: 读写器的SDK |
3 | 2 | ||
3 | +# 读写器的SDK | ||
4 | + | ||
5 | + | ||
6 | +# 概要 | ||
4 | 本SDK主要用于UHF RFID读写器的调用。完全用node来实现Socket的硬件通讯。 | 7 | 本SDK主要用于UHF RFID读写器的调用。完全用node来实现Socket的硬件通讯。 |
5 | 8 | ||
6 | 已实现接口: | 9 | 已实现接口: |
@@ -15,7 +18,8 @@ | @@ -15,7 +18,8 @@ | ||
15 | * 获取固件版本(B4 00 00 00 00) | 18 | * 获取固件版本(B4 00 00 00 00) |
16 | * 设置盘点时长(B0 00 00 00 00) | 19 | * 设置盘点时长(B0 00 00 00 00) |
17 | 20 | ||
18 | -类关联图 | 21 | + |
22 | +# 类关联图 | ||
19 | 23 | ||
20 | ``` | 24 | ``` |
21 | index + config | 25 | index + config |
@@ -31,8 +35,7 @@ UHF模块指令类 配置指令类 盘点指令类 | @@ -31,8 +35,7 @@ UHF模块指令类 配置指令类 盘点指令类 | ||
31 | ``` | 35 | ``` |
32 | 36 | ||
33 | 37 | ||
34 | -使用方法 | ||
35 | -============== | 38 | +# 使用方法 |
36 | 39 | ||
37 | ### 环境准备 | 40 | ### 环境准备 |
38 | 41 | ||
@@ -52,7 +55,8 @@ cd rfid-sdk | @@ -52,7 +55,8 @@ cd rfid-sdk | ||
52 | npm install | 55 | npm install |
53 | ``` | 56 | ``` |
54 | 57 | ||
55 | -es5调用方法 | 58 | + |
59 | +# es5调用方法 | ||
56 | 60 | ||
57 | ``` javascript | 61 | ``` javascript |
58 | //调用 | 62 | //调用 |
@@ -67,6 +71,7 @@ require("babel/register");//需要加入Babel的polyfill | @@ -67,6 +71,7 @@ require("babel/register");//需要加入Babel的polyfill | ||
67 | var sdk = require('../src/index');//模块入口 | 71 | var sdk = require('../src/index');//模块入口 |
68 | ``` | 72 | ``` |
69 | 73 | ||
74 | +# 例子 | ||
70 | 75 | ||
71 | ``` javascript | 76 | ``` javascript |
72 | //开始盘点 | 77 | //开始盘点 |
@@ -98,11 +103,8 @@ sdk.start({ | @@ -98,11 +103,8 @@ sdk.start({ | ||
98 | 103 | ||
99 | ``` | 104 | ``` |
100 | 105 | ||
106 | +# 问题: | ||
101 | 107 | ||
102 | - | ||
103 | - | ||
104 | -## 问题: | ||
105 | - | ||
106 | -1.对于多个返回结果的拆分(已解决) | ||
107 | -2.设置定频,没有起作用(待验证) | ||
108 | -3.socket连接异常处理 | ||
108 | +1. 对于多个返回结果的拆分(已解决) | ||
109 | +2. 设置定频,没有起作用(待验证) | ||
110 | +3. socket连接异常处理 |
1 | //调用 | 1 | //调用 |
2 | -var sdk = require('../dist/index'); | 2 | +var sdk = require('../index'); |
3 | 3 | ||
4 | //开始盘点 | 4 | //开始盘点 |
5 | sdk.start({ | 5 | sdk.start({ |
@@ -24,6 +24,6 @@ sdk.start({ | @@ -24,6 +24,6 @@ sdk.start({ | ||
24 | }], | 24 | }], |
25 | inventoryTime: 400, //设置盘点时长 | 25 | inventoryTime: 400, //设置盘点时长 |
26 | power: [15, 0, 0, 0] //功率设置 | 26 | power: [15, 0, 0, 0] //功率设置 |
27 | -}, function (data) { | 27 | +}, function(err, data) { |
28 | console.log(data); | 28 | console.log(data); |
29 | }); | 29 | }); |
index.js
0 → 100644
@@ -3,16 +3,17 @@ | @@ -3,16 +3,17 @@ | ||
3 | "version": "1.0.0", | 3 | "version": "1.0.0", |
4 | "description": "rfid uhf sdk", | 4 | "description": "rfid uhf sdk", |
5 | "main": "index.js", | 5 | "main": "index.js", |
6 | + "repository": "git@git.dev.yoho.cn:web/rfid-sdk.git", | ||
6 | "dependencies": { | 7 | "dependencies": { |
7 | - "async": "^0.9.0", | ||
8 | - "babel": "~5.1.9", | 8 | + "async": "0.9.0", |
9 | + "babel": "5.x.x", | ||
9 | "lodash": "3.6.x", | 10 | "lodash": "3.6.x", |
10 | "log4js": "0.6.x", | 11 | "log4js": "0.6.x", |
11 | "pm2": "~0.12.3", | 12 | "pm2": "~0.12.3", |
12 | "redis": "^0.12.1" | 13 | "redis": "^0.12.1" |
13 | }, | 14 | }, |
14 | "scripts": { | 15 | "scripts": { |
15 | - "test": "echo \"Error: no test specified\" && exit 1" | 16 | + "test": "make test" |
16 | }, | 17 | }, |
17 | "author": "", | 18 | "author": "", |
18 | "license": "ISC", | 19 | "license": "ISC", |
@@ -21,6 +22,11 @@ | @@ -21,6 +22,11 @@ | ||
21 | "gulp-babel": "^5.1.0", | 22 | "gulp-babel": "^5.1.0", |
22 | "gulp-doxx": "0.0.4", | 23 | "gulp-doxx": "0.0.4", |
23 | "gulp-jshint": "^1.10.0", | 24 | "gulp-jshint": "^1.10.0", |
24 | - "jshint-stylish": "^1.0.2" | 25 | + "jshint-stylish": "^1.0.2", |
26 | + "mocha": "*", | ||
27 | + "expect.js": "*", | ||
28 | + "muk": "*", | ||
29 | + "rewire": "*", | ||
30 | + "babel-plugin-rewire": "*" | ||
25 | } | 31 | } |
26 | -} | 32 | +} |
@@ -2,54 +2,57 @@ | @@ -2,54 +2,57 @@ | ||
2 | * @fileOverview 调用入口 | 2 | * @fileOverview 调用入口 |
3 | * @author qiqi.zhou@yoho.cn | 3 | * @author qiqi.zhou@yoho.cn |
4 | */ | 4 | */ |
5 | -//require("babel/register"); //es6 polyfill | ||
6 | 5 | ||
7 | /** | 6 | /** |
8 | * 连接器 | 7 | * 连接器 |
9 | * @type {Connector} | 8 | * @type {Connector} |
10 | */ | 9 | */ |
11 | -var Connector = require('./lib/connector'); | 10 | +import Connector from './lib/connector'; |
12 | /** | 11 | /** |
13 | * order config | 12 | * order config |
14 | * @type {Object} | 13 | * @type {Object} |
15 | */ | 14 | */ |
16 | -var orders = require('./lib/interface'); | 15 | +import orders from './lib/interface'; |
17 | /** | 16 | /** |
18 | * uhf dirctive module | 17 | * uhf dirctive module |
19 | * @type {Uhf} | 18 | * @type {Uhf} |
20 | */ | 19 | */ |
21 | -var Uhf = require('./lib/directive/uhf'); | 20 | +import Uhf from './lib/directive/uhf'; |
22 | /** | 21 | /** |
23 | * config dirctive module | 22 | * config dirctive module |
24 | * @type {[Config]} | 23 | * @type {[Config]} |
25 | */ | 24 | */ |
26 | -var Config = require('./lib/directive/config'); | 25 | +import Config from './lib/directive/config'; |
27 | /** | 26 | /** |
28 | * inventory dirctive module | 27 | * inventory dirctive module |
29 | * @type {Inventory} | 28 | * @type {Inventory} |
30 | */ | 29 | */ |
31 | -var Inventory = require('./lib/directive/inventory'); | 30 | +import Inventory from './lib/directive/inventory'; |
32 | 31 | ||
33 | -var _ = require('lodash'); | ||
34 | -var util = require('./lib/util'); | 32 | +import _ from 'lodash'; |
35 | 33 | ||
36 | -var appConfig = require('./config'); | 34 | +import { |
35 | + locateOrder | ||
36 | +} | ||
37 | +from './lib/util'; | ||
38 | + | ||
39 | +import appConfig from './config'; | ||
37 | 40 | ||
38 | /** | 41 | /** |
39 | * 读写器初始化模块实例 | 42 | * 读写器初始化模块实例 |
40 | * @type {Uhf} | 43 | * @type {Uhf} |
41 | */ | 44 | */ |
42 | -var uhf = new Uhf(); | 45 | +let uhf = new Uhf(); |
43 | /** | 46 | /** |
44 | * 配置读写器实例 | 47 | * 配置读写器实例 |
45 | * @type {Config} | 48 | * @type {Config} |
46 | */ | 49 | */ |
47 | -var config = new Config(); | 50 | +let config = new Config(); |
48 | /** | 51 | /** |
49 | * 读写器盘点实例 | 52 | * 读写器盘点实例 |
50 | * @type {Inventory} | 53 | * @type {Inventory} |
51 | */ | 54 | */ |
52 | -var inventory = new Inventory(); | 55 | +let inventory = new Inventory(); |
53 | 56 | ||
54 | /** | 57 | /** |
55 | * 打开读写器 | 58 | * 打开读写器 |
@@ -59,22 +62,25 @@ var inventory = new Inventory(); | @@ -59,22 +62,25 @@ var inventory = new Inventory(); | ||
59 | * @param {Number} port 端口 | 62 | * @param {Number} port 端口 |
60 | * @param {Function} callback 打开成功回调 | 63 | * @param {Function} callback 打开成功回调 |
61 | */ | 64 | */ |
62 | -exports.open = function (host, port, callback) { | ||
63 | - if (!host || !port) { | ||
64 | - return; | 65 | +export let open = function(host, port, callback) { |
66 | + if (!host || !port || !callback) { | ||
67 | + throw new Error("错误:ip,端口,回调必须要传"); | ||
65 | } | 68 | } |
66 | //连接一个读写器 | 69 | //连接一个读写器 |
67 | var connector = new Connector({ | 70 | var connector = new Connector({ |
68 | host: host, | 71 | host: host, |
69 | port: port | 72 | port: port |
70 | - }, function () { | 73 | + }, function() { |
71 | uhf.send(connector, orders.openUhf); | 74 | uhf.send(connector, orders.openUhf); |
72 | }, receive); | 75 | }, receive); |
73 | - | ||
74 | //当打开读写器模块 | 76 | //当打开读写器模块 |
75 | - connector.on('openUhf', function (data) { | ||
76 | - if ((data.code === 1 || data.code === 2) && callback) { | ||
77 | - callback(connector); | 77 | + connector.on('openUhf', function(data) { |
78 | + if (data.code === 1 || data.code === 2) { | ||
79 | + callback(null, connector); | ||
80 | + } else if (data) { | ||
81 | + var error = new Error(data.message); | ||
82 | + error.code = data.code; | ||
83 | + callback(error); | ||
78 | } | 84 | } |
79 | }); | 85 | }); |
80 | }; | 86 | }; |
@@ -86,7 +92,7 @@ exports.open = function (host, port, callback) { | @@ -86,7 +92,7 @@ exports.open = function (host, port, callback) { | ||
86 | * @param {Connector} connector 连接器 | 92 | * @param {Connector} connector 连接器 |
87 | * @param {Array} 传参,依次是4个天线的功率 eg: [15,15,15,0] | 93 | * @param {Array} 传参,依次是4个天线的功率 eg: [15,15,15,0] |
88 | */ | 94 | */ |
89 | -exports.setPower = function (connector, param) { | 95 | +export let setPower = function(connector, param) { |
90 | config.send(connector, orders.setPower, param); | 96 | config.send(connector, orders.setPower, param); |
91 | }; | 97 | }; |
92 | 98 | ||
@@ -97,7 +103,7 @@ exports.setPower = function (connector, param) { | @@ -97,7 +103,7 @@ exports.setPower = function (connector, param) { | ||
97 | * @param {Connector} connect 连接器 | 103 | * @param {Connector} connect 连接器 |
98 | * param {Object} 是否开启灵敏度和灵敏度值 eg:{on:'1',rssi:-60} | 104 | * param {Object} 是否开启灵敏度和灵敏度值 eg:{on:'1',rssi:-60} |
99 | */ | 105 | */ |
100 | -exports.setFilter = function (connector, param) { | 106 | +export let setFilter = function(connector, param) { |
101 | config.send(connector, orders.setFilter, param); | 107 | config.send(connector, orders.setFilter, param); |
102 | }; | 108 | }; |
103 | 109 | ||
@@ -108,7 +114,7 @@ exports.setFilter = function (connector, param) { | @@ -108,7 +114,7 @@ exports.setFilter = function (connector, param) { | ||
108 | * @param {Connector} connector 连接器 | 114 | * @param {Connector} connector 连接器 |
109 | * @param {Object} param 天线端口和定频的值 eg: {ant: 0, freq: 10 } | 115 | * @param {Object} param 天线端口和定频的值 eg: {ant: 0, freq: 10 } |
110 | */ | 116 | */ |
111 | -exports.setFrequency = function (connector, param) { | 117 | +export let setFrequency = function(connector, param) { |
112 | config.send(connector, orders.setFrequency, param); | 118 | config.send(connector, orders.setFrequency, param); |
113 | }; | 119 | }; |
114 | 120 | ||
@@ -119,18 +125,18 @@ exports.setFrequency = function (connector, param) { | @@ -119,18 +125,18 @@ exports.setFrequency = function (connector, param) { | ||
119 | * @param {Connector} connect 连接器 | 125 | * @param {Connector} connect 连接器 |
120 | * @param {Number} param 时长,单位毫秒 eg: 400 | 126 | * @param {Number} param 时长,单位毫秒 eg: 400 |
121 | */ | 127 | */ |
122 | -exports.setInventoryTime = function (connector, param) { | 128 | +export let setInventoryTime = function(connector, param) { |
123 | inventory.send(connector, orders.setInventoryTime, param); | 129 | inventory.send(connector, orders.setInventoryTime, param); |
124 | }; | 130 | }; |
125 | 131 | ||
126 | /** | 132 | /** |
127 | * 开始盘点 | 133 | * 开始盘点 |
128 | - * @method sartInventory | ||
129 | - * @exports index/sartInventory | 134 | + * @method startInventory |
135 | + * @exports index/startInventory | ||
130 | * @param {Connector} connect 连接器 | 136 | * @param {Connector} connect 连接器 |
131 | */ | 137 | */ |
132 | -exports.sartInventory = function (connector) { | ||
133 | - inventory.send(connector, orders.sartInventory); | 138 | +export let startInventory = function(connector) { |
139 | + inventory.send(connector, orders.startInventory); | ||
134 | }; | 140 | }; |
135 | 141 | ||
136 | /** | 142 | /** |
@@ -139,7 +145,7 @@ exports.sartInventory = function (connector) { | @@ -139,7 +145,7 @@ exports.sartInventory = function (connector) { | ||
139 | * @exports index/stopInventory | 145 | * @exports index/stopInventory |
140 | * @param {Connector} connector 连接器 | 146 | * @param {Connector} connector 连接器 |
141 | */ | 147 | */ |
142 | -exports.stopInventory = function (connector) { | 148 | +export let stopInventory = function(connector) { |
143 | inventory.send(connector, orders.stopInventory); | 149 | inventory.send(connector, orders.stopInventory); |
144 | }; | 150 | }; |
145 | 151 | ||
@@ -172,72 +178,49 @@ exports.stopInventory = function (connector) { | @@ -172,72 +178,49 @@ exports.stopInventory = function (connector) { | ||
172 | }], | 178 | }], |
173 | inventoryTime: 400,//设置盘点时长 | 179 | inventoryTime: 400,//设置盘点时长 |
174 | power: [15, 0, 0, 0]//功率设置 | 180 | power: [15, 0, 0, 0]//功率设置 |
175 | - }, function (data) { | 181 | + }, function (error,data) { |
176 | //console.log(data); | 182 | //console.log(data); |
177 | }); | 183 | }); |
178 | */ | 184 | */ |
179 | -var isStop = false, | ||
180 | - checkInventoryFlag, | ||
181 | - checkSpeedFlag; | ||
182 | -exports.start = function (options, callback) { | 185 | +let isStop = false; |
186 | + | ||
187 | +export let start = function(options, callback) { | ||
183 | var rets = []; | 188 | var rets = []; |
184 | var countMap = {}; | 189 | var countMap = {}; |
185 | - exports.open(options.host, options.port, function (connector) { | ||
186 | - | ||
187 | - if (options.filter) { //开启灵敏度过滤 | ||
188 | - exports.setFilter(connector, options.filter); | ||
189 | - } | ||
190 | - | ||
191 | - if (options.frequencys) { //开启固定频率 | ||
192 | - _.forIn(options.frequencys, function (val, key) { | ||
193 | - exports.setFrequency(connector, val); | ||
194 | - }); | 190 | + open(options.host, options.port, function(err, connector) { |
191 | + if (!options.filter || !options.frequencys || !options.inventoryTime || !options.power) { | ||
192 | + let error = new Error('Error:灵敏度,定频,盘点时长,功率必须要传!'); | ||
193 | + callback(error); | ||
195 | } | 194 | } |
195 | + //开启灵敏度过滤 | ||
196 | + setFilter(connector, options.filter); | ||
196 | 197 | ||
197 | - if (options.inventoryTime) { //设置盘点时长 | ||
198 | - exports.setInventoryTime(connector, options.inventoryTime); | ||
199 | - } | 198 | + //开启固定频率 |
199 | + _.forIn(options.frequencys, function(val, key) { | ||
200 | + setFrequency(connector, val); | ||
201 | + }); | ||
200 | 202 | ||
201 | - if (options.power) { //设置功率 | ||
202 | - connector.on('setInventoryTime', function (data) { | ||
203 | - exports.setPower(connector, options.power); | ||
204 | - }); | 203 | + //设置盘点时长 |
204 | + setInventoryTime(connector, options.inventoryTime); | ||
205 | 205 | ||
206 | - } | 206 | + //设置功率 |
207 | + connector.on('setInventoryTime', function(data) { | ||
208 | + setPower(connector, options.power); | ||
209 | + }); | ||
207 | 210 | ||
208 | - connector.on('setPower', function (data) { | ||
209 | - exports.sartInventory(connector); //开始盘点 | 211 | + //开始盘点 |
212 | + connector.on('setPower', function(data) { | ||
213 | + startInventory(connector); | ||
210 | }); | 214 | }); |
211 | 215 | ||
212 | - connector.on('sartInventory', function (data) { | ||
213 | - //console.log(data.code); | 216 | + connector.on('startInventory', function(data) { |
214 | if (data.code !== 2 && !isStop) { | 217 | if (data.code !== 2 && !isStop) { |
215 | - exports.sartInventory(connector); | 218 | + startInventory(connector); |
216 | } else if (data.code === 2) { | 219 | } else if (data.code === 2) { |
217 | rets = procTagInfo(countMap, rets, data.data); | 220 | rets = procTagInfo(countMap, rets, data.data); |
218 | } | 221 | } |
219 | - callback(rets); | 222 | + callback(null, rets); |
220 | }); | 223 | }); |
221 | - | ||
222 | - checkInventoryFlag = setInterval(function () { //检查是否在架 | ||
223 | - for (var i = 0; i < rets.length; i++) { | ||
224 | - var offTime = 1 * new Date() - rets[i].updateTime; | ||
225 | - if (offTime > appConfig.offInventory) { | ||
226 | - rets[i].state = 'off'; | ||
227 | - rets[i].speed = 0; | ||
228 | - } | ||
229 | - } | ||
230 | - }, appConfig.checkInventory); | ||
231 | - | ||
232 | - checkSpeedFlag = setInterval(function () { //检查读取速度(times/sec) | ||
233 | - for (var i = 0; i < rets.length; i++) { | ||
234 | - if (countMap[rets[i].epc]) { | ||
235 | - var prevCount = Number(countMap[rets[i].epc]); | ||
236 | - rets[i].speed = prevCount / (appConfig.checkCount / 1000); | ||
237 | - countMap[rets[i].epc] = 0; | ||
238 | - } | ||
239 | - } | ||
240 | - }, appConfig.checkCount); | ||
241 | }); | 224 | }); |
242 | }; | 225 | }; |
243 | 226 | ||
@@ -245,12 +228,9 @@ exports.start = function (options, callback) { | @@ -245,12 +228,9 @@ exports.start = function (options, callback) { | ||
245 | * 停止间歇盘点 | 228 | * 停止间歇盘点 |
246 | * @method stop | 229 | * @method stop |
247 | * @exports index/stop | 230 | * @exports index/stop |
248 | - * @param {Connector} connect 连接器 | ||
249 | */ | 231 | */ |
250 | -exports.stop = function (connector) { | 232 | +export let stop = function() { |
251 | isStop = true; | 233 | isStop = true; |
252 | - clearInterval(checkInventoryFlag); | ||
253 | - clearInterval(checkSpeedFlag); | ||
254 | }; | 234 | }; |
255 | 235 | ||
256 | /** | 236 | /** |
@@ -261,7 +241,7 @@ exports.stop = function (connector) { | @@ -261,7 +241,7 @@ exports.stop = function (connector) { | ||
261 | * @param {Object} data 读取一次的内容 | 241 | * @param {Object} data 读取一次的内容 |
262 | */ | 242 | */ |
263 | function procTagInfo(countMap, rets, data) { | 243 | function procTagInfo(countMap, rets, data) { |
264 | - var isExist = false, | 244 | + let isExist = false, |
265 | now = 1 * new Date(); | 245 | now = 1 * new Date(); |
266 | for (var i = 0; i < rets.length; i++) { | 246 | for (var i = 0; i < rets.length; i++) { |
267 | if (rets[i].epc === data.epc) { | 247 | if (rets[i].epc === data.epc) { |
@@ -291,26 +271,26 @@ function procTagInfo(countMap, rets, data) { | @@ -291,26 +271,26 @@ function procTagInfo(countMap, rets, data) { | ||
291 | * @param {Object} data 接收到的数据 | 271 | * @param {Object} data 接收到的数据 |
292 | */ | 272 | */ |
293 | function receive(client, data, err) { | 273 | function receive(client, data, err) { |
294 | - var className = null, | 274 | + let className = null, |
295 | ret = null; | 275 | ret = null; |
296 | if (!data) { | 276 | if (!data) { |
297 | - return; | 277 | + return null; |
298 | } | 278 | } |
299 | if (err) { | 279 | if (err) { |
300 | - console.log(err); | 280 | + throw err; |
301 | } | 281 | } |
302 | - className = util.locateOrder(orders, data).val.directive; | 282 | + className = locateOrder(orders, data).val.directive; |
303 | 283 | ||
304 | switch (className) { | 284 | switch (className) { |
305 | - case 'uhf': | ||
306 | - ret = uhf.receive(client, data); | ||
307 | - break; | ||
308 | - case 'config': | ||
309 | - ret = config.receive(client, data); | ||
310 | - break; | ||
311 | - case 'inventory': | ||
312 | - ret = inventory.receive(client, data); | ||
313 | - break; | 285 | + case 'uhf': |
286 | + ret = uhf.receive(client, data); | ||
287 | + break; | ||
288 | + case 'config': | ||
289 | + ret = config.receive(client, data); | ||
290 | + break; | ||
291 | + case 'inventory': | ||
292 | + ret = inventory.receive(client, data); | ||
293 | + break; | ||
314 | } | 294 | } |
315 | return ret; | 295 | return ret; |
316 | } | 296 | } |
@@ -36,7 +36,7 @@ class Connector extends events.EventEmitter { | @@ -36,7 +36,7 @@ class Connector extends events.EventEmitter { | ||
36 | let that = this; | 36 | let that = this; |
37 | 37 | ||
38 | //初始化 | 38 | //初始化 |
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(); |
@@ -44,7 +44,7 @@ class Connector extends events.EventEmitter { | @@ -44,7 +44,7 @@ class Connector extends events.EventEmitter { | ||
44 | 44 | ||
45 | 45 | ||
46 | //接收数据 | 46 | //接收数据 |
47 | - client.on('data', function (data) { | 47 | + client.on('data', function(data) { |
48 | 48 | ||
49 | let arr = toHex(data), | 49 | let arr = toHex(data), |
50 | rets = spliteRet(that.orderMq, arr), | 50 | rets = spliteRet(that.orderMq, arr), |
@@ -61,7 +61,7 @@ class Connector extends events.EventEmitter { | @@ -61,7 +61,7 @@ class Connector extends events.EventEmitter { | ||
61 | }); | 61 | }); |
62 | 62 | ||
63 | //异常处理 | 63 | //异常处理 |
64 | - client.on('error', function (error) { | 64 | + client.on('error', function(error) { |
65 | log.error('错误:' + error.code); | 65 | log.error('错误:' + error.code); |
66 | 66 | ||
67 | // 读写器连接错误或者突然断开 | 67 | // 读写器连接错误或者突然断开 |
@@ -71,7 +71,7 @@ class Connector extends events.EventEmitter { | @@ -71,7 +71,7 @@ class Connector extends events.EventEmitter { | ||
71 | receive(client, null, error); | 71 | receive(client, null, error); |
72 | }); | 72 | }); |
73 | 73 | ||
74 | - client.on('close', function () { | 74 | + client.on('close', function() { |
75 | log.info('读写器连接断开'); | 75 | log.info('读写器连接断开'); |
76 | // 触发重连事件 | 76 | // 触发重连事件 |
77 | that.emit('reconnect'); | 77 | that.emit('reconnect'); |
@@ -22,7 +22,7 @@ class Inventory extends Base { | @@ -22,7 +22,7 @@ class Inventory extends Base { | ||
22 | let setInventoryTime = info.setInventoryTime.input; | 22 | let setInventoryTime = info.setInventoryTime.input; |
23 | this.reproc[setInventoryTime] = this.preSetInventoryTime; | 23 | this.reproc[setInventoryTime] = this.preSetInventoryTime; |
24 | this.proc = { | 24 | this.proc = { |
25 | - sartInventory: this.start | 25 | + startInventory: this.start |
26 | }; | 26 | }; |
27 | } | 27 | } |
28 | 28 | ||
@@ -72,7 +72,7 @@ class Inventory extends Base { | @@ -72,7 +72,7 @@ class Inventory extends Base { | ||
72 | retKey = data; | 72 | retKey = data; |
73 | } | 73 | } |
74 | ip = client.remoteAddress; | 74 | ip = client.remoteAddress; |
75 | - output = info.sartInventory.output; | 75 | + output = info.startInventory.output; |
76 | returnVal = returnProc(retKey, ip, output); | 76 | returnVal = returnProc(retKey, ip, output); |
77 | 77 | ||
78 | returnVal.data = retData; | 78 | returnVal.data = retData; |
@@ -104,7 +104,7 @@ export default { | @@ -104,7 +104,7 @@ export default { | ||
104 | 'b44ff0': commonMessage.ff0 | 104 | 'b44ff0': commonMessage.ff0 |
105 | } | 105 | } |
106 | }, | 106 | }, |
107 | - sartInventory: { | 107 | + startInventory: { |
108 | directive: 'inventory', | 108 | directive: 'inventory', |
109 | input: 'F100000000', | 109 | input: 'F100000000', |
110 | output: { | 110 | output: { |
test/lib/index.test.js
0 → 100644
1 | +import lib from '../../src'; | ||
2 | +import expect from 'expect.js'; | ||
3 | +import muk from 'muk'; | ||
4 | +import rewire from 'rewire'; | ||
5 | +import { | ||
6 | + ret, orderName, setRet, setOrderName, uhf as FakeUhf | ||
7 | +} | ||
8 | +from '../mock/fakeUhf'; | ||
9 | +import FakeConnector from '../mock/fakeConnector'; | ||
10 | +import FakeConfig from '../mock/fakeConfig'; | ||
11 | + | ||
12 | +describe('index.js', function() { | ||
13 | + let index; | ||
14 | + before(function() { | ||
15 | + index = muk('../../src', { | ||
16 | + './lib/connector': FakeConnector, | ||
17 | + './lib/directive/uhf': FakeUhf | ||
18 | + }); | ||
19 | + }); | ||
20 | + describe('open UHF', function() { | ||
21 | + it('open expect return', function() { | ||
22 | + expect(function() { | ||
23 | + lib.open(null, null, null); | ||
24 | + }).to.throwException('错误:ip,端口,回调必须要传'); | ||
25 | + expect(function() { | ||
26 | + lib.open('1.1.1.1', null, null); | ||
27 | + }).to.throwException('错误:ip,端口,回调必须要传'); | ||
28 | + expect(function() { | ||
29 | + lib.open('1.1.1.1', '123', null); | ||
30 | + }).to.throwException('错误:ip,端口,回调必须要传'); | ||
31 | + }); | ||
32 | + | ||
33 | + | ||
34 | + it('open expect success', function(done) { | ||
35 | + index.open('1.1.1.1', '123', function(err, data) { | ||
36 | + expect(data).to.be.a(FakeConnector); | ||
37 | + done(); | ||
38 | + }); | ||
39 | + }); | ||
40 | + | ||
41 | + it('open expect error', function(done) { | ||
42 | + let val = { | ||
43 | + code: 0, | ||
44 | + message: 'fake error' | ||
45 | + }; | ||
46 | + setRet(val); | ||
47 | + index.open('1.1.1.1', '123', function(err, data) { | ||
48 | + expect(err.message).to.be('fake error'); | ||
49 | + done(); | ||
50 | + }); | ||
51 | + | ||
52 | + }); | ||
53 | + }); | ||
54 | + | ||
55 | + describe('start inventory', function() { | ||
56 | + it('start expect error', function() { | ||
57 | + expect(function() { | ||
58 | + index.start(); | ||
59 | + }).to.throwException('错误:ip,端口,回调必须要传'); | ||
60 | + | ||
61 | + expect(function() { | ||
62 | + index.start({ | ||
63 | + 'port': '123' | ||
64 | + }); | ||
65 | + }).to.throwException('错误:ip,端口,回调必须要传'); | ||
66 | + | ||
67 | + }); | ||
68 | + | ||
69 | + it('start expect asyn error', function(done) { | ||
70 | + index.start({ | ||
71 | + 'port': '123', | ||
72 | + 'host': '1.1.1.1' | ||
73 | + }, function(err, data) { | ||
74 | + expect(err.message).to.be('Error:灵敏度,定频,盘点时长,功率必须要传!') | ||
75 | + done(); | ||
76 | + }); | ||
77 | + }); | ||
78 | + | ||
79 | + | ||
80 | + it('start epxect ok', function(done) { | ||
81 | + let val = { | ||
82 | + code: 1 | ||
83 | + }; | ||
84 | + | ||
85 | + | ||
86 | + setRet(val); | ||
87 | + | ||
88 | + let index = rewire("../../src") | ||
89 | + | ||
90 | + index.__set__('setFilter', function() {}); | ||
91 | + index.__set__('setFrequency', function() {}); | ||
92 | + index.__set__('setInventoryTime', function(connector, param) { | ||
93 | + process.nextTick(function() { | ||
94 | + connector.done('setInventoryTime', ret); | ||
95 | + }); | ||
96 | + }); | ||
97 | + | ||
98 | + index.__set__('setPower', function(connector, param) { | ||
99 | + process.nextTick(function() { | ||
100 | + connector.done('setPower', ret) | ||
101 | + }); | ||
102 | + }); | ||
103 | + | ||
104 | + index.__set__('startInventory', function(connector, param) { | ||
105 | + process.nextTick(function() { | ||
106 | + connector.done('startInventory', { | ||
107 | + code: 2, | ||
108 | + data: { | ||
109 | + epc: '123' | ||
110 | + } | ||
111 | + }) | ||
112 | + }); | ||
113 | + }); | ||
114 | + | ||
115 | + index.__set__('Connector', FakeConnector); | ||
116 | + index.__set__('uhf', new FakeUhf()); | ||
117 | + | ||
118 | + index.start({ | ||
119 | + 'port': '123', | ||
120 | + 'host': '1.1.1.1', | ||
121 | + filter: { //设置灵敏度的过滤 | ||
122 | + 'on': '1', | ||
123 | + rssi: -60 | ||
124 | + }, | ||
125 | + frequencys: [{ //天线的定频 | ||
126 | + ant: 0, | ||
127 | + freq: 10 | ||
128 | + }, { | ||
129 | + ant: 1, | ||
130 | + freq: 10 | ||
131 | + }, { | ||
132 | + ant: 2, | ||
133 | + freq: 10 | ||
134 | + }, { | ||
135 | + ant: 3, | ||
136 | + freq: 10 | ||
137 | + }], | ||
138 | + inventoryTime: 400, //设置盘点时长 | ||
139 | + power: [15, 0, 0, 0] //功率设置 | ||
140 | + }, | ||
141 | + function(err, data) { | ||
142 | + expect(data).to.have.length(1); | ||
143 | + done(); | ||
144 | + }) | ||
145 | + | ||
146 | + }); | ||
147 | + }); | ||
148 | + | ||
149 | + describe('stop inventory', function() { | ||
150 | + it('stop ok', function() { | ||
151 | + expect(index.stop).to.be.ok(); | ||
152 | + }); | ||
153 | + }); | ||
154 | + | ||
155 | + describe('procTagInfo', function() { | ||
156 | + it('procTagInfo exit proc', function() { | ||
157 | + let proc = rewire("../../src").__get__('procTagInfo'); | ||
158 | + let countMap = { | ||
159 | + '1': 1 | ||
160 | + }; | ||
161 | + let rets = [{ | ||
162 | + epc: '1', | ||
163 | + rssi: '2', | ||
164 | + updateTime: 1 * new Date(), | ||
165 | + state: 'on', | ||
166 | + ant: 1 | ||
167 | + }]; | ||
168 | + let data = { | ||
169 | + epc: '1', | ||
170 | + rssi: '3', | ||
171 | + ant: 2 | ||
172 | + } | ||
173 | + rets = proc(countMap, rets, data); | ||
174 | + expect(rets[0].ant).to.be(2); | ||
175 | + expect(rets[0].rssi).to.be('3'); | ||
176 | + expect(rets[0].epc).to.be('1'); | ||
177 | + expect(countMap['1']).to.be(2); | ||
178 | + expect(rets).to.have.length(1); | ||
179 | + }); | ||
180 | + | ||
181 | + it('procTagInfo noExist proc', function() { | ||
182 | + let proc = rewire("../../src").__get__('procTagInfo'); | ||
183 | + let rets = proc({}, [], { | ||
184 | + epc: '1', | ||
185 | + rssi: '3', | ||
186 | + ant: 2 | ||
187 | + }); | ||
188 | + | ||
189 | + expect(rets[0].state).to.be('on'); | ||
190 | + expect(rets[0].speed).to.be(0); | ||
191 | + expect(rets).to.have.length(1); | ||
192 | + }); | ||
193 | + | ||
194 | + }); | ||
195 | + describe('receive', function() { | ||
196 | + it('receive expect uhf and config and inventory ret', function() { | ||
197 | + let indexM = rewire("../../src"); | ||
198 | + let expectClassNames = ['uhf', 'config', 'inventory']; | ||
199 | + | ||
200 | + for (let i = 0; i < expectClassNames.length; i++) { | ||
201 | + indexM.__set__('locateOrder', function() { | ||
202 | + return { | ||
203 | + val: { | ||
204 | + directive: expectClassNames[i] | ||
205 | + } | ||
206 | + } | ||
207 | + }); | ||
208 | + indexM.__set__(expectClassNames[i], { | ||
209 | + receive: function() { | ||
210 | + return expectClassNames[i]; | ||
211 | + } | ||
212 | + }); | ||
213 | + let receive = indexM.__get__('receive'); | ||
214 | + | ||
215 | + let retData = receive(true, {}); | ||
216 | + | ||
217 | + expect(retData).to.be(expectClassNames[i]); | ||
218 | + } | ||
219 | + }); | ||
220 | + it('receive param data null expect null', function() { | ||
221 | + let indexM = rewire("../../src"); | ||
222 | + let retData = indexM.__get__('receive')(true, null); | ||
223 | + expect(retData).to.be(null); | ||
224 | + }); | ||
225 | + | ||
226 | + it('receive param err expect error', function() { | ||
227 | + let indexM = rewire("../../src"); | ||
228 | + expect(function() { | ||
229 | + indexM.__get__('receive')(true, {}, new Error('mock error')); | ||
230 | + }).to.throwException(/mock error/); | ||
231 | + }); | ||
232 | + }); | ||
233 | + after(function() { | ||
234 | + muk.restore(); | ||
235 | + }); | ||
236 | +}); |
test/mock/fakeConfig.js
0 → 100644
1 | +/** | ||
2 | + * @fileOverview 配置 | ||
3 | + * @module config | ||
4 | + * @property {string} log 日志的记录路径 | ||
5 | + * @property {string} logLevel 日志级别 | ||
6 | + * @property {number} checkInventory 检查盘点标签的状态 | ||
7 | + * @property {number} offInventory 离线标签临界点 | ||
8 | + * @property {number} checkCount 读取数检查 | ||
9 | + */ | ||
10 | +export default { | ||
11 | + log: './log', | ||
12 | + logLevel: 'ERROR', | ||
13 | + checkInventory: 200, | ||
14 | + offInventory: 500, | ||
15 | + checkCount: 1000 | ||
16 | +}; |
test/mock/fakeConnector.js
0 → 100644
1 | +import events from 'events'; | ||
2 | + | ||
3 | +/** | ||
4 | + * @class 假的连接器(用于单元测试) | ||
5 | + * @extends {EventEmitter} | ||
6 | + * @module Connector | ||
7 | + * @member {Socket} client | ||
8 | + * @member {Array} orderMq | ||
9 | + * @description 连接到读写器端 | ||
10 | + */ | ||
11 | +class FakeConnector extends events.EventEmitter { | ||
12 | + constructor(config, send, receive) { | ||
13 | + super(); | ||
14 | + this.connected = false; | ||
15 | + this.client = {}; | ||
16 | + let that = this; | ||
17 | + | ||
18 | + //初始化 | ||
19 | + process.nextTick(function() { | ||
20 | + that.connected = true; | ||
21 | + send(); | ||
22 | + }); | ||
23 | + } | ||
24 | + send() { | ||
25 | + | ||
26 | + } | ||
27 | + done(order, ret) { | ||
28 | + this.emit(order, ret); | ||
29 | + }; | ||
30 | +} | ||
31 | + | ||
32 | +/** | ||
33 | + * @exports mock/FakeConnector | ||
34 | + */ | ||
35 | +export default FakeConnector; |
test/mock/fakeUhf.js
0 → 100644
1 | +/** | ||
2 | + * 预期结果 | ||
3 | + * @type {Object} | ||
4 | + */ | ||
5 | +export let ret = { | ||
6 | + code: 1 | ||
7 | +}; | ||
8 | +/** | ||
9 | + * 发起的指令名称 | ||
10 | + * @type {String} | ||
11 | + */ | ||
12 | +export let orderName = 'openUhf'; | ||
13 | + | ||
14 | +export let setRet = function(val) { | ||
15 | + ret = val; | ||
16 | +} | ||
17 | + | ||
18 | +export let setOrderName = function(val) { | ||
19 | + orderName = val; | ||
20 | +} | ||
21 | + | ||
22 | +/** | ||
23 | + * @class FakeUHF 假的读写器的指令操作上 | ||
24 | + * @extends {Base} | ||
25 | + * @member @see {@link Base} | ||
26 | + */ | ||
27 | +export default class FakeUhf { | ||
28 | + constructor() {} | ||
29 | + send(connector, order) { | ||
30 | + process.nextTick(function() { | ||
31 | + connector.done(orderName, ret); | ||
32 | + }); | ||
33 | + } | ||
34 | +} | ||
35 | + | ||
36 | +export let uhf = FakeUhf; |
test/mock/fakeUtil.js
0 → 100644
1 | +/** | ||
2 | + * @fileOverview 假的工具方法库 | ||
3 | + * @module lib/util | ||
4 | + * @author qiqi.zhou@yoho.cn | ||
5 | + */ | ||
6 | + | ||
7 | + | ||
8 | +/** | ||
9 | + * @mixes 日志记录对象 | ||
10 | + * @exports util/log | ||
11 | + */ | ||
12 | +export let log = { | ||
13 | + info: function() {}, | ||
14 | + error: function() {} | ||
15 | +}; | ||
16 | + | ||
17 | +/** | ||
18 | + * @method 无符号转16进制 | ||
19 | + * @exports util/tenToHex | ||
20 | + * @param {Number} number 十进制数 | ||
21 | + * @return {Number} 16进制数字 | ||
22 | + */ | ||
23 | +export let tenToHex = function(number) {}; | ||
24 | + | ||
25 | +/** | ||
26 | + * @method 无符号16进制转10进制 | ||
27 | + * @exports util/hexToTen | ||
28 | + * @param {Number} number 十六机制数 | ||
29 | + * @param {Boolean} minus true表示负数,false表示正数 | ||
30 | + * @return {Number} 十进制数字 | ||
31 | + */ | ||
32 | +export let hexToTen = function(number, minus) { | ||
33 | + | ||
34 | +}; | ||
35 | + | ||
36 | +/** | ||
37 | + * @method 转16进制 | ||
38 | + * @exports util/toHex | ||
39 | + * @param {Buff} data 返回数据流 | ||
40 | + * @return {Array} 转成16进制的数组 | ||
41 | + */ | ||
42 | +export let toHex = function(data) { | ||
43 | + | ||
44 | +}; | ||
45 | + | ||
46 | + | ||
47 | +/** | ||
48 | + * @method 处理返回码 | ||
49 | + * @exports util/returnProc | ||
50 | + * @param {Array} data 指令结果 | ||
51 | + * @param {Array} ip 读写器IP | ||
52 | + * @param {Object} output 标准输出的规格对象 | ||
53 | + * @return {object} 标准输出 | ||
54 | + */ | ||
55 | +export let returnProc = function(data, ip, output) { | ||
56 | + | ||
57 | +}; | ||
58 | + | ||
59 | +/** | ||
60 | + * @method 返回结果拆分 | ||
61 | + * @exports util/spliteRet | ||
62 | + * @param {Array} mq 发起指令的数组集合 | ||
63 | + * @param {String} data 得到的指令数据结果集 | ||
64 | + * @return {Object} 返回拆分结果和拆分依据的指令头 | ||
65 | + */ | ||
66 | +export let spliteRet = function(mq, data) { | ||
67 | + | ||
68 | +}; | ||
69 | + | ||
70 | +/** | ||
71 | + * @method 定位指令名称 | ||
72 | + * @exports util/spliteRet | ||
73 | + * @param {Array} orders 指令规格数组 | ||
74 | + * @param {Array} data 返回的结果集 eg. ['f0','2','0','0'] | ||
75 | + * @return {Object} 返回定位到的指令 | ||
76 | + */ | ||
77 | +export let locateOrder = function(orders, data) { | ||
78 | + | ||
79 | +}; |
-
Please register or login to post a comment