fxbox / foxbox

The FoxBox daemon
Mozilla Public License 2.0
29 stars 29 forks source link

We need an API to monitor changes to the properties of one or more devices #28

Closed Yoric closed 8 years ago

Yoric commented 8 years ago

Most Apps (both Control and Monitor) will need to monitor changes to device properties.

For instance:

To implement any of these apps, we need an API that will monitor a set of properties of devices and callback each time the value of a given property of a given device enter/leaves some interval (or at each change if no interval is specified).

We need this API at least in WebSocket, as all consumers can be built on top of WebSocket. Ideally, we'd like to provide a concise JS API on top of the WebSocket API. It would be useful to have this API available to Rust code, too, for the sake of the rules engine and Monitor Workers.

I'll describe a pseudo-API in Rust, because I have attempted to do it in other languages and it was actually more complicated:

trait Watcher {
  /// Create a new watcher.
  pub fn new() -> Watcher;

  /// Watch a property of a device.
  ///
  /// If the device is smart enough, 
  ///
  /// # Example
  ///
  /// let witness = watcher.add(&the_terminator,
  ///                                           &InputCapability::DetectSarahConnor,
  ///                                           &Range::any(),
  ///                                           &tx);
  ///
  /// Until `witness` is dropped, whenever property `DetectSarahConnor` of `the_terminator`
  /// changes, the watcher will send a message on `tx`.
  ///
  /// let witness_2 = watcher.add(&the_terminator,
  ///                                               &InputCapability::Ammo,
  ///                                               &Range::boundary(100),
  ///                                                &tx_2);
  ///
  /// Until `witness_2` is dropped, whenever property `Ammo` of `the_terminator` goes
  /// above/beyond 100, the watcher will send a message on `tx_2`.
  pub fn<T> add(&mut self, &Device, &InputCapability<T>, &Range<T>, &Sender<T>) -> Witness;
}

/// A structure used to stop watching a property once it is dropped.
pub struct Witness {
  // ...
}

This depends heavily on our ongoing efforts on Taxonomy.

Note that JS versions of this API will most likely use a addEventListener/removeEventListener pair, rather than channels (unless streams have been standardized by then). The server will monitor WebSocket disconnection to ensure garbage-collection of Watcher.

Cc @mcav, @jedireza, @davidflanagan .

mcav commented 8 years ago

Is this issue obsoleted by the taxonomy work?

Yoric commented 8 years ago

Let's say that it's replaced by "Let's integrate the Taxonomy".