From 0bcbadbdd0bdf499d54488108bd69201473fe040 Mon Sep 17 00:00:00 2001 From: jb-alvarado Date: Fri, 18 Feb 2022 16:47:11 +0100 Subject: [PATCH] check for playlist update, add media probe node --- src/utils/playlist.rs | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/src/utils/playlist.rs b/src/utils/playlist.rs index f62de67c..c4256407 100644 --- a/src/utils/playlist.rs +++ b/src/utils/playlist.rs @@ -1,9 +1,12 @@ use crate::utils::{ get_config, json_reader::{read_json, Program}, + modified_time, MediaProbe, }; pub struct CurrentProgram { + json_mod: String, + json_path: String, nodes: Vec, idx: usize, } @@ -12,12 +15,31 @@ impl CurrentProgram { fn new() -> Self { let config = get_config(); let json = read_json(&config, true); - let program: Vec = json.program; + Self { - nodes: program, + json_mod: json.modified.unwrap(), + json_path: json.current_file.unwrap(), + nodes: json.program.into(), idx: json.start_index.unwrap(), } } + + fn check_update(&mut self) { + let config = get_config(); + let mod_time = modified_time(self.json_path.clone()); + + if !mod_time.unwrap().to_string().eq(&self.json_mod) { + // when playlist has changed, reload it + let json = read_json(&config, true); + + self.json_mod = json.modified.unwrap(); + self.nodes = json.program.into(); + } + } + + fn append_probe(&mut self, node: &mut Program) { + node.probe = Some(MediaProbe::new(node.source.clone())) + } } impl Iterator for CurrentProgram { @@ -25,18 +47,24 @@ impl Iterator for CurrentProgram { fn next(&mut self) -> Option { if self.idx < self.nodes.len() { - let current = self.nodes[self.idx].clone(); + self.check_update(); + let mut current = self.nodes[self.idx].clone(); self.idx += 1; + self.append_probe(&mut current); + Some(current) } else { - // play first item from next playlist let config = get_config(); - let program: Vec = read_json(&config, false).program; - self.nodes = program; + let json = read_json(&config, false); + self.json_mod = json.modified.unwrap(); + self.json_path = json.current_file.unwrap(); + self.nodes = json.program.into(); self.idx = 1; - let current = self.nodes[0].clone(); + let mut current = self.nodes[0].clone(); + + self.append_probe(&mut current); Some(current) }