How to use webthing - 10 common examples

To help you get started, we’ve selected a few webthing examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github rzr / webthing-iotjs / example / single-thing.js View on Github external
function runServer() {
  const thing = makeThing();

  // If adding more than one thing, use MultipleThings() with a name.
  // In the single thing case, the thing's name will be broadcast.
  const server = new WebThingServer(new SingleThing(thing), 8888);

  process.on('SIGINT', () => {
    server.stop().then(() => process.exit()).catch(() => process.exit());
  });

  server.start().catch(console.error);
}
github rzr / webthing-iotjs / example / multiple-things.js View on Github external
function runServer() {
  // Create a thing that represents a dimmable light
  const light = new ExampleDimmableLight();

  // Create a thing that represents a humidity sensor
  const sensor = new FakeGpioHumiditySensor();

  // If adding more than one thing, use MultipleThings() with a name.
  // In the single thing case, the thing's name will be broadcast.
  const server = new WebThingServer(new MultipleThings([light, sensor],
                                                       'LightAndTempDevice'),
                                    8888);

  process.on('SIGINT', () => {
    server.stop().then(() => process.exit()).catch(() => process.exit());
  });

  server.start().catch(console.error);
}
github rzr / webthing-iotjs / example / platform / index.js View on Github external
function runServer() {
  const port = process.argv[3] ? Number(process.argv[3]) : 8888;
  const url = `http://localhost:${port}`;

  log(`Usage:\n\
${process.argv[0]} ${process.argv[1]} [board] [port]\n\
Try:\ncurl -H "Accept: application/json" ${url}\
\n`);
  const thing = new BoardThing();
  const server = new WebThingServer(new SingleThing(thing), port);
  process.on('SIGINT', () => {
    const cleanup = () => {
      thing && thing.close();
      log(`log: board: ${board}: Exit`);
      process.exit();
    };
    server.stop().then(cleanup).catch(cleanup);
  });
  server.start().catch(console.error);
  log(`log: board: ${board}: Started`);
}
github rzr / webthing-iotjs / example / gpio-binary-sensor-thing.js View on Github external
function runServer() {
  const port = process.argv[2] ? Number(process.argv[2]) : 8888;
  const pin = process.argv[3] ? Number(process.argv[3]) : 11;
  const url = `http://localhost:${port}/properties/on`;

  console.log(`Usage:\n
${process.argv[0]} ${process.argv[1]} [port] [gpio]

Try:
curl -H 'Content-Type: application/json' ${url}
`);

  const thing = makeThing();
  const server = new WebThingServer(new SingleThing(thing), port);
  process.on('SIGINT', () => {
    server.stop();
    gpio.unexport(pin);
    process.exit();
  });
  const input = gpio.export(pin, {
    direction: 'in',
    ready: () => {
      input.value = undefined;
      input._get((value) => {
        console.log(`log: GPIO${pin}: ready: ${value}`);
        input.on("change", (value) => {
          console.log(`log: GPIO${pin}: change: ${value}`);
          thing.value.notifyOfExternalUpdate(Boolean(value));
        });
        server.start();
github rzr / webthing-iotjs / example / mastodon-thing.js View on Github external
function runServer() {
  const port = process.argv[2] ? Number(process.argv[2]) : 8042;
  const url = `http://localhost:${port}/properties/multiLevelSwitch`;
  console.log('Usage:\n'
              + process.argv[0] + ' ' + process.argv[1] + ' [port]\n'
              + 'Try:\ncurl -H "Content-Type: application/json" '
              + url + '\n');

  const thing = makeThing();
  const server = new WebThingServer(new SingleThing(thing), port);
  process.on('SIGINT', () => {
    server.stop();
    process.exit();
  });
  server.start();
}
github rzr / webthing-iotjs / example / multiple-things.js View on Github external
constructor() {
    super(
      'urn:dev:ops:my-lamp-1234',
      'My Lamp',
      ['OnOffSwitch', 'Light'],
      'A web connected lamp'
    );

    this.addProperty(
      new Property(
        this,
        'on',
        new Value(true, (v) => console.log('On-State is now', v)),
        {
          '@type': 'OnOffProperty',
          title: 'On/Off',
          type: 'boolean',
          description: 'Whether the lamp is turned on',
        }));

    this.addProperty(
      new Property(
        this,
        'brightness',
        new Value(50, (v) => console.log('Brightness is now', v)),
        {
github rzr / webthing-iotjs / example / platform / gpio / gpio-property.js View on Github external
constructor(thing, name, value, metadata, config) {
    super(thing, name, new Value(Boolean(value)),
          {
            '@type': 'OnOffProperty',
            title: (metadata && metadata.title) || `On/Off: ${name}`,
            type: 'boolean',
            description: (metadata && metadata.description) ||
              (`GPIO Actuator on pin=${config.pin}`),
          });
    const self = this;
    this.config = config;
    this.port = gpio.export(config.pin,
                            {direction: 'out',
                             ready: () => {
                               log(`log: GPIO: ${self.getName()}: open:`);
                               self.value.valueForwarder = (value) => {
                                 try {
                                   log(`log: GPIO: ${self.getName()}: \
github rzr / webthing-iotjs / example / single-thing.js View on Github external
function makeThing() {
  const thing = new Thing('urn:dev:ops:my-lamp-1234',
                          'My Lamp',
                          ['OnOffSwitch', 'Light'],
                          'A web connected lamp');

  thing.addProperty(
    new Property(thing,
                 'on',
                 new Value(true),
                 {
                   '@type': 'OnOffProperty',
                   title: 'On/Off',
                   type: 'boolean',
                   description: 'Whether the lamp is turned on',
                 }));
  thing.addProperty(
    new Property(thing,
                 'brightness',
                 new Value(50),
                 {
                   '@type': 'BrightnessProperty',
                   title: 'Brightness',
                   type: 'integer',
                   description: 'The level of light from 0-100',
                   minimum: 0,
github rzr / webthing-iotjs / example / multiple-things.js View on Github external
constructor() {
    super(
      'urn:dev:ops:my-humidity-sensor-1234',
      'My Humidity Sensor',
      ['MultiLevelSensor'],
      'A web connected humidity sensor'
    );

    this.level = new Value(0.0);
    this.addProperty(
      new Property(
        this,
        'level',
        this.level,
        {
          '@type': 'LevelProperty',
          title: 'Humidity',
          type: 'number',
          description: 'The current humidity in %',
          minimum: 0,
          maximum: 100,
          unit: 'percent',
          readOnly: true,
        }));
github rzr / webthing-iotjs / example / mastodon-thing.js View on Github external
function makeThing() {
  const thing = new Thing('MastodonMultiLevelSwitchExample', 'multiLevelSwitch', 'An actuator example that just blog');

  thing.addProperty(
    new Property(thing,
                 'level',
                 new Value(0, handleLevelUpdate),
                 {
                   label: 'Level',
                   type: 'number',
                   description: 'Whether the output is changed',
                 }));
  return thing;
}

webthing

HTTP Web Thing implementation

MPL-2.0
Latest version published 4 years ago

Package Health Score

48 / 100
Full package analysis