|
| 1 | ++++ |
| 2 | +title = "wot-discovery design notes" |
| 3 | + |
| 4 | +[taxonomies] |
| 5 | +categories = ["Releases"] |
| 6 | +tags = ["rust", "wot"] |
| 7 | +authors = ["Luca Barbato"] |
| 8 | ++++ |
| 9 | + |
| 10 | +We are quickly iterating over the [wot-discovery](https://crates.io/crates/wot-discovery) crate. |
| 11 | +Here some notes about [Discovery specification][Discovery]. |
| 12 | + |
| 13 | +## Discovery and Directory |
| 14 | + |
| 15 | +The [W3C Web of Things](https://www.w3.org/WoT/) is all about to make connected devices (Things) |
| 16 | +interoperate and cooperate. |
| 17 | + |
| 18 | +In my opinion the greatest achievement is coming up with the [Thing Description][TD] to make possible |
| 19 | +to both describe the status quo and pave the way of a more rational alternative. In WoT jargon the former |
| 20 | +is called `brownfield` the latter `greenfield`. |
| 21 | +But since being able to describe how to interact with Things is only part of the problems we have additional |
| 22 | +facets specified in other documents, one of them id [WoT Discovery][Discovery]. |
| 23 | + |
| 24 | +The document is mostly oriented on possible greenfield implementations and in my opinion it tries to bring in |
| 25 | +too much at the same time. |
| 26 | + |
| 27 | +Once you come up with a mechanism to discover unknown nodes in your networks one may argue that the problem is |
| 28 | +solved, other may argue that it doesn't stop at enumeration and you would like to do more: |
| 29 | +- Your network may be partitioned so you would need to export one neightbourhood information to another, e.g. |
| 30 | + You may want to have gateways across different kind of networks (e.g. bluetooth vs wifi) and keep a full |
| 31 | + picture. |
| 32 | +- Querying may be important if the whole lot of devices may be too much to deal with at the same time, e.g. |
| 33 | + all the sensors in a big city. |
| 34 | + |
| 35 | +Thus the [WoT Discovery](Discovery) document is conceptually split in two: |
| 36 | +- Actual means to Advertise and Discover Things. |
| 37 | +- Means to aggregate and query and present the Things discovered. |
| 38 | + |
| 39 | +In wot-rust we split the implementations addressing the two concerns in two crates: |
| 40 | +- [wot-discovery][w-ds], a library crate, so far is able to discover HTTP/S Things using mDNS-SD. |
| 41 | +- [wot-directory][w-dr], a [wot-serve](https://crates.io/crates/wot-serve) extension. |
| 42 | + |
| 43 | +## Discovery |
| 44 | + |
| 45 | +Of the different [Introduction Mechanisms](https://www.w3.org/TR/wot-discovery/#introduction-mech) detailed, |
| 46 | +what is currently implemented is the mDNS-mediated DNS-SD system. |
| 47 | + |
| 48 | +The small API surface exposed boils down to setting up a `Discover` that starts the mDNS discovery system and provides a |
| 49 | +[Stream](https://docs.rs/futures/latest/futures/stream/trait.Stream.html) of Things by fetching them using HTTP/HTTPS. |
| 50 | + |
| 51 | +In `0.2.0`, the API supports the [wot-td][w-td] [extension](https://docs.rs/wot-td/latest/wot_td/extend/index.html) system. |
| 52 | + |
| 53 | +Since in order to consume the produced Things it is necessary to have a populated `Thing::base` and right now the |
| 54 | +specification is not yet explicit on what trasformations/patching the Directory may do with the `0.3.0` we |
| 55 | +decided to simply provide the Thing wrapped in a [Discovered](https://docs.rs/wot-discovery/latest/wot_discovery/struct.Discovered.html) |
| 56 | +struct and provide accessors to know the address and port of the Servient that provided the Thing Description. |
| 57 | + |
| 58 | +Here the example that lists all the Things available on the local network: |
| 59 | + |
| 60 | +``` rust |
| 61 | +use std::future::ready; |
| 62 | + |
| 63 | +use futures_util::StreamExt; |
| 64 | +use serde::{Deserialize, Serialize}; |
| 65 | +use tracing::{info, trace, warn}; |
| 66 | +use tracing_subscriber::EnvFilter; |
| 67 | +use wot_discovery::{Discovered, Discoverer}; |
| 68 | +use wot_td::extend::ExtendableThing; |
| 69 | + |
| 70 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 71 | +struct A {} |
| 72 | + |
| 73 | +impl ExtendableThing for A { |
| 74 | + type InteractionAffordance = (); |
| 75 | + type PropertyAffordance = (); |
| 76 | + type ActionAffordance = (); |
| 77 | + type EventAffordance = (); |
| 78 | + type Form = (); |
| 79 | + type ExpectedResponse = (); |
| 80 | + type DataSchema = (); |
| 81 | + type ObjectSchema = (); |
| 82 | + type ArraySchema = (); |
| 83 | +} |
| 84 | + |
| 85 | +#[tokio::main] |
| 86 | +async fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 87 | + let filter = EnvFilter::try_from_default_env() |
| 88 | + .or_else(|_| EnvFilter::try_new("info")) |
| 89 | + .unwrap(); |
| 90 | + |
| 91 | + tracing_subscriber::fmt().with_env_filter(filter).init(); |
| 92 | + |
| 93 | + let d = Discoverer::new()?.ext::<A>(); |
| 94 | + |
| 95 | + d.stream()? |
| 96 | + .for_each(|discovered| { |
| 97 | + match discovered { |
| 98 | + Ok(Discovered { thing: t, .. }) => { |
| 99 | + info!("found {:?} {:?}", t.title, t.id,); |
| 100 | + trace!("{}", serde_json::to_string_pretty(&t).unwrap()); |
| 101 | + } |
| 102 | + Err(e) => warn!("something went wrong {:?}", e), |
| 103 | + } |
| 104 | + ready(()) |
| 105 | + }) |
| 106 | + .await; |
| 107 | + |
| 108 | + Ok(()) |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +## Directory |
| 113 | + |
| 114 | +Right now the repository is empty and as the WoT Discovery 2.0 specification start we'll implement a tiny subset of the Directory and |
| 115 | +hopefully see if there is enough consensus to support small query languages along with richer but heavy systems such as [SPARQL][SPQL] |
| 116 | +even if [oxygraph](https://crates.io/crates/oxigraph) provides us the building blocks to provide it if there is enough interest. |
| 117 | + |
| 118 | +[Discovery]: https://www.w3.org/TR/wot-discovery |
| 119 | +[TD]: https://www.w3.org/TR/wot-thing-description |
| 120 | +[w-ds]: https://crates.io/crates/wot-discovery |
| 121 | +[w-dr]: https://crates.io/crates/wot-directory |
| 122 | +[w-td]: https://crates.io/crates/wot-td |
| 123 | +[SPQL]: https://www.w3.org/TR/sparql11-overview |
0 commit comments