diff options
| author | Scott Lawrence <scott+git@ineffectivetheory.com> | 2024-06-29 00:19:31 -0700 |
|---|---|---|
| committer | Scott Lawrence <scott+git@ineffectivetheory.com> | 2024-06-29 00:19:31 -0700 |
| commit | 11091289776ef6941f8c6558991484c7fcfa1069 (patch) | |
| tree | 05ab60611a1003bc1ef14e9c9a85b7b19edf7786 | |
| parent | 812cf030c0b53aee68627aa117d2051d6b8ae21c (diff) | |
| download | varanus-11091289776ef6941f8c6558991484c7fcfa1069.tar.gz varanus-11091289776ef6941f8c6558991484c7fcfa1069.tar.bz2 varanus-11091289776ef6941f8c6558991484c7fcfa1069.zip | |
Periodic updating of the state
| -rw-r--r-- | src/main.rs | 36 | ||||
| -rwxr-xr-x | varanus | 2 |
2 files changed, 37 insertions, 1 deletions
diff --git a/src/main.rs b/src/main.rs index 4086419..d25f128 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,6 @@ -use std::time::SystemTime; +use std::thread::sleep; +use std::time::{Duration,Instant,SystemTime}; +use std::os::unix::net::{UnixStream,UnixListener}; use clap::Parser; use serde::{Serialize,Deserialize}; @@ -9,6 +11,8 @@ struct Cli { daemon: bool, #[arg(short='v', long)] verbose: bool, + #[arg(short='D', long, default_value="10")] + delay: u64, } #[derive(Serialize, Deserialize)] @@ -16,7 +20,12 @@ struct Battery { } #[derive(Serialize, Deserialize)] +struct Power { +} + +#[derive(Serialize, Deserialize)] struct Process { + power: Power, } #[derive(Serialize, Deserialize)] @@ -24,9 +33,34 @@ struct State { asof: SystemTime, } +fn init_state() -> State { + let asof = SystemTime::now(); + return State { + asof: asof, + } +} + +fn update_state(state: &mut State) { + let asof = SystemTime::now(); + state.asof = asof; +} + fn main() { let args = Cli::parse(); if args.daemon { + let start = Instant::now(); + let delay_ms: u64 = args.delay * 1000; + let mut cycle: u64 = 0; + let mut state = init_state(); + loop { + cycle += 1; + sleep(Duration::from_millis(cycle*delay_ms) - start.elapsed()); + if args.verbose { + println!("{:?} elapsed; updating state...", start.elapsed()); + } + update_state(&mut state); + } } else { } } + @@ -0,0 +1,2 @@ +#!/bin/sh +exec cargo run -q -- $* |
