forked from ewilken/hap-rs
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetting_values_after_server_start.rs
More file actions
79 lines (62 loc) · 2.29 KB
/
setting_values_after_server_start.rs
File metadata and controls
79 lines (62 loc) · 2.29 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use tokio;
use hap::{
accessory::{motion_sensor::MotionSensorAccessory, AccessoryCategory, AccessoryInformation},
serde_json::Value,
server::{IpServer, Server},
storage::{FileStorage, Storage},
Config,
HapType,
MacAddress,
Pin,
Result,
};
#[tokio::main]
async fn main() -> Result<()> {
let sensor = MotionSensorAccessory::new(1, AccessoryInformation {
name: "Acme Sensor".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 Sensor".into(),
device_id: MacAddress::from([10, 20, 30, 40, 50, 63]),
category: AccessoryCategory::Sensor,
..Default::default()
};
storage.save_config(&config).await?;
config
},
};
let server = IpServer::new(config, storage).await?;
let sensor_ptr = server.add_accessory(sensor).await?;
let handle = server.run_handle();
let value_set_interval = async move {
let mut interval = tokio::time::interval(std::time::Duration::from_secs(2));
tokio::time::sleep(std::time::Duration::from_secs(60)).await;
loop {
interval.tick().await;
let mut motion_sensor_accessory = sensor_ptr.lock().await;
let motion_sensor_service = motion_sensor_accessory.get_mut_service(HapType::MotionSensor).unwrap();
let motion_detected_characteristic = motion_sensor_service
.get_mut_characteristic(HapType::MotionDetected)
.unwrap();
motion_detected_characteristic.set_value(Value::Bool(true)).await?;
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
motion_detected_characteristic.set_value(Value::Bool(false)).await?;
}
#[allow(unreachable_code)]
Ok(())
};
std::env::set_var("RUST_LOG", "hap=debug");
env_logger::init();
futures::try_join!(handle, value_set_interval)?;
Ok(())
}