summaryrefslogtreecommitdiff
path: root/src/routes
diff options
context:
space:
mode:
authorFxQnLr <[email protected]>2024-04-15 21:12:04 +0200
committerGitHub <[email protected]>2024-04-15 21:12:04 +0200
commitea4f36ea34a4e425ef23e2ae0d4ea210d4a8a145 (patch)
treed89f0da1841b984c10a6a24997a8733524b75f75 /src/routes
parentb615f6e34e084d520dcc301058b5926074188500 (diff)
parent7876dd605a8e4b595436035a87a5151be187c01d (diff)
downloadwebol-ea4f36ea34a4e425ef23e2ae0d4ea210d4a8a145.tar
webol-ea4f36ea34a4e425ef23e2ae0d4ea210d4a8a145.tar.gz
webol-ea4f36ea34a4e425ef23e2ae0d4ea210d4a8a145.zip
Merge pull request #36 from FxQnLr/optional_ip
Optional ip and better docs
Diffstat (limited to 'src/routes')
-rw-r--r--src/routes/device.rs27
-rw-r--r--src/routes/start.rs9
2 files changed, 23 insertions, 13 deletions
diff --git a/src/routes/device.rs b/src/routes/device.rs
index 49361f2..f767eab 100644
--- a/src/routes/device.rs
+++ b/src/routes/device.rs
@@ -36,7 +36,7 @@ pub struct DPayload {
36 id: String, 36 id: String,
37 mac: String, 37 mac: String,
38 broadcast_addr: String, 38 broadcast_addr: String,
39 ip: String, 39 ip: Option<String>,
40} 40}
41 41
42#[utoipa::path( 42#[utoipa::path(
@@ -48,15 +48,17 @@ pub struct DPayload {
48 ), 48 ),
49 security((), ("api_key" = [])) 49 security((), ("api_key" = []))
50)] 50)]
51pub async fn put( 51pub async fn put(Json(payload): Json<DPayload>) -> Result<Json<Value>, Error> {
52 Json(payload): Json<DPayload>,
53) -> Result<Json<Value>, Error> {
54 info!( 52 info!(
55 "add device {} ({}, {}, {})", 53 "add device {} ({}, {}, {:?})",
56 payload.id, payload.mac, payload.broadcast_addr, payload.ip 54 payload.id, payload.mac, payload.broadcast_addr, payload.ip
57 ); 55 );
58 56
59 let ip = IpNetwork::from_str(&payload.ip)?; 57 let ip = if let Some(ip_s) = payload.ip {
58 Some(IpNetwork::from_str(&ip_s)?)
59 } else {
60 None
61 };
60 let mac = MacAddress::from_str(&payload.mac)?; 62 let mac = MacAddress::from_str(&payload.mac)?;
61 let device = Device { 63 let device = Device {
62 id: payload.id, 64 id: payload.id,
@@ -79,14 +81,17 @@ pub async fn put(
79 ), 81 ),
80 security((), ("api_key" = [])) 82 security((), ("api_key" = []))
81)] 83)]
82pub async fn post( 84pub async fn post(Json(payload): Json<DPayload>) -> Result<Json<Value>, Error> {
83 Json(payload): Json<DPayload>,
84) -> Result<Json<Value>, Error> {
85 info!( 85 info!(
86 "edit device {} ({}, {}, {})", 86 "edit device {} ({}, {}, {:?})",
87 payload.id, payload.mac, payload.broadcast_addr, payload.ip 87 payload.id, payload.mac, payload.broadcast_addr, payload.ip
88 ); 88 );
89 let ip = IpNetwork::from_str(&payload.ip)?; 89
90 let ip = if let Some(ip_s) = payload.ip {
91 Some(IpNetwork::from_str(&ip_s)?)
92 } else {
93 None
94 };
90 let mac = MacAddress::from_str(&payload.mac)?; 95 let mac = MacAddress::from_str(&payload.mac)?;
91 let times = Device::read(&payload.id)?.times; 96 let times = Device::read(&payload.id)?.times;
92 97
diff --git a/src/routes/start.rs b/src/routes/start.rs
index bbc6ab8..192a54a 100644
--- a/src/routes/start.rs
+++ b/src/routes/start.rs
@@ -69,6 +69,9 @@ fn send_wol(
69 let dev_id = device.id.clone(); 69 let dev_id = device.id.clone();
70 let uuid = if let Some(pl) = payload { 70 let uuid = if let Some(pl) = payload {
71 if pl.ping.is_some_and(|ping| ping) { 71 if pl.ping.is_some_and(|ping| ping) {
72 if device.ip.is_none() {
73 return Err(Error::NoIpOnPing);
74 }
72 Some(setup_ping(state, device)) 75 Some(setup_ping(state, device))
73 } else { 76 } else {
74 None 77 None
@@ -86,8 +89,10 @@ fn send_wol(
86 89
87fn setup_ping(state: Arc<crate::AppState>, device: Device) -> String { 90fn setup_ping(state: Arc<crate::AppState>, device: Device) -> String {
88 let mut uuid: Option<String> = None; 91 let mut uuid: Option<String> = None;
92 // Safe: Only called when ip is set
93 let ip = device.ip.unwrap();
89 for (key, value) in state.ping_map.clone() { 94 for (key, value) in state.ping_map.clone() {
90 if value.ip == device.ip { 95 if value.ip == ip {
91 debug!("service already exists"); 96 debug!("service already exists");
92 uuid = Some(key); 97 uuid = Some(key);
93 break; 98 break;
@@ -103,7 +108,7 @@ fn setup_ping(state: Arc<crate::AppState>, device: Device) -> String {
103 state.ping_map.insert( 108 state.ping_map.insert(
104 uuid_gen.clone(), 109 uuid_gen.clone(),
105 PingValue { 110 PingValue {
106 ip: device.ip, 111 ip,
107 eta: get_eta(device.clone().times), 112 eta: get_eta(device.clone().times),
108 online: false, 113 online: false,
109 }, 114 },