<commit_before>"use strict";

/** 
 * Simple example script that demonstrates what might be a typical flow:
 * 
 * 1. Connect to cloud.
 * 2. Find tag managers.
 * 3. For each tag manager, find its associated tags.
 * 4. For each tag, find its sensors.
 * 5. For each sensor, log useful properties (here: reading, event
 *    state (such as "Too High" for temperature), and whether it is
 *    armed (meaning that events trigger alerts, such as push notification).
 * 6. Keep looping for a set period of time by updating the data for
 *    each tag at the interval configured for each tag.
 * 
 * The implementation here uses mostly event handlers to proceed once
 * the initial connect() succeeds. One could equally well use
 * Promise-chaining (via .then()).
 *
 * Both tag and sensor objects override the .toString() method to
 * produce a compact stringified-JSON representation of their key
 * properties, so dumping this to the terminal can be informative.
 * Uncomment the corresponding lines in the code below to do so.
 */
var Platform = require('../'),
    platform = Platform.create();

const END_AFTER = 30 * 60 * 1000; // value is in milliseconds

platform.on('connect', () => {
    console.log("connected to Wireless Tag cloud");
    platform.discoverTagManagers().
        catch((e) => { console.error(e.stack ? e.stack : e) });
});
platform.on('discover', (manager) => {
    console.log("found manager", manager.name, manager.mac);
    manager.on('discover', (tag) => {
        logTag(tag);
        tag.on('discover', (sensor) => { logSensor(sensor); });
        tag.on('data', (tag) => { logTag(tag); tag.eachSensor(logSensor); });
        tag.discoverSensors().
            then((sensors) => {
                tag.startUpdateLoop();
                setTimeout(tag.stopUpdateLoop.bind(tag), END_AFTER);
                return sensors;  // only needed if we kept chaining
            }).
            catch((e) => { console.error(e.stack ? e.stack : e); throw e; });
    });
    manager.discoverTags().catch((e) => {console.error(e.stack ? e.stack : e)});
});

platform.connect(Platform.loadConfig()).
    catch((e) => { console.error(e.stack ? e.stack : e) });

function logTag(tag) {
    console.log("Tag", tag.name, "(slaveId=" + tag.slaveId + ")");
    // console.log(".. properties:", tag.toString());
}

function logSensor(sensor) {
    console.log("..", sensor.sensorType,
                "of", sensor.wirelessTag.name + ":");
    console.log("    reading:", sensor.reading);
    if (sensor.eventState !== undefined) {
        console.log("    state:", sensor.eventState);
        console.log("    armed:", sensor.isArmed());
    }<commit_msg>Mostly cosmetic fixes to the example doc.<commit_after>"use strict";

/** 
 * Simple example script that demonstrates what might be a
 * typical flow for reading sensors.
 * 
 * The principle flow is the following:
 *
 * 1. Connect to cloud.
 * 2. Find tag managers.
 * 3. For each tag manager, find its associated tags.
 * 4. For each tag, find its sensors.
 * 5. For each tag and its sensors, log useful properties (for
 *    sensors, reading, event state (such as "Too High" for
 *    temperature), and whether it is armed (meaning that events
 *    trigger alerts, such as push notification).
 * 6. Keep looping for a set period of time by updating the data for
 *    each tag at the interval configured for each tag. Log sensor
 *    properties every time data for the respective tag is updated.
 * 
 * The implementation here uses mostly event handlers to proceed once
 * the initial connect() succeeds. One could equally well use
 * Promise-chaining (via `.then()`).
 *
 * Both tag and sensor objects override the .toString() method to
 * produce a compact stringified-JSON representation of their key
 * properties, so dumping this to the terminal can be informative.
 * Uncomment the corresponding lines in the code below to do so.
 *
 * @module
 */

var Platform = require('../'),
    platform = Platform.create();

const END_AFTER = 30 * 60 * 1000; // value is in milliseconds

platform.on('connect', () => {
    console.log("connected to Wireless Tag cloud");
    platform.discoverTagManagers().
        catch((e) => { console.error(e.stack ? e.stack : e) });
});
platform.on('discover', (manager) => {
    console.log("found manager", manager.name, manager.mac);
    manager.on('discover', (tag) => {
        logTag(tag);
        tag.on('discover', (sensor) => { logSensor(sensor); });
        tag.on('data', (tag) => { logTag(tag); tag.eachSensor(logSensor); });
        tag.discoverSensors().
            then((sensors) => {
                tag.startUpdateLoop();
                setTimeout(tag.stopUpdateLoop.bind(tag), END_AFTER);
                return sensors;  // only needed if we kept chaining
            }).
            catch((e) => { console.error(e.stack ? e.stack : e); throw e; });
    });
    manager.discoverTags().catch((e) => {console.error(e.stack ? e.stack : e)});
});

platform.connect(Platform.loadConfig()).
    catch((e) => { console.error(e.stack ? e.stack : e) });

/** Logs properties of the given tag. */
function logTag(tag) {
    console.log("Tag", tag.name, "(slaveId=" + tag.slaveId + ")");
    // console.log(".. properties:", tag.toString());
}

/** Logs properties of the given sensor. */
function logSensor(sensor) {
    console.log("..", sensor.sensorType,
                "of", sensor.wirelessTag.name + ":");
    console.log("    reading:", sensor.reading);
    if (sensor.eventState !== undefined) {
        console.log("    state:", sensor.eventState);
        console.log("    armed:", sensor.isArmed());
    }
{'fraction_non_alphanumeric': 0.14508968211685314, 'fraction_numerical': 0.004972473805718345, 'mean_word_length': 2.952280701754386, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, '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': '6578458', 'n_tokens_mistral': 1627, 'n_tokens_neox': 1566, 'n_words': 540}