Skip to content

Commit

Permalink
Added WebApiProject
Browse files Browse the repository at this point in the history
  • Loading branch information
Zexuz committed Feb 16, 2019
1 parent 08f630e commit 42dd794
Show file tree
Hide file tree
Showing 12 changed files with 278 additions and 64 deletions.
69 changes: 6 additions & 63 deletions Mpv.JsonIpc.Runner/Program.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;

namespace Mpv.JsonIpc.Runner
{
Expand All @@ -17,75 +14,21 @@ static void Main(string[] args)

using (var manager = new Manager(pipeFactory))
{
var api = new Api(manager);
var ipc = new Ipc(manager);
var api = new Api(ipc);

while (true)
{
// var volume = api.GetVolume().Result;
// Console.WriteLine($"The volume is {volume}");
api.ShowText("Robin",TimeSpan.FromSeconds(10)).Wait();
Thread.Sleep(5000);
// api.ShowText("Robin",TimeSpan.FromSeconds(10)).Wait();
var response = ipc.CycleProperty<Response<object>>(Property.Pause);
Thread.Sleep(1000 * 5);
}
}
}
}

public class Api
{
private readonly Manager _manager;

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

public async Task ShowText(string text, TimeSpan duration)
{
var request = new Request
{
Command = new[] {"show-text", text, duration.TotalMilliseconds.ToString(CultureInfo.InvariantCulture)},
RequestId = 1337
};


await _manager.Execute<string>(request);
}

public async Task<float> GetVolume()
{
var result = await GetProperty<float>(Property.Volume);
return result.Data;
}

private async Task<Response<T>> GetProperty<T>(Property property, params string[] args)
{
var request = new Request
{
Command = new[] {"get_property", property.GetStringValue()}.Concat(args).ToArray(),
RequestId = 1337
};


return await _manager.Execute<T>(request);
}
}


public enum Property
{
Volume
}

public static class PropertyEnumConverterExtension
{
private static readonly Dictionary<Property, string> Map = new Dictionary<Property, string>
{
{Property.Volume, "volume"}
};

public static string GetStringValue(this Property property)
{
return Map[property];
}
}

}
95 changes: 95 additions & 0 deletions Mpv.JsonIpc/Api.cs
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);
}
}
}
8 changes: 8 additions & 0 deletions Mpv.JsonIpc/Property.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Mpv.JsonIpc
{
public enum Property
{
Volume,
Pause
}
}
18 changes: 18 additions & 0 deletions Mpv.JsonIpc/PropertyEnumConverterExtension.cs
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];
}
}
}
2 changes: 1 addition & 1 deletion Mpv.JsonIpc/Request.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Mpv.JsonIpc
public class Request
{
[JsonProperty("command")]
public string[] Command { get; set; }
public object[] Command { get; set; }

[JsonProperty("request_id")]
public int RequestId { get; set; }
Expand Down
45 changes: 45 additions & 0 deletions Mpv.WebApi/Controllers/ValuesController.cs
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)
{
}
}
}
11 changes: 11 additions & 0 deletions Mpv.WebApi/Mpv.WebApi.csproj
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>
24 changes: 24 additions & 0 deletions Mpv.WebApi/Program.cs
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>();
}
}
47 changes: 47 additions & 0 deletions Mpv.WebApi/Startup.cs
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();
}
}
}
9 changes: 9 additions & 0 deletions Mpv.WebApi/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
8 changes: 8 additions & 0 deletions Mpv.WebApi/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}
Loading

0 comments on commit 42dd794

Please sign in to comment.