Compare commits

..

No commits in common. "f909cbbdd3d953020e3d77ebc1004b193910f29c" and "8731609911fd304163ebe20b6599ff4fcf2b0d8e" have entirely different histories.

4 changed files with 16 additions and 77 deletions

4
Cargo.lock generated
View file

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

View file

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

View file

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

View file

@ -1,9 +1,6 @@
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, SensorValue};
use crate::ingest_protocol::{NMDeviceDataPacket, NMJsonPacket};
use crate::web_server::app_error::AppError;
@ -11,10 +8,6 @@ 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], но это слишком большая морока
@ -24,13 +17,6 @@ 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,
}
@ -47,45 +33,8 @@ 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 mut parsed: HashMap<String, String> = serde_qs::from_str(input)?;
let parsed: HashMap<&str, &str> = serde_qs::from_str(input)?;
let (_, _device_mac) = if let Some(id) = parsed.get("ID") {
parse_mac_address(id)?
@ -93,28 +42,15 @@ 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: _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)?,
mac: [0u8; 6],
name: None,
values: Default::default(),
owner: None,
lat: None,
lon: None,
alt: None,
time: None,
};
Ok(device_data)