GithubHelp home page GithubHelp logo

webthingsio / gateway-addon-node Goto Github PK

View Code? Open in Web Editor NEW
13.0 13.0 19.0 538 KB

Node bindings for developing add-ons for WebThings Gateway

License: Mozilla Public License 2.0

JavaScript 8.37% TypeScript 91.63%

gateway-addon-node's People

Contributors

benfrancis avatar dependabot[bot] avatar dhylands avatar mozilla-github-standards avatar mrstegeman avatar relu91 avatar rzr avatar sogaani avatar tim-hellhake avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

gateway-addon-node's Issues

Tag v1.0.1 release

@tim-hellhake I'm thinking we should tag and release v1.0.1 release of this npm package to ship with gateway v1.0.1.

Before we do that I'm wondering if we should reverse de6a33a to use the "v" prefix in version names again, to be consistent with gateway-addon-python.

What do you think?

REST API PUT times out

I've noticed the following behavior while doing some changes on my miLight adapter: if I issue two consecutive REST PUT requests for the same property with the same value, the 2nd request times out, for ex. set the brightness level to 40 via REST and repeat the request.

This is the setValue code for my Property:

setValue(value) {
    return new Promise((resolve) => {
      const oldValue = this.value;
      this.setCachedValue(value);
      const hasChanged = (oldValue !== this.value);
      if (hasChanged) {
        this.device.notifyPropertyChanged(this, oldValue);
      }
      resolve(this.value);
    });
}

As far as I was able to trace the problem, it comes down to the fact that the notifyPropertyChanged is not getting called and implicitly the notifyPropertyChanged of the parent Device class is also not getting invoked so the addon manager proxy is not notified.

Any ideas on this?

handleDeviceSaved() / removeThing() pair, not fully reliable

using version 0.12

as per handleDeviceSaved() documentation:

* This can be used for keeping track of what devices have previously been
* discovered, such that the adapter can rebuild those, clean up old nodes,
* etc.

I wanted to use these functions to keep track of user saved devices VS all devices provided by the addon.
If a device is connected (a Device object exists and is present in addon-manager.jss this.device map) then an adapter's removeThing(deviceObj) is called.
If a device is disconnected (its ex Device object was deleted from addon-manager.jss this.device map) then AddonManager's removeThing(deviceId) does not find a valid device object and and addon's removeThing(deviceObj) is, of course, not called anymore.

So, if a developer wants to push a device ID in handleDeviceSaved() into an array, and then use removeThing() to pop that device ID from that array - to keep track of the devices added/removed by the user from the UI/API, it will not work reliably.

Have I misunderstood how to use these functions?
How can I detect in my addon when a disconnected device is removed by the user from the UI/API?

log filled with repeated change notifications - is it possible to throttle?

The gateway log is filled with lines such as the following

2020-04-13 18:58:45.008 INFO : timer-adapter: setCachedValueAndNotify for property seconds from 4 to 4.5 for interval-c4c415fc99b87af0d464d777c94a1c73

This means that the log wraps to 2 or even 3 separate logs per day

I raised an issue in the Timer addon project and, in response, @tim-hellhake referred me to this line of code

Is it possible to throttle the logging so that a frequently changing property is logged, say, once per minute?

thanks :)

Unable to add Things since upgrade of gateway-addon-ipc-schema

Since I upgraded the schema submodule to the latest master of gateway-addon-ipc-schema (WebThingsIO/gateway-addon-ipc-schema@d885b01) I'm no longer able to add things to the gateway. This is with the gateway using the latest 2.0.0-alpha.2 release of gateway-addon-node, built from master.

STR:

  • Check out the master branch of the gateway repo, which is now using the 2.0.0-alpha.2 release of gateway-addon-node.
  • Run the gateway
  • Install virtual things add-on
  • Click add button

Expected:

  • Virtual things available to add appear in the UI

Actual:

  • No things appear

I'm trying to figure out which one of my commits in gateway-addon-ipc-schema caused the regression, but I'm a bit confused about which commit the submodule was at before.

It appears that at the time of the 2.0.0-alpha.1 release of the gateway-addon npm package, the schema submodule was set to https://github.com/WebThingsIO/gateway-addon-ipc-schema/tree/7397a0bfcd13ec39ebddea1f8b4dc164dacd3835, but that commit doesn't belong to any branch in the WebThingsIO/gateway-addon-ipc-schema repo.

@tim-hellhake @relu91 Any idea what's going on here?

Actions API: define the life cycle of action and clarify the role of action class.

Since the current API is a little bit free for implementation of actions, there are many things to consider such as control of action lifecycle and asynchronous processing at development time.
As a developer, it is better to implement methods which is empty now on the device base class and reduce the methods that the developer should implement.

I'm implementing an action with my add-on.
This is my implementation of the device class and developer maybe can handle various actions by implementing the following method.

  • device.createAction
  • action.perform
  • action.cancel

Access Gateway from ApiHandler

Hi @mrstegeman! Recently I asked you a few questions WebThingsIO/gateway#2248. Now I'm stuck on accessing the gateway in ApiHandler. I understand that I have to do this through the PluginManager or AddonManagerProxy, but I don’t understand how to access it. Сan you help me with this (sample code with access to the list of devices)?

