This commit is contained in:
@@ -65,7 +65,7 @@ impl Database {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn add_display_name(
|
||||
pub async fn update_display_name(
|
||||
&self,
|
||||
device_id: &MacAddress,
|
||||
display_name: &str,
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
|
||||
use actix_web::{get, post, web, HttpResponse, Responder};
|
||||
use actix_web::{get, post, put, web, HttpResponse, Responder};
|
||||
use log::{error, info};
|
||||
use sqlx::types::mac_address::MacAddress;
|
||||
|
||||
use crate::{schemas::{AppState, TelemetryMessageFromDevice, ValueMessageFromDevice}, util::parse_mac_address};
|
||||
use crate::{schemas::{AppState, DeviceMetadata, TelemetryMessageFromDevice, ValueMessageFromDevice}, util::parse_mac_address};
|
||||
|
||||
#[post("/telemetry/{device_id}")]
|
||||
async fn receive_telemetry(
|
||||
@@ -110,4 +110,25 @@ async fn get_devices(data: web::Data<AppState>) -> impl Responder {
|
||||
}
|
||||
};
|
||||
HttpResponse::Ok().json(devices)
|
||||
}
|
||||
}
|
||||
|
||||
#[put("/device/{device_id}/display_name")]
|
||||
async fn set_display_name(data: web::Data<AppState>, device_id: web::Path<String>, device_data: web::Json<DeviceMetadata>) -> impl Responder {
|
||||
info!("PUT - device/{device_id} - Display Name", );
|
||||
let display_name = device_data.0.display_name;
|
||||
let db = &data.db;
|
||||
let Ok(mac_converted) = parse_mac_address(&device_id) else {
|
||||
error!("Failed to convert mac address");
|
||||
return HttpResponse::InternalServerError().finish();
|
||||
};
|
||||
let mac_converted = MacAddress::from(mac_converted);
|
||||
match db.update_display_name(&mac_converted, &display_name).await {
|
||||
Ok(_) => HttpResponse::Ok().finish(),
|
||||
Err(_) => {
|
||||
error!("Failed to update/set displayName for device");
|
||||
HttpResponse::InternalServerError().finish()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.app_data(web::Data::new(AppState {
|
||||
db: db.clone(),
|
||||
firmwares_path: PathBuf::from("./fw"),
|
||||
hostname: "127.0.0.1:8282".to_string(),
|
||||
hostname: "0.0.0.0:8282".to_string(),
|
||||
}))
|
||||
.app_data(web::PayloadConfig::new(256 * 1024 * 1024)) //256MB
|
||||
.service(device_telemetry_api::receive_telemetry)
|
||||
|
||||
@@ -98,4 +98,9 @@ pub struct AppState {
|
||||
pub db: Database,
|
||||
pub firmwares_path: PathBuf,
|
||||
pub hostname: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct DeviceMetadata {
|
||||
pub display_name: String,
|
||||
}
|
||||
Reference in New Issue
Block a user