Skip to content

Commit

Permalink
Added WebApiProject
Browse files Browse the repository at this point in the history
  • Loading branch information
Zexuz committed Mar 3, 2019
1 parent edec825 commit 9a67bbd
Show file tree
Hide file tree
Showing 22 changed files with 290 additions and 125 deletions.
73 changes: 3 additions & 70 deletions Mpv.JsonIpc/Api.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;

namespace Mpv.JsonIpc
{
public class Api
public class Api : IApi
{
private readonly Ipc _ipc;
private readonly IIpc _ipc;

public Api(Ipc ipc)
public Api(IIpc ipc)
{
_ipc = ipc;
}
Expand All @@ -27,69 +25,4 @@ public async Task<float> GetVolume()
return result.Data;
}
}

public class Ipc
{
private readonly Manager _manager;

public Ipc(Manager manager)
{
_manager = manager;
}

public async Task<Response<T>> ExecuteCommand<T>(Request request)
{
return await _manager.Execute<T>(request);
}

public async Task<Response<T>> GetProperty<T>(Property property, params string[] args)
{
var request = CreateCommand(new[] {"get_property", property.GetStringValue()}, args);
return await _manager.Execute<T>(request);
}

public async Task<Response<T>> SetProperty<T>(Property property, params object[] args)
{
var request = CreateCommand(new[] {"set_property", property.GetStringValue()}, args);
return await _manager.Execute<T>(request);
}

public async Task<Response<T>> SetPropertyString<T>(Property property, params string[] args)
{
var request = CreateCommand(new[] {"set_property_string", property.GetStringValue()}, args);
return await _manager.Execute<T>(request);
}

public async Task<Response<T>> CycleProperty<T>(Property property)
{
var request = CreateCommand(new[] {"cycle", property.GetStringValue()});
return await _manager.Execute<T>(request);
}

public Request CreateCommand(IEnumerable<object> command)
{
return CreateCommand(command, Array.Empty<object>());
}

public Request CreateCommand(IEnumerable<object> command, object arg)
{
return CreateCommand(command, new[] {arg});
}

public Request CreateCommand(IEnumerable<object> command, IEnumerable<object> args)
{
return new Request
{
Command = command.Concat(args).ToArray(),
RequestId = GenerateNewRequestId(),
};
}


private static int GenerateNewRequestId()
{
return 10;
// return _random.Next(0, 10000);
}
}
}
15 changes: 15 additions & 0 deletions Mpv.JsonIpc/DependencyModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Autofac;

namespace Mpv.JsonIpc
{
public class DependencyModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<NamedPipeFactory>().As<INamedPipeFactory>();
builder.RegisterType<Manager>().As<IManager>();
builder.RegisterType<Ipc>().As<IIpc>();
builder.RegisterType<Api>().As<IApi>();
}
}
}
11 changes: 11 additions & 0 deletions Mpv.JsonIpc/IApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Threading.Tasks;

namespace Mpv.JsonIpc
{
public interface IApi
{
Task ShowText(string text, TimeSpan duration);
Task<float> GetVolume();
}
}
17 changes: 17 additions & 0 deletions Mpv.JsonIpc/IIpc.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Mpv.JsonIpc
{
public interface IIpc
{
Task<Response<T>> ExecuteCommand<T>(Request request);
Task<Response<T>> GetProperty<T>(Property property, params string[] args);
Task<Response<T>> SetProperty<T>(Property property, params object[] args);
Task<Response<T>> SetPropertyString<T>(Property property, params string[] args);
Task<Response<T>> CycleProperty<T>(Property property);
Request CreateCommand(IEnumerable<object> command);
Request CreateCommand(IEnumerable<object> command, object arg);
Request CreateCommand(IEnumerable<object> command, IEnumerable<object> args);
}
}
10 changes: 10 additions & 0 deletions Mpv.JsonIpc/IManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Threading.Tasks;

namespace Mpv.JsonIpc
{
public interface IManager
{
Task<Response<T>> Execute<T>(Request message);
void Dispose();
}
}
9 changes: 9 additions & 0 deletions Mpv.JsonIpc/INamedPipeFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.IO.Pipes;

