-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathswitch.rs
50 lines (42 loc) · 1.29 KB
/
switch.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use tokio;
use hap::{
accessory::{switch::SwitchAccessory, AccessoryCategory, AccessoryInformation},
server::{IpServer, Server},
storage::{FileStorage, Storage},
Config,
MacAddress,
Pin,
Result,
};
#[tokio::main]
async fn main() -> Result<()> {
let switch = SwitchAccessory::new(1, AccessoryInformation {
name: "Acme Switch".into(),
..Default::default()
})?;
let mut storage = FileStorage::current_dir().await?;
let config = match storage.load_config().await {
Ok(mut config) => {
config.redetermine_local_ip();
storage.save_config(&config).await?;
config
},
Err(_) => {
let config = Config {
pin: Pin::new([1, 1, 1, 2, 2, 3, 3, 3])?,
name: "Acme Switch".into(),
device_id: MacAddress::from([10, 20, 30, 40, 50, 60]),
category: AccessoryCategory::Switch,
..Default::default()
};
storage.save_config(&config).await?;
config
},
};
let server = IpServer::new(config, storage).await?;
server.add_accessory(switch).await?;
let handle = server.run_handle();
std::env::set_var("RUST_LOG", "hap=debug");
env_logger::init();
handle.await
}