index.js 5.89 KB
/**
 * 调用入口
 */
require("babel/register"); //es6 polyfill
var Connector = require('./lib/connector'); //socket connect
var orders = require('./lib/interface'); // order config
var Uhf = require('./lib/directive/uhf'); //uhf dirctive module
var Config = require('./lib/directive/config'); // config dirctive module
var Inventory = require('./lib/directive/inventory'); //inventory dirctive module
var _ = require('lodash');
var util = require('./lib/util');
var appConfig = require('./config');

//实例化指令模块
var uhf = new Uhf();
var config = new Config();
var inventory = new Inventory();

/**
 * 打开读写器
 */
exports.open = function (host, port, callback) {
    if (!host || !port) {
        return;
    }
    //连接一个读写器
    var connector = new Connector({
        host: host,
        port: port
    }, function () {
        uhf.send(connector, orders.openUhf);
    }, receive);

    //当打开读写器模块
    connector.on('openUhf', function (data) {
        if ((data.code === 1 || data.code === 2) && callback) {
            callback(connector);
        }
    });
}

/**
 * 设置功率
 * param array eg: [15,15,15,0]
 */
exports.setPower = function (connector, param) {
    config.send(connector, orders.setPower, param);
}

/**
 * 设置灵敏度筛选
 * param object eg:{on:'1',rssi:-600}
 */
exports.setFilter = function (connector, param) {
    config.send(connector, orders.setFilter, param);
}

/**
 * 设置固定频率
 * param object eg: {ant: 0, freq: 10 }
 */
exports.setFrequency = function (connector, param) {
    config.send(connector, orders.setFrequency, param);
}

/**
 * 设置盘点时长
 * param number eg: 400
 */
exports.setInventoryTime = function (connector, param) {
    inventory.send(connector, orders.setInventoryTime, param);
}

/**
 * 开始盘点
 */
exports.sartInventory = function (connector) {
    inventory.send(connector, orders.sartInventory);
}

/**
 * 结束盘点
 */
exports.stopInventory = function (connector) {
    inventory.send(connector, orders.stopInventory);
}

/**
 * 开始间歇盘点封装
 */
var isStop = false,
    checkInventoryFlag,
    checkSpeedFlag;
exports.start = function (options, callback) {
    var rets = [];
    var countMap = {};
    exports.open(options.host, options.port, function (connector) {

        if (options.filter) { //开启灵敏度过滤
            exports.setFilter(connector, options.filter);
        }

        if (options.frequencys) { //开启固定频率
            _.forIn(options.frequencys, function (val, key) {
                exports.setFrequency(connector, val);
            });
        }

        if (options.inventoryTime) { //设置盘点时长
            exports.setInventoryTime(connector, options.inventoryTime);
        }

        if (options.power) { //设置功率
            connector.on('setInventoryTime', function (data) {
                exports.setPower(connector, options.power);
            });

        }

        connector.on('setPower', function (data) {
            exports.sartInventory(connector); //开始盘点
        });

        connector.on('sartInventory', function (data) {
            //console.log(data.code);
            if (data.code !== 2 && !isStop) {
                exports.sartInventory(connector);
            } else if (data.code === 2) {
                rets = procTagInfo(options.group, countMap, rets, data.data);
            }
            callback(rets);
        });

        checkInventoryFlag = setInterval(function () { //检查是否在架
            for (var i = 0; i < rets.length; i++) {
                if (1 * new Date() - rets[i].updateTime > appConfig.offInventory) {
                    rets[i].state = 'off';
                    rets[i].speed = 0;
                }
            }
        }, appConfig.checkInventory);

        checkSpeedFlag = setInterval(function () { //检查读取速度(times/sec)
            for (var i = 0; i < rets.length; i++) {
                if (countMap[rets[i].epc]) {
                    rets[i].speed = Number(countMap[rets[i].epc]) / (appConfig.checkCount / 1000);
                    countMap[rets[i].epc] = 0;
                }
            }
        }, appConfig.checkCount);
    });
}

/**
 * 停止间歇盘点
 */
exports.stop = function (connector) {
    isStop = true;
    clearInterval(checkInventoryFlag);
    clearInterval(checkSpeedFlag);
}

/**
 * 标签处理
 */
function procTagInfo(group, countMap, rets, data) {
    var isExist = false,
        now = 1 * new Date();
    for (var i = 0; i < rets.length; i++) {
        if (rets[i].epc === data.epc) {
            rets[i].rssi = data.rssi;
            rets[i].updateTime = now;
            rets[i].state = 'on';
            rets[i].ant = data.ant;
            rets[i].group = group;
            countMap[rets[i].epc] ++;
            isExist = true;
        }
    }
    if (!isExist) {
        data.updateTime = now;
        data.state = 'on';
        data.speed = 0;
        countMap[data.epc] = 1;
        rets.push(data);
    }
    return rets;
}


/**
 * 接收数据处理
 */
function receive(client, data, err) {
    var className = null,
        ret = null;
    if (!data) {
        return;
    }
    if (err) {
        console.log(err);
    }
    className = util.locateOrder(orders, data).val.directive;

    switch (className) {
    case 'uhf':
        ret = uhf.receive(client, data);
        break;
    case 'config':
        ret = config.receive(client, data);
        break;
    case 'inventory':
        ret = inventory.receive(client, data);
        break;
    }
    return ret;
}

/*
exports.start({
    host: '172.16.13.6',
    port: '9761',
    filter: {
        'on': '1',
        rssi: -600
    },
    frequencys: [{
        ant: 0,
        freq: 10
    }, {
        ant: 1,
        freq: 10
    }, {
        ant: 2,
        freq: 10
    }, {
        ant: 3,
        freq: 10
    }],
    inventoryTime: 400,
    power: [15, 0, 0, 0],
    5
}, function (data) {
    console.log(data);
});*/