From de396ed05f8ac9dec7a3edaa0b4ae5a847e49462 Mon Sep 17 00:00:00 2001 From: nm17 Date: Thu, 30 May 2024 14:28:42 +0400 Subject: [PATCH] feat(old_device_sensor_api): commit to a different key naming convention --- Cargo.toml | 4 +-- docs/kv_db_arch.md | 5 ++- src/ingest_protocol/packet_types.rs | 1 - src/ingest_protocol/parser.rs | 1 - src/web_server/app_error.rs | 10 +++--- src/web_server/old_device_sensor_api/mod.rs | 40 ++++++++++++--------- 6 files changed, 34 insertions(+), 27 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 00757bd..e76204a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,7 @@ derive_more = "0.99.17" dotenvy = "0.15.7" fred = { version = "6.3.0", features = ["nom"] } heapless = { version = "0.7.16", features = ["ufmt-impl"] } -hex = { version = "0.4.3", default-features = false } +hex = { version = "0.4.3", default-features = false, features = ["std", "alloc"] } hifitime = "3.8.2" lazy_static = "1.4.0" nom = { version = "7.1.3", default-features = false, features = ["std", "alloc"] } @@ -30,4 +30,4 @@ serde_with = { version = "3.6.1", features = ["hex"] } smallstr = { version = "0.3.0", features = ["std", "union"] } thiserror = "1.0.40" tokio = { version = "1.28.2", features = ["full"] } -ufmt = "0.2.0" +ufmt = { version = "0.2.0", features = ["std"] } diff --git a/docs/kv_db_arch.md b/docs/kv_db_arch.md index 0f35c79..74a3cff 100644 --- a/docs/kv_db_arch.md +++ b/docs/kv_db_arch.md @@ -8,9 +8,8 @@ - Поля - Вся информация о девайсе - `devices_{device_id}_{tai_timestamp}_{sensor_id}` - - Поля - - `value` - - `unit` + - Только значение - `devices_{device_id}` - Поля - `exists`: bool + - `unit`: str diff --git a/src/ingest_protocol/packet_types.rs b/src/ingest_protocol/packet_types.rs index a8538cd..e1c07b5 100644 --- a/src/ingest_protocol/packet_types.rs +++ b/src/ingest_protocol/packet_types.rs @@ -13,7 +13,6 @@ use crate::ingest_protocol::parser::parse_mac_address; use hifitime::Epoch; use rust_decimal::Decimal; use serde::{Deserialize, Serialize}; -use serde_with::formats::Separator; use serde_with::serde_as; use std::collections::HashSet; diff --git a/src/ingest_protocol/parser.rs b/src/ingest_protocol/parser.rs index 2b4fdfe..ffc4133 100644 --- a/src/ingest_protocol/parser.rs +++ b/src/ingest_protocol/parser.rs @@ -3,7 +3,6 @@ use nom::bytes::complete::tag; use nom::bytes::complete::take_until1; use nom::bytes::complete::{take, take_while, take_while1}; use nom::character::complete::hex_digit1; -use nom::{Parser}; use std::str::FromStr; use crate::ingest_protocol::error::Error; diff --git a/src/web_server/app_error.rs b/src/web_server/app_error.rs index aa9ea10..38bcad5 100644 --- a/src/web_server/app_error.rs +++ b/src/web_server/app_error.rs @@ -1,16 +1,13 @@ use derive_more::Display; use fred::prelude::*; -use ntex::http::{StatusCode}; +use ntex::http::StatusCode; use ntex::web; use ntex::web::{HttpRequest, HttpResponse}; - - use thiserror::Error; - use crate::insert_header; use crate::web_server::old_device_sensor_api::qs_parser::QSParserError; use rust_decimal::Decimal; @@ -51,6 +48,9 @@ pub enum AppError { json_err: Option, query_error: Option, }, + + #[display(fmt = "IDK")] + DeviceNotFound(String) } impl web::error::WebResponseError for AppError { @@ -64,6 +64,7 @@ impl web::error::WebResponseError for AppError { AppError::ApiKeyInvalid { .. } => StatusCode::BAD_REQUEST, AppError::UnknownBody { .. } => StatusCode::BAD_REQUEST, AppError::QSError(..) => StatusCode::BAD_REQUEST, + AppError::DeviceNotFound(..) => StatusCode::BAD_REQUEST } } @@ -79,6 +80,7 @@ impl web::error::WebResponseError for AppError { "Can't figure out where and in what encoding the main data is" } AppError::QSError(..) => "UrlEncoded body or query params are incorrect", + AppError::DeviceNotFound(..) => "Device not found", }; let status_code = self.status_code(); diff --git a/src/web_server/old_device_sensor_api/mod.rs b/src/web_server/old_device_sensor_api/mod.rs index fe109eb..9a140df 100644 --- a/src/web_server/old_device_sensor_api/mod.rs +++ b/src/web_server/old_device_sensor_api/mod.rs @@ -2,16 +2,17 @@ pub mod qs_parser; -use std::str::FromStr; use crate::ingest_protocol::{NMDeviceDataPacket, NMJsonPacket}; use crate::web_server::app_error::AppError; +use fred::types::RedisMap; use ntex::http::{HttpMessage, StatusCode}; use ntex::util::{Bytes, HashMap}; use ntex::{http, web}; use fred::prelude::*; use hifitime::Epoch; +use ntex::web::types::State; use thiserror::Error; use ufmt::uwrite; use crate::web_server::NMAppState; @@ -21,10 +22,10 @@ use crate::web_server::old_device_sensor_api::qs_parser::QSParserError; pub enum Error { #[error("Device not found")] DeviceNotFound(String), - #[error("Time sent with the device is way to behind now")] + #[error("Time sent with the device is way too behind now")] TimeIsLongBehindNow, #[error("{0}")] - QSParserError(#[from] QSParserError) + QSParserError(#[from] QSParserError), } @@ -37,7 +38,7 @@ pub enum Error { pub async fn device_handler<'a>( request: web::HttpRequest, body: Bytes, - app_state: &NMAppState, + app_state: State, ) -> Result { let mut real_body: Option = None; let mut json_error = None; @@ -79,30 +80,36 @@ pub async fn device_handler<'a>( for device in real_body.devices { let mut device_key_str = String::new(); - let device_tai_timestamp = device.time - .unwrap_or(Epoch::now().unwrap()) + let now = Epoch::now().unwrap(); + let mut device_time = device.time + .unwrap_or(now); + + // TODO: Добавить гистерезис + // Отчёт совместимости: отсутствует + if device_time > now { + device_time = now; + } + + let device_tai_timestamp = device_time .to_duration_since_j1900() .to_seconds(); - uwrite!(&mut device_key_str, "devices_{}", device.mac)?; - + uwrite!(device_key_str, "devices_{}", hex::encode(device.mac)); let device_exists: bool = app_state.redis_client.hget(device_key_str.as_str(), "exists").await?; if !device_exists { - return Err(Error::DeviceNotFound(device.mac)) + return Err(AppError::DeviceNotFound(hex::encode(&device.mac))); } // devices_{device_id}_{tai_timestamp}_{sensor_id} for sensor in device.values { let mut device_report_key_str = String::new(); - uwrite!(&mut device_report_key_str, "devices_{}_{}_{}", device.mac, device_tai_timestamp, sensor.mac)?; - let mut device_report_key_str = String::new(); + uwrite!(&mut device_report_key_str, "devices_{}_{}_{}", hex::encode(device.mac), device_tai_timestamp.to_string(), sensor.mac); - - app_state.redis_client.hset(device_key_str.as_str(), HashMap::from([ - // TODO - ])).await?; + app_state.redis_client.set( + device_key_str.as_str(), sensor.value.to_string(), None, None, false, + ).await?; } } } else { @@ -112,5 +119,6 @@ pub async fn device_handler<'a>( }); } - Ok(web::HttpResponse::build(StatusCode::OK).finish()) + Ok(web::HttpResponseBuilder::new(StatusCode::OK) + .finish()) }