Authored by 周奇琪

project init

  1 +node_modules/
  2 +log
  1 +读写器的SDK
  2 +------------
  1 +/**
  2 + * 配置
  3 + */
  4 +export default {
  5 + log: './log',
  6 + logLevel: 'INFO'
  7 +};
  1 +/**
  2 + * 调用入口
  3 + */
  4 +require("babel/register");
  5 +var con = require('./lib/connector');
  6 +var orders = require('./lib/interface');
  7 +var Uhf = require('./lib/directive/uhf');
  8 +var uhf = new Uhf();
  9 +
  10 +//连接一个读写器
  11 +var test = new con({
  12 + host: '172.16.13.6',
  13 + port: '9761'
  14 +}, function (client) {
  15 + uhf.send(client, orders.openUhf);
  16 + setTimeout(function () {
  17 + uhf.send(client, orders.getVersion);
  18 + }, 2000);
  19 +}, function (client, data, err) {
  20 + uhf.receive(client, data);
  21 + if (err) {
  22 + console.log(err);
  23 + }
  24 +});
  1 +/**
  2 + * 连接器
  3 + */
  4 +import net from 'net';
  5 +import orders from './interface';
  6 +import util from './util';
  7 +var log = util.log;
  8 +
  9 +//连接到服务端
  10 +export default function (config, send, receive) {
  11 + var client = new net.Socket();
  12 +
  13 + //初始化
  14 + client.connect(config.port, config.host, function () {
  15 + log.info(config.host + ':' + config.port + ':读写器连接成功!');
  16 + send(client);
  17 + });
  18 +
  19 + //接收数据
  20 + client.on('data', function (data) {
  21 + var arr = [];
  22 + arr = util.toHex(data);
  23 + receive(client, arr);
  24 + });
  25 +
  26 + //异常处理
  27 + client.on('error', function (error) {
  28 + log.error('错误:' + error.code);
  29 + client.destroy();
  30 + receive(client, null, error);
  31 + });
  32 +
  33 + client.on('close', function () {
  34 + log.info('读写器连接断开');
  35 + });
  36 +};
  1 +/**
  2 + * 读写器的指令操作基类
  3 + */
  4 +import orders from '../interface';
  5 +import util from '../util';
  6 +import _ from 'lodash';
  7 +
  8 +var log = util.log;
  9 +
  10 +class Base {
  11 + constructor() {
  12 + this.reproc = null;
  13 + this.proc = {};
  14 + }
  15 +
  16 + /**
  17 + * 发送读写器模块指令
  18 + */
  19 + send(client, order) {
  20 + if (this.reproc) {
  21 + order.input = this.reproc[order.input]();
  22 + }
  23 + let call = new Buffer(order.input, 'hex');
  24 + client.write(call);
  25 + let address = client.remoteAddress;
  26 + log.info('读写器(' + address + ')发送指令:' + order.input);
  27 + }
  28 +
  29 + /**
  30 + * 接收读写器模块
  31 + */
  32 + receive(client, data) {
  33 + let proc = this.proc,
  34 + ret = null;
  35 +
  36 + if (!_.isArray(data) || _.size(data) < 1) {
  37 + return;
  38 + }
  39 +
  40 + log.info('读写器(' + client.remoteAddress + ')接收指令:' + data.join(' '));
  41 +
  42 +
  43 + //读取命令配置
  44 + _.forIn(orders, function (val, key) {
  45 + let order = val.input.toLowerCase();
  46 + let prefix = data[0].toLowerCase();
  47 + if (_.startsWith(order, prefix) && proc[key]) {
  48 + ret = proc[key](data, client, key);
  49 + }
  50 + });
  51 +
  52 + return ret;
  53 + }
  54 + /**
  55 + * 处理返回结果
  56 + */
  57 + procFunc(data, client, method) {
  58 + return util.returnProc(data, client.remoteAddress, info[method].output);
  59 + }
  60 +
  61 +}
  62 +export default Base;
  1 +/**
  2 + * 配置读写器信息
  3 + * 频率,功率,灵敏度过滤
  4 + */
  5 +import Base from './base';
  6 +import _ from 'lodash';
  7 +import util from '../util';
  8 +import info from '../interface';
  9 +
  10 +let log = util.log;
  11 +
  12 +class Config extends Base {
  13 + constructor() {
  14 + super(null);
  15 +
  16 + let power = info.setPower.input,
  17 + filter = info.setFilter.input,
  18 + frequency = info.setFrequency.input;
  19 +
  20 + this.reproc = {//处理输入
  21 + power: this.prePower,
  22 + filter: this.preFilter,
  23 + frequency: this.preFrequency
  24 + };
  25 +
  26 + this.proc = {//处理结果
  27 + setPower: this.procFunc,
  28 + setFrequency: this.procFunc,
  29 + setFilter: this.procFunc
  30 + };
  31 + }
  32 +
  33 + /**
  34 + * 设置功率输入预处理
  35 + */
  36 + prePower() {
  37 +
  38 + }
  39 +
  40 + /**
  41 + * 设置灵敏度过滤
  42 + */
  43 + preFilter() {
  44 +
  45 + }
  46 +
  47 + /**
  48 + * 设置频率
  49 + */
  50 + setFrequency() {
  51 +
  52 + }
  53 +}
  54 +export default Uhf;*/
  1 +/**
  2 + * 管理盘点
  3 + */
  4 +
  5 +/**
  6 + * 开始盘点
  7 + */
  8 +function start(client, callback) {
  9 +
  10 +}
  11 +
  12 +/**
  13 + * 结束盘点
  14 + */
  15 +function stop(client, callback) {
  16 +
  17 +}
  18 +
  19 +/**
  20 + * 设置盘点时长
  21 + */
  22 +function setTime(client, callback) {
  23 +
  24 +}
  1 +/**
  2 + * UHF 读写器的指令操作上
  3 + */
  4 +import Base from './base';
  5 +import _ from 'lodash';
  6 +import util from '../util';
  7 +import info from '../interface';
  8 +
  9 +let log = util.log;
  10 +
  11 +class Uhf extends Base {
  12 + constructor() {
  13 + super(null);
  14 + this.proc = {
  15 + openUhf: this.procFunc,
  16 + closeUhf: this.procFunc,
  17 + getConfig: this.getConfig,
  18 + getVersion: this.getVersion
  19 + };
  20 + }
  21 +
  22 + /**
  23 + * 获取读写器信息返回处理
  24 + */
  25 + getConfig(data, client) {
  26 + let region = ['FCC', 'CCC', 'NCC', 'JPN'],
  27 + power = [],
  28 + retData = {},
  29 + retKey = null,
  30 + returnVal = null;
  31 +
  32 + for (let val of _.slice(data, 2, 4)) {
  33 + power.push(parseInt(val, 16));
  34 + }
  35 + //处理返回结果
  36 + if (data[1] === '16') {
  37 + retData = {
  38 + power: power,
  39 + region: region[Number(data[6])],
  40 + speedMode: Number(data[7]),
  41 + invTime: parseInt(_.slice(data, 8, 12).join(''), 16),
  42 + invCount: data[12]
  43 + }
  44 + retKey = _.slice(data, 0, 2);
  45 + } else {
  46 + retKey = data;
  47 + }
  48 +
  49 + returnVal = util.returnProc(retKey, client.remoteAddress, info.getConfig.output);
  50 + returnVal.data = retData;
  51 + return returnVal;
  52 + }
  53 +
  54 + /**
  55 + *获取版本返回处理
  56 + */
  57 + getVersion(data, client) {
  58 + let retData = null;
  59 + let retKey = _.slice(data, 0, 4);
  60 + let returnVal = util.returnProc(retKey, client.remoteAddress, info.getVersion.output);
  61 + if (data[2] === '0' && data[3] === '0') {
  62 + returnVal.data = {
  63 + majorVersion: parseInt(data[4], 16),
  64 + minorVersion: parseInt(data[5], 16)
  65 + };
  66 + }
  67 +
  68 + return returnVal;
  69 + }
  70 +}
  71 +export default Uhf;
  1 +/**
  2 + * 通用返回信息
  3 + */
  4 +var commonMessage = {
  5 + '00': {
  6 + code: 1,
  7 + message: '操作成功'
  8 + },
  9 + 'ffff': {
  10 + code: 0,
  11 + message: '没有响应'
  12 + },
  13 + 'fffe': {
  14 + code: 3,
  15 + message: '读写器正忙'
  16 + },
  17 + 'ff0': {
  18 + code: -1,
  19 + message: '操作失败'
  20 + }
  21 + }
  22 + /**
  23 + * 指令集枚举
  24 + */
  25 +export default {
  26 + openUhf: {
  27 + input: 'A000000000',
  28 + output: {
  29 + 'a0200': commonMessage['00'],
  30 + 'a02ff0': commonMessage['ff0'],
  31 + 'a02f00': {
  32 + code: 2,
  33 + message: 'UHF模块已打开'
  34 + }
  35 +
  36 + }
  37 + },
  38 + closeUhf: {
  39 + input: 'A100000000',
  40 + output: {
  41 + 'a1200': commonMessage['00'],
  42 + 'a12ff0': commonMessage['ff0']
  43 + }
  44 + },
  45 + setPower: {
  46 + input: 'F3',
  47 + output: {
  48 + 'f3200': commonMessage['00'],
  49 + 'f32ffff': commonMessage['ffff'],
  50 + 'f32fffe': commonMessage['fffe'],
  51 + 'f32ff0': commonMessage['ff0']
  52 + }
  53 + },
  54 + setFilter: {
  55 + input: 'B7',
  56 + output: {
  57 + 'b7200': commonMessage['00'],
  58 + 'b72ffff': commonMessage['ffff'],
  59 + 'b72fffe': commonMessage['fffe'],
  60 + 'b72ff0': commonMessage['ff0']
  61 + }
  62 + },
  63 + setFrequency: {
  64 + input: 'BF'
  65 + output: {
  66 + 'bf200': commonMessage['00'],
  67 + 'bf2ffff': commonMessage['ffff'],
  68 + 'bf2fffe': commonMessage['fffe'],
  69 + 'bf2ff0': commonMessage['ff0']
  70 + }
  71 + },
  72 + getConfig: {
  73 + input: 'B300000000',
  74 + output: {
  75 + 'b3200': commonMessage['00'],
  76 + 'b32ffff': commonMessage['ffff'],
  77 + 'b32fffe': commonMessage['fffe'],
  78 + 'b316': {
  79 + code: 1,
  80 + message: '返回信息'
  81 + }
  82 +
  83 +
  84 + }
  85 + },
  86 + getVersion: {
  87 + input: 'B400000000',
  88 + output: {
  89 + 'b4400': commonMessage['00'],
  90 + 'b44ffff': commonMessage['ffff'],
  91 + 'b44fffe': commonMessage['fffe'],
  92 + 'b44ff0': commonMessage['ff0']
  93 + }
  94 + },
  95 + sartInventory: {
  96 + input: 'F100000000'
  97 + },
  98 + stopInventory: {
  99 + input: 'F200000000'
  100 + },
  101 + read: {
  102 + input: 'F700000000'
  103 + },
  104 + write: {
  105 + input: 'F2'
  106 + },
  107 +};
  1 +/**
  2 + * 工具方法库
  3 + */
  4 +
  5 +import config from '../config';
  6 +import log4js from 'log4js';
  7 +import _ from 'lodash';
  8 +
  9 +log4js.loadAppender('file');
  10 +log4js.addAppender(log4js.appenders.file(config.log), 'log');
  11 +
  12 +let log = log4js.getLogger('log');
  13 +log.setLevel(config.logLevel); //配置日志
  14 +
  15 +/**
  16 + * 日志
  17 + */
  18 +exports.log = log;
  19 +
  20 +
  21 +/**
  22 + * 转16进制
  23 + */
  24 + exports.toHex = function(data)
  25 + {
  26 + let arr = [];
  27 + for(let i=0;i<data.length;i++)
  28 + {
  29 + arr[i] = data.readUIntLE(i).toString(16);
  30 + }
  31 + return arr;
  32 + };
  33 +
  34 + /**
  35 + * 处理返回码
  36 + */
  37 + exports.returnProc = function(data,ip,output)
  38 + {
  39 + if(!_.isArray(data) || _.size(data)<1)
  40 + {
  41 + return false;
  42 + }
  43 +
  44 + let ret = data.join('');
  45 + if(!output[ret])
  46 + {
  47 + return false;
  48 + }
  49 + log.info("读写器(" + ip + ")返回:" + output[ret].message+" 返回码:"+ret);
  50 + output[ret].hex = data;
  51 + return output[ret];
  52 + }
  53 +
  1 +{
  2 + "name": "rfid-sdk",
  3 + "version": "1.0.0",
  4 + "description": "rfid uhf sdk",
  5 + "main": "index.js",
  6 + "dependencies": {
  7 + "async": "0.2.x",
  8 + "log4js": "0.6.x",
  9 + "lodash":"3.6.x",
  10 + "pm2":"~0.12.3"
  11 + },
  12 + "scripts": {
  13 + "test": "echo \"Error: no test specified\" && exit 1"
  14 + },
  15 + "author": "",
  16 + "license": "ISC"
  17 +}