Skip to content

Commit

Permalink
Added flagging func.
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Jul 27, 2023
1 parent 9b7f03b commit 52cc0be
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 12 deletions.
36 changes: 35 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"
81 changes: 71 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 &&
Expand Down Expand Up @@ -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!()

}

0 comments on commit 52cc0be

Please sign in to comment.