在Electron中使用Mozilla WebThings API构建应用

时间:2019-06-27 21:29:11

标签: electron mozilla

我想构建一个可以在智能家居中控制几件事的应用程序。我的意图是为用户提供许多自定义和功能,并让他们根据需要选择任何便宜的ZigBee或Z-Wave设备。

出于安全原因以及我不想重新发明轮子的事实,我将使用Mozilla WebThings API。我将首先从DESKTOP开始,因此我想使用Electron,因为我已经可以对网站进行编程了。

我找到了以下API示例:

const {
    Action,
    Event,
    Property,
    SingleThing,
    Thing,
    Value,
    WebThingServer,
  } = require('webthing');
  const uuidv4 = require('uuid/v4');

  class OverheatedEvent extends Event {
    constructor(thing, data) {
      super(thing, 'overheated', data);
    }
  }

  class FadeAction extends Action {
    constructor(thing, input) {
      super(uuidv4(), thing, 'fade', input);
    }

    performAction() {
      return new Promise((resolve) => {
        setTimeout(() => {
          this.thing.setProperty('brightness', this.input.brightness);
          this.thing.addEvent(new OverheatedEvent(this.thing, 102));
          resolve();
        }, this.input.duration);
      });
    }
  }

  function makeThing() {
    const thing = new Thing('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,
                     maximum: 100,
                     unit: 'percent',
                   }));

    thing.addAvailableAction(
      'fade',
      {
        title: 'Fade',
        description: 'Fade the lamp to a given level',
        input: {
          type: 'object',
          required: [
            'brightness',
            'duration',
          ],
          properties: {
            brightness: {
              type: 'integer',
              minimum: 0,
              maximum: 100,
              unit: 'percent',
            },
            duration: {
              type: 'integer',
              minimum: 1,
              unit: 'milliseconds',
            },
          },
        },
      },
      FadeAction);

    thing.addAvailableEvent(
      'overheated',
      {
        description: 'The lamp has exceeded its safe operating temperature',
        type: 'number',
        unit: 'degree celsius',
      });

    return thing;
  }

  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);
  }

  runServer();

那么,有谁知道某个人做了类似的事情,或者我如何开始? (我都是新来的)

0 个答案:

没有答案