namespace Mpv.JsonIpc
{
public interface INamedPipeFactory
{
NamedPipeClientStream CreateNamedPipe();
}
}
73 changes: 73 additions & 0 deletions Mpv.JsonIpc/Ipc.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Mpv.JsonIpc
{
public class Ipc : IIpc
{
private readonly IManager _manager;

public Ipc(IManager manager)
{
_manager = manager;
}

public async Task<Response<T>> ExecuteCommand<T>(Request request)
{
return await _manager.Execute<T>(request);
}

public async Task<Response<T>> GetProperty<T>(Property property, params string[] args)
{
var request = CreateCommand(new[] {"get_property", property.GetStringValue()}, args);
return await _manager.Execute<T>(request);
}

public async Task<Response<T>> SetProperty<T>(Property property, params object[] args)
{
var request = CreateCommand(new[] {"set_property", property.GetStringValue()}, args);
return await _manager.Execute<T>(request);
}

public async Task<Response<T>> SetPropertyString<T>(Property property, params string[] args)
{
var request = CreateCommand(new[] {"set_property_string", property.GetStringValue()}, args);
return await _manager.Execute<T>(request);
}

public async Task<Response<T>> CycleProperty<T>(Property property)
{
var request = CreateCommand(new[] {"cycle", property.GetStringValue()});
return await _manager.Execute<T>(request);
}

public Request CreateCommand(IEnumerable<object> command)
{
return CreateCommand(command, Array.Empty<object>());
}

public Request CreateCommand(IEnumerable<object> command, object arg)
{
return CreateCommand(command, new[] {arg});
}

public Request CreateCommand(IEnumerable<object> command, IEnumerable<object> args)
{
return new Request
{
Command = command.Concat(args).ToArray(),
RequestId = GenerateNewRequestId(),
};
}


private static int GenerateNewRequestId()
{
return 10;
// return _random.Next(0, 10000);
}
}
}
6 changes: 3 additions & 3 deletions Mpv.JsonIpc/Manager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@

namespace Mpv.JsonIpc
{
public sealed class Manager : IDisposable
public sealed class Manager : IDisposable, IManager
{
private readonly NamedPipeClientStream _pipe;
private readonly StreamReader _pipeIn;

private readonly StreamWriter _pipeOut;

public Manager(NamedPipeFactory pipeFactory)
public Manager(INamedPipeFactory pipeFactory)
{
_pipe = pipeFactory.CrateNamedPipe();
_pipe = pipeFactory.CreateNamedPipe();
_pipe.Connect();

_pipeIn = new StreamReader(_pipe);
Expand Down
1 change: 1 addition & 0 deletions Mpv.JsonIpc/Mpv.JsonIpc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Autofac" Version="4.9.1" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
</ItemGroup>

Expand Down
4 changes: 2 additions & 2 deletions Mpv.JsonIpc/NamedPipeFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Mpv.JsonIpc
{
public class NamedPipeFactory
public class NamedPipeFactory : INamedPipeFactory
{
public NamedPipeClientStream CrateNamedPipe()
public NamedPipeClientStream CreateNamedPipe()
{
var pipeName = GetPipeNameForCurrentOs();
return new NamedPipeClientStream(".", pipeName, PipeDirection.InOut, PipeOptions.Asynchronous, TokenImpersonationLevel.Anonymous);
Expand Down
13 changes: 10 additions & 3 deletions Mpv.WebApi/Controllers/RemoteController.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Mpv.JsonIpc;

namespace Mpv.WebApi.Controllers
{
[Route("api/[controller]/v1")]
[ApiController]
public class Remote : ControllerBase
{
private readonly IApi _api;

public Remote(IApi api)
{
_api = api;
}

[HttpPost("toggle")]
public ActionResult Post()
{
_api.ShowText("asdasd", TimeSpan.FromSeconds(3));

return Ok();
}
}
Expand Down
11 changes: 11 additions & 0 deletions Mpv.WebApi/DependencyModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Autofac;

namespace Mpv.WebApi
{
public class DependencyModule : Module
{
protected override void Load(ContainerBuilder builder)
{
}
}
}
13 changes: 13 additions & 0 deletions Mpv.WebApi/Mpv.WebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,20 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Autofac">
<Version>4.9.1</Version>
</PackageReference>
<PackageReference Include="Autofac.Extensions.DependencyInjection">
<Version>4.4.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Swashbuckle.AspNetCore">
<Version>4.0.1</Version>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Mpv.JsonIpc\Mpv.JsonIpc.csproj" />
</ItemGroup>

</Project>
14 changes: 6 additions & 8 deletions Mpv.WebApi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.IO;
using Autofac.Extensions.DependencyInjection;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace Mpv.WebApi
{
Expand All @@ -19,6 +14,9 @@ public static void Main(string[] args)

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
.UseStartup<Startup>()
.UseKestrel(options => { })
.ConfigureServices(services => services.AddAutofac())
.UseContentRoot(Directory.GetCurrentDirectory());
}
}
Loading

0 comments on commit 9a67bbd

Please sign in to comment.