npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

iot-device-simulator

v1.0.9

Published

IoT device simulator to generate random data in custom format and publish to MQTT broker

Downloads

23

Readme

IoT Device Simulator

IoT device simulator to auto-generate random data in the customized format and publish to MQTT broker.

Feedback, issues, and pull requests are always appreciated :)

Installation

Run npm install iot-device-simulator --save to download the library.

Features

  • Easily generate dummy data from a customized format
  • Publish customized report to MQTT broker in interval time
  • Easily change the state of the device simulator to generate different data to represent the different scenario

Module Schema

Import Modules

// Approach 1
import simulator from 'iot-device-simulator';
// Approach 2
import {
  createConnectionToBroker,
  createDevice,
  createDeviceType,
} from 'iot-device-simulator';

Usage

1. Create Connection

import { createConnectionToBroker } from 'iot-device-simulator';
const connection = createConnectionToBroker({
  url: 'http://your.host.and.port:1234',
  clientId: 'your_clientId',
  username: 'your_name',
  password: 'your_password',
});

2. Create Device Type And States

  • A device type has multiple states.
  • Every state has a customized report format.
  • Dummy data will generate randomly according to the format of the state.
  • How to define format of state?
const lum = createDeviceType('luminance_meter');

// Create State With Custimised Report Format
const lumFormat = [
  { id: 'version', value: ['v1', 'v2'] },
  { id: 'voltage', value: { max: 100, min: 0, digit: 0 } },
  { id: 'module', value: 1.1 },
];

// Every state has it's own report format
lum.addState({
  name: 'dark',
  payload: [
    ...lumFormat,
    { id: 'brightness', value: { max: 20, min: 0, digit: 1 } }, // different states may return different lum
  ],
});
lum.addState({
  name: 'dusk',
  payload: [
    ...lumFormat,
    { id: 'brightness', value: { max: 40, min: 20, digit: 1 } },
  ],
});
lum.addState({
  name: 'day',
  payload: [
    ...lumFormat,
    { id: 'brightness', value: { max: 100, min: 40, digit: 1 } },
  ],
});

3.Create Device Instance From Device Type

const lumBot = createDevice({
  type: lum,
  state: 'day',
  connection,
  topic: '$device/IOTAWESOME/report',
});
// change state to change report
lumBot.setState('dusk');
// change interval to change time to send report to broker every interval seconds
lumBot.setInterval(2000);
// change topic to report
lumBot.setTopic('$device/IOTAWESOME/report');
// change state of device, when active constantly send report, when inactive stop to report
lumBot.toggleIsActive();

4. Subscribe Topic To Trigger State Changing

  • examples
    • change the state to 'dark', when get topic $device/IOTAWESOME/cmd publish message, and the payload index 0 , 'value' key's value equals to 10
lumBot.addSubTopic({
  topic: '$device/IOTAWESOME/cmd', // topic to sbscribe
  trigger: { key: '0.value', value: 10, state: 'dark' },
  // key to get value from key and the expected state
  // key will be split by '.' and for the keys array to get the value. like example below:
  // example payload => [{ id: 'brightness', value: 10 }];
  // payload[0]['value'] = 10
});

State Format Column Definition

There are two categories of type 'value' and 'reference'. 'value' type is the base case of the column, and the reference type is the combination of the base case. Please see the examples below.

ps: If you want to test or get dummy data simply from the format can use the 'generateDataFromFormat' function

1. Value Type column

Value type columns are the base case of a column. If you nested the base case in the reference type column, it will recursively loop into it and generate dummy data until the end.

  1. boolean

  2. number

  3. string

import { generateDataFromFormat } from 'iot-device-simulator';
console.log(generateDataFromFormat(true)); // true
console.log(generateDataFromFormat(123)); // 123
console.log(generateDataFromFormat('IoT')); // IoT

2. Reference Type column**

Reference type columns are a combination of base case columns. The function for generating data will recursively loop into and generate dummy data until hit a base case end.

  • range
    • An object with max, min, and digit
    • Return a random number with specified decimals equal to the digit
const result = generateDataFromFormat({ max: 100, min: 0, digit: 2 });
console.log(result); // number 0~100 with 2 decimals, ex: 29.03
  • tuple
    • Combined multiple base columns and reference columns into an array
    • Return element in the array randomly
const result = generateDataFromFormat([
  8, // number
  { max: 100, min: 0, digit: 0 }, // range
  'voltage', // string
  {
    // object
    id: 'voltage',
    value: { max: 10, min: 0, digit: 0 },
  },
]);
console.log(result); // [ 8, 52, 'voltage', { id: 'voltage', value: 10 } ]
  • enum
    • Combined multiple base columns into array
    • Return element in the array randomly
const result = generateDataFromFormat([
  0, // number
  'v1.0', // string
  123, // number
]);
console.log(result); // 0 or 'v1.0' or 123
  • object
    • object with keys storing a value of value or reference type of columns
    • return an object of recursively generated data
const result = generateDataFromFormat({
  id: 'axisZ', // string
  value: 6.6, // number
  range: { max: 100, min: 0, digit: 0 }, // range
  // or any other types in value even object itself, nested combination is allowed
});
console.log(result); // { id: 'axisZ', value: 6.6, range: 56 }

Test

To start a test, run the following command:

npm run test

License

© Mason Yu (masonCalmAndCode), 2022-NOW

Released under the MIT License