inventory.js 1.66 KB
/**
 * 管理盘点
 */
import Base from './base';
import _ from 'lodash';
import {
    log, returnProc, tenToHex, hexToTen
}
from '../util';
import info from '../interface';

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

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

    /**
     * 获取读写器信息返回处理
     */
    start(data, client) {
        let retData = {},
            retKey,
            returnVal,
            retEpc;

        //处理返回结果
        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;
        }
        returnVal = returnProc(retKey, client.remoteAddress, info.sartInventory.output);

        returnVal.data = retData;
        return returnVal;
    }

}
export default Inventory;