uhf.js 2.46 KB
/**
 * @fileOverview 读写器模块类
 * @author qiqi.zhou@yoho.cn
 */
import Base from './base';
import _ from 'lodash';
import {
    log, returnProc
}
from '../util';
import info from '../interface';

/**
 * @class UHF 读写器的指令操作上
 * @extends {Base}
 * @member @see {@link Base}
 */
class Uhf extends Base {
    constructor() {
        super(null);
        this.proc = {
            getConfig: this.getConfig,
            getVersion: this.getVersion
        };
    }

    /**
     * @method getConfig
     * @description 获取读写器信息返回处理
     * @param {Array} data 返回的指令数组
     * @param {Socket} client socket客户端
     * @return {Object} 标准输出
     */
    getConfig(data, client) {
        let region = ['FCC', 'CCC', 'NCC', 'JPN'],
            power = [],
            retData = {},
            retKey = null,
            returnVal = null,
            ip,
            output;

        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;
        }
        ip = client.remoteAddress;
        output = info.getConfig.output;
        returnVal = returnProc(retKey, ip, output);

        returnVal.data = retData;
        return returnVal;
    }

    /**
     * @method getVersion
     * @description 获取版本返回处理
     * @param {Array} data 返回指令数组
     * @param {Socket} client socket连接客户端
     * @return {Object} 标准输出
     */
    getVersion(data, client) {
        let retData = null,
            retKey = _.slice(data, 0, 4),
            ip = client.remoteAddress,
            output = info.getVersion.output;
        let returnVal = returnProc(retKey, ip, output);
        if (data[2] === '0' && data[3] === '0') {
            returnVal.data = {
                majorVersion: parseInt(data[4], 16),
                minorVersion: parseInt(data[5], 16)
            };
        }

        return returnVal;
    }
}

/**
 * @exports lib/directive/Uhf
 */
export default Uhf;