-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from pyrocumulus/net-batch-status
Added methods to for adding Net batch status and Cumulative batch status
- Loading branch information
Showing
11 changed files
with
436 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Dawn; | ||
using PVOutput.Net.Objects; | ||
using PVOutput.Net.Objects.Core; | ||
using PVOutput.Net.Objects.Modules; | ||
|
||
namespace PVOutput.Net.Builders | ||
{ | ||
/// <summary> | ||
/// Builder that creates batch outputs to post to PVOutput. | ||
/// </summary> | ||
public sealed class BatchNetStatusPostBuilder | ||
{ | ||
internal BatchNetStatusPost _statusPost { get; set; } | ||
|
||
/// <summary> | ||
/// Creates a new builder. | ||
/// </summary> | ||
public BatchNetStatusPostBuilder() | ||
{ | ||
_statusPost = new BatchNetStatusPost(); | ||
} | ||
|
||
/// <summary> | ||
/// Sets the timestamp for the status. | ||
/// </summary> | ||
/// <param name="timestamp">Timestamp.</param> | ||
/// <returns>The builder.</returns> | ||
public BatchNetStatusPostBuilder SetTimeStamp(DateTime timestamp) | ||
{ | ||
Guard.Argument(timestamp, nameof(timestamp)).IsNoFutureDate(); | ||
|
||
_statusPost.Timestamp = timestamp; | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Sets the net power exported for the status. | ||
/// </summary> | ||
/// <param name="powerExported">Net power exported consumed.</param> | ||
/// <returns>The builder.</returns> | ||
public BatchNetStatusPostBuilder SetPowerExported(int powerExported) | ||
{ | ||
Guard.Argument(powerExported, nameof(powerExported)).Min(0); | ||
|
||
_statusPost.PowerExported = powerExported; | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Sets the net power imported for the status. | ||
/// </summary> | ||
/// <param name="powerImported">Net power imported consumed.</param> | ||
/// <returns>The builder.</returns> | ||
public BatchNetStatusPostBuilder SetPowerImported(int powerImported) | ||
{ | ||
Guard.Argument(powerImported, nameof(powerImported)).Min(0); | ||
|
||
_statusPost.PowerImported = powerImported; | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Resets the builder to it's default state. Ready to build a new status. | ||
/// </summary> | ||
public void Reset() => _statusPost = new BatchNetStatusPost(); | ||
|
||
/// <summary> | ||
/// Uses information within the builder to return the built status. | ||
/// </summary> | ||
/// <returns>The status.</returns> | ||
public IBatchNetStatusPost Build() | ||
{ | ||
ValidateStatus(); | ||
return _statusPost; | ||
} | ||
|
||
/// <summary> | ||
/// Uses information within the builder to return the built status. | ||
/// Resets the builder to it's default state after building. | ||
/// </summary> | ||
/// <returns>The status.</returns> | ||
public IBatchNetStatusPost BuildAndReset() | ||
{ | ||
IBatchNetStatusPost result = Build(); | ||
Reset(); | ||
return result; | ||
} | ||
|
||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1303:Do not pass literals as localized parameters", Justification = "Exception messages are non translatable for now")] | ||
private void ValidateStatus() | ||
{ | ||
if (_statusPost.PowerExported == null | ||
&& _statusPost.PowerImported == null) | ||
{ | ||
throw new InvalidOperationException("Status has no power values"); | ||
} | ||
} | ||
} | ||
} |
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
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,25 @@ | ||
using System; | ||
|
||
namespace PVOutput.Net.Objects | ||
{ | ||
/// <summary> | ||
/// A single net batch status used for posting multiple statuses. | ||
/// </summary> | ||
public interface IBatchNetStatusPost | ||
{ | ||
/// <summary> | ||
/// Timestamp for the recorded status. | ||
/// </summary> | ||
DateTime Timestamp { get; set; } | ||
|
||
/// <summary> | ||
/// Total energy generated up to and including the timestamp. | ||
/// </summary> | ||
int? PowerExported { get; set; } | ||
|
||
/// <summary> | ||
/// Actual power being generated at the moment of the timestamp. | ||
/// </summary> | ||
int? PowerImported { get; set; } | ||
} | ||
} |
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,15 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace PVOutput.Net.Objects.Modules | ||
{ | ||
internal class BatchNetStatusPost : IBatchNetStatusPost | ||
{ | ||
public DateTime Timestamp { get; set; } | ||
public int? PowerExported { get; set; } | ||
public int? PowerImported { get; set; } | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/PVOutput.Net/Requests/Modules/AddBatchNetStatusRequest.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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Text; | ||
using PVOutput.Net.Objects.Core; | ||
using PVOutput.Net.Objects; | ||
using PVOutput.Net.Requests.Base; | ||
|
||
namespace PVOutput.Net.Requests.Modules | ||
{ | ||
internal class AddBatchNetStatusRequest : PostRequest | ||
{ | ||
public IEnumerable<IBatchNetStatusPost> StatusPosts { get; set; } | ||
|
||
public override HttpMethod Method => HttpMethod.Post; | ||
|
||
public override string UriTemplate => "addbatchstatus.jsp{?n,data}"; | ||
|
||
public override IDictionary<string, object> GetUriPathParameters() => new Dictionary<string, object> | ||
{ | ||
["n"] = 1, | ||
["data"] = FormatStatusPosts() | ||
}; | ||
|
||
private string FormatStatusPosts() | ||
{ | ||
var sb = new StringBuilder(); | ||
|
||
foreach (IBatchNetStatusPost status in StatusPosts) | ||
{ | ||
sb.Append(FormatStatusPost(status)).Append(';'); | ||
} | ||
|
||
return sb.ToString(); | ||
} | ||
|
||
internal static string FormatStatusPost(IBatchNetStatusPost status) | ||
{ | ||
var sb = new StringBuilder(); | ||
sb.Append(FormatHelper.GetDateAsString(status.Timestamp)); | ||
sb.Append(','); | ||
sb.Append(FormatHelper.GetTimeAsString(status.Timestamp)); | ||
sb.Append(",-1,"); // Skip single field as per documentation | ||
sb.Append(status.PowerExported); | ||
sb.Append(",-1,"); // Skip single field as per documentation | ||
sb.Append(status.PowerImported); | ||
return sb.ToString(); | ||
} | ||
} | ||
} |
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
46 changes: 46 additions & 0 deletions
46
tests/PVOutput.Net.Tests/Modules/Status/AddBatchNetStatusRequestTests.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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System; | ||
using NUnit.Framework; | ||
using PVOutput.Net.Requests.Modules; | ||
using PVOutput.Net.Objects.Modules.Implementations; | ||
using System.Threading.Tasks; | ||
using PVOutput.Net.Objects; | ||
using PVOutput.Net.Tests.Utils; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using RichardSzalay.MockHttp; | ||
using PVOutput.Net.Builders; | ||
using PVOutput.Net.Objects.Modules; | ||
|
||
namespace PVOutput.Net.Tests.Modules.Status | ||
{ | ||
public class AddBatchNetStatusRequestTests : BaseRequestsTest | ||
{ | ||
private string[] GetSplitStatusPostLine(BatchNetStatusPost post) => AddBatchNetStatusRequest.FormatStatusPost(post).Split(','); | ||
|
||
[Test] | ||
public void Parameter_Timestamp_CreatesCorrectUriParameters() | ||
{ | ||
var post = new BatchNetStatusPost() { Timestamp = new DateTime(2020, 2, 1, 13, 12, 20) }; | ||
|
||
string[] postArray = GetSplitStatusPostLine(post); | ||
Assert.That(postArray[0], Is.EqualTo("20200201")); | ||
Assert.That(postArray[1], Is.EqualTo("13:12")); | ||
} | ||
|
||
[Test] | ||
public void Parameter_PowerExported_CreatesCorrectUriParameters() | ||
{ | ||
var post = new BatchNetStatusPost() { PowerExported = 1111 }; | ||
string[] postArray = GetSplitStatusPostLine(post); | ||
Assert.That(postArray[3], Is.EqualTo("1111")); | ||
} | ||
|
||
[Test] | ||
public void Parameter_PowerImported_CreatesCorrectUriParameters() | ||
{ | ||
var post = new BatchNetStatusPost() { PowerImported = 2222 }; | ||
string[] postArray = GetSplitStatusPostLine(post); | ||
Assert.That(postArray[5], Is.EqualTo("2222")); | ||
} | ||
} | ||
} |
Oops, something went wrong.