Compare commits

...

2 commits

Author SHA1 Message Date
f909cbbdd3
feat(old_devices_api): implemented qs parsing 2024-03-04 13:43:45 +04:00
5f6a7866d3
fix: update ahash 2024-03-04 13:41:28 +04:00
4 changed files with 77 additions and 16 deletions

4
Cargo.lock generated
View file

@ -4,9 +4,9 @@ version = 3
[[package]]
name = "ahash"
version = "0.7.6"
version = "0.7.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47"
checksum = "891477e0c6a8957309ee5c45a6368af3ae14bb510732d2684ffa19af310920f9"
dependencies = [
"getrandom",
"once_cell",

View file

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

View file

@ -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;

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::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<serde_qs::Error>),
#[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<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> {
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") {
parse_mac_address(id)?
@ -42,15 +93,28 @@ pub async fn parse_nm_qs_format(input: &str) -> Result<NMDeviceDataPacket, QSPar
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 {
mac: [0u8; 6],
name: None,
values: Default::default(),
owner: None,
lat: None,
lon: None,
alt: None,
time: None,
mac: _device_mac,
name: parsed.remove("name").map(|v| v.to_owned()),
owner: parsed.remove("owner").map(|v| v.to_owned()),
lat: parse_decimal_if_exists(&mut parsed, "lat")?,
lon: parse_decimal_if_exists(&mut parsed, "lon")?,
alt: parse_decimal_if_exists(&mut parsed, "alt")?,
time: parse_epoch_if_exists(&mut parsed, "time")?,
values: qs_rest_to_values(parsed)?,
};
Ok(device_data)