1
0
mirror of https://github.com/ZetaKebab/MpcNET.git synced 2024-09-16 05:30:09 +00:00

Merge pull request #1 from glucaci/consoleApp

Console app
This commit is contained in:
Gabriel Lucaci 2016-12-05 17:17:08 +01:00 committed by GitHub
commit ec24b3f01a
8 changed files with 190 additions and 28 deletions

12
.gitignore vendored
View File

@ -3,7 +3,15 @@
################################################################################
/.vs
/LibMpc/bin
/LibMpc/obj
/LibMpc/LibMpc.xproj.user
/LibMpc/obj/Debug/netstandard1.6
/LibMpc/bin/Debug/netstandard1.6
/LibMpc/project.lock.json
/LibMpcApp/bin
/LibMpcApp/obj
/LibMpcApp/LibMpcApp.xproj.user
/LibMpcApp/project.lock.json
/LibMpc.sln.DotSettings.user

View File

@ -5,6 +5,8 @@ VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "LibMpc", "LibMpc\LibMpc.xproj", "{8994C820-7BA9-4BB8-B9EA-C608B07C4A11}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "LibMpcApp", "LibMpcApp\LibMpcApp.xproj", "{FF176AD5-D6DA-41EE-B27D-1DBEF0CE462F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -15,6 +17,10 @@ Global
{8994C820-7BA9-4BB8-B9EA-C608B07C4A11}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8994C820-7BA9-4BB8-B9EA-C608B07C4A11}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8994C820-7BA9-4BB8-B9EA-C608B07C4A11}.Release|Any CPU.Build.0 = Release|Any CPU
{FF176AD5-D6DA-41EE-B27D-1DBEF0CE462F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FF176AD5-D6DA-41EE-B27D-1DBEF0CE462F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FF176AD5-D6DA-41EE-B27D-1DBEF0CE462F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FF176AD5-D6DA-41EE-B27D-1DBEF0CE462F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -39,6 +39,29 @@ namespace LibMpc
return _connection.IsConnected;
}
public async Task DisconnectAsync()
{
if (_connection == null)
{
return;
}
if (_connection.IsConnected)
{
await _connection?.DisconnectAsync();
}
}
// TODO: create response type
public async Task<object> SendAsync(IMpcCommand command)
{
var mpdResponse = await _connection.Exec(command.Value);
var respose = command.ParseResponse(mpdResponse);
return respose;
}
#region Admin Commands
/// <summary>
/// Disables an MPD output.

View File

@ -63,7 +63,7 @@ namespace LibMpc
var firstLine = _reader.ReadLine();
if (!firstLine.StartsWith(FIRST_LINE_PREFIX))
{
await Disconnect();
await DisconnectAsync();
throw new InvalidDataException("Response of mpd does not start with \"" + FIRST_LINE_PREFIX + "\"." );
}
_version = firstLine.Substring(FIRST_LINE_PREFIX.Length);
@ -74,7 +74,7 @@ namespace LibMpc
await ReadResponseAsync();
}
private Task Disconnect()
public Task DisconnectAsync()
{
if (_tcpClient == null)
{
@ -113,7 +113,7 @@ namespace LibMpc
}
catch (Exception)
{
try { await Disconnect(); } catch (Exception) { }
try { await DisconnectAsync(); } catch (Exception) { }
return null; // TODO: Create Null Object for MpdResponse
}
}
@ -161,7 +161,7 @@ namespace LibMpc
}
catch (Exception)
{
try { await Disconnect(); } catch (Exception) { }
try { await DisconnectAsync(); } catch (Exception) { }
throw;
}
}

21
LibMpcApp/LibMpcApp.xproj Normal file
View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>ff176ad5-d6da-41ee-b27d-1dbef0ce462f</ProjectGuid>
<RootNamespace>LibMpcApp</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

65
LibMpcApp/Program.cs Normal file
View File

@ -0,0 +1,65 @@
using System;
using System.Net;
using LibMpc;
using System.Collections.Generic;
namespace LibMpcApp
{
/// <summary>
/// Simple console app to test commands and parsed responses.
/// </summary>
public class Program
{
private static readonly Dictionary<int, Func<object, IMpcCommand>> _commands = new Dictionary<int, Func<object, IMpcCommand>>
{
{ 1, input => new Commands.Reflection.TagTypes() }
};
public static void Main(string[] args)
{
var mpc = new Mpc(new IPEndPoint(IPAddress.Loopback, 6600));
var connected = mpc.ConnectAsync().GetAwaiter().GetResult();
if (connected)
{
Console.WriteLine("Connected to MPD.");
StartReadCommands(mpc);
}
else
{
Console.WriteLine("Could not connect to MPD.");
}
mpc.DisconnectAsync().GetAwaiter().GetResult();
}
private static void StartReadCommands(Mpc mpc)
{
int userInput = 0;
while ((userInput = DisplayMenu()) != 99)
{
Func<object, IMpcCommand> command;
var response = new object();
if (_commands.TryGetValue(userInput, out command))
{
response = mpc.SendAsync(command(null)).GetAwaiter().GetResult();
}
Console.WriteLine("Response: ");
Console.WriteLine(response);
}
}
static public int DisplayMenu()
{
Console.WriteLine();
Console.WriteLine("Commands: ");
Console.WriteLine("1. tagtypes");
Console.WriteLine("99. Exit");
Console.WriteLine();
var result = Console.ReadLine();
return Convert.ToInt32(result);
}
}
}

View File

@ -0,0 +1,19 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Windows User")]
[assembly: AssemblyProduct("LibMpcApp")]
[assembly: AssemblyTrademark("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("ff176ad5-d6da-41ee-b27d-1dbef0ce462f")]

20
LibMpcApp/project.json Normal file
View File

@ -0,0 +1,20 @@
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true
},
"dependencies": {
"LibMpc": "1.0.0-*",
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.1"
}
},
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50"
}
}
}