Authored by 周奇琪

project init

node_modules/
log
... ...
读写器的SDK
------------
\ No newline at end of file
... ...
/**
* 配置
*/
export default {
log: './log',
logLevel: 'INFO'
};
\ No newline at end of file
... ...
/**
* 调用入口
*/
require("babel/register");
var con = require('./lib/connector');
var orders = require('./lib/interface');
var Uhf = require('./lib/directive/uhf');
var uhf = new Uhf();
//连接一个读写器
var test = new con({
host: '172.16.13.6',
port: '9761'
}, function (client) {
uhf.send(client, orders.openUhf);
setTimeout(function () {
uhf.send(client, orders.getVersion);
}, 2000);
}, function (client, data, err) {
uhf.receive(client, data);
if (err) {
console.log(err);
}
});
\ No newline at end of file
... ...
/**
* 连接器
*/
import net from 'net';
import orders from './interface';
import util from './util';
var log = util.log;
//连接到服务端
export default function (config, send, receive) {
var client = new net.Socket();
//初始化
client.connect(config.port, config.host, function () {
log.info(config.host + ':' + config.port + ':读写器连接成功!');
send(client);
});
//接收数据
client.on('data', function (data) {
var arr = [];
arr = util.toHex(data);
receive(client, arr);
});
//异常处理
client.on('error', function (error) {
log.error('错误:' + error.code);
client.destroy();
receive(client, null, error);
});
client.on('close', function () {
log.info('读写器连接断开');
});
};
\ No newline at end of file
... ...
/**
* 读写器的指令操作基类
*/
import orders from '../interface';
import util from '../util';
import _ from 'lodash';
var log = util.log;
class Base {
constructor() {
this.reproc = null;
this.proc = {};
}
/**
* 发送读写器模块指令
*/
send(client, order) {
if (this.reproc) {
order.input = this.reproc[order.input]();
}
let call = new Buffer(order.input, 'hex');
client.write(call);
let address = client.remoteAddress;
log.info('读写器(' + address + ')发送指令:' + order.input);
}
/**
* 接收读写器模块
*/
receive(client, data) {
let proc = this.proc,
ret = null;
if (!_.isArray(data) || _.size(data) < 1) {
return;
}
log.info('读写器(' + client.remoteAddress + ')接收指令:' + data.join(' '));
//读取命令配置
_.forIn(orders, function (val, key) {
let order = val.input.toLowerCase();
let prefix = data[0].toLowerCase();
if (_.startsWith(order, prefix) && proc[key]) {
ret = proc[key](data, client, key);
}
});
return ret;
}
/**
* 处理返回结果
*/
procFunc(data, client, method) {
return util.returnProc(data, client.remoteAddress, info[method].output);
}
}
export default Base;
\ No newline at end of file
... ...
/**
* 配置读写器信息
* 频率,功率,灵敏度过滤
*/
import Base from './base';
import _ from 'lodash';
import util from '../util';
import info from '../interface';
let log = util.log;
class Config extends Base {
constructor() {
super(null);
let power = info.setPower.input,
filter = info.setFilter.input,
frequency = info.setFrequency.input;
this.reproc = {//处理输入
power: this.prePower,
filter: this.preFilter,
frequency: this.preFrequency
};
this.proc = {//处理结果
setPower: this.procFunc,
setFrequency: this.procFunc,
setFilter: this.procFunc
};
}
/**
* 设置功率输入预处理
*/
prePower() {
}
/**
* 设置灵敏度过滤
*/
preFilter() {
}
/**
* 设置频率
*/
setFrequency() {
}
}
export default Uhf;*/
\ No newline at end of file
... ...
/**
* 管理盘点
*/
/**
* 开始盘点
*/
function start(client, callback) {
}
/**
* 结束盘点
*/
function stop(client, callback) {
}
/**
* 设置盘点时长
*/
function setTime(client, callback) {
}
\ No newline at end of file
... ...
/**
* UHF 读写器的指令操作上
*/
import Base from './base';
import _ from 'lodash';
import util from '../util';
import info from '../interface';
let log = util.log;
class Uhf extends Base {
constructor() {
super(null);
this.proc = {
openUhf: this.procFunc,
closeUhf: this.procFunc,
getConfig: this.getConfig,
getVersion: this.getVersion
};
}
/**
* 获取读写器信息返回处理
*/
getConfig(data, client) {
let region = ['FCC', 'CCC', 'NCC', 'JPN'],
power = [],
retData = {},
retKey = null,
returnVal = null;
for (let val of _.slice(data, 2, 4)) {
power.push(parseInt(val, 16));
}
//处理返回结果
if (data[1] === '16') {
retData = {
power: power,
region: region[Number(data[6])],
speedMode: Number(data[7]),
invTime: parseInt(_.slice(data, 8, 12).join(''), 16),
invCount: data[12]
}
retKey = _.slice(data, 0, 2);
} else {
retKey = data;
}
returnVal = util.returnProc(retKey, client.remoteAddress, info.getConfig.output);
returnVal.data = retData;
return returnVal;
}
/**
*获取版本返回处理
*/
getVersion(data, client) {
let retData = null;
let retKey = _.slice(data, 0, 4);
let returnVal = util.returnProc(retKey, client.remoteAddress, info.getVersion.output);
if (data[2] === '0' && data[3] === '0') {
returnVal.data = {
majorVersion: parseInt(data[4], 16),
minorVersion: parseInt(data[5], 16)
};
}
return returnVal;
}
}
export default Uhf;
\ No newline at end of file
... ...
/**
* 通用返回信息
*/
var commonMessage = {
'00': {
code: 1,
message: '操作成功'
},
'ffff': {
code: 0,
message: '没有响应'
},
'fffe': {
code: 3,
message: '读写器正忙'
},
'ff0': {
code: -1,
message: '操作失败'
}
}
/**
* 指令集枚举
*/
export default {
openUhf: {
input: 'A000000000',
output: {
'a0200': commonMessage['00'],
'a02ff0': commonMessage['ff0'],
'a02f00': {
code: 2,
message: 'UHF模块已打开'
}
}
},
closeUhf: {
input: 'A100000000',
output: {
'a1200': commonMessage['00'],
'a12ff0': commonMessage['ff0']
}
},
setPower: {
input: 'F3',
output: {
'f3200': commonMessage['00'],
'f32ffff': commonMessage['ffff'],
'f32fffe': commonMessage['fffe'],
'f32ff0': commonMessage['ff0']
}
},
setFilter: {
input: 'B7',
output: {
'b7200': commonMessage['00'],
'b72ffff': commonMessage['ffff'],
'b72fffe': commonMessage['fffe'],
'b72ff0': commonMessage['ff0']
}
},
setFrequency: {
input: 'BF'
output: {
'bf200': commonMessage['00'],
'bf2ffff': commonMessage['ffff'],
'bf2fffe': commonMessage['fffe'],
'bf2ff0': commonMessage['ff0']
}
},
getConfig: {
input: 'B300000000',
output: {
'b3200': commonMessage['00'],
'b32ffff': commonMessage['ffff'],
'b32fffe': commonMessage['fffe'],
'b316': {
code: 1,
message: '返回信息'
}
}
},
getVersion: {
input: 'B400000000',
output: {
'b4400': commonMessage['00'],
'b44ffff': commonMessage['ffff'],
'b44fffe': commonMessage['fffe'],
'b44ff0': commonMessage['ff0']
}
},
sartInventory: {
input: 'F100000000'
},
stopInventory: {
input: 'F200000000'
},
read: {
input: 'F700000000'
},
write: {
input: 'F2'
},
};
\ No newline at end of file
... ...
/**
* 工具方法库
*/
import config from '../config';
import log4js from 'log4js';
import _ from 'lodash';
log4js.loadAppender('file');
log4js.addAppender(log4js.appenders.file(config.log), 'log');
let log = log4js.getLogger('log');
log.setLevel(config.logLevel); //配置日志
/**
* 日志
*/
exports.log = log;
/**
* 转16进制
*/
exports.toHex = function(data)
{
let arr = [];
for(let i=0;i<data.length;i++)
{
arr[i] = data.readUIntLE(i).toString(16);
}
return arr;
};
/**
* 处理返回码
*/
exports.returnProc = function(data,ip,output)
{
if(!_.isArray(data) || _.size(data)<1)
{
return false;
}
let ret = data.join('');
if(!output[ret])
{
return false;
}
log.info("读写器(" + ip + ")返回:" + output[ret].message+" 返回码:"+ret);
output[ret].hex = data;
return output[ret];
}
... ...
{
"name": "rfid-sdk",
"version": "1.0.0",
"description": "rfid uhf sdk",
"main": "index.js",
"dependencies": {
"async": "0.2.x",
"log4js": "0.6.x",
"lodash":"3.6.x",
"pm2":"~0.12.3"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
... ...