<commit_before>/*jslint node:true */

/*
 * examples/node-discovery.js
 * https://github.com/101100/xbee-rx
 *
 * Example that performs node discovery.
 *
 * Copyright (c) 2015-2016 <NAME>
 * Licensed under the MIT license.
 */

"use strict";

var R = require('ramda');
var rx = require('rx');
var xbee_api = require('xbee-api');
var xbeeRx = require('../lib/xbee-rx.js');

var xbee = xbeeRx({
    serialport: '/dev/ttyUSB0',
    serialportOptions: {
        baudrate: 57600
    },
    module: "ZigBee",
    // turn on debugging to see what the library is doing
    debug: false
});

xbee
    .localCommand({
        command: 'NT',
    })
    .flatMap(function (ntResult) {
        // Fulfill promise when NT expires
        // NT is 1/10 seconds
        var timeoutMs = ntResult.readInt16BE(0) * 100;
        console.log("Got node discovery timeout:", timeoutMs, "ms");
        xbee.localCommand({ command: 'ND' }).subscribe();
        return xbee
            .allPackets
            .takeUntil(rx.Observable.timer(timeoutMs * 2))
            .where(R.propEq("type", xbee_api.constants.FRAME_TYPE.AT_COMMAND_RESPONSE))
            .where(R.propEq("command", "ND"))
            .pluck("nodeIdentification");
    })
    .subscribe(function (nodeIdentification) {
        console.log("Found node:\n", nodeIdentification);
    }, function (e) {
        console.log("Command failed:\n", e);
        xbee.close();
    }, function () {
        console.log("Timeout reached; done finding nodes");
        xbee.close();
    });
<commit_msg>Update node discovery example to finish sooner<commit_after>/*jslint node:true */

/*
 * examples/node-discovery.js
 * https://github.com/101100/xbee-rx
 *
 * Example that performs node discovery.
 *
 * Copyright (c) 2015-2016 <NAME>
 * Licensed under the MIT license.
 */

"use strict";

var R = require('ramda');
var rx = require('rx');
var xbee_api = require('xbee-api');
var xbeeRx = require('../lib/xbee-rx.js');

var xbee = xbeeRx({
    serialport: '/dev/ttyUSB0',
    serialportOptions: {
        baudrate: 57600
    },
    module: "ZigBee",
    // turn on debugging to see what the library is doing
    debug: false
});

xbee
    .localCommand({
        command: 'NT',
    })
    .flatMap(function (ntResult) {
        // Fulfill promise when NT expires
        // NT is 1/10 seconds
        var timeoutMs = ntResult.readInt16BE(0) * 100;
        console.log("Got node discovery timeout:", timeoutMs, "ms");
        xbee.localCommand({ command: 'ND' }).subscribe();
        return xbee
            .allPackets
            .takeUntil(rx.Observable.timer(timeoutMs + 1000))
            .where(R.propEq("type", xbee_api.constants.FRAME_TYPE.AT_COMMAND_RESPONSE))
            .where(R.propEq("command", "ND"))
            .pluck("nodeIdentification");
    })
    .subscribe(function (nodeIdentification) {
        console.log("Found node:\n", nodeIdentification);
    }, function (e) {
        console.log("Command failed:\n", e);
        xbee.close();
    }, function () {
        console.log("Timeout reached; done finding nodes");
        xbee.close();
    });

{'fraction_non_alphanumeric': 0.1503225806451613, 'fraction_numerical': 0.02032258064516129, 'mean_word_length': 2.527872582480091, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7456110', 'n_tokens_mistral': 1024, 'n_tokens_neox': 970, 'n_words': 228}