device.rs
1 use crate::api::{DeviceStatusEnvelope, SleepConfigResponse}; 2 use reqwest::Error; 3 use serde_json::json; 4 5 pub struct DeviceService; 6 7 impl DeviceService { 8 pub async fn get_status(base_url: &str) -> Result<DeviceStatusEnvelope, Error> { 9 reqwest::get(format!("{base_url}/api/system/device/status")) 10 .await? 11 .json() 12 .await 13 } 14 15 pub async fn get_status_for_location(base_url: &str, location: &str) -> Result<DeviceStatusEnvelope, Error> { 16 let location = urlencoding::encode(location); 17 reqwest::get(format!("{base_url}/api/system/device/status?location={location}")) 18 .await? 19 .json() 20 .await 21 } 22 23 pub async fn update_sleep_config( 24 base_url: &str, 25 enabled: bool, 26 duration_seconds: u64, 27 ) -> Result<SleepConfigResponse, Error> { 28 reqwest::Client::new() 29 .post(format!("{base_url}/api/system/sleep/config")) 30 .json(&json!({ 31 "enabled": enabled, 32 "duration_seconds": duration_seconds, 33 })) 34 .send() 35 .await? 36 .json() 37 .await 38 } 39 40 pub async fn trigger_sleep(base_url: &str) -> Result<SleepConfigResponse, Error> { 41 reqwest::Client::new() 42 .post(format!("{base_url}/api/system/sleep/actions/trigger")) 43 .send() 44 .await? 45 .json() 46 .await 47 } 48 }