1
0
mirror of https://github.com/ZetaKebab/MpcNET.git synced 2025-07-02 00:57:38 +00:00

Each command can format the mpd response. User response is a MpdMessage which conatins the requested command and the formatted response.

This commit is contained in:
glucaci
2016-12-06 18:01:28 +01:00
parent 9212021be6
commit 53325604e8
13 changed files with 250 additions and 270 deletions

View File

@ -1,63 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace LibMpc
{
public interface IMpdResponse
public interface IMpdResponse<T>
{
IEnumerable<string> Response { get; }
IReadOnlyDictionary<string, string> Values { get; }
IMpdResponseState State { get; }
IReadOnlyDictionary<string, IList<T>> Body { get; }
}
public class MpdResponse : IMpdResponse
public class MpdResponse<T> : IMpdResponse<T>
{
private static readonly Regex LinePattern = new Regex("^(?<key>[A-Za-z]*):[ ]{0,1}(?<value>.*)$");
public MpdResponse(ICollection<string> response)
public MpdResponse(string endLine, IReadOnlyDictionary<string, IList<T>> body, bool connected)
{
response.CheckNotNull();
var endLine = response.Take(response.Count - 2).Single();
Response = response.Take(response.Count - 2).ToList();
State = new MpdResponseState(endLine);
Values = GetValuesFromResponse();
State = new MpdResponseState(endLine, connected);
Body = body;
}
public IMpdResponseState State { get; }
public IEnumerable<string> Response { get; }
public IReadOnlyDictionary<string, string> Values { get; }
private IReadOnlyDictionary<string, string> GetValuesFromResponse()
{
var result = new Dictionary<string, string>();
foreach (var line in Response)
{
var match = LinePattern.Match(line);
if (match.Success)
{
var mpdKey = match.Result("${key}");
if (!string.IsNullOrEmpty(mpdKey))
{
var mpdValue = match.Result("${value}");
if (string.IsNullOrEmpty(mpdValue))
{
result.Add(mpdKey, mpdValue);
}
}
}
}
return result;
}
public IReadOnlyDictionary<string, IList<T>> Body { get; }
}
public static class CheckNotNullExtension