Skip to content
This repository has been archived by the owner on Jun 13, 2019. It is now read-only.

addEventListeners seems really complicated when you want to expose multiple sensors at the same time #41

Open
kenchris opened this issue Mar 2, 2016 · 2 comments

Comments

@kenchris
Copy link

kenchris commented Mar 2, 2016

Instead of

    oic.addEventListener("updaterequest", onLightUpdate);
    oic.addEventListener("observerequest", onLightObserve);
    oic.addEventListener("deleterequest", onLightDelete);
    ...

and handling all sensors inside each method, wouldn't it not be better to give it an object instead, like Bleno does with Characteristics. That also allows you to easily share objects/value for each sensor instead of relying on global variables. And it allows you to inherit from common objects when they are very similar in spirit.

pseudocode (ignore naming) [the *Callback methods are modelled after latest ES6 compatible Custom Elements spec]:

class BlueLED extends oic.Resource {
  constructor() {
    this._value = 0.5;
    this._onlyGrow = true;

    super({
      deviceId: oic.device.uuid,
      path: "/light/ambience/blue",
      resourceTypes: [ "light" ],
      interfaces: [ "/oic/if/rw" ],

      discoverable: true,
      observable: true,

      properties: { 
        color: "blue",
        dimmer: this._value;
      }
    })
  }

  updateRequestCallback(value) {
     if (this._onlyGrow)
       this._value = Math.max(this._value, value);
  }

  deleteRequestCallback() {
  }
}

function startServer() {
  oic.registerResource(new BlueLED())
  .catch(function(err) { 
    console.log(err)
  });
}
@zolkis
Copy link
Contributor

zolkis commented Mar 2, 2016

Looks good, it's mostly syntactic sugar. Your method is complementing events but AFAICT is not replacing them. What is the callback dispatching mechanism? We need support from the main loop.
What idioms are better known by developers? Events at least are pretty well known.

@kenchris
Copy link
Author

kenchris commented Mar 8, 2016

They should be called async on the object itself (so it becomes ). I don't see why main loop support would be needed, as all the resources will be registered in one place.

I like that you naturally keep the relevant code together and this method might be popularized with custom elements and node.js libraries like noble.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants