rewrite hostname_stream to use dbus
This commit is contained in:
parent
9dfa11f0cf
commit
9ae994f637
|
@ -1,5 +1,6 @@
|
|||
use crate::tile::Block;
|
||||
use crate::tiles;
|
||||
use dbus::nonblock::SyncConnection;
|
||||
use futures::{stream::BoxStream, Stream};
|
||||
use serde::{Deserialize, Deserializer};
|
||||
use smart_default::SmartDefault;
|
||||
|
@ -7,6 +8,7 @@ use std::env::var;
|
|||
use std::error::Error;
|
||||
use std::io;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use structopt::StructOpt;
|
||||
use tokio::fs::File;
|
||||
use tokio::prelude::*;
|
||||
|
@ -113,11 +115,12 @@ pub async fn read_config() -> Result<Config, Box<dyn std::error::Error>> {
|
|||
|
||||
pub fn process_tile(
|
||||
tile: &TileConfig,
|
||||
connection: &Arc<SyncConnection>,
|
||||
) -> BoxStream<'static, Result<Block, Box<dyn std::error::Error + Send + Sync>>> {
|
||||
let five_secs = Duration::from_secs(5);
|
||||
match &tile.config_type {
|
||||
TileConfigType::Battery => wrap(tiles::battery_stream(), tile.update.or(Some(five_secs))),
|
||||
TileConfigType::Hostname => wrap(tiles::hostname_stream(), tile.update),
|
||||
TileConfigType::Hostname => wrap(tiles::hostname_stream(connection.clone()), tile.update),
|
||||
TileConfigType::Load => wrap(tiles::load_stream(), tile.update.or(Some(five_secs))),
|
||||
TileConfigType::Memory => wrap(tiles::memory_stream(), tile.update.or(Some(five_secs))),
|
||||
TileConfigType::Time(c) => wrap(tiles::time_stream(c.clone()), tile.update),
|
||||
|
|
|
@ -4,7 +4,7 @@ mod output;
|
|||
mod tile;
|
||||
mod tiles;
|
||||
|
||||
use dbus_tokio::connection::new_session_sync;
|
||||
use dbus_tokio::connection::new_system_sync;
|
||||
use futures::channel::mpsc::{channel, Sender};
|
||||
use futures::{stream::BoxStream, StreamExt};
|
||||
use std::fmt::Debug;
|
||||
|
@ -16,7 +16,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
let config = config::read_config().await?;
|
||||
|
||||
// We can't do much until we have a D-Bus connection so just do it synchronously
|
||||
let (resource, _conn) = new_session_sync()?;
|
||||
let (resource, conn) = new_system_sync()?;
|
||||
|
||||
// Now start listening on our D-Bus connection
|
||||
tokio::spawn(async {
|
||||
|
@ -29,7 +29,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
let tiles: Vec<_> = config
|
||||
.tile
|
||||
.iter()
|
||||
.map(config::process_tile)
|
||||
.map(|tile| config::process_tile(tile, &conn))
|
||||
.enumerate()
|
||||
.map(|(index, stream)| spawn_stream(index, stream, sender.clone()))
|
||||
.collect();
|
||||
|
|
|
@ -1,22 +1,28 @@
|
|||
use crate::tile::Block;
|
||||
use futures::stream;
|
||||
use futures::Stream;
|
||||
use tokio::fs::File;
|
||||
use tokio::prelude::*;
|
||||
use dbus::nonblock::stdintf::org_freedesktop_dbus::Properties;
|
||||
use dbus::nonblock::{Proxy, SyncConnection};
|
||||
use futures::{FutureExt, Stream};
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
pub fn hostname_stream(
|
||||
connection: Arc<SyncConnection>,
|
||||
) -> impl Stream<Item = Result<Block, Box<dyn std::error::Error + Send + Sync>>> {
|
||||
stream::once(async {
|
||||
let mut raw = String::new();
|
||||
File::open("/proc/sys/kernel/hostname")
|
||||
.await?
|
||||
.read_to_string(&mut raw)
|
||||
.await?;
|
||||
let proxy = Proxy::new(
|
||||
"org.freedesktop.hostname1",
|
||||
"/org/freedesktop/hostname1",
|
||||
Duration::from_secs(5),
|
||||
connection,
|
||||
);
|
||||
let reply = proxy.get("org.freedesktop.hostname1", "Hostname");
|
||||
async {
|
||||
let hostname: String = reply.await?;
|
||||
let block = Block {
|
||||
full_text: raw.trim_end_matches('\n').into(),
|
||||
full_text: hostname.into(),
|
||||
name: "hostname".into(),
|
||||
..Default::default()
|
||||
};
|
||||
Ok(block)
|
||||
})
|
||||
}
|
||||
.into_stream()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue