41ac93733e
- Guard logger calls with IsEnabled so value-type arguments are not boxed when the level is disabled - Make helper members static, narrow write-only properties to init or private get, and delete members no call site reaches - Flatten nested conditionals and convert if/else chains into switch and ternary form - Drop the unused *MonitorInit interfaces; NetDaemon instantiates the concrete app classes - Drop the unused IConfiguration parameter from AddNordPool and AddPowerMonitor, and the unused IEntityManager.CreateEntitiesAsync - Remove the ignored parameters of the boiler run-time tracker stub - Pin the NordPool DTO price mapping explicitly so the getter use is visible to static analysis - Fail Qodana on any finding and record the registration changes in AGENTS.md
96 lines
2.9 KiB
C#
96 lines
2.9 KiB
C#
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace NordPool.Models;
|
|
|
|
[JsonConverter(typeof(CompactNordPoolDayPriceConverter))]
|
|
public readonly struct CompactNordPoolDayPrice
|
|
{
|
|
private const long NullTimestampSentinel = 0L;
|
|
|
|
public long Start { get; init; }
|
|
public long End { get; init; }
|
|
public double? Price { get; init; }
|
|
|
|
public static CompactNordPoolDayPrice FromDayPrice(NordPoolDayPrice dayPrice)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(dayPrice);
|
|
|
|
return new CompactNordPoolDayPrice
|
|
{
|
|
Start = dayPrice.Start.HasValue
|
|
? ToUnixSeconds(dayPrice.Start.Value)
|
|
: NullTimestampSentinel,
|
|
End = dayPrice.End.HasValue
|
|
? ToUnixSeconds(dayPrice.End.Value)
|
|
: NullTimestampSentinel,
|
|
Price = dayPrice.Price
|
|
};
|
|
}
|
|
|
|
private static long ToUnixSeconds(DateTime dateTime)
|
|
{
|
|
var utc = dateTime.Kind == DateTimeKind.Utc
|
|
? dateTime
|
|
: dateTime.ToUniversalTime();
|
|
|
|
return new DateTimeOffset(utc, TimeSpan.Zero).ToUnixTimeSeconds();
|
|
}
|
|
}
|
|
|
|
internal sealed class CompactNordPoolDayPriceConverter : JsonConverter<CompactNordPoolDayPrice>
|
|
{
|
|
public override CompactNordPoolDayPrice Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
{
|
|
if (reader.TokenType != JsonTokenType.StartArray)
|
|
{
|
|
throw new JsonException("Expected start of array.");
|
|
}
|
|
|
|
if (!reader.Read() || reader.TokenType != JsonTokenType.Number)
|
|
{
|
|
throw new JsonException("Expected number for Start at position 0.");
|
|
}
|
|
|
|
var start = reader.GetInt64();
|
|
|
|
if (!reader.Read() || reader.TokenType != JsonTokenType.Number)
|
|
{
|
|
throw new JsonException("Expected number for End at position 1.");
|
|
}
|
|
|
|
var end = reader.GetInt64();
|
|
|
|
if (!reader.Read() || (reader.TokenType != JsonTokenType.Number && reader.TokenType != JsonTokenType.Null))
|
|
{
|
|
throw new JsonException("Expected number or null for Price at position 2.");
|
|
}
|
|
|
|
double? price = reader.TokenType == JsonTokenType.Null ? null : reader.GetDouble();
|
|
|
|
reader.Read();
|
|
|
|
return reader.TokenType == JsonTokenType.EndArray
|
|
? new CompactNordPoolDayPrice { Start = start, End = end, Price = price }
|
|
: throw new JsonException("Expected end of array.");
|
|
}
|
|
|
|
public override void Write(Utf8JsonWriter writer, CompactNordPoolDayPrice value, JsonSerializerOptions options)
|
|
{
|
|
writer.WriteStartArray();
|
|
writer.WriteNumberValue(value.Start);
|
|
writer.WriteNumberValue(value.End);
|
|
|
|
if (value.Price.HasValue)
|
|
{
|
|
writer.WriteNumberValue(value.Price.Value);
|
|
}
|
|
else
|
|
{
|
|
writer.WriteNullValue();
|
|
}
|
|
|
|
writer.WriteEndArray();
|
|
}
|
|
}
|