1
0
mirror of https://github.com/ZetaKebab/MpcNET.git synced 2025-07-01 08:47:36 +00:00

Response from MPD will be specific type. MpdDirectory and MpdFile created. Metadata for MpdFile still to be done.

This commit is contained in:
glucaci
2016-12-19 12:12:22 +01:00
parent 15c81b96e9
commit f6653e0075
15 changed files with 275 additions and 711 deletions

View File

@ -1,5 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using LibMpc.Types;
namespace LibMpc
{
@ -12,7 +12,7 @@ namespace LibMpc
{
// TODO: count
public class Find : IMpcCommand<IList<IDictionary<string, string>>>
public class Find : IMpcCommand<IEnumerable<MpdFile>>
{
private readonly ITag _tag;
private readonly string _searchText;
@ -25,22 +25,25 @@ namespace LibMpc
public string Value => string.Join(" ", "find", _tag.Value, _searchText);
public IDictionary<string, IList<IDictionary<string, string>>> FormatResponse(IList<KeyValuePair<string, string>> response)
public IEnumerable<MpdFile> FormatResponse(IList<KeyValuePair<string, string>> response)
{
var results = new Dictionary<string, IList<IDictionary<string, string>>>
{
{ "files", new List<IDictionary<string, string>>() }
};
var results = new List<MpdFile>();
var fileBuilder = new MpdFileBuidler();
foreach (var line in response)
{
if (line.Key.Equals("file"))
{
results["files"].Add(new Dictionary<string, string> { { "file", line.Value } });
if (fileBuilder.IsInitialized)
{
results.Add(fileBuilder.Build());
}
fileBuilder.Init(line.Value);
}
else
{
results["files"].Last().Add(line.Key, line.Value);
fileBuilder.WithProperty(line.Key, line.Value);
}
}
@ -59,50 +62,38 @@ namespace LibMpc
public string Value => string.Join(" ", "list", _tag);
public IDictionary<string, string> FormatResponse(IList<KeyValuePair<string, string>> response)
public string FormatResponse(IList<KeyValuePair<string, string>> response)
{
return response.ToDefaultDictionary();
// TODO:
return response.ToString();
}
}
// TODO: findadd
public class ListAll : IMpcCommand<IList<IDictionary<string, IList<string>>>>
public class ListAll : IMpcCommand<MpdDirectory>
{
public string Value => "listall";
public IDictionary<string, IList<IDictionary<string, IList<string>>>> FormatResponse(IList<KeyValuePair<string, string>> response)
public MpdDirectory FormatResponse(IList<KeyValuePair<string, string>> response)
{
var results = new Dictionary<string, IList<IDictionary<string, IList<string>>>>
{
{ "directories", new List<IDictionary<string, IList<string>>>() }
};
// Add by default the root directory
results["directories"].Add(new Dictionary<string, IList<string>>
{
{ "path", new List<string>() },
{ "files", new List<string>() }
});
var rootDirectory = new MpdDirectory("/");
foreach (var line in response)
{
if (line.Key.Equals("file"))
{
results["directories"].Last()["files"].Add(line.Value);
rootDirectory.AddFile(line.Value);
}
if (line.Key.Equals("directory"))
{
results["directories"].Add(new Dictionary<string, IList<string>>
{
{ "path", new []{ line.Value } },
{ "files", new List<string>() }
});
rootDirectory.AddDirectory(line.Value);
}
}
return results;
return rootDirectory;
}
}
@ -119,9 +110,10 @@ namespace LibMpc
// TODO: Extend command: < update [URI] >
public string Value => "update";
public IDictionary<string, string> FormatResponse(IList<KeyValuePair<string, string>> response)
public string FormatResponse(IList<KeyValuePair<string, string>> response)
{
return response.ToDefaultDictionary();
// TODO:
return response.ToString();
}
}

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using LibMpc.Types;
namespace LibMpc
{
@ -23,9 +24,10 @@ namespace LibMpc
public string Value => string.Join(" ", "disableoutput", _outputId);
public IDictionary<string, string> FormatResponse(IList<KeyValuePair<string, string>> response)
public string FormatResponse(IList<KeyValuePair<string, string>> response)
{
return response.ToDefaultDictionary();
// TODO:
return response.ToString();
}
}
@ -43,9 +45,10 @@ namespace LibMpc
public string Value => string.Join(" ", "enableoutput", _outputId);
public IDictionary<string, string> FormatResponse(IList<KeyValuePair<string, string>> response)
public string FormatResponse(IList<KeyValuePair<string, string>> response)
{
return response.ToDefaultDictionary();
// TODO:
return response.ToString();
}
}
@ -54,29 +57,21 @@ namespace LibMpc
/// <summary>
/// Shows information about all outputs.
/// </summary>
public class Outputs : IMpcCommand<IList<IDictionary<string, string>>>
public class Outputs : IMpcCommand<IEnumerable<MpdOutput>>
{
public string Value => "outputs";
public IDictionary<string, IList<IDictionary<string, string>>> FormatResponse(IList<KeyValuePair<string, string>> response)
public IEnumerable<MpdOutput> FormatResponse(IList<KeyValuePair<string, string>> response)
{
var result = new Dictionary<string, IList<IDictionary<string, string>>>
{
{ "outputs", new List<IDictionary<string, string>>() }
};
var result = new List<MpdOutput>();
for (var i = 0; i < response.Count; i += 3)
{
var outputId = response[i].Value;
var outputId = int.Parse(response[i].Value);
var outputName = response[i + 1].Value;
var outputEnabled = response[i + 2].Value;
var outputEnabled = bool.Parse(response[i + 2].Value);
result["outputs"].Add(new Dictionary<string, string>
{
{"id", outputId},
{"name", outputName},
{"enabled", outputEnabled}
});
result.Add(new MpdOutput(outputId, outputName, outputEnabled));
}
return result;

View File

@ -14,16 +14,13 @@ namespace LibMpc
// TODO: commands
// TODO: notcommands
public class TagTypes : IMpcCommand<IList<string>>
public class TagTypes : IMpcCommand<IEnumerable<string>>
{
public string Value => "tagtypes";
IDictionary<string, IList<string>> IMpcCommand<IList<string>>.FormatResponse(IList<KeyValuePair<string, string>> response)
public IEnumerable<string> FormatResponse(IList<KeyValuePair<string, string>> response)
{
var result = new Dictionary<string, IList<string>>
{
{ "tagtypes", response.Where(item => item.Key.Equals("tagtype")).Select(item => item.Value).ToList() }
};
var result = response.Where(item => item.Key.Equals("tagtype")).Select(item => item.Value);
return result;
}

View File

@ -3,18 +3,10 @@ using System.Linq;
namespace LibMpc
{
public interface IMpcCommand<T>
public interface IMpcCommand<out T>
{
string Value { get; }
IDictionary<string, T> FormatResponse(IList<KeyValuePair<string, string>> response);
}
internal static class MpdCommandExtensions
{
public static IDictionary<string, string> ToDefaultDictionary(this IList<KeyValuePair<string, string>> items)
{
return items.ToDictionary(item => item.Key, item => item.Value);
}
T FormatResponse(IList<KeyValuePair<string, string>> response);
}
}