From 52cc0be655728c6eb54b41e3f51ce9325ae1c912 Mon Sep 17 00:00:00 2001 From: = <=> Date: Thu, 27 Jul 2023 12:24:42 -0400 Subject: [PATCH] Added flagging func. --- Cargo.lock | 36 +++++++++++++++++++++++- Cargo.toml | 3 +- src/main.rs | 81 ++++++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 108 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 99c0368..c62e220 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -49,9 +49,10 @@ dependencies = [ [[package]] name = "ard_serial_comm" -version = "0.1.0" +version = "0.2.0" dependencies = [ "chrono", + "csv", "serialport", "time 0.3.23", ] @@ -113,6 +114,27 @@ version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa" +[[package]] +name = "csv" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "626ae34994d3d8d668f4269922248239db4ae42d538b14c398b74a52208e8086" +dependencies = [ + "csv-core", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "csv-core" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b2466559f260f48ad25fe6317b3c8dac77b5bdb5763ac7d9d6103530663bc90" +dependencies = [ + "memchr", +] + [[package]] name = "iana-time-zone" version = "0.1.57" @@ -136,6 +158,12 @@ dependencies = [ "cc", ] +[[package]] +name = "itoa" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" + [[package]] name = "js-sys" version = "0.3.64" @@ -281,6 +309,12 @@ version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5ea92a5b6195c6ef2a0295ea818b312502c6fc94dde986c5553242e18fd4ce2" +[[package]] +name = "ryu" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741" + [[package]] name = "scopeguard" version = "1.2.0" diff --git a/Cargo.toml b/Cargo.toml index 8c4a45b..1ef25eb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ard_serial_comm" -version = "0.1.0" +version = "0.2.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -10,3 +10,4 @@ edition = "2021" serialport = "4.2.1" time = {version="0.3.22", features=["macros"]} chrono = "0.4.26" +csv = "1.2.2" diff --git a/src/main.rs b/src/main.rs index 0202f30..421be88 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,20 @@ -use std::{thread, time::{Duration, SystemTime}, io::Result}; +use std::{thread, + time::{Duration, SystemTime}, + io::Result, env, error::Error, + ffi::OsString,process, path::{Path, self}}; use serialport::{DataBits, StopBits}; use chrono::{NaiveDateTime, Datelike, Timelike}; -const TIME_OFFSET: u64 = 60*11 + 44; - +const TIME_OFFSET: u64 = 60*11 + 44; //offset between the NOxWerx and the computer this is running on +const DISPLAYTIME_OFFSET: u64 = 60*60*4; //offset btw EST and UTC (?) fn main() { - println!("Waiting for next 15-minute interval..."); + println!("Waiting for next 5-minute interval..."); + let filepath: &Path = Path::new("C:\\Users\\EIS\\Desktop\\valve_flags"); + setup(5*60); //starts it on the next 5mins. Accounts for the time it takes for // the port to reset as well. + loop { //below func gets system time. let time_startloop:u64 = SystemTime::now() @@ -29,7 +35,10 @@ fn main() { .unwrap() .as_secs() + TIME_OFFSET; - let displaytime = NaiveDateTime::from_timestamp_opt(time_now as i64,1_u32).unwrap(); + let displaytime = + NaiveDateTime::from_timestamp_opt( + (time_now - DISPLAYTIME_OFFSET) as i64,0_u32) + .unwrap(); if time_now-time_startloop <= max_buffer && timing_flags[0]!=1 { @@ -41,9 +50,8 @@ fn main() { timing_flags[0] = 1; println!("Level 1 at {}/{}/{} {}:{}:{}", displaytime.year(), displaytime.month(), displaytime.day(), - displaytime.hour(), displaytime.minute(), displaytime.second()); - thread::sleep(Duration::from_secs(routime) - ); + displaytime.hour(), displaytime.minute(), displaytime.second()); + _ = write_flags(routime, &filepath, &displaytime, 1); } if time_now - time_startloop >= routime @@ -59,7 +67,7 @@ fn main() { displaytime.year(), displaytime.month(), displaytime.day(), displaytime.hour(), displaytime.minute(), displaytime.second()); - thread::sleep(Duration::from_secs(routime)); + _ = write_flags(routime, &filepath, &displaytime, 2); } if time_now - time_startloop >= routime*2 @@ -74,7 +82,7 @@ fn main() { println!("Level 3 at {}/{}/{} {}:{}:{}", displaytime.year(), displaytime.month(), displaytime.day(), displaytime.hour(), displaytime.minute(), displaytime.second()); - thread::sleep(Duration::from_secs(routime)); + _ = write_flags(routime, &filepath, &displaytime, 3); } if time_now - time_startloop >= routime*3 + 15 && @@ -123,3 +131,56 @@ fn setup(routime:u64) { } } +fn write_flags(routime:u64, + filepath:&Path, + displaytime:&NaiveDateTime, + position:u8)-> Result<()> { + + let displaystring = format!("{}{}{}{}{}", //format YMDHM + displaytime.year(), + displaytime.month(),displaytime.day(), + displaytime.hour(),displaytime.minute() + ); + let filenamestr: String = format!("rob_noxbox_{}.csv",&displaystring); //concatenating the filenamestr + let file = filepath.join(&filenamestr); //final filename + let mut wtr = csv::Writer::from_path(file)?; //pass it to the writer function + + let mut counter: u64 = 0; + + while counter < routime { //write 1 entry per second with the flags. + wtr.write_record(vec![&displaytime.to_string(), &position.to_string()])?; + counter += 1; + thread::sleep(Duration::from_millis(1000)) + } + wtr.flush()?; + Ok(()) +} + + +#[test] +//time test +fn filename() { + let time_now:u64 = SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .unwrap() + .as_secs() + TIME_OFFSET; + + let displaytime = + NaiveDateTime::from_timestamp_opt( + (time_now - DISPLAYTIME_OFFSET) as i64,0_u32) + .unwrap(); + + let displaystr = format!("{}{}{}{}{}", + displaytime.year(),displaytime.month(), + displaytime.day(),displaytime.hour(), + displaytime.minute()); + + let filepath: &Path = Path::new("C:\\Users\\EIS\\Desktop\\valve_flags"); + let filenamestr: String = format!("rob_noxbox_{}.csv", &displaytime.to_string()); + let file = filepath.join(&filenamestr); + + println!("{}",file.display()); + debug_assert!() + +} +