diff --git a/Cargo.lock b/Cargo.lock index caaa7a1..5b55ec6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,15 +2,316 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "android-tzdata" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + [[package]] name = "asotr_csv" version = "0.1.0" dependencies = [ "byteorder", + "chrono", ] +[[package]] +name = "autocfg" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" + +[[package]] +name = "bumpalo" +version = "3.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" + [[package]] name = "byteorder" version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "cc" +version = "1.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13208fcbb66eaeffe09b99fffbe1af420f00a7b35aa99ad683dfc1aa76145229" +dependencies = [ + "shlex", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "chrono" +version = "0.4.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e36cc9d416881d2e24f9a963be5fb1cd90966419ac844274161d10488b3e825" +dependencies = [ + "android-tzdata", + "iana-time-zone", + "js-sys", + "num-traits", + "wasm-bindgen", + "windows-targets", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + +[[package]] +name = "iana-time-zone" +version = "0.1.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "235e081f3925a06703c2d0117ea8b91f042756fd6e7a6e5d901e8ca1a996b220" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + +[[package]] +name = "js-sys" +version = "0.3.77" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f" +dependencies = [ + "once_cell", + "wasm-bindgen", +] + +[[package]] +name = "libc" +version = "0.2.169" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" + +[[package]] +name = "log" +version = "0.4.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04cbf5b083de1c7e0222a7a51dbfdba1cbe1c6ab0b15e29fff3f6c077fd9cd9f" + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.20.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" + +[[package]] +name = "proc-macro2" +version = "1.0.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60946a68e5f9d28b0dc1c21bb8a97ee7d018a8b322fa57838ba31cc878e22d99" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rustversion" +version = "1.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7c45b9784283f1b2e7fb61b42047c2fd678ef0960d4f6f1eba131594cc369d4" + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + +[[package]] +name = "syn" +version = "2.0.96" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d5d0adab1ae378d7f53bdebc67a39f1f151407ef230f0ce2883572f5d8985c80" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" + +[[package]] +name = "wasm-bindgen" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6" +dependencies = [ + "bumpalo", + "log", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "windows-core" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/Cargo.toml b/Cargo.toml index 1f7c1e2..a3ed1a0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,3 +5,4 @@ edition = "2021" [dependencies] byteorder = "1.4.3" +chrono = "0.4" diff --git a/src/main.rs b/src/main.rs index bc16cc5..5db254d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,91 +1,162 @@ - pub mod asotr_data { use std::{fs::File, io::Read}; use byteorder::{LittleEndian, ReadBytesExt}; + use chrono::{DateTime, Utc}; + use std::time::{SystemTime, UNIX_EPOCH, Duration}; - #[derive(Debug)] - pub struct Temp { - ch: [f32; 6] - } + // #[derive(Debug)] + // pub struct Temp { + // time_s: u64, + // time_mks: u16, + // datetime: String, + // date: String, + // time: String, + // ch: [f32; 6] + // } - #[derive(Debug)] - pub struct Pow { - ch: [u16; 6] - } + // impl Temp { + // pub fn new(tstamp_s: u64, tstamp_mks: u16, chan: [f32; 6]) -> Temp { + // Temp { + // time_s: tstamp_s, + // time_mks: tstamp_mks, + // datetime: "".to_string(), + // date: "".to_string(), + // time: "".to_string(), + // ch: chan, + // } + // } + // } - fn parse_data_f32(buf: Vec) -> Result { + // #[derive(Debug)] + // pub struct Pow { + // time_s: u64, + // time_mks: u16, + // datetime: String, + // date: String, + // time: String, + // ch: [u16; 6] + // } + + // impl Pow { + // pub fn new(tstamp_s: u64, tstamp_mks: u16, chan: [u16; 6]) -> Pow { + // Pow { + // time_s: tstamp_s, + // time_mks: tstamp_mks, + // datetime: "".to_string(), + // date: "".to_string(), + // time: "".to_string(), + // ch: chan, + // } + // } + // } + + fn parse_data_f32(buf: Vec) -> Result<[f32; 6], String> { let mut data = &buf[..]; - let mut temp = Temp { ch: [0.0, 0.0, 0.0, 0.0, 0.0, 1.0] }; + let mut ch: [f32; 6] = [0.0; 6]; for i in 0..6 { - temp.ch[i] = match data.read_f32::() { + ch[i] = match data.read_f32::() { Ok(val) => val, Err(msg) => { return Err(format!("failed parsing float32 data: {}", msg)); } } } - return Ok(temp); + return Ok(ch); } - fn parse_data_u16(buf: Vec) -> Result { + fn parse_data_u16(buf: Vec) -> Result<[u16; 6], String> { let mut data = &buf[..]; - let mut pow = Pow { ch: [0, 0, 0, 0, 0, 0] }; + let mut ch: [u16; 6] = [0; 6]; for i in 0..6 { - pow.ch[i] = match data.read_u16::() { + ch[i] = match data.read_u16::() { Ok(val) => val, Err(msg) => { return Err(format!("failed parsing float32 data: {}", msg)); } } } - return Ok(pow); + return Ok(ch); } - pub fn parse_temp_file(fname: String) -> Result { - let mut buf = Vec::new(); - let _temp = Temp { ch: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0] }; + pub fn parse_data(path: String, filename: String) -> Result { + let ch_u16: [u16; 6]; + let ch_f32: [f32; 6]; - let mut data = match File::open(fname) { - Ok(file) => file, - Err(msg) => { - return Err(format!("could not open data file: {}", msg)) - } + if filename.len() != 32 { return Err(format!("Error parsing filename: Unsupported file ({})", filename)); } + + let time_str_ = filename[0..10].parse::(); + let time_str = match &time_str_ { + Ok(data) => data, + Err(msg) => { return Err(format!("Error parsing filename: expected digits in timestamp part ({})", msg)); } }; - let _st = match data.read_to_end(&mut buf) { - Ok(stat) => stat, - Err(msg) => { - return Err(format!("could not read data file: {}", msg)) - } + let binding = filename[22..24].parse::(); + let data_type = match &binding { + Ok(data) => data, + Err(msg) => { return Err(format!("Error parsing filename: expected digits in data type part ({})", msg)); } }; - let _temp = parse_data_f32(buf)?; - return Ok(_temp); + let _kit = &filename[30..32]; + let _time_str_mks = &filename[11..17]; + + if *data_type == 1 || *data_type == 2 || *data_type == 6 { + + let mut buf = Vec::new(); + let mut out = String::new(); + + let time: SystemTime = UNIX_EPOCH + Duration::from_secs(*time_str); + + let date_time = DateTime::::from(time); + + let date_s = date_time.format("%d.%m.%Y").to_string(); + let time_s = date_time.format("%H.%M.%S").to_string(); + + let mut data = match File::open(format!("{}{}", path, filename)) + { + Ok(file) => file, + Err(msg) => { return Err(format!("could not open data file ({}): {}", filename, msg)) } + }; + + match data.read_to_end(&mut buf) { + Ok(stat) => stat, + Err(msg) => { return Err(format!("could not read data file ({}): {}", filename, msg)) } + }; + + + + out.push_str(&time_str.to_string()); + out.push(';'); + out.push_str(&date_s); + out.push(';'); + out.push_str(&time_s); + out.push(';'); + + if *data_type == 1 || *data_type == 6 { + ch_f32 = parse_data_f32(buf)?; + for elem in ch_f32 { + out.push_str(&elem.to_string()); + out.push(';'); + } + } + else if *data_type == 2 { + ch_u16 = parse_data_u16(buf)?; + for elem in ch_u16 { + out.push_str(&elem.to_string()); + out.push(';'); + } + } + + out.remove(out.len() - 1); + + return Ok(out); + } + else { + return Err("unsupported file type! Parser supports data types: .data01.asotr01(02), data02.asotr01(02), data06.asotr01(02)".to_string()); + } } - pub fn parse_pow_file(fname: String) -> Result { - let mut buf = Vec::new(); - let _pow = Pow { ch: [0, 0, 0, 0, 0, 0] }; - - let mut data = match File::open(fname) { - Ok(file) => file, - Err(msg) => { - return Err(format!("could not open data file: {}", msg)) - } - }; - - let _st = match data.read_to_end(&mut buf) { - Ok(stat) => stat, - Err(msg) => { - return Err(format!("could not read data file: {}", msg)) - } - }; - - let _pow = parse_data_u16(buf)?; - return Ok(_pow); - } } @@ -94,18 +165,22 @@ pub mod asotr_data { fn main() { - use crate::asotr_data::*; + use crate::asotr_data::*; - let fname = "data01_.asotr01".to_string(); - let fname_set = "data06.asotr01".to_string(); - let fname_pow = "data02.asotr01".to_string(); + let path = "../data/".to_string(); + // let fname = "1737354986_202730_data01.asotr02".to_string(); + let fname = "1737354361_630401_data06.asotr01".to_string(); + + let s = match parse_data(path, fname) { + Ok(elem) => elem, + Err(msg) => { + println!("error: {}", msg); + return; + } + }; + + println!("{}", s); - let temp = parse_temp_file(fname.clone()); - let temp_set = parse_temp_file(fname_set.clone()); - let pow = parse_pow_file(fname_pow.clone()); - println!("{:?}", temp); - println!("{:?}", temp_set); - println!("{:?}", pow); }