1
0
mirror of https://github.com/ZetaKebab/MpcNET.git synced 2025-01-14 22:18:43 +00:00

Line pattern extended. New commands in test app.

This commit is contained in:
glucaci 2016-12-06 18:18:06 +01:00
parent 53325604e8
commit 4a176e8f45
4 changed files with 10 additions and 91 deletions

View File

@ -76,6 +76,7 @@ namespace LibMpc
public class Update : IMpcCommand<string> public class Update : IMpcCommand<string>
{ {
// TODO: Extend command: < update [URI] >
public string Value => "update"; public string Value => "update";
public IReadOnlyDictionary<string, IList<string>> FormatResponse(IReadOnlyDictionary<string, IList<string>> response) public IReadOnlyDictionary<string, IList<string>> FormatResponse(IReadOnlyDictionary<string, IList<string>> response)

View File

@ -13,7 +13,7 @@ namespace LibMpc
public class MpdMessage<T> : IMpdMessage<T> public class MpdMessage<T> : IMpdMessage<T>
{ {
private readonly Regex _linePattern = new Regex("^(?<key>[A-Za-z]*):[ ]{0,1}(?<value>.*)$"); private readonly Regex _linePattern = new Regex("^(?<key>[A-Za-z_]*):[ ]{0,1}(?<value>.*)$");
private readonly IList<string> _rawResponse; private readonly IList<string> _rawResponse;
public MpdMessage(IMpcCommand<T> command, bool connected, IReadOnlyCollection<string> response) public MpdMessage(IMpcCommand<T> command, bool connected, IReadOnlyCollection<string> response)

View File

@ -62,94 +62,7 @@ namespace LibMpc
/* /*
#region Admin Commands #region Admin Commands
/// <summary>
/// Disables an MPD output.
/// </summary>
/// <param name="id">The id of the output.</param>
/// <returns>If the action was successful.</returns>
public async Task<bool> DisableOutputAsync(int id)
{
var mpdResponse = await _connection.Exec("disableoutput", new string[] { id.ToString() });
return !mpdResponse.IsError;
}
/// <summary>
/// Enables an MPD output.
/// </summary>
/// <param name="id">The id of the output.</param>
/// <returns>If the action was successful.</returns>
public async Task<bool> EnableOutputAsync(int id)
{
var mpdResponse = await _connection.Exec("enableoutput", new string[] { id.ToString() });
return !mpdResponse.IsError;
}
/// <summary>
/// Lists all outputs of the MPD.
/// </summary>
/// <returns>The list of all MPD outputs.</returns>
public async Task<MpdOutput[]> OutputsAsync()
{
MpdResponse response = await _connection.SendAsync("outputs");
if (response.Message.Count % 3 != 0)
throw new InvalidMpdResponseException();
MpdOutput[] ret = new MpdcountsOutput[response.Message.Count / 3];
for (int i = 0; i < ret.Length; i++)
{
int id;
string name;
int enabled;
KeyValuePair<string, string> idLine = response[i * 3];
if (idLine.Key == null)
throw new InvalidMpdResponseException("Invalid form of line " + (i * 3));
if (!idLine.Key.Equals("outputid"))
throw new InvalidMpdResponseException("Key of line " + (i * 3) + " is not 'outputid'");
if (!int.TryParse(idLine.Value, out id))
throw new InvalidMpdResponseException("Value of line " + (i * 3) + " is not a number");
KeyValuePair<string, string> nameLine = response[i * 3 + 1];
if (nameLine.Key == null)
throw new InvalidMpdResponseException("Invalid form of line " + (i * 3 + 1));
if (!nameLine.Key.Equals("outputname"))
throw new InvalidMpdResponseException("Key of line " + (i * 3 + 1) + " is not 'outputname'");
name = nameLine.Value;
KeyValuePair<string, string> enabledLine = response[i * 3 + 2];
if (enabledLine.Key == null)
throw new InvalidMpdResponseException("Invalid form of line " + (i * 3 + 2));
if (!enabledLine.Key.Equals("outputenabled"))
throw new InvalidMpdResponseException("Key of line " + (i * 3 + 2) + " is not 'outputenabled'");
if (!int.TryParse(enabledLine.Value, out enabled))
throw new InvalidMpdResponseException("Value of line " + (i * 3 + 2) + " is not a number");
ret[i] = new MpdOutput(id, name, enabled > 0);
}
return ret;
}
/// <summary>
/// Returns the list of tag types the MPD supports.
/// </summary>
/// <returns>The list of tag types the MPD supports.</returns>
public async Task<string[]> TagTypesAsync()
{
MpdResponse response = await _connection.SendAsync("tagtypes");
string[] ret = new string[response.Message.Count];
for (int i = 0; i < ret.Length; i++)
{
KeyValuePair<string, string> line = response[i];
if (!line.Key.Equals("tagtype"))
throw new InvalidMpdResponseException("Key of line " + (i) + " is not 'tagtype'");
ret[i] = line.Value;
}
return ret;
}
/// <summary> /// <summary>
/// Starts an update of the MPD database. /// Starts an update of the MPD database.
/// </summary> /// </summary>

View File

@ -49,6 +49,10 @@ namespace LibMpcApp
case 24: case 24:
response = mpc.SendAsync(new Commands.Reflection.TagTypes()).GetAwaiter().GetResult(); response = mpc.SendAsync(new Commands.Reflection.TagTypes()).GetAwaiter().GetResult();
break; break;
case 313:
response = mpc.SendAsync(new Commands.Database.Update()).GetAwaiter().GetResult();
break;
} }
Console.WriteLine("Response: "); Console.WriteLine("Response: ");
@ -61,17 +65,18 @@ namespace LibMpcApp
Console.WriteLine(); Console.WriteLine();
Console.WriteLine("Commands: "); Console.WriteLine("Commands: ");
// Ouput
Console.WriteLine(); Console.WriteLine();
Console.WriteLine("11. disableoutput 0"); Console.WriteLine("11. disableoutput 0");
Console.WriteLine("12. enableoutput 0"); Console.WriteLine("12. enableoutput 0");
Console.WriteLine("13. outputs"); Console.WriteLine("13. outputs");
// Reflection
Console.WriteLine(); Console.WriteLine();
Console.WriteLine("Reflection");
Console.WriteLine("24. tagtypes"); Console.WriteLine("24. tagtypes");
// Database Console.WriteLine();
Console.WriteLine("Database");
Console.WriteLine("313. update");
Console.WriteLine(); Console.WriteLine();
Console.WriteLine("99. Exit"); Console.WriteLine("99. Exit");