Move MPD idle command to mpd.rs
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Sijmen 2023-05-01 16:13:08 +02:00
parent 092c82b6cd
commit 700a5ad1ac
Signed by: vijfhoek
GPG Key ID: DAF7821E067D9C48
2 changed files with 37 additions and 29 deletions

View File

@ -1,12 +1,6 @@
use std::path::Path;
use anyhow::anyhow;
use askama::Template;
use async_std::prelude::*;
use async_std::{
io::{BufReader, WriteExt},
net::TcpStream,
};
use serde::Deserialize;
mod mpd;
@ -137,34 +131,14 @@ async fn get_art(req: tide::Request<()>) -> tide::Result {
}
async fn sse(_req: tide::Request<()>, sender: tide::sse::Sender) -> tide::Result<()> {
// Needs to be async and all async mpd libraries suck
let mut stream = TcpStream::connect(mpd::host()).await?;
let mut reader = BufReader::new(stream.clone());
// skip OK MPD line
// TODO check if it is indeed OK
let mut buffer = String::new();
reader.read_line(&mut buffer).await?;
// Update everything on connect
sender.send("playlist", "", None).await?;
sender.send("player", "", None).await?;
loop {
stream.write_all(b"idle playlist player database\n").await?;
loop {
buffer.clear();
reader.read_line(&mut buffer).await?;
if buffer == "OK\n" {
break;
}
let (_, changed) = buffer
.trim_end()
.split_once(": ")
.ok_or(anyhow!("unexpected response from MPD"))?;
sender.send(changed, "", None).await?;
let systems = mpd::idle(&["playlist", "player", "database"]).await?;
for system in systems {
sender.send(&system, "", None).await?;
}
}
}

View File

@ -1,5 +1,10 @@
use std::borrow::Cow;
use anyhow::anyhow;
use async_std::{
io::{prelude::BufReadExt, BufReader, WriteExt},
net::TcpStream,
};
use mpdrs::lsinfo::LsInfoResponse;
pub(crate) fn host() -> String {
@ -85,3 +90,32 @@ pub(crate) enum Entry {
path: String,
},
}
pub(crate) async fn idle(systems: &[&str]) -> anyhow::Result<Vec<String>> {
let mut stream = TcpStream::connect(host()).await?;
let mut reader = BufReader::new(stream.clone());
// skip OK MPD line
// TODO check if it is indeed OK
let mut buffer = String::new();
reader.read_line(&mut buffer).await?;
let systems = systems.join(" ");
let command = format!("idle {systems}\n");
stream.write_all(command.as_bytes()).await?;
let mut updated = vec![];
loop {
buffer.clear();
reader.read_line(&mut buffer).await?;
if buffer == "OK\n" {
break Ok(updated);
}
let (_, changed) = buffer
.trim_end()
.split_once(": ")
.ok_or(anyhow!("unexpected response from MPD"))?;
updated.push(changed.to_string());
}
}