P.S. You will save a lot of my time
P.P.S. I can program only with Googling =)
P.P.P.S gateway wiki is outdated (https://github.com/mozilla-iot/wiki/wiki)

'use strict';

const {APIHandler, APIResponse, Adapter} = require('gateway-addon');
...

class DialogsAPIHandler extends APIHandler {
  constructor(addonManager) {
    super(addonManager, manifest.id);
    addonManager.addAPIHandler(this);
  }

  async handleRequest(request) {
    console.info(JSON.stringify(request))
    var baseResponse = ...
    if (request.method === 'HEAD' && request.path === '/v1.0/') {
      new APIResponse({
        status: 200
      });
    } else if (request.method === 'POST' ...) {
     ...
    } else if (request.method === 'GET' && request.path === '/v1.0/user/devices') {

      // Here I need to work with the gateway through some kind of internal mechanism
     var gatewayDevices = this.<something>.getDevices();
      

      return new APIResponse({
        status: 200,
        contentType: 'application/json',
        content: JSON.stringify(baseResponse),
      });
    } else {
      return new APIResponse({
        status: 404
      });
    }
  }
}
module.exports = DialogsAPIHandler;

CODE_OF_CONDUCT.md file missing

As of January 1 2019, Mozilla requires that all GitHub projects include this CODE_OF_CONDUCT.md file in the project root. The file has two parts:

  1. Required Text - All text under the headings Community Participation Guidelines and How to Report, are required, and should not be altered.
  2. Optional Text - The Project Specific Etiquette heading provides a space to speak more specifically about ways people can work effectively and inclusively together. Some examples of those can be found on the Firefox Debugger project, and Common Voice. (The optional part is commented out in the raw template file, and will not be visible until you modify and uncomment that part.)

If you have any questions about this file, or Code of Conduct policies and procedures, please reach out to [email protected].

(Message COC001)

Typescript

May I migrate this project to typescript?

This would also benefit JS developers as VS Code supports code completion from typings.

Issue with responses from API handlers

When only setting a status code on the response, an error is generated.

2020-12-07 13:06:37.107 ERROR  : Invalid message received: {
  "messageType": 20484,
  "data": {
    "packageName": "webhook-events",
    "messageId": 1,
    "response": {
      "status": 200
    },
    "pluginId": "webhook-events"
  }
}
2020-12-07 13:06:37.107 ERROR  : Validation error: [
  {
    "keyword": "required",
    "dataPath": ".data.response",
    "schemaPath": "#/properties/data/properties/response/required",
    "params": {
      "missingProperty": "contentType"
    },
    "message": "should have required property 'contentType'"
  }
]

Publish a pre-release of the latest changes

@mrstegeman there are several locations in the gateway where we need the latest changes from this repo.
I wonder if we could publish a pre-release.
Depending on the next gateway version, this could be something like 1.1.0-alpha.1.

Change access level for devices field

In the Adapter class, the field devices is declared as private. However, in the ThingURLAdapter the field is accessed directly - https://github.com/WebThingsIO/thing-url-adapter/blob/23b0f08ebc17b9df54c084b5191fa6883f90da01/thing-url-adapter.js#L618. And it's possible only because js simply doesn't care, but we cannot do the same if creating an adapter in ts. So, I would like to propose to change this field to protected or public instead.

The issue appeared while working on the new adapter.

@tim-hellhake
@benfrancis
@relu91

Change timestamp format

Utils.timestamp() currently generates timestamps in the following format:

2022-01-20T17:21:12+00:00

For some reason the default output of Date().toISOString() is modified so that it ends in "+00:00" rather than "Z". This is probably because that's the format used by the examples do in the Web Thing API specification.

The latest draft of the W3C WoT Profile specification constrains the date format so that it must end in "Z".

I therefore propose changing the timestamp method to return the raw output of Date().toISOString() rather than modify it, so that datestamps look like:

2011-10-05T14:48:00.000Z

Before doing this we should check if it breaks anything.

Also, double check that the milliseconds portion of the datestamp is expected by WoT Profile.

Note: These timestamps are used in action resources in the gateway's API to show the requested and completed times of an action, and also for the timestamps of events. I'm not really sure why the gateway uses this utility function from the add-on library and they can probably call Date().toISOString() directly instead. If this utility function isn't used anywhere else then it could probably just be removed.

How to cleanup resources allocated in subclassed Device objects?

Hi,

I have the following setup:

class MyDevice extends Device {
  constructor (adapter, id, deviceDescription) {
  // ...
  this.disconnectTimer = this._startDisconnectTimer()
  }
}

How should I clear the timer when MyDevice object is removed/destroyed/deleted?

Right now I'm doing it by:

class MyAdapter extends Adapter {
  removeThing (device) {
    if (device.disconnectTimer){
      clearTimeout(device.disconnectTimer)
      device.disconnectTimer = null
    }
    super.removeThing(device)
  }
}

because there is no callback in Device object that I could use.

It's a bit weird to use the MyAdapter object to release resources allocated by MyDevice object. I think Device object should be given a callback so that addon developers could use it to clear resources allocated during its lifetime.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.