Skip to content

Commit

Permalink
2022.12.0
Browse files Browse the repository at this point in the history
  • Loading branch information
LAB02 Research committed Jun 16, 2022
1 parent 6353b64 commit b348431
Show file tree
Hide file tree
Showing 338 changed files with 22,910 additions and 6,849 deletions.
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
using System.Diagnostics.CodeAnalysis;
using Grapevine;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;

namespace HASS.Agent.Notifications
{
/// <summary>
/// Configuration for the Notification API
/// </summary>
[SuppressMessage("ReSharper", "UnusedMember.Global")]
public class NotifierConfiguration
{
public IConfiguration Configuration { get; private set; }

public NotifierConfiguration(IConfiguration configuration)
{
Configuration = configuration;
}

public void ConfigureServices(IServiceCollection services)
{
services.AddLogging(loggingBuilder =>
{
loggingBuilder.ClearProviders();
loggingBuilder.AddSerilog();
});
}

public void ConfigureServer(IRestServer server)
{
server.Prefixes.Add($"http://+:{Variables.AppSettings.NotifierApiPort}/");
server.Router.Options.SendExceptionMessages = true;
}
}
}
using System.Diagnostics.CodeAnalysis;
using Grapevine;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;

namespace HASS.Agent.API
{
/// <summary>
/// Configuration for the local API
/// </summary>
[SuppressMessage("ReSharper", "UnusedMember.Global")]
public class ApiConfiguration
{
public IConfiguration Configuration { get; private set; }

public ApiConfiguration(IConfiguration configuration)
{
Configuration = configuration;
}

public void ConfigureServices(IServiceCollection services)
{
services.AddLogging(loggingBuilder =>
{
loggingBuilder.ClearProviders();
loggingBuilder.AddSerilog();
});
}

public void ConfigureServer(IRestServer server)
{
server.Prefixes.Add($"http://+:{Variables.AppSettings.NotifierApiPort}/");
server.Router.Options.SendExceptionMessages = true;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;

namespace HASS.Agent.Notifications
{
/// <summary>
/// Deserialization options for the Notification API
/// </summary>
[SuppressMessage("ReSharper", "UnusedMember.Global")]
public abstract class NotifierDeserialization
{
public static JsonSerializerOptions SerializerOptions = new()
{
PropertyNameCaseInsensitive = true
};

public static async Task<T> DeserializeAsync<T>(Stream stream)
{
return await DeserializeAsync<T>(stream, CancellationToken.None);
}

public static async Task<T> DeserializeAsync<T>(Stream stream, CancellationToken token)
{
return await JsonSerializer.DeserializeAsync<T>(stream, SerializerOptions, token);
}
}
}
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;

namespace HASS.Agent.API
{
/// <summary>
/// Deserialization options for the local API
/// </summary>
[SuppressMessage("ReSharper", "UnusedMember.Global")]
public abstract class ApiDeserialization
{
public static JsonSerializerOptions SerializerOptions = new()
{
PropertyNameCaseInsensitive = true
};

public static async Task<T> DeserializeAsync<T>(Stream stream)
{
return await DeserializeAsync<T>(stream, CancellationToken.None);
}

public static async Task<T> DeserializeAsync<T>(Stream stream, CancellationToken token)
{
return await JsonSerializer.DeserializeAsync<T>(stream, SerializerOptions, token);
}
}
}
81 changes: 81 additions & 0 deletions src/HASS.Agent/API/ApiEndpoints.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using Grapevine;
using HASS.Agent.Enums;
using HASS.Agent.Extensions;
using HASS.Agent.Managers;
using HASS.Agent.Media;
using HASS.Agent.Models.HomeAssistant;
using Serilog;
using HttpMethod = System.Net.Http.HttpMethod;

namespace HASS.Agent.API
{
/// <summary>
/// Endpoints for the local API
/// </summary>
public class ApiEndpoints : ApiDeserialization
{
/// <summary>
/// Notification route, handles all incoming notifications on '/notify'
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public static async Task NotifyRoute(IHttpContext context)
{
try
{
var notification = await DeserializeAsync<Notification>(context.Request.InputStream, context.CancellationToken);
_ = Task.Run(() => NotificationManager.ShowNotification(notification));
}
catch (Exception ex)
{
Log.Fatal(ex, "[LOCALAPI] Error while processing incoming notification: {ex}", ex.Message);
}
finally
{
await context.Response.SendResponseAsync("notification processed");
}
}

/// <summary>
/// Media route, handles all incoming requests on '/media'
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public static async Task MediaRoute(IHttpContext context)
{
try
{
if (context.Request.HttpMethod != HttpMethod.Get) return;
if (!context.Request.QueryString.HasKeys()) return;

var apiMediaRequest = context.Request.QueryString.ParseApiMediaRequest();

switch (apiMediaRequest.RequestType)
{
case MediaRequestType.Unknown:
// unable to parse, drop
return;

case MediaRequestType.Request:
// HA's waiting for info, have the mediamanager return it
await context.Response.SendResponseAsync(MediaManager.ProcessRequest(apiMediaRequest.Request));
break;

case MediaRequestType.Command:
// have HA wait for us to complete
MediaManager.ProcessCommand(apiMediaRequest.Command);
break;

case MediaRequestType.PlayMedia:
// media might take a while, process async
_ = Task.Run(() => MediaManager.ProcessMedia(apiMediaRequest.MediaUri));
break;
}
}
catch (Exception ex)
{
Log.Fatal(ex, "[LOCALAPI] Error while processing incoming media request: {ex}", ex.Message);
}
}
}
}
Loading

0 comments on commit b348431

Please sign in to comment.