简体   繁体   English

JSON数组到Node.js可读对象流

[英]JSON array to nodejs Readable object stream

I have an event that fires every time a packet is received from a bluetooth device. 我有一个事件,每次从蓝牙设备接收到数据包时都会触发。 The packet comes in as a JSON array and always contains 10 objects similar to the ones below. 数据包以JSON数组的形式出现,并且始终包含10个与以下对象相似的对象。

var s1_data = [{"record":0,"sensor":1,"timestamp":26566,"date":{"day":7,"hour":10,"minute":45,"month":5,"second":38,"year":18}},
{"record":1,"sensor":1,"timestamp":26567,"date":{"day":7,"hour":10,"minute":45,"month":5,"second":38,"year":18}},
{"record":2,"sensor":1,"timestamp":26568,"date":{"day":7,"hour":10,"minute":45,"month":5,"second":38,"year":18}},
{"record":3,"sensor":1,"timestamp":26569,"date":{"day":7,"hour":10,"minute":45,"month":5,"second":38,"year":18}},
{"record":4,"sensor":1,"timestamp":26570,"date":{"day":7,"hour":10,"minute":45,"month":5,"second":38,"year":18}},
{"record":5,"sensor":1,"timestamp":26571,"date":{"day":7,"hour":10,"minute":45,"month":5,"second":38,"year":18}},
{"record":6,"sensor":1,"timestamp":26572,"date":{"day":7,"hour":10,"minute":45,"month":5,"second":38,"year":18}},
{"record":7,"sensor":1,"timestamp":26573,"date":{"day":7,"hour":10,"minute":45,"month":5,"second":38,"year":18}},
{"record":8,"sensor":1,"timestamp":26574,"date":{"day":7,"hour":10,"minute":45,"month":5,"second":38,"year":18}},
{"record":9,"sensor":1,"timestamp":26575,"date":{"day":7,"hour":10,"minute":45,"month":5,"second":38,"year":18}}]

I need to push each individual object into a node.js stream that is being consumed by another stream. 我需要将每个单独的对象推送到另一个流正在使用的node.js流中。

Can anyone give an example on how I can push these to a Readable object stream on an ongoing basis (or do I need to use a PassThrough stream)? 谁能举例说明如何将其不断推送到Readable对象流中(或者我需要使用PassThrough流)? I can't seem to get my head around how to do this! 我似乎无法理解该怎么做!

Edited to add sample code below. 编辑后在下面添加示例代码。 chunk.toString() fails as chunk is undefined chunk.toString()失败,因为未定义块

var Readable = require('stream').Readable;
var util = require('util');
var through2 = require('through2');

var s1 = require('./sensor1.js');
var s2 = require('./sensor2.js');

var minLength = s1.length;
//console.log(s1);
var s1stream = new Readable({ objectMode: true });
var s2stream = new Readable({ objectMode: true });


s1stream._read = function () {
    this.push();
};

s2stream._read = function () {
    this.push();
};

if (s2.length < minLength){
  minLength = s2.length;
}

var n1 = 0;
setInterval(function() {
    if (n1++ < minLength) {
        console.log(s1[n1].record);
        s1stream.push(s1[n1]);
    } else if (n1++ === minLength) {
        s1stream.push(null);
    }
}, 1000);

var n2 = 0;
setInterval(function() {
    if (n2++ < minLength) {
        s2stream.push(s2[n2]);
    } else if (n2++ === minLength) {
        s2stream.push(null);
    }
}, 1000);


s1stream.pipe(through2.obj(function (chunk, enc, callback) {
    this.push(chunk.toString())
    callback()
  })).pipe(process.stdout);

Edit below shows working code. 下面的编辑显示了工作代码。 Looks like it was to do with the way I was creating the read functions 看起来这与我创建 read 函数的方式有关

var Readable = require('stream').Readable;
var util = require('util');
var through2 = require('through2');
var Transform = require('stream').Transform;

var s1 = require('./sensor1.js');
var s2 = require('./sensor2.js');

var minLength = s1.length;
//console.log(s1);

var s1stream = new Readable({
   objectMode: true,
   read() {}
 })

 const s2stream = new Readable({
    objectMode: true,
    read() {}
  })

if (s2.length < minLength){
  minLength = s2.length;
}

var n1 = 0;
setInterval(function() {
    if (n1 < minLength) {
        s1stream.push(s1[n1]);
        n1++;
    } else if (n1 == minLength) {
        s1stream.push(null);
    }
}, 1000);

var n2 = 0;
setInterval(function() {
    if (n2++ < minLength) {
        s2stream.push(s2[n2]);
    } else if (n2++ === minLength) {
        s2stream.push(null);
    }
}, 1000);

var jsonStream = through2.obj(function(file, encoding, cb) {
    this.push(file.record.toString())
    cb()
})

s1stream.pipe(jsonStream).pipe(process.stdout);

You will need to implement a Readable stream that "wraps" your bluetooth source. 您将需要实现一个“包装”您的蓝牙源的可读流。 When the packet received event is fired you'll loop through your array of objects and call your Readable stream's push method, passing in each object. 触发数据包接收事件时,您将遍历对象数组并调用Readable流的push方法,传入每个对象。 A somewhat similar example of this can be seen here 在这里可以看到一些类似的例子

Note that since you'll be sending actual JSON objects you'll want to enable the objectMode option when you write your implementation as described here 注意,因为您要发送的实际JSON对象你要启用objectMode选项,当你写你的执行情况描述在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM