Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using a UUID-based URN as the ID in the device's TD #6

Merged
merged 3 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ picoserve = { version = "0.12.2", features = ["embassy", "alloc"] }
embassy-sync = "0.6.0"
wot-td = { version = "0.6.2", default-features = false, features = ["alloc"] }
serde_json = { version = "1.0.133", default-features = false, features = ["alloc"] }
uuid = { version = "1.11.0", default-features = false }
const-random = "0.1.15"

[patch.crates-io]
# TODO: Remove this override once https://github.com/tkaitchuck/constrandom/issues/36 has been resolved
const-random-macro = { git = 'https://github.com/tkaitchuck/constrandom.git', rev = "4f71cb510e77eb6a26f8c7296c17811d0416fd41"}

[features]
default = ["uuid-id"]
uuid-id = []

[profile.dev]
# Rust debug is too slow.
Expand Down
24 changes: 21 additions & 3 deletions src/bin/thermometer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

extern crate alloc;

use alloc::{format, string::String};
use alloc::{
format,
string::{String, ToString},
};
use const_random::const_random;
use embassy_executor::Spawner;
use embassy_net::{Stack, StackResources};
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, mutex::Mutex};
Expand Down Expand Up @@ -33,6 +37,7 @@ use picoserve::{
routing::get,
};
use shtcx::{self, sensor_class::Sht2Gen, shtc3, PowerMode, ShtCx};
use uuid::Builder;
use wot_td::{builder::*, Thing};

#[derive(Clone, Copy)]
Expand All @@ -48,6 +53,8 @@ type AppRouter = impl picoserve::routing::PathRouter<AppState>;

const WEB_TASK_POOL_SIZE: usize = 1;

const UUID_SEED: [u8; 16] = const_random!([u8; 16]);

#[embassy_executor::task(pool_size = WEB_TASK_POOL_SIZE)]
async fn web_task(
id: usize,
Expand Down Expand Up @@ -86,6 +93,12 @@ macro_rules! mk_static {
const SSID: &str = env!("SSID");
const PASSWORD: &str = env!("PASSWORD");

fn generate_uuid_urn() -> alloc::string::String {
let uuid = Builder::from_random_bytes(UUID_SEED).into_uuid();

uuid.urn().to_string()
}

#[esp_hal_embassy::main]
async fn main(spawner: Spawner) {
esp_println::logger::init_logger_from_env();
Expand Down Expand Up @@ -173,11 +186,16 @@ async fn main(spawner: Spawner) {

let sht = mk_static!(ShtCx<Sht2Gen, &'static mut I2c<'static, Blocking, AnyI2c>>, shtc3(i2c));

let device_id = stack.hardware_address();
let id = if cfg!(feature = "uuid-id") {
generate_uuid_urn()
} else {
let device_id = stack.hardware_address().to_string();
format!("urn:example/shtc3/{device_id}")
};

let td = Thing::builder("shtc3")
.finish_extend()
.id(format!("urn:example/shtc3/{device_id}"))
.id(id)
.base(base_uri)
.description("Example Thing exposing a shtc3 sensor")
.security(|builder| builder.no_sec().required().with_key("nosec_sc"))
Expand Down