-
-
Notifications
You must be signed in to change notification settings - Fork 75
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
LAB02 Research
committed
Jun 16, 2022
1 parent
6353b64
commit b348431
Showing
338 changed files
with
22,910 additions
and
6,849 deletions.
There are no files selected for viewing
76 changes: 38 additions & 38 deletions
76
...nt/Notifications/NotifierConfiguration.cs → src/HASS.Agent/API/ApiConfiguration.cs
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 |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
54 changes: 27 additions & 27 deletions
54
.../Notifications/NotifierDeserialization.cs → src/HASS.Agent/API/ApiDeserialization.cs
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 |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
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,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); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.