Rewrite electricity server to .NET web api
This commit is contained in:
36
src/Electricity.Api/Models/Day.cs
Normal file
36
src/Electricity.Api/Models/Day.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
namespace Electricity.Api.Models;
|
||||
|
||||
public record Day(uint DateTime, double TotalPowerUse, double TotalPowerReturn, double TotalGasUse)
|
||||
{
|
||||
public static Day Empty(DateOnly date)
|
||||
{
|
||||
return new Day(
|
||||
ConvertToEpoch(date.ToDateTime(new TimeOnly(0, 0), DateTimeKind.Utc)),
|
||||
0, 0, 0);
|
||||
}
|
||||
|
||||
public static Day FromEntity(Entities.ElectricityLog entity)
|
||||
{
|
||||
return new Day(
|
||||
DateTime: ConvertToEpoch(ParseEntityDateTime(entity.Date, entity.TimeUtc)),
|
||||
TotalPowerUse: entity.TotalPowerConsumptionDay + entity.TotalPowerConsumptionNight,
|
||||
TotalPowerReturn: entity.TotalPowerReturnDay + entity.TotalPowerReturnNight,
|
||||
TotalGasUse: entity.GasConsumptionInCubicMeters
|
||||
);
|
||||
}
|
||||
|
||||
private static DateTime ParseEntityDateTime(string date, string timeUtc)
|
||||
{
|
||||
return System.DateTime
|
||||
.Parse(
|
||||
$"{date}T{timeUtc}Z",
|
||||
null,
|
||||
System.Globalization.DateTimeStyles.AssumeUniversal)
|
||||
.ToUniversalTime();
|
||||
}
|
||||
|
||||
private static uint ConvertToEpoch(DateTime dateTime)
|
||||
{
|
||||
return (uint)Math.Round((dateTime - System.DateTime.UnixEpoch).TotalSeconds);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user