front tracking

This commit is contained in:
Cadey Ratio 2020-04-07 08:56:56 -04:00
parent 62829525d4
commit 62fa3b74c7
5 changed files with 1180 additions and 7 deletions

1073
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -8,6 +8,16 @@ edition = "2018"
[dependencies] [dependencies]
chrono = "0.4" chrono = "0.4"
log = "0.4"
env_logger = "0.7"
serde = { version = "1", features = [ "derive" ] }
serde_json = "1"
toml = "0.5"
xdg = "2.2"
[dependencies.reqwest]
version = "0.10"
features = [ "blocking", "json" ]
[dependencies.x11] [dependencies.x11]
features = ["xlib"] features = ["xlib"]

View File

@ -11,5 +11,6 @@ in pkgs.mkShell {
# dev dependencies # dev dependencies
xorg.libX11 xorg.libX11
pkg-config pkg-config
openssl
]; ];
} }

42
src/config.rs Normal file
View File

@ -0,0 +1,42 @@
use serde::{Deserialize, Serialize};
use std::{
fs::File,
io::{Read, Result, Write},
};
use xdg::*;
#[derive(Deserialize, Serialize, Debug, Clone)]
pub(crate) struct Config {
pub(crate) front_url: String,
pub(crate) weather_url: String,
}
pub(crate) fn load() -> Result<Config> {
let xdg_dirs =
BaseDirectories::with_prefix("cabytcini").expect("pu djica lo nu finti lei datnyveiste");
let config_path = xdg_dirs
.place_config_file("gaftercu'a.toml")
.expect("pu djica lo nu le datnyveiste be lo gaftercu'a zvati");
match File::open(&config_path) {
Ok(mut fin) => {
let mut datni = String::new();
fin.read_to_string(&mut datni)
.expect("pu djica lo nu tcidu le sfaile");
let cfg: Config =
toml::from_str(datni.as_str()).expect("pu djica lo nu jimpe lo sfaile");
Ok(cfg)
}
Err(why) => {
log::info!("pu na kargau le sfaile: {:?}", why);
let mut fout = File::create(config_path).unwrap();
let cfg = Config {
front_url: "".to_string(),
weather_url: "".to_string(),
};
write!(fout, "{}", toml::to_string_pretty(&cfg).unwrap()).unwrap();
Ok(cfg)
}
}
}

View File

@ -1,6 +1,8 @@
mod config;
mod xsetroot; mod xsetroot;
use chrono::prelude::*; use chrono::prelude::*;
use log::*;
use std::{ use std::{
fmt, fmt,
io::{prelude::*, BufReader}, io::{prelude::*, BufReader},
@ -11,7 +13,7 @@ use std::{
}; };
use xsetroot::XSetRoot; use xsetroot::XSetRoot;
type MTState = Arc<Mutex<State>>; pub type MTState = Arc<Mutex<State>>;
fn handle_client(stream: UnixStream, st: MTState) { fn handle_client(stream: UnixStream, st: MTState) {
let mut rdr = BufReader::new(&stream); let mut rdr = BufReader::new(&stream);
@ -21,6 +23,7 @@ fn handle_client(stream: UnixStream, st: MTState) {
let mut data = st.lock().unwrap(); let mut data = st.lock().unwrap();
data.msg = msg.trim().to_string(); data.msg = msg.trim().to_string();
info!("new message: {}", data.msg);
stream.shutdown(Shutdown::Both).expect("socket to close"); stream.shutdown(Shutdown::Both).expect("socket to close");
} }
@ -38,10 +41,37 @@ fn update_every_so_often(st: MTState) {
} }
} }
const FIVE_MINUTES: u64 = 60 * 5;
fn front_update(st: MTState, cfg: config::Config) {
loop {
match reqwest::blocking::get(&cfg.front_url) {
Ok(who) => {
let mut data = st.lock().unwrap();
let who = who.text().unwrap().trim().to_string();
if who != data.front {
data.front = who;
info!("new front: {}", data.front);
}
}
Err(why) => {
error!("front error: {:?}", why);
}
}
let dur = time::Duration::new(FIVE_MINUTES, 0);
thread::sleep(dur);
}
}
fn main() -> std::io::Result<()> { fn main() -> std::io::Result<()> {
let st: State = State::init(); let st: State = State::init();
st.show(); st.show();
let mtst = Arc::new(Mutex::new(st)); let mtst = Arc::new(Mutex::new(st));
env_logger::init();
let cfg = config::load()?;
info!("{:?}", cfg);
// start update thread // start update thread
{ {
@ -49,6 +79,13 @@ fn main() -> std::io::Result<()> {
thread::spawn(move || update_every_so_often(mtst)); thread::spawn(move || update_every_so_often(mtst));
} }
// start front thread
{
let mtst = mtst.clone();
let cfg = cfg.clone();
thread::spawn(move || front_update(mtst, cfg));
}
let _ = std::fs::remove_file("/home/cadey/tmp/cabytcini.sock")?; let _ = std::fs::remove_file("/home/cadey/tmp/cabytcini.sock")?;
let listener = UnixListener::bind("/home/cadey/tmp/cabytcini.sock")?; let listener = UnixListener::bind("/home/cadey/tmp/cabytcini.sock")?;
for stream in listener.incoming() { for stream in listener.incoming() {
@ -58,6 +95,7 @@ fn main() -> std::io::Result<()> {
thread::spawn(move || handle_client(stream, mtst)); thread::spawn(move || handle_client(stream, mtst));
} }
Err(err) => { Err(err) => {
error!("unix listener error: {:?}", err);
break; break;
} }
} }
@ -66,8 +104,9 @@ fn main() -> std::io::Result<()> {
Ok(()) Ok(())
} }
struct State { pub struct State {
msg: String, msg: String,
front: String,
} }
impl fmt::Display for State { impl fmt::Display for State {
@ -80,16 +119,26 @@ impl State {
fn init() -> State { fn init() -> State {
State { State {
msg: "".to_string(), msg: "".to_string(),
front: "".to_string(),
} }
} }
fn show(&self) { fn show(&self) {
let now = Local::now().format("%H:%M M%m %-d %a"); let now = Local::now().format("%H:%M M%m %-d %a");
let xsr = XSetRoot::init(); let xsr = XSetRoot::init();
if self.msg == "" {
xsr.render(format!("{}", now)) let mut msg = String::new();
} else {
xsr.render(format!("{} | {}", self.msg, now)); if self.msg != "" {
msg.push_str(format!("{} | ", msg).as_str());
} }
if self.front != "" {
msg.push_str(format!("{} | ", self.front).as_str());
}
msg.push_str(format!("{}", now).as_str());
xsr.render(msg);
} }
} }