feat(old_devices_api): implemented qs parsing

This commit is contained in:
nm17 2024-03-04 13:43:45 +04:00
parent 5f6a7866d3
commit f909cbbdd3
Signed by: nm17
GPG key ID: 3303B70C59145CD4
3 changed files with 75 additions and 14 deletions

View file

@ -1,7 +1,6 @@
pub mod error; pub mod error;
mod packet_types; mod packet_types;
pub mod parser; pub mod parser;
mod server;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;

View file

@ -1,5 +1,3 @@
#![feature(impl_trait_in_fn_trait_return)]
/* /*
Три датчика реалтайм: Три датчика реалтайм:
#26-94-1D-75-C2-F8 #26-94-1D-75-C2-F8
@ -33,7 +31,7 @@ C названием и координатами:
#ALT#233 #ALT#233
## ##
*/ */
#![feature(try_blocks)]
extern crate core; extern crate core;
mod hashes; mod hashes;

View file

@ -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::error::Error;
use crate::ingest_protocol::parser::parse_mac_address; 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; use crate::web_server::app_error::AppError;
@ -8,6 +11,10 @@ use ntex::http::{HttpMessage, StatusCode};
use ntex::util::{Bytes, HashMap}; use ntex::util::{Bytes, HashMap};
use ntex::{http, web}; use ntex::{http, web};
use std::sync::Arc; use std::sync::Arc;
use fred::bytes_utils::Str;
use hifitime::efmt::consts::ISO8601;
use hifitime::Epoch;
use rust_decimal::Decimal;
use thiserror::Error; use thiserror::Error;
/// В иделае было бы хорошо сделать всё как у [serde_json::Error], но это слишком большая морока /// В иделае было бы хорошо сделать всё как у [serde_json::Error], но это слишком большая морока
@ -17,6 +24,13 @@ pub enum QSParserError {
SerdeQSError(#[from] Arc<serde_qs::Error>), SerdeQSError(#[from] Arc<serde_qs::Error>),
#[error("asd")] #[error("asd")]
ParsingError(String), ParsingError(String),
#[error("asd")]
FloatParseError(#[from] ParseFloatError),
#[error("failed to parse into decimal")]
DecimalParseError(#[from] rust_decimal::Error),
#[error("asd")] #[error("asd")]
NoMAC, NoMAC,
} }
@ -33,8 +47,45 @@ impl From<serde_qs::Error> for QSParserError {
} }
} }
pub fn qs_rest_to_values(parsed: HashMap<String, String>) -> Result<HashSet<SensorValue>, 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<String, String>, key: &str) -> Result<Option<Decimal>, 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<String, String>, key: &str) -> Result<Option<Epoch>, 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<NMDeviceDataPacket, QSParserError> { pub async fn parse_nm_qs_format(input: &str) -> Result<NMDeviceDataPacket, QSParserError> {
let parsed: HashMap<&str, &str> = serde_qs::from_str(input)?; let mut parsed: HashMap<String, String> = serde_qs::from_str(input)?;
let (_, _device_mac) = if let Some(id) = parsed.get("ID") { let (_, _device_mac) = if let Some(id) = parsed.get("ID") {
parse_mac_address(id)? parse_mac_address(id)?
@ -42,15 +93,28 @@ pub async fn parse_nm_qs_format(input: &str) -> Result<NMDeviceDataPacket, QSPar
return Err(QSParserError::NoMAC); return Err(QSParserError::NoMAC);
}; };
for keys in parsed.keys() {
if keys.starts_with("") {
}
}
let lat = if let Some(lat) = parsed.remove("lat") {
Some(Decimal::from_str(lat.as_str())?)
} else {
None
};
let device_data = NMDeviceDataPacket { let device_data = NMDeviceDataPacket {
mac: [0u8; 6], mac: _device_mac,
name: None, name: parsed.remove("name").map(|v| v.to_owned()),
values: Default::default(), owner: parsed.remove("owner").map(|v| v.to_owned()),
owner: None, lat: parse_decimal_if_exists(&mut parsed, "lat")?,
lat: None, lon: parse_decimal_if_exists(&mut parsed, "lon")?,
lon: None, alt: parse_decimal_if_exists(&mut parsed, "alt")?,
alt: None, time: parse_epoch_if_exists(&mut parsed, "time")?,
time: None,
values: qs_rest_to_values(parsed)?,
}; };
Ok(device_data) Ok(device_data)