aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormetamuffin <yvchraiqi@protonmail.com>2022-06-13 12:21:07 +0200
committermetamuffin <yvchraiqi@protonmail.com>2022-06-13 12:21:07 +0200
commita7abc26af31b69db06a5875fc3fbc756adc838b1 (patch)
treeb98d56b7572039a90ac6f23d76f38ce2ad66845c
parent1441b718c9bbf8016f563cfc89f4ee322e4f42e4 (diff)
downloadkarlender-a7abc26af31b69db06a5875fc3fbc756adc838b1.tar
karlender-a7abc26af31b69db06a5875fc3fbc756adc838b1.tar.bz2
karlender-a7abc26af31b69db06a5875fc3fbc756adc838b1.tar.zst
blub
-rw-r--r--karlc/src/pretty.rs1
-rw-r--r--karlcommon/src/protocol.rs1
-rw-r--r--karld/src/condition.rs1
-rw-r--r--karld/src/helper.rs156
-rw-r--r--karld/src/main.rs30
-rw-r--r--karld/src/schedule.rs43
6 files changed, 146 insertions, 86 deletions
diff --git a/karlc/src/pretty.rs b/karlc/src/pretty.rs
index abd5f96..35ad6d2 100644
--- a/karlc/src/pretty.rs
+++ b/karlc/src/pretty.rs
@@ -6,6 +6,7 @@ pub fn indent(s: &str) -> String {
pub fn fmt_condition(c: &Condition) -> String {
match c {
+ Condition::Never => format!("(never)"),
Condition::From(_) => todo!(),
Condition::Or(cs) => cs
.iter()
diff --git a/karlcommon/src/protocol.rs b/karlcommon/src/protocol.rs
index 4dc8724..d0aad41 100644
--- a/karlcommon/src/protocol.rs
+++ b/karlcommon/src/protocol.rs
@@ -71,6 +71,7 @@ impl Schedule {
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Condition {
+ Never,
From(Box<Condition>),
Or(Vec<Condition>),
diff --git a/karld/src/condition.rs b/karld/src/condition.rs
index 765e039..1a9abab 100644
--- a/karld/src/condition.rs
+++ b/karld/src/condition.rs
@@ -45,6 +45,7 @@ pub trait ConditionFind {
impl ConditionFind for Condition {
fn find(&self, edge: Edge, dir: Direction, mut from: NaiveDateTime) -> Option<NaiveDateTime> {
match self {
+ Condition::Never => None,
Condition::And(cs) => loop {
// TODO improve efficiency for backward search
// TODO fix endless search
diff --git a/karld/src/helper.rs b/karld/src/helper.rs
index f106df6..c5df979 100644
--- a/karld/src/helper.rs
+++ b/karld/src/helper.rs
@@ -1,85 +1,87 @@
-use crate::condition::{ConditionFind, Direction, Edge};
-use chrono::NaiveDateTime;
-use karlcommon::{Schedule, Task};
-use std::{collections::HashMap, ops::Range};
+use std::ops::Range;
-pub struct DiscreteCache {
- tasks: HashMap<u64, DiscreteCacheTask>,
-}
-pub struct DiscreteCacheTask {
- inner: Task,
- cached: Option<Range<NaiveDateTime>>,
- cache: Vec<Range<NaiveDateTime>>,
-}
+// use crate::condition::{ConditionFind, Direction, Edge};
+// use chrono::NaiveDateTime;
+// use karlcommon::{Schedule, Task};
+// use std::{collections::HashMap, ops::Range};
-impl DiscreteCache {
- pub fn new_for(tasks: HashMap<u64, Task>) -> Self {
- Self {
- tasks: HashMap::from_iter(
- tasks
- .into_iter()
- .map(|(k, v)| (k, DiscreteCacheTask::new(v))),
- ),
- }
- }
-}
+// pub struct DiscreteCache {
+// tasks: HashMap<u64, DiscreteCacheTask>,
+// }
+// pub struct DiscreteCacheTask {
+// inner: Task,
+// cached: Option<Range<NaiveDateTime>>,
+// cache: Vec<Range<NaiveDateTime>>,
+// }
-impl DiscreteCacheTask {
- pub fn new(inner: Task) -> Self {
- Self {
- inner,
- cached: None,
- cache: vec![],
- }
- }
+// impl DiscreteCache {
+// pub fn new_for(tasks: HashMap<u64, Task>) -> Self {
+// Self {
+// tasks: HashMap::from_iter(
+// tasks
+// .into_iter()
+// .map(|(k, v)| (k, DiscreteCacheTask::new(v))),
+// ),
+// }
+// }
+// }
- pub fn find(&mut self, from: NaiveDateTime, dir: Direction) -> Range<Option<NaiveDateTime>> {
- // TODO cache
- // if let Some(c) = self.cached {}
- return self.find_uncached(from, dir);
- }
+// impl DiscreteCacheTask {
+// pub fn new(inner: Task) -> Self {
+// Self {
+// inner,
+// cached: None,
+// cache: vec![],
+// }
+// }
- pub fn find_uncached(
- &mut self,
- from: NaiveDateTime,
- dir: Direction,
- ) -> Range<Option<NaiveDateTime>> {
- assert_eq!(dir, Direction::Forward); // TODO undefined behaviour if dir is not forward (maybe it even works backward)
- match &self.inner.schedule {
- Schedule::Condition(o) => {
- let start = o.find(Edge::Start, dir, from);
- let end = o.find(Edge::End, dir, from);
- match (start, end) {
- (Some(start), Some(end)) => {
- if end < start {
- if let Some(start) = o.find(Edge::Start, dir.invert(), from) {
- assert!(start < end);
- Some(start)..Some(end)
- } else {
- None..Some(end)
- }
- } else {
- Some(start)..Some(end)
- }
- }
- (None, Some(end)) => {
- if let Some(start) = o.find(Edge::Start, dir.invert(), from) {
- assert!(start < end);
- Some(start)..Some(end)
- } else {
- None..Some(end)
- }
- }
- (Some(start), None) => Some(start)..None,
- (None, None) => None..None,
- }
- }
- Schedule::Never => None..None,
- Schedule::Static(_) => None..None, // TODO
- Schedule::Dynamic { .. } => None..None, // TODO
- }
- }
-}
+// pub fn find(&mut self, from: NaiveDateTime, dir: Direction) -> Range<Option<NaiveDateTime>> {
+// // TODO cache
+// // if let Some(c) = self.cached {}
+// return self.find_uncached(from, dir);
+// }
+
+// pub fn find_uncached(
+// &mut self,
+// from: NaiveDateTime,
+// dir: Direction,
+// ) -> Range<Option<NaiveDateTime>> {
+// assert_eq!(dir, Direction::Forward); // TODO undefined behaviour if dir is not forward (maybe it even works backward)
+// match &self.inner.schedule {
+// Schedule::Condition(o) => {
+// let start = o.find(Edge::Start, dir, from);
+// let end = o.find(Edge::End, dir, from);
+// match (start, end) {
+// (Some(start), Some(end)) => {
+// if end < start {
+// if let Some(start) = o.find(Edge::Start, dir.invert(), from) {
+// assert!(start < end);
+// Some(start)..Some(end)
+// } else {
+// None..Some(end)
+// }
+// } else {
+// Some(start)..Some(end)
+// }
+// }
+// (None, Some(end)) => {
+// if let Some(start) = o.find(Edge::Start, dir.invert(), from) {
+// assert!(start < end);
+// Some(start)..Some(end)
+// } else {
+// None..Some(end)
+// }
+// }
+// (Some(start), None) => Some(start)..None,
+// (None, None) => None..None,
+// }
+// }
+// Schedule::Never => None..None,
+// Schedule::Static(_) => None..None, // TODO
+// Schedule::Dynamic { .. } => None..None, // TODO
+// }
+// }
+// }
pub trait Overlaps<T> {
fn overlaps(&self, v: T) -> bool;
diff --git a/karld/src/main.rs b/karld/src/main.rs
index 35cca04..24f70a3 100644
--- a/karld/src/main.rs
+++ b/karld/src/main.rs
@@ -1,3 +1,5 @@
+#![feature(box_syntax)]
+
pub mod condition;
pub mod helper;
pub mod interface;
@@ -15,6 +17,8 @@ use karlcommon::{
use log::{debug, error, info};
use std::{collections::HashMap, sync::RwLock};
+use crate::schedule::schedule_dynamic;
+
fn main() {
env_logger::init();
info!("logging");
@@ -73,6 +77,32 @@ fn main() {
schedule: Schedule::Static(1654997366..1655007366),
},
);
+
+ TASKS.write().unwrap().insert(
+ 3,
+ Task {
+ id: 3,
+ description: None,
+ name: "Staubsaugen".to_string(),
+ tags: vec!["Unwichtig".to_string()],
+ schedule: Schedule::Dynamic {
+ scheduled: None,
+ duration: 15 * 60,
+ priority: 1.0,
+ condition: Condition::Equal {
+ prop: Property::Monthofyear,
+ value: 6,
+ modulus: None,
+ },
+ },
+ },
+ );
+
+ std::thread::spawn(move || {
+ std::thread::sleep(std::time::Duration::from_secs_f64(0.1));
+ schedule_dynamic();
+ });
+
network_loop();
}
diff --git a/karld/src/schedule.rs b/karld/src/schedule.rs
index 0d0e783..971c63a 100644
--- a/karld/src/schedule.rs
+++ b/karld/src/schedule.rs
@@ -1,16 +1,19 @@
-use chrono::NaiveDateTime;
-use karlcommon::{Schedule, Task};
+use karlcommon::{Condition, Schedule};
use crate::TASKS;
pub fn schedule_dynamic() {
- let tasks = TASKS.write().unwrap();
+ let mut tasks = TASKS.write().unwrap();
- let colliders = tasks.values().filter(|t| !t.schedule.is_dynamic());
- let mut dynamic = tasks
- .values()
- .filter(|t| t.schedule.is_dynamic())
- .collect::<Vec<_>>();
+ let mut colliders = vec![];
+ let mut dynamic = vec![];
+ for t in tasks.values_mut() {
+ if t.schedule.is_dynamic() {
+ dynamic.push(t)
+ } else {
+ colliders.push(t)
+ }
+ }
dynamic.sort_by_key(|t| {
if let Schedule::Dynamic { priority, .. } = t.schedule {
@@ -19,5 +22,27 @@ pub fn schedule_dynamic() {
} else {
0
}
- })
+ });
+
+ let cond = Condition::Invert(box Condition::Or(
+ colliders
+ .iter()
+ .map(|c| match &c.schedule {
+ Schedule::Never => Condition::Never,
+ Schedule::Condition(c) => c.clone(),
+ Schedule::Static(r) => Condition::Range {
+ min: r.start,
+ max: r.end,
+ modulus: None,
+ prop: karlcommon::Property::Unix,
+ },
+ Schedule::Dynamic { .. } => unreachable!(),
+ })
+ .collect(),
+ ));
+ println!("{:?}", cond);
+
+ while dynamic.len() != 0 {
+ todo!()
+ }
}