inventory.js 2.41 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 = {
            startInventory: 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') {
            var newdata = _.chunk(data, 17);
            for(let l = 0; l < newdata.length; l++) {
                let item = newdata[l];
                for (let i = 0; i < item.length; i++) {
                    if (item[i].length < 2 && i > 2) {
                        item[i] = '0' + item[i];
                    }
                }

                retEpc = _.slice(item, 5);

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

        returnVal.data = retData;
        return returnVal;
    }

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