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
|
mod common;
use common::AsyncTcp;
use embassy_executor::{Executor, Spawner};
use embassy_time::Timer;
use static_cell::StaticCell;
static RESOURCES: StaticCell<embassy_ha::DeviceResources> = StaticCell::new();
#[embassy_executor::task]
async fn main_task(spawner: Spawner) {
let mut stream = AsyncTcp::connect(std::env!("MQTT_ADDRESS"));
let mut device = embassy_ha::new(
RESOURCES.init(Default::default()),
embassy_ha::DeviceConfig {
device_id: "example-device-id",
device_name: "Example Device Name",
manufacturer: "Example Device Manufacturer",
model: "Example Device Model",
},
);
let tracker = embassy_ha::create_device_tracker(
&device,
"device-tracker-id",
embassy_ha::DeviceTrackerConfig {
common: embassy_ha::EntityCommonConfig {
name: Some("Device Tracker Name"),
..Default::default()
},
},
);
spawner.must_spawn(tracker_task(tracker));
embassy_ha::run(&mut device, &mut stream).await.unwrap();
}
#[embassy_executor::task]
async fn tracker_task(mut tracker: embassy_ha::DeviceTracker<'static>) {
let locations = [
embassy_ha::DeviceTrackerLocation {
latitude: 38.72197768549349,
longitude: -9.195954862428767,
accuracy: None,
},
embassy_ha::DeviceTrackerLocation {
latitude: 38.72253035645279,
longitude: -9.179484976517816,
accuracy: None,
},
embassy_ha::DeviceTrackerLocation {
latitude: 38.72962258768138,
longitude: -9.195895830579625,
accuracy: None,
},
];
let mut idx = 0;
loop {
tracker.publish(locations[idx]);
idx = (idx + 1) % locations.len();
Timer::after_secs(1).await;
}
}
example_main!();
|