update endpoint

This commit is contained in:
Mika Bomm 2024-10-14 18:08:28 +02:00
parent abdc45a799
commit fe65349b54
4 changed files with 68 additions and 22 deletions

View file

@ -0,0 +1,25 @@
meta {
name: update node
type: http
seq: 4
}
put {
url: http://localhost:8080/api/v1/nodes/:id
body: json
auth: none
}
params:path {
id: 04-7c-16-06-b3-53
}
body:json {
{
"coord_la":1,
"coord_lo":2,
"battery_minimum":99,
"battery_maximum":9,
"group":"32e0f9af-867b-4888-8261-cb99dfff5675"
}
}

View file

@ -1,10 +1,12 @@
use crate::AppState; use crate::AppState;
use actix_web::web::Path;
use actix_web::{ use actix_web::{
error::{ErrorBadRequest, ErrorInternalServerError}, error::{ErrorBadRequest, ErrorInternalServerError},
web, Responder, web, Responder,
}; };
use chrono::Utc; use chrono::Utc;
use entity::{node, node_group, sensor_data}; use entity::{node, node_group, sensor_data};
use eui48::ParseError;
use sea_orm::{entity::*, query::*, ActiveModelTrait, ActiveValue, EntityTrait}; use sea_orm::{entity::*, query::*, ActiveModelTrait, ActiveValue, EntityTrait};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -29,13 +31,18 @@ pub struct NodeWithMac {
group: uuid::Uuid, group: uuid::Uuid,
} }
#[derive(Deserialize, Serialize)]
pub struct UpdateNode {
coord_la: f64,
coord_lo: f64,
battery_minimum: f64,
battery_maximum: f64,
group: uuid::Uuid,
}
impl From<node::Model> for NodeWithMac { impl From<node::Model> for NodeWithMac {
fn from(value: node::Model) -> Self { fn from(value: node::Model) -> Self {
let mac_id_bytes = value.id.to_be_bytes(); let mac = convert_id_to_mac(&value.id);
let mut mac_bytes: [u8; 6] = [0; 6];
mac_bytes.copy_from_slice(&mac_id_bytes[2..]);
let mac = eui48::MacAddress::new(mac_bytes).to_string(eui48::MacAddressFormat::Canonical);
Self { Self {
id: mac, id: mac,
coord_la: value.coord_la, coord_la: value.coord_la,
@ -51,13 +58,9 @@ impl TryInto<node::Model> for NodeWithMac {
type Error = eui48::ParseError; type Error = eui48::ParseError;
fn try_into(self) -> Result<node::Model, Self::Error> { fn try_into(self) -> Result<node::Model, Self::Error> {
let mac = eui48::MacAddress::parse_str(&self.id)?; let mac = convert_mac_to_id(&self.id)?;
let mac_bytes = mac.to_array();
let mut mac_id_bytes: [u8; 8] = [0; 8];
mac_id_bytes[2..].copy_from_slice(&mac_bytes);
let mac_id = i64::from_be_bytes(mac_id_bytes);
Ok(node::Model { Ok(node::Model {
id: mac_id, id: mac,
coord_la: self.coord_la, coord_la: self.coord_la,
coord_lo: self.coord_lo, coord_lo: self.coord_lo,
battery_minimum: self.battery_minimum, battery_minimum: self.battery_minimum,
@ -67,6 +70,21 @@ impl TryInto<node::Model> for NodeWithMac {
} }
} }
pub fn convert_mac_to_id(mac: &str) -> Result<i64, ParseError> {
let mac = eui48::MacAddress::parse_str(mac)?;
let mac_bytes = mac.to_array();
let mut mac_id_bytes: [u8; 8] = [0; 8];
mac_id_bytes[2..].copy_from_slice(&mac_bytes);
Ok(i64::from_be_bytes(mac_id_bytes))
}
pub fn convert_id_to_mac(id: &i64) -> String {
let mac_id_bytes = id.to_be_bytes();
let mut mac_bytes: [u8; 6] = [0; 6];
mac_bytes.copy_from_slice(&mac_id_bytes[2..]);
eui48::MacAddress::new(mac_bytes).to_string(eui48::MacAddressFormat::Canonical)
}
#[derive(Serialize)] #[derive(Serialize)]
struct GroupWithNode { struct GroupWithNode {
#[serde(flatten)] #[serde(flatten)]
@ -172,28 +190,30 @@ pub async fn create_node(
Ok(web::Json(result)) Ok(web::Json(result))
} }
/*
pub async fn update_node( pub async fn update_node(
state: web::Data<AppState>, state: web::Data<AppState>,
node: web::Json<NodeWithMac>, node: web::Json<UpdateNode>,
path: Path<Uuid>, path: Path<String>,
) -> actix_web::Result<impl Responder> { ) -> actix_web::Result<impl Responder> {
let db = &state.db; let db = &state.db;
let node: NodeWithMac = node.into_inner(); let node = node.into_inner();
let id = path.into_inner(); let id = path.into_inner();
let node: = entity::node::ActiveModel { let mac_id = convert_mac_to_id(&id).map_err(ErrorBadRequest)?;
id: ActiveValue::NotSet, let node = entity::node::ActiveModel {
coord_la: ActiveValue::Set(node.coord_la), id: ActiveValue::Unchanged(mac_id),
coord_lo: ActiveValue::Set(node.coord_lo),
battery_minimum: ActiveValue::Set(node.battery_minimum), battery_minimum: ActiveValue::Set(node.battery_minimum),
battery_maximum: ActiveValue::Set(node.battery_maximum), battery_maximum: ActiveValue::Set(node.battery_maximum),
coord_la: ActiveValue::Set(node.coord_la),
coord_lo: ActiveValue::Set(node.coord_lo),
group: ActiveValue::Set(node.group), group: ActiveValue::Set(node.group),
}; };
Ok(web::Json(node)) let result = node.update(db).await.map_err(ErrorInternalServerError)?;
Ok(web::Json(result))
} }
*/
/* /*
pub async fn delete_node( pub async fn delete_node(
state: web::Data<AppState>, state: web::Data<AppState>,

View file

@ -54,7 +54,7 @@ async fn main() -> std::io::Result<()> {
loop { loop {
if let Ok(_) = stream.read(&mut buffer).await { if let Ok(_) = stream.read(&mut buffer).await {
println!("{:#x?}", &buffer); println!("{:#x?}", &buffer);
if let Ok((_, mut value)) = Data::from_bytes((&buffer, 0)) { if let Ok((_, value)) = Data::from_bytes((&buffer, 0)) {
println!("Received: {:#?}", value); println!("Received: {:#?}", value);
let mut mac = value.mac; let mut mac = value.mac;

View file

@ -19,6 +19,7 @@ pub fn config(cfg: &mut web::ServiceConfig) {
.get(node::get_nodes) .get(node::get_nodes)
.post(node::create_node), .post(node::create_node),
) )
.service(web::resource("/nodes/{id}").put(node::update_node))
.service(web::resource("/data").get(node::get_data)) .service(web::resource("/data").get(node::get_data))
//.service(web::resource("/nodes/{id}").delete(node::delete_node)) //.service(web::resource("/nodes/{id}").delete(node::delete_node))
.service(web::resource("/groups").post(node::create_group)), .service(web::resource("/groups").post(node::create_group)),