-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
278 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Mpv.JsonIpc | ||
{ | ||
public class Api | ||
{ | ||
private readonly Ipc _ipc; | ||
|
||
public Api(Ipc ipc) | ||
{ | ||
_ipc = ipc; | ||
} | ||
|
||
public async Task ShowText(string text, TimeSpan duration) | ||
{ | ||
var request = _ipc.CreateCommand(new[] {"show-text", text}, duration.TotalMilliseconds.ToString(CultureInfo.InvariantCulture)); | ||
await _ipc.ExecuteCommand<string>(request); | ||
} | ||
|
||
public async Task<float> GetVolume() | ||
{ | ||
var result = await _ipc.GetProperty<float>(Property.Volume); | ||
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Mpv.JsonIpc | ||
{ | ||
public enum Property | ||
{ | ||
Volume, | ||
Pause | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Mpv.JsonIpc | ||
{ | ||
public static class PropertyEnumConverterExtension | ||
{ | ||
private static readonly Dictionary<Property, string> Map = new Dictionary<Property, string> | ||
{ | ||
{Property.Volume, "volume"}, | ||
{Property.Pause, "pause"}, | ||
}; | ||
|
||
public static string GetStringValue(this Property property) | ||
{ | ||
return Map[property]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Mpv.WebApi.Controllers | ||
{ | ||
[Route("api/[controller]")] | ||
[ApiController] | ||
public class ValuesController : ControllerBase | ||
{ | ||
// GET api/values | ||
[HttpGet] | ||
public ActionResult<IEnumerable<string>> Get() | ||
{ | ||
return new string[] {"value1", "value2"}; | ||
} | ||
|
||
// GET api/values/5 | ||
[HttpGet("{id}")] | ||
public ActionResult<string> Get(int id) | ||
{ | ||
return "value"; | ||
} | ||
|
||
// POST api/values | ||
[HttpPost] | ||
public void Post([FromBody] string value) | ||
{ | ||
} | ||
|
||
// PUT api/values/5 | ||
[HttpPut("{id}")] | ||
public void Put(int id, [FromBody] string value) | ||
{ | ||
} | ||
|
||
// DELETE api/values/5 | ||
[HttpDelete("{id}")] | ||
public void Delete(int id) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp2.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.App" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Mpv.WebApi | ||
{ | ||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
CreateWebHostBuilder(args).Build().Run(); | ||
} | ||
|
||
public static IWebHostBuilder CreateWebHostBuilder(string[] args) => | ||
WebHost.CreateDefaultBuilder(args) | ||
.UseStartup<Startup>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.HttpsPolicy; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Mpv.WebApi | ||
{ | ||
public class Startup | ||
{ | ||
public Startup(IConfiguration configuration) | ||
{ | ||
Configuration = configuration; | ||
} | ||
|
||
public IConfiguration Configuration { get; } | ||
|
||
// This method gets called by the runtime. Use this method to add services to the container. | ||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); | ||
} | ||
|
||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | ||
public void Configure(IApplicationBuilder app, IHostingEnvironment env) | ||
{ | ||
if (env.IsDevelopment()) | ||
{ | ||
app.UseDeveloperExceptionPage(); | ||
} | ||
else | ||
{ | ||
app.UseHsts(); | ||
} | ||
|
||
app.UseHttpsRedirection(); | ||
app.UseMvc(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Debug", | ||
"System": "Information", | ||
"Microsoft": "Information" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
Oops, something went wrong.