diff options
Diffstat (limited to 'examples/device_tracker.rs')
| -rw-r--r-- | examples/device_tracker.rs | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/examples/device_tracker.rs b/examples/device_tracker.rs new file mode 100644 index 0000000..7f02333 --- /dev/null +++ b/examples/device_tracker.rs | |||
| @@ -0,0 +1,68 @@ | |||
| 1 | mod common; | ||
| 2 | |||
| 3 | use common::AsyncTcp; | ||
| 4 | use embassy_executor::{Executor, Spawner}; | ||
| 5 | use embassy_time::Timer; | ||
| 6 | use static_cell::StaticCell; | ||
| 7 | |||
| 8 | static RESOURCES: StaticCell<embassy_ha::DeviceResources> = StaticCell::new(); | ||
| 9 | |||
| 10 | #[embassy_executor::task] | ||
| 11 | async fn main_task(spawner: Spawner) { | ||
| 12 | let mut stream = AsyncTcp::connect(std::env!("MQTT_ADDRESS")); | ||
| 13 | |||
| 14 | let mut device = embassy_ha::new( | ||
| 15 | RESOURCES.init(Default::default()), | ||
| 16 | embassy_ha::DeviceConfig { | ||
| 17 | device_id: "example-device-id", | ||
| 18 | device_name: "Example Device Name", | ||
| 19 | manufacturer: "Example Device Manufacturer", | ||
| 20 | model: "Example Device Model", | ||
| 21 | }, | ||
| 22 | ); | ||
| 23 | |||
| 24 | let tracker = embassy_ha::create_device_tracker( | ||
| 25 | &device, | ||
| 26 | "device-tracker-id", | ||
| 27 | embassy_ha::DeviceTrackerConfig { | ||
| 28 | common: embassy_ha::EntityCommonConfig { | ||
| 29 | name: Some("Device Tracker Name"), | ||
| 30 | ..Default::default() | ||
| 31 | }, | ||
| 32 | }, | ||
| 33 | ); | ||
| 34 | |||
| 35 | spawner.must_spawn(tracker_task(tracker)); | ||
| 36 | |||
| 37 | embassy_ha::run(&mut device, &mut stream).await.unwrap(); | ||
| 38 | } | ||
| 39 | |||
| 40 | #[embassy_executor::task] | ||
| 41 | async fn tracker_task(mut tracker: embassy_ha::DeviceTracker<'static>) { | ||
| 42 | let locations = [ | ||
| 43 | embassy_ha::DeviceTrackerLocation { | ||
| 44 | latitude: 38.72197768549349, | ||
| 45 | longitude: -9.195954862428767, | ||
| 46 | accuracy: None, | ||
| 47 | }, | ||
| 48 | embassy_ha::DeviceTrackerLocation { | ||
| 49 | latitude: 38.72253035645279, | ||
| 50 | longitude: -9.179484976517816, | ||
| 51 | accuracy: None, | ||
| 52 | }, | ||
| 53 | embassy_ha::DeviceTrackerLocation { | ||
| 54 | latitude: 38.72962258768138, | ||
| 55 | longitude: -9.195895830579625, | ||
| 56 | accuracy: None, | ||
| 57 | }, | ||
| 58 | ]; | ||
| 59 | |||
| 60 | let mut idx = 0; | ||
| 61 | loop { | ||
| 62 | tracker.publish(locations[idx]); | ||
| 63 | idx = (idx + 1) % locations.len(); | ||
| 64 | Timer::after_secs(1).await; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | example_main!(); | ||
