This commit is contained in:
2023-09-24 15:04:44 +00:00
parent 3b8926fb61
commit 25655b7bcb
5 changed files with 39 additions and 2 deletions

View File

@@ -7,6 +7,8 @@ services:
volumes:
- ..:/workspace:cached
command: sleep infinity
ports:
- 1234:8080
db:
image: postgres
@@ -16,6 +18,7 @@ services:
POSTGRES_DB: iot
ports:
- 5432:5432
restart: always
command: ["postgres", "-c", "log_statement=all"]
adminer:

View File

@@ -0,0 +1 @@
name: Pull Request Created Action

View File

@@ -4,7 +4,7 @@ use log::{error, info};
use sqlx::{migrate, pool, postgres::PgPoolOptions, query, query_as, PgPool, Pool, Postgres};
use thiserror::Error;
use crate::schemas::{TelemetryMessage, TelemetryMessageFromDevice, ValueMessageFromDevice, ValueMessage};
use crate::schemas::{TelemetryMessage, TelemetryMessageFromDevice, ValueMessageFromDevice, ValueMessage, Device};
#[derive(Clone)]
pub struct Database {
@@ -140,4 +140,17 @@ impl Database {
Ok(values)
}
pub async fn get_devices(&self) -> Result<Vec<Device>, DatabaseError> {
info!("Getting all devices");
let devices = query_as!(
Device,
"SELECT id, display_name
FROM Devices;",
)
.fetch_all(&self.conn_pool)
.await?;
Ok(devices)
}
}

View File

@@ -84,6 +84,19 @@ async fn get_value(device_id: web::Path<String>, data: web::Data<AppState>) -> i
HttpResponse::Ok().json(messages)
}
#[get("/device/")]
async fn get_devices(data: web::Data<AppState>) -> impl Responder {
info!("GET - devices - Processing");
let devices = match data.db.get_devices().await {
Ok(devs) => devs,
Err(e) => {
error!("Getting Devices from DB failed \n{}", e);
return HttpResponse::InternalServerError().finish();
}
};
HttpResponse::Ok().json(devices)
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
env_logger::init();
@@ -100,8 +113,9 @@ async fn main() -> std::io::Result<()> {
.service(get_telemetry)
.service(receive_value)
.service(get_value)
.service(get_devices)
})
.bind(("127.0.0.1", 8080))?
.bind(("0.0.0.0", 8080))?
.run()
.await
}

View File

@@ -31,3 +31,9 @@ pub struct ValueMessage {
pub value_id: i32,
pub timestamp: NaiveDateTime,
}
#[derive(Deserialize, Debug, Serialize)]
pub struct Device {
pub display_name: Option<String>,
pub id: String
}