1.0.1 • Published 2 years ago

thsq-simulator v1.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

Thingsquare device simulator

The thsq-simulator is a simulator for Thingsquare devices. This simulator can run on any desktop machine, which makes it easier to work with than the actual hardware.

The purpose of the simulator is to make backend integration and algorithm development easier.

Requirements

NodeJS and npm.

Installation

npm install --save thsq-simulator

Create a first simulator

Here's a full implementation of a simple simulator. This implementation can be modified to alter the behavior of the simulated devices. Save this content to simulator.js.

var thsqsim = require('thsq-simulator');
var getopts = require('getopts');
require('console-stamp')(console, { pattern : 'HH:MM:ss.l' });

function usage() {
    console.info('usage: node simulator.js -u <token> -d <num devices> -n <num networks>');
    console.info('<token> is an API token that can be retrieved from https://developer.thingsquare.com/web/, under the User tab, by checking the "Enable API access" checkbox');
    console.info('<num devices> is the number of simulated devices to spin up');
    console.info('optional: -f <frontend> -b <backend>');
    process.exit(1);
}

var options = getopts(process.argv.slice(2), {
    alias: {
        user: 'u',
        frontend: 'f',
        backend: 'b',
        networks: 'n',
        devices: 'd',
        platform: 'p',
        applatform: 'a',
        filename: 'F'
    },
    default: {
        frontend: 'b72af87d-42d7-4f09-bf8c-b9f721a3e6ef',
        backend: 'developer.thingsquare.com',
        networks: 1,
        devices: 10,
        proptime: 100,
        period: 3600 * 10,
    }
});

if (!options.user || !options.devices) {
    usage();
    return;
}

thsqsim.on('boot', function (device) {
});

thsqsim.on('variable', function (device, key, val, oldval) {
});

thsqsim.on('command', function (device, command, data) {
});


thsqsim.init({
    token: options.user,
    frontend: options.frontend,
    backend: options.backend,
    platform: options.platform,
    applatform: options.applatform,
    devices: options.devices,
    period: options.period,
    proptime: options.proptime,
    networks: options.networks,
    deadleaf: options.deadleaf,
    statefilename: options.filename }, function () {
        console.log('started');
});

Running the simulator

First grab an API key.

Go to https://developer.thingsquare.com/web/ and log in.

The API key can be found under the User tab (make sure to enable API access first).

Then run:

node thsq-simulator.js -u <API token from above> -n <number of simulated devices>

This will produce output similar to this:

$ node simulator.js -u <the API token> -n 10
[18:00:00.726] [LOG]    Creating device 0 / 11
[18:00:01.760] [LOG]    Creating device 1 / 11
[18:00:02.914] [LOG]    Creating device 2 / 11
[18:00:04.078] [LOG]    Creating device 3 / 11
[18:00:05.384] [LOG]    Creating device 4 / 11
[18:00:06.397] [LOG]    Creating device 5 / 11
[18:00:07.637] [LOG]    Creating device 6 / 11
[18:00:08.197] [LOG]    Creating device 7 / 11
[18:00:08.581] [LOG]    Creating device 8 / 11
[18:00:08.948] [LOG]    Creating device 9 / 11
[18:00:10.082] [LOG]    Creating device 10 / 11
[18:00:10.901] [LOG]    Creating device 11 / 11
[18:00:10.902] [LOG]    All simulated devices started

The simulated devices will then show up in https://developer.thingsquare.com/web/.

Customizing the simulator

By default simulated devices post a randomized value to the server variable t. To override what the device post, pass a periodiccallback parameters to the thsqsim.init function. Here's a full example of how to instead set and push a myvariable server variable.

function pushmyvariable(device) {
    device.pushVariable('s', 'myvariable', 'myvalue');
    device.sendStats();
}

thsqsim.init({
    ...
    periodiccallback: pushmyvariable,
    ...
});