Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notification Service #37

Merged
merged 3 commits into from
Aug 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 182 additions & 0 deletions src/PVOutput.Net/Modules/NotificationService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Dawn;
using PVOutput.Net.Builders;
using PVOutput.Net.Objects;
using PVOutput.Net.Objects.Core;
using PVOutput.Net.Requests.Handler;
using PVOutput.Net.Requests.Modules;
using PVOutput.Net.Responses;

namespace PVOutput.Net.Modules
{
/// <summary>
/// The Notification service enables (de-)registering alert notifications.
/// <para>See the official <see href="https://pvoutput.org/help.html#api-registernotification">API information</see>.</para>
/// </summary>
public sealed class NotificationService : BaseService
{
/// <summary>
/// Alert for a new private message.
/// </summary>
public const int PrivateMessageAlert = 1;

/// <summary>
/// Alert for joining a new team.
/// </summary>
public const int JoinedTeamAlert = 3;

/// <summary>
/// Alert for an added favourite.
/// </summary>
public const int AddedFavouriteAlert = 4;

/// <summary>
/// Alert for high consumption.
/// </summary>
public const int HighConsumptionAlert = 5;

/// <summary>
/// Alert for an idle system.
/// </summary>
public const int SystemIdleAlert = 6;

/// <summary>
/// Alert for low generation.
/// </summary>
public const int LowGenerationAlert = 8;

/// <summary>
/// Alert for low performance.
/// </summary>
public const int PerformanceAlert = 11;

/// <summary>
/// Alerty for a high standby cost alert.
/// </summary>
public const int StandbyCostAlert = 14;

/// <summary>
/// Alert for extended data element 7.
/// </summary>
public const int ExtendedDataV7Alert = 15;

/// <summary>
/// Alert for extended data element 8.
/// </summary>
public const int ExtendedDataV8Alert = 16;

/// <summary>
/// Alert for extended data element 9.
/// </summary>
public const int ExtendedDataV9Alert = 17;

/// <summary>
/// Alert for extended data element 10.
/// </summary>
public const int ExtendedDataV10Alert = 18;

/// <summary>
/// Alert for extended data element 11.
/// </summary>
public const int ExtendedDataV11Alert = 19;

/// <summary>
/// Alert for extended data element 12.
/// </summary>
public const int ExtendedDataV12Alert = 20;

/// <summary>
/// Alert for high net power.
/// </summary>
public const int HighNetPowerAlert = 23;

/// <summary>
/// Alert for low net power.
/// </summary>
public const int LowNetPowerAlert = 24;

internal NotificationService(PVOutputClient client) : base(client)
{
}

/// <summary>
/// Registers an application for a notification.
/// </summary>
/// <param name="applicationId">ApplicationId to register the notification under.</param>
/// <param name="callbackUrl">The url that should get called for each notification.</param>
/// <param name="alertType">A specific type of alert to send, leave empty for all alert types.</param>
/// <param name="cancellationToken">A cancellation token for the request.</param>
/// <returns>If the operation succeeded.</returns>
public Task<PVOutputBasicResponse> RegisterNotificationAsync(string applicationId, string callbackUrl, int? alertType = null, CancellationToken cancellationToken = default)
{
var loggingScope = new Dictionary<string, object>()
{
[LoggingEvents.RequestId] = LoggingEvents.NotificationService_RegisterNotification,
[LoggingEvents.Parameter_ApplicationId] = applicationId,
[LoggingEvents.Parameter_CallBackUrl] = callbackUrl,
[LoggingEvents.Parameter_AlertType] = alertType

};

Guard.Argument(applicationId, nameof(applicationId)).MaxLength(100).NotEmpty();
Guard.Argument(callbackUrl, nameof(callbackUrl)).MaxLength(150).NotEmpty();

var handler = new RequestHandler(Client);
return handler.ExecutePostRequestAsync(new RegisterNotificationRequest() { ApplicationId = applicationId, CallbackUri = new Uri(callbackUrl), AlertType = alertType }, loggingScope, cancellationToken);
}


/// <summary>
/// Registers an application for a notification.
/// </summary>
/// <param name="applicationId">ApplicationId to register the notification under.</param>
/// <param name="callbackUri">The uri that should get called for each notification.</param>
/// <param name="alertType">A specific type of alert to send, leave empty for all alert types.</param>
/// <param name="cancellationToken">A cancellation token for the request.</param>
/// <returns>If the operation succeeded.</returns>
public Task<PVOutputBasicResponse> RegisterNotificationAsync(string applicationId, Uri callbackUri, int? alertType = null, CancellationToken cancellationToken = default)
{
var loggingScope = new Dictionary<string, object>()
{
[LoggingEvents.RequestId] = LoggingEvents.NotificationService_RegisterNotification,
[LoggingEvents.Parameter_ApplicationId] = applicationId,
[LoggingEvents.Parameter_CallBackUrl] = callbackUri.AbsoluteUri,
[LoggingEvents.Parameter_AlertType] = alertType

};

Guard.Argument(applicationId, nameof(applicationId)).MaxLength(100).NotEmpty();
Guard.Argument(callbackUri, nameof(callbackUri)).NotNull();
Guard.Argument(callbackUri.AbsoluteUri, nameof(callbackUri)).MaxLength(150).NotEmpty();

var handler = new RequestHandler(Client);
return handler.ExecutePostRequestAsync(new RegisterNotificationRequest() { ApplicationId = applicationId, CallbackUri = callbackUri, AlertType = alertType }, loggingScope, cancellationToken);
}

/// <summary>
/// Deregisters an application for a notification.
/// </summary>
/// <param name="applicationId">ApplicationId to register the notification under.</param>
/// <param name="alertType">A specific type of alert to send, leave empty for all alert types.</param>
/// <param name="cancellationToken">A cancellation token for the request.</param>
/// <returns>If the operation succeeded.</returns>
public Task<PVOutputBasicResponse> DeregisterNotificationAsync(string applicationId, int? alertType = null, CancellationToken cancellationToken = default)
{
var loggingScope = new Dictionary<string, object>()
{
[LoggingEvents.RequestId] = LoggingEvents.NotificationService_DeregisterNotification,
[LoggingEvents.Parameter_ApplicationId] = applicationId,
[LoggingEvents.Parameter_AlertType] = alertType
};

Guard.Argument(applicationId, nameof(applicationId)).MaxLength(100);

var handler = new RequestHandler(Client);
return handler.ExecutePostRequestAsync(new DeregisterNotificationRequest { ApplicationId = applicationId, AlertType = alertType }, loggingScope, cancellationToken);
}
}
}
2 changes: 1 addition & 1 deletion src/PVOutput.Net/Objects/Core/BaseObjectStringReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public async Task<TReturnType> ReadObjectAsync(TextReader reader, CancellationTo
if (reader != null && reader.Peek() >= 0)
{
TReturnType output = CreateObjectInstance();
ParseProperties(output, reader);
ParseProperties(output, reader, cancellationToken);
return output;
}

Expand Down
5 changes: 5 additions & 0 deletions src/PVOutput.Net/Objects/Core/LoggingEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ internal class LoggingEvents
public const string Parameter_Search_TeamName = "Teamname";
public const string Parameter_Search_Orientation = "Orientation";
public const string Parameter_Search_Tilt = "Tilt";
public const string Parameter_ApplicationId = "ApplicationId";
public const string Parameter_CallBackUrl = "CallBackUrl";
public const string Parameter_AlertType = "AlertType";

/*
* RequestHandler base events
Expand Down Expand Up @@ -92,5 +95,7 @@ internal class LoggingEvents
public static readonly EventId TeamService_GetTeam = new EventId(21101, "GetTeam");
public static readonly EventId TeamService_JoinTeam = new EventId(21102, "JoinTeam");
public static readonly EventId TeamService_LeaveTeam = new EventId(21103, "LeaveTeam");
public static readonly EventId NotificationService_RegisterNotification = new EventId(21201, "RegisterNotification");
public static readonly EventId NotificationService_DeregisterNotification = new EventId(21202, "DeregisterNotification");
}
}
8 changes: 4 additions & 4 deletions src/PVOutput.Net/PVOutput.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ https://github.com/pyrocumulus/pvoutput.net/blob/master/CHANGELOG.md</PackageRel
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Dawn.Guard" Version="1.11.0" />
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8">
<PackageReference Include="Dawn.Guard" Version="1.12.0" />
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.1.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.7" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.1.7" />
<PackageReference Include="System.Runtime" Version="4.3.1" />
<PackageReference Include="Tavis.UriTemplates" Version="1.1.1" />
</ItemGroup>
Expand Down
7 changes: 7 additions & 0 deletions src/PVOutput.Net/PVOutputClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ internal ILogger<PVOutputClient> Logger
/// </summary>
public SearchService Search { get; private set; }

/// <summary>
/// The notification service
/// <para>See the official <see href="https://pvoutput.org/help.html#api-registernotification">API information</see>.</para>
/// </summary>
public NotificationService Notification { get; private set; }

/// <summary>
/// Creates a new PVOutputClient.
/// </summary>
Expand Down Expand Up @@ -177,6 +183,7 @@ private void CreateServices()
Insolation = new InsolationService(this);
Supply = new SupplyService(this);
Search = new SearchService(this);
Notification = new NotificationService(this);
}
}
}
25 changes: 25 additions & 0 deletions src/PVOutput.Net/Requests/Modules/DeregisterNotificationRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Collections.Generic;
using System.Net.Http;
using PVOutput.Net.Requests.Base;

namespace PVOutput.Net.Requests.Modules
{
internal class DeregisterNotificationRequest : PostRequest
{
public string ApplicationId { get; set; }
public int? AlertType { get; set; }

public override HttpMethod Method => HttpMethod.Post;

public override string UriTemplate => "deregisternotification.jsp{?appid,type}";

public override IDictionary<string, object> GetUriPathParameters()
{
return new Dictionary<string, object>
{
["appid"] = ApplicationId,
["type"] = AlertType ?? 0
};
}
}
}
2 changes: 1 addition & 1 deletion src/PVOutput.Net/Requests/Modules/OutputRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal class OutputRequest : GetRequest<IOutput>
["insolation"] = Insolation ? 1 : 0
};

private string GetAggregationParameter(AggregationPeriod? aggregationPeriod)
private static string GetAggregationParameter(AggregationPeriod? aggregationPeriod)
{
if (aggregationPeriod == null)
{
Expand Down
27 changes: 27 additions & 0 deletions src/PVOutput.Net/Requests/Modules/RegisterNotificationRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Collections.Generic;
using System.Net.Http;
using PVOutput.Net.Requests.Base;

namespace PVOutput.Net.Requests.Modules
{
internal class RegisterNotificationRequest : PostRequest
{
public string ApplicationId { get; set; }
public System.Uri CallbackUri { get; set; }
public int? AlertType { get; set; }

public override HttpMethod Method => HttpMethod.Post;

public override string UriTemplate => "registernotification.jsp{?appid,url,type}";

public override IDictionary<string, object> GetUriPathParameters()
{
return new Dictionary<string, object>
{
["appid"] = ApplicationId,
["url"] = CallbackUri?.AbsoluteUri ?? "",
["type"] = AlertType ?? 0
};
}
}
}
Loading