From f909cbbdd3d953020e3d77ebc1004b193910f29c Mon Sep 17 00:00:00 2001 From: nm17 Date: Mon, 4 Mar 2024 13:43:45 +0400 Subject: [PATCH] feat(old_devices_api): implemented qs parsing --- src/ingest_protocol/mod.rs | 1 - src/main.rs | 4 +- src/web_server/old_devices_api/mod.rs | 84 +++++++++++++++++++++++---- 3 files changed, 75 insertions(+), 14 deletions(-) diff --git a/src/ingest_protocol/mod.rs b/src/ingest_protocol/mod.rs index e05e262..7ea31e5 100644 --- a/src/ingest_protocol/mod.rs +++ b/src/ingest_protocol/mod.rs @@ -1,7 +1,6 @@ pub mod error; mod packet_types; pub mod parser; -mod server; #[cfg(test)] mod tests; diff --git a/src/main.rs b/src/main.rs index 355943c..12c99ed 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,3 @@ -#![feature(impl_trait_in_fn_trait_return)] - /* Три датчика реалтайм: #26-94-1D-75-C2-F8 @@ -33,7 +31,7 @@ C названием и координатами: #ALT#233 ## */ - +#![feature(try_blocks)] extern crate core; mod hashes; diff --git a/src/web_server/old_devices_api/mod.rs b/src/web_server/old_devices_api/mod.rs index e19be14..1410e13 100644 --- a/src/web_server/old_devices_api/mod.rs +++ b/src/web_server/old_devices_api/mod.rs @@ -1,6 +1,9 @@ +use std::collections::HashSet; +use std::num::ParseFloatError; +use std::str::FromStr; use crate::ingest_protocol::error::Error; use crate::ingest_protocol::parser::parse_mac_address; -use crate::ingest_protocol::{NMDeviceDataPacket, NMJsonPacket}; +use crate::ingest_protocol::{NMDeviceDataPacket, NMJsonPacket, SensorValue}; use crate::web_server::app_error::AppError; @@ -8,6 +11,10 @@ use ntex::http::{HttpMessage, StatusCode}; use ntex::util::{Bytes, HashMap}; use ntex::{http, web}; use std::sync::Arc; +use fred::bytes_utils::Str; +use hifitime::efmt::consts::ISO8601; +use hifitime::Epoch; +use rust_decimal::Decimal; use thiserror::Error; /// В иделае было бы хорошо сделать всё как у [serde_json::Error], но это слишком большая морока @@ -17,6 +24,13 @@ pub enum QSParserError { SerdeQSError(#[from] Arc), #[error("asd")] ParsingError(String), + + #[error("asd")] + FloatParseError(#[from] ParseFloatError), + + #[error("failed to parse into decimal")] + DecimalParseError(#[from] rust_decimal::Error), + #[error("asd")] NoMAC, } @@ -33,8 +47,45 @@ impl From for QSParserError { } } +pub fn qs_rest_to_values(parsed: HashMap) -> Result, QSParserError> { + let mut hashset = HashSet::new(); + + for (key, value) in parsed { + hashset.insert(SensorValue { + mac: key, + value: Decimal::from_str(value.as_str())?, + + time: None, + unit: None, + name: None, + }); + } + + Ok(hashset) +} + +pub fn parse_decimal_if_exists(parsed: &mut HashMap, key: &str) -> Result, QSParserError> { + return if let Some(unwrapped_value) = parsed.remove(key) { + Ok(Some( + Decimal::from_str(unwrapped_value.as_str())? + )) + } else { + Ok(None) + } +} + +pub fn parse_epoch_if_exists(parsed: &mut HashMap, key: &str) -> Result, QSParserError> { + return if let Some(unwrapped_value) = parsed.remove(key) { + Ok(Some( + Epoch::from_unix_seconds(unwrapped_value.parse()?) + )) + } else { + Ok(None) + } +} + pub async fn parse_nm_qs_format(input: &str) -> Result { - let parsed: HashMap<&str, &str> = serde_qs::from_str(input)?; + let mut parsed: HashMap = serde_qs::from_str(input)?; let (_, _device_mac) = if let Some(id) = parsed.get("ID") { parse_mac_address(id)? @@ -42,15 +93,28 @@ pub async fn parse_nm_qs_format(input: &str) -> Result