inventory.js 2.13 KB
/**
 * @fileOverview 管理盘点
 * @author qiqi.zhou@yoho.cn
 */
import Base from './base';
import _ from 'lodash';
import {
    log, returnProc, tenToHex, hexToTen
}
from '../util';
import info from '../interface';

/**
 * @class UHF 读写器的指令操作盘点
 * @module lib/directive/Inventory
 * @extends {Base}
 * @member @see {@link Base}
 */
class Inventory extends Base {
    constructor() {
        super(null);
        let setInventoryTime = info.setInventoryTime.input;
        this.reproc[setInventoryTime] = this.preSetInventoryTime;
        this.proc = {
            sartInventory: this.start
        };
    }

    /**
     * @method preSetInventoryTime
     * @description 预处理设置读写器盘点时长
     * @param {Number} param 读写时长 eg .1000 (单位:ms)
     * @return {String} 指令
     */
    preSetInventoryTime(param) {
        let time = tenToHex(param).toLocaleUpperCase();
        time = _.padLeft(time, 8, '0');
        return info.setInventoryTime.input + time;
    }

    /**
     * @method start
     * @description 获取读写器信息返回处理
     * @param {Array} data 指令数组
     * @param {Connector} client socket客户端
     */
    start(data, client) {
        let retData = {},
            retKey,
            returnVal,
            retEpc,
            ip,
            output;

        //处理返回结果
        if (data[1] !== '2') {
            for (let i = 0; i < data.length; i++) {
                if (data[i].length < 2 && i > 2) {
                    data[i] = '0' + data[i];
                }
            }

            retEpc = _.slice(data, 5);

            retData = {
                ant: Number(data[2]),
                rssi: hexToTen(data[3] + data[4], true) / 10,
                epc: retEpc.join('')
            };
            retKey = _.slice(data, 0, 1);
        } else {
            retKey = data;
        }
        ip = client.remoteAddress;
        output = info.sartInventory.output;
        returnVal = returnProc(retKey, ip, output);

        returnVal.data = retData;
        return returnVal;
    }

}
/**
 * @exports lib/directive/Inventory
 */
export default Inventory;