mirror of
https://github.com/ZetaKebab/MpcNET.git
synced 2025-07-01 08:47:36 +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:
@ -1,24 +1,69 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace LibMpc
|
||||
{
|
||||
public interface IMpdMessage
|
||||
public interface IMpdMessage<T>
|
||||
{
|
||||
IMpdRequest Request { get; }
|
||||
IMpdResponse Response { get; }
|
||||
IMpdRequest<T> Request { get; }
|
||||
IMpdResponse<T> Response { get; }
|
||||
}
|
||||
|
||||
public class MpdMessage : IMpdMessage
|
||||
public class MpdMessage<T> : IMpdMessage<T>
|
||||
{
|
||||
public MpdMessage(IMpcCommand command)
|
||||
private readonly Regex _linePattern = new Regex("^(?<key>[A-Za-z]*):[ ]{0,1}(?<value>.*)$");
|
||||
private readonly IList<string> _rawResponse;
|
||||
|
||||
public MpdMessage(IMpcCommand<T> command, bool connected, IReadOnlyCollection<string> response)
|
||||
{
|
||||
Request = new MpdRequest(command);
|
||||
Request = new MpdRequest<T>(command);
|
||||
|
||||
var endLine = response.Skip(response.Count - 1).Single();
|
||||
_rawResponse = response.Take(response.Count - 1).ToList();
|
||||
|
||||
var values = Request.Command.FormatResponse(GetValuesFromResponse());
|
||||
Response = new MpdResponse<T>(endLine, values, connected);
|
||||
}
|
||||
|
||||
public void AddResponse(IMpdResponse response)
|
||||
{
|
||||
Response = response;
|
||||
public IMpdRequest<T> Request { get; }
|
||||
public IMpdResponse<T> Response { get; }
|
||||
|
||||
private IReadOnlyDictionary<string, IList<string>> GetValuesFromResponse()
|
||||
{
|
||||
var result = new Dictionary<string, IList<string>>();
|
||||
|
||||
foreach (var line in _rawResponse)
|
||||
{
|
||||
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))
|
||||
{
|
||||
if (!result.ContainsKey(mpdKey))
|
||||
{
|
||||
result.Add(mpdKey, new List<string>() { mpdValue });
|
||||
}
|
||||
else
|
||||
{
|
||||
result[mpdKey].Add(mpdValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public IMpdRequest Request { get; }
|
||||
public IMpdResponse Response { get; private set; }
|
||||
public override string ToString()
|
||||
{
|
||||
return JsonConvert.SerializeObject(this, Formatting.Indented);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,17 +1,17 @@
|
||||
namespace LibMpc
|
||||
{
|
||||
public interface IMpdRequest
|
||||
public interface IMpdRequest<T>
|
||||
{
|
||||
IMpcCommand Command { get; }
|
||||
IMpcCommand<T> Command { get; }
|
||||
}
|
||||
|
||||
public class MpdRequest : IMpdRequest
|
||||
public class MpdRequest<T> : IMpdRequest<T>
|
||||
{
|
||||
public MpdRequest(IMpcCommand command)
|
||||
public MpdRequest(IMpcCommand<T> command)
|
||||
{
|
||||
Command = command;
|
||||
}
|
||||
|
||||
public IMpcCommand Command { get; }
|
||||
public IMpcCommand<T> Command { get; }
|
||||
}
|
||||
}
|
@ -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
|
||||
|
@ -7,7 +7,8 @@ namespace LibMpc
|
||||
string Status { get; }
|
||||
string ErrorMessage { get; }
|
||||
string MpdError { get; }
|
||||
bool IsError { get; }
|
||||
bool Error { get; }
|
||||
bool Connected { get; }
|
||||
}
|
||||
|
||||
public class MpdResponseState : IMpdResponseState
|
||||
@ -16,16 +17,17 @@ namespace LibMpc
|
||||
|
||||
private readonly string _endLine;
|
||||
|
||||
public MpdResponseState(string endLine)
|
||||
public MpdResponseState(string endLine, bool connected)
|
||||
{
|
||||
_endLine = endLine;
|
||||
Connected = connected;
|
||||
|
||||
if (!string.IsNullOrEmpty(_endLine))
|
||||
{
|
||||
if (_endLine.Equals(Constants.Ok))
|
||||
{
|
||||
Status = _endLine;
|
||||
IsError = false;
|
||||
Error = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -34,7 +36,8 @@ namespace LibMpc
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsError { get; private set; } = true;
|
||||
public bool Connected { get; } = false;
|
||||
public bool Error { get; } = true;
|
||||
public string Status { get; private set; } = "UNKNOWN";
|
||||
public string ErrorMessage { get; private set; } = string.Empty;
|
||||
public string MpdError { get; private set; } = string.Empty;
|
||||
|
Reference in New Issue
Block a user