Fixed stuff
All checks were successful
Build Project / test (push) Successful in 5m30s

This commit is contained in:
2025-01-15 23:10:54 +00:00
parent b9551628f7
commit e19a11d5f3
7 changed files with 127 additions and 50 deletions

View File

@@ -1,11 +1,11 @@
use std::{env, fs, process, str::FromStr};
use std::{env, fs, path::PathBuf, process, str::FromStr};
use crate::schemas::{TelemetryMessageFromDevice, ValueMessageFromDevice};
use actix_web::{get, post, put, web, App, HttpResponse, HttpServer, Responder};
use database::Database;
use dotenvy::dotenv;
use log::{error, info};
use schemas::{BoardConfig, BoardType, Device, OTAConfiguration};
use log::{debug, error, info};
use schemas::{BoardConfig, BoardType, Device, OTAConfiguration, OTAConfigurationList};
use sqlx::types::mac_address::MacAddress;
use util::parse_mac_address;
@@ -127,43 +127,49 @@ async fn get_devices(data: web::Data<AppState>) -> impl Responder {
#[put("/firmware/{product}/{config}/{version}")]
async fn upload_firmware(path: web::Path<(String, String, String)>, body: web::Bytes) -> impl Responder {
let (product, config, version) = path.into_inner();
println!("Uploading firmware version: {}", version);
let version = version.replace(".", "_");
let firmware_root_path = PathBuf::from(env::var("FIMRWARE_FOLDER").unwrap_or("./firmware".to_string()));
let fw_folder = format!("./firmware/{product}");
let firmware_folder = firmware_root_path.join(&product).join(&config);
let firmware_path = firmware_folder.join(format!("ver_{}", &version)).with_extension("bin");
info!("Uploading firmware with product: {product}, config: {config} and version: {version} to {firmware_path:?}");
fs::create_dir_all(&fw_folder).unwrap();
let file_path = format!("{fw_folder}/firmware_{config}_{version}.bin");
info!("Saving to {file_path}");
tokio::fs::write(&file_path, &body).await.unwrap();
fs::create_dir_all(&firmware_folder).unwrap();
let x = tokio::fs::write(&firmware_path, &body).await;
debug!("{x:?}");
HttpResponse::Ok().body(format!("Firmware version {} uploaded successfully", version))
}
// TODO this is more or less a placeholder. Firmware upload will be more detailed in the future
#[get("/firmware/{product}")]
async fn get_firmware_json(product: web::Path<String>) -> impl Responder {
let product = product.into_inner();
let mut configs = Vec::new();
if let Ok(entries) = fs::read_dir(format!("./firmware/{product}")) {
for entry in entries.flatten() {
let path = entry.path();
if path.is_file() {
println!("File: {:?}", path);
let split_name: Vec<_> = path.file_name().unwrap().to_str().unwrap().split("_").collect();
let version = split_name[2].strip_suffix(".bin").unwrap();
let board_config = BoardConfig::from_str(split_name[1]).unwrap();
let board_type = BoardType::from_str(&product).unwrap();
let cfg = OTAConfiguration{board: board_type, configuration: board_config, version: version.to_string(), url: path.to_str().to_owned().unwrap().to_owned() };
configs.push(cfg);
} else if path.is_dir() {
println!("Directory: {:?}", path);
}
}
} else {
return HttpResponse::InternalServerError().finish()
}
#[get("/firmware/{product}/{config}/{version}")]
async fn get_firmware_json(path: web::Path<(String, String, String)>) -> impl Responder {
let (product, config, version) = path.into_inner();
let version = version.replace(".", "_");
HttpResponse::Ok().json(configs)
let mut configs = Vec::new();
HttpResponse::Ok().json(OTAConfigurationList{configurations: configs} )
}
#[get("/firmware/waterlevel/firmware_INA233_1.0.0.bin")]
async fn serve_firmware() -> impl Responder {
let file_path = PathBuf::from("./firmware/waterlevel/firmware_INA233_1.0.0.bin");
if file_path.exists() {
// Serve the file as a download
HttpResponse::Ok()
.content_type("application/octet-stream") // Binary file MIME type
.insert_header((
"Content-Disposition",
format!("attachment; filename=\"{}\"", "firmware_INA233_1.0.0.bin"),
))
.body(std::fs::read(file_path).unwrap_or_else(|_| Vec::new()))
} else {
HttpResponse::NotFound().body("Firmware version not found")
}
}
#[actix_web::main]
@@ -195,8 +201,9 @@ async fn main() -> std::io::Result<()> {
.service(get_devices)
.service(upload_firmware)
.service(get_firmware_json)
.service(serve_firmware)
})
.bind(("0.0.0.0", 8484))?
.bind(("0.0.0.0", 8282))?
.run()
.await
}