Nodejs之Kafka篇

关键代码片段:

var kafka = require('kafka-node');
var Mock = require('mockjs');
var Producer = kafka.Producer,
    client = new kafka.KafkaClient(),
    producer = new Producer(client);

producer.on('ready', function() {
    setInterval(
        () => {
            var payloads = getPayloads();
            producer.send(payloads, function(err, data) {
                // console.log(data);
            })
        }, 10000
    );
    setInterval(
        () => {
            var stsObject = getStatusPayloads();
            producer.send(stsObject.payloads, function(err, data) {
                // console.log(data);
            })
            var sensorstsObject = getSensorStatusPayloads();
            producer.send(sensorstsObject.payloads, function(err, data) {
                // console.log(data);
            })
        }, 30000
    )
});

producer.on('error', function(err) {
    console.log(err);
});

/*function getPayloads() {
    var number = Mock.mock('@integer(1, 5)');
    var msg = '6184000' + number + "," + Mock.Random.now('minute') + ",";
    if (number == 5) {
        msg += Mock.mock('@float(200, 220, 2, 2)') + '@';
        msg += Mock.mock('@float(0, 3, 2, 2)') + '@';
        msg += Mock.mock('@float(20, 100, 2, 2)') + '@';
        msg += Mock.mock('@float(0, 0, 2, 2)') + '@';
        msg += Mock.mock('@float(49, 50, 2, 2)') + '@';
        msg += Mock.mock('@float(0, 10, 2, 2)');
    } else {
        msg += Mock.mock('@float(20, 100, 2, 2)');
    }
    console.log("===消息内容:=== ", msg);
    var payload = {
        topic: 'IOT-DATA',
        messages: msg
    }
    return [payload];
}

function getStatusPayloads() {
    var status = Mock.mock('@integer(0, 1)');
    var id = Mock.mock('@integer(1, 2)');
    var msg = 'C184000' + id + "," + Mock.Random.now('minute') + "," + status;
    console.log("===消息内容:=== ", msg);   
    var payload = {
        topic: 'CLIENT-STATUS',
        messages: msg
    }
    return {
        payloads: [payload],
        status: status,
        id: id
    };
}

function getSensorStatusPayloads() {
    var status = Mock.mock('@integer(0, 5)');
    var id = Mock.mock('@integer(1, 5)');
    var msg = '6184000' + id + "," + Mock.Random.now('minute') + "," + status;
    console.log("===消息内容:=== ", msg);   
    var payload = {
        topic: 'IOT-SENSOR-STATUS',
        messages: msg
    }
    return {
        payloads: [payload],
        status: status,
        id: id
    };
}*/

function getStatusPayloads() {
    var msg = 'C1840002,' + Mock.Random.now('minute') + ", 1";
    console.log("===消息内容:=== ", msg);
    var payload = {
        topic: 'CLIENT-STATUS',
        messages: msg
    }
    return {
        payloads: [payload]
    };
}

function getSensorStatusPayloads() {
    var status = Mock.mock('@integer(0, 5)');
    var msg = '61840005,' + Mock.Random.now('minute') + "," + status;
    console.log("===消息内容:=== ", msg);
    var payload = {
        topic: 'IOT-SENSOR-STATUS',
        messages: msg
    }
    return {
        payloads: [payload]
    };
}

function getPayloads() {
    var msg = '61840005,' + Mock.Random.now('minute') + ",";
    msg += Mock.mock('@float(200, 220, 2, 2)') + '@';
    msg += Mock.mock('@float(0, 3, 2, 2)') + '@';
    msg += Mock.mock('@float(20, 100, 2, 2)') + '@';
    msg += Mock.mock('@float(0, 0, 2, 2)') + '@';
    msg += Mock.mock('@float(49, 50, 2, 2)') + '@';
    msg += Mock.mock('@float(0, 10, 2, 2)');

    console.log("===消息内容:=== ", msg);
    var payload = {
        topic: 'IOT-DATA',
        messages: msg
    }
    return [payload];
}

你可能感兴趣的:(前端)