refactor: reorganize Infrastructure layer into folder structure with DI
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace GreenHome.Infrastructure;
|
||||
namespace GreenHome.Infrastructure.Data;
|
||||
|
||||
public sealed class GreenHomeDbContext : DbContext
|
||||
{
|
||||
@@ -26,6 +26,7 @@ public sealed class GreenHomeDbContext : DbContext
|
||||
public DbSet<Domain.ChecklistItemCompletion> ChecklistItemCompletions => Set<Domain.ChecklistItemCompletion>();
|
||||
public DbSet<Domain.DevicePost> DevicePosts => Set<Domain.DevicePost>();
|
||||
public DbSet<Domain.DevicePostImage> DevicePostImages => Set<Domain.DevicePostImage>();
|
||||
public DbSet<Domain.PowerOutageLog> PowerOutageLogs => Set<Domain.PowerOutageLog>();
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
@@ -372,5 +373,22 @@ public sealed class GreenHomeDbContext : DbContext
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
b.HasIndex(x => x.DevicePostId);
|
||||
});
|
||||
|
||||
modelBuilder.Entity<Domain.PowerOutageLog>(b =>
|
||||
{
|
||||
b.ToTable("PowerOutageLogs");
|
||||
b.HasKey(x => x.Id);
|
||||
b.Property(x => x.PreviousPower).IsRequired();
|
||||
b.Property(x => x.CurrentPower).IsRequired();
|
||||
b.Property(x => x.EventType).IsRequired().HasConversion<int>();
|
||||
b.Property(x => x.PersianDate).HasMaxLength(10);
|
||||
b.HasOne(x => x.Device)
|
||||
.WithMany()
|
||||
.HasForeignKey(x => x.DeviceId)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
b.HasIndex(x => new { x.DeviceId, x.TimestampUtc });
|
||||
b.HasIndex(x => new { x.DeviceId, x.PersianYear, x.PersianMonth });
|
||||
b.HasIndex(x => x.EventType);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
// <auto-generated />
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using GreenHome.Infrastructure;
|
||||
using GreenHome.Infrastructure.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
@@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GreenHome.Infrastructure.Migrations
|
||||
namespace GreenHome.Infrastructure.Data.Migrations
|
||||
{
|
||||
[DbContext(typeof(GreenHomeDbContext))]
|
||||
[Migration("20251005195032_init")]
|
||||
@@ -1,9 +1,9 @@
|
||||
using System;
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GreenHome.Infrastructure.Migrations
|
||||
namespace GreenHome.Infrastructure.Data.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class init : Migration
|
||||
@@ -1,6 +1,6 @@
|
||||
// <auto-generated />
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using GreenHome.Infrastructure;
|
||||
using GreenHome.Infrastructure.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
@@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GreenHome.Infrastructure.Migrations
|
||||
namespace GreenHome.Infrastructure.Data.Migrations
|
||||
{
|
||||
[DbContext(typeof(GreenHomeDbContext))]
|
||||
[Migration("20251008205106_addminmaxdanger")]
|
||||
@@ -1,9 +1,9 @@
|
||||
using System;
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GreenHome.Infrastructure.Migrations
|
||||
namespace GreenHome.Infrastructure.Data.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class addminmaxdanger : Migration
|
||||
@@ -1,6 +1,6 @@
|
||||
// <auto-generated />
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using GreenHome.Infrastructure;
|
||||
using GreenHome.Infrastructure.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
@@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GreenHome.Infrastructure.Migrations
|
||||
namespace GreenHome.Infrastructure.Data.Migrations
|
||||
{
|
||||
[DbContext(typeof(GreenHomeDbContext))]
|
||||
[Migration("20251118204845_UpdateDeviceUserRelationship")]
|
||||
@@ -1,9 +1,9 @@
|
||||
using System;
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GreenHome.Infrastructure.Migrations
|
||||
namespace GreenHome.Infrastructure.Data.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class UpdateDeviceUserRelationship : Migration
|
||||
@@ -1,6 +1,6 @@
|
||||
// <auto-generated />
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using GreenHome.Infrastructure;
|
||||
using GreenHome.Infrastructure.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
@@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GreenHome.Infrastructure.Migrations
|
||||
namespace GreenHome.Infrastructure.Data.Migrations
|
||||
{
|
||||
[DbContext(typeof(GreenHomeDbContext))]
|
||||
[Migration("20251119133827_AddUserRolesAndDeviceUsers")]
|
||||
@@ -1,8 +1,8 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GreenHome.Infrastructure.Migrations
|
||||
namespace GreenHome.Infrastructure.Data.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class AddUserRolesAndDeviceUsers : Migration
|
||||
@@ -1,6 +1,6 @@
|
||||
// <auto-generated />
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using GreenHome.Infrastructure;
|
||||
using GreenHome.Infrastructure.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
@@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GreenHome.Infrastructure.Migrations
|
||||
namespace GreenHome.Infrastructure.Data.Migrations
|
||||
{
|
||||
[DbContext(typeof(GreenHomeDbContext))]
|
||||
[Migration("20251119135914_AddAlertNotifications")]
|
||||
@@ -1,9 +1,9 @@
|
||||
using System;
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GreenHome.Infrastructure.Migrations
|
||||
namespace GreenHome.Infrastructure.Data.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class AddAlertNotifications : Migration
|
||||
@@ -1,6 +1,6 @@
|
||||
// <auto-generated />
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using GreenHome.Infrastructure;
|
||||
using GreenHome.Infrastructure.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
@@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GreenHome.Infrastructure.Migrations
|
||||
namespace GreenHome.Infrastructure.Data.Migrations
|
||||
{
|
||||
[DbContext(typeof(GreenHomeDbContext))]
|
||||
[Migration("20251119142729_FixAlertNotifications")]
|
||||
@@ -1,8 +1,8 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GreenHome.Infrastructure.Migrations
|
||||
namespace GreenHome.Infrastructure.Data.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class FixAlertNotifications : Migration
|
||||
@@ -1,6 +1,6 @@
|
||||
// <auto-generated />
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using GreenHome.Infrastructure;
|
||||
using GreenHome.Infrastructure.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
@@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GreenHome.Infrastructure.Migrations
|
||||
namespace GreenHome.Infrastructure.Data.Migrations
|
||||
{
|
||||
[DbContext(typeof(GreenHomeDbContext))]
|
||||
[Migration("20251127125625_AddServerTimestampUtc")]
|
||||
@@ -1,9 +1,9 @@
|
||||
using System;
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GreenHome.Infrastructure.Migrations
|
||||
namespace GreenHome.Infrastructure.Data.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class AddServerTimestampUtc : Migration
|
||||
@@ -1,6 +1,6 @@
|
||||
// <auto-generated />
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using GreenHome.Infrastructure;
|
||||
using GreenHome.Infrastructure.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
@@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GreenHome.Infrastructure.Migrations
|
||||
namespace GreenHome.Infrastructure.Data.Migrations
|
||||
{
|
||||
[DbContext(typeof(GreenHomeDbContext))]
|
||||
[Migration("20251216113127_AddAIQueryTable")]
|
||||
@@ -1,9 +1,9 @@
|
||||
using System;
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GreenHome.Infrastructure.Migrations
|
||||
namespace GreenHome.Infrastructure.Data.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class AddAIQueryTable : Migration
|
||||
@@ -1,6 +1,6 @@
|
||||
// <auto-generated />
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using GreenHome.Infrastructure;
|
||||
using GreenHome.Infrastructure.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
@@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GreenHome.Infrastructure.Migrations
|
||||
namespace GreenHome.Infrastructure.Data.Migrations
|
||||
{
|
||||
[DbContext(typeof(GreenHomeDbContext))]
|
||||
[Migration("20251216120357_adddailyreport")]
|
||||
@@ -1,9 +1,9 @@
|
||||
using System;
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GreenHome.Infrastructure.Migrations
|
||||
namespace GreenHome.Infrastructure.Data.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class adddailyreport : Migration
|
||||
@@ -1,6 +1,6 @@
|
||||
// <auto-generated />
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using GreenHome.Infrastructure;
|
||||
using GreenHome.Infrastructure.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
@@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GreenHome.Infrastructure.Migrations
|
||||
namespace GreenHome.Infrastructure.Data.Migrations
|
||||
{
|
||||
[DbContext(typeof(GreenHomeDbContext))]
|
||||
[Migration("20251216131032_UpdateAlertSystemWithConditions")]
|
||||
@@ -1,9 +1,9 @@
|
||||
using System;
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GreenHome.Infrastructure.Migrations
|
||||
namespace GreenHome.Infrastructure.Data.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class UpdateAlertSystemWithConditions : Migration
|
||||
@@ -2,7 +2,7 @@ using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GreenHome.Infrastructure.Migrations
|
||||
namespace GreenHome.Infrastructure.Data.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class AddDailyReportsTable : Migration
|
||||
@@ -1,6 +1,6 @@
|
||||
// <auto-generated />
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using GreenHome.Infrastructure;
|
||||
using GreenHome.Infrastructure.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
@@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GreenHome.Infrastructure.Migrations
|
||||
namespace GreenHome.Infrastructure.Data.Migrations
|
||||
{
|
||||
[DbContext(typeof(GreenHomeDbContext))]
|
||||
[Migration("20251216204600_AddAllNewFeatures")]
|
||||
@@ -1,9 +1,9 @@
|
||||
using System;
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GreenHome.Infrastructure.Migrations
|
||||
namespace GreenHome.Infrastructure.Data.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class AddAllNewFeatures : Migration
|
||||
@@ -1,6 +1,6 @@
|
||||
// <auto-generated />
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using GreenHome.Infrastructure;
|
||||
using GreenHome.Infrastructure.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
@@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GreenHome.Infrastructure.Migrations
|
||||
namespace GreenHome.Infrastructure.Data.Migrations
|
||||
{
|
||||
[DbContext(typeof(GreenHomeDbContext))]
|
||||
[Migration("20251217154130_AddDeviceTokenAndPhoneFields")]
|
||||
@@ -1,9 +1,9 @@
|
||||
using System;
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GreenHome.Infrastructure.Migrations
|
||||
namespace GreenHome.Infrastructure.Data.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class AddDeviceTokenAndPhoneFields : Migration
|
||||
@@ -1,6 +1,6 @@
|
||||
// <auto-generated />
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using GreenHome.Infrastructure;
|
||||
using GreenHome.Infrastructure.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
@@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GreenHome.Infrastructure.Migrations
|
||||
namespace GreenHome.Infrastructure.Data.Migrations
|
||||
{
|
||||
[DbContext(typeof(GreenHomeDbContext))]
|
||||
[Migration("20260106150615_POWER_VOLTAGE_FIELDS")]
|
||||
@@ -1,8 +1,8 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GreenHome.Infrastructure.Migrations
|
||||
namespace GreenHome.Infrastructure.Data.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class POWER_VOLTAGE_FIELDS : Migration
|
||||
@@ -1,6 +1,6 @@
|
||||
// <auto-generated />
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using GreenHome.Infrastructure;
|
||||
using GreenHome.Infrastructure.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
@@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GreenHome.Infrastructure.Migrations
|
||||
namespace GreenHome.Infrastructure.Data.Migrations
|
||||
{
|
||||
[DbContext(typeof(GreenHomeDbContext))]
|
||||
[Migration("20260106151206_FIX_VOLTAGE_TYPE")]
|
||||
@@ -1,8 +1,8 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GreenHome.Infrastructure.Migrations
|
||||
namespace GreenHome.Infrastructure.Data.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class FIX_VOLTAGE_TYPE : Migration
|
||||
@@ -1,6 +1,6 @@
|
||||
// <auto-generated />
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using GreenHome.Infrastructure;
|
||||
using GreenHome.Infrastructure.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
@@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GreenHome.Infrastructure.Migrations
|
||||
namespace GreenHome.Infrastructure.Data.Migrations
|
||||
{
|
||||
[DbContext(typeof(GreenHomeDbContext))]
|
||||
[Migration("20260107144244_AddSmsRecievers")]
|
||||
@@ -1,8 +1,8 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GreenHome.Infrastructure.Migrations
|
||||
namespace GreenHome.Infrastructure.Data.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class AddSmsRecievers : Migration
|
||||
@@ -1,6 +1,6 @@
|
||||
// <auto-generated />
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using GreenHome.Infrastructure;
|
||||
using GreenHome.Infrastructure.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
@@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GreenHome.Infrastructure.Migrations
|
||||
namespace GreenHome.Infrastructure.Data.Migrations
|
||||
{
|
||||
[DbContext(typeof(GreenHomeDbContext))]
|
||||
[Migration("20260113032844_AddBatteryInfoFields")]
|
||||
@@ -1,8 +1,8 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GreenHome.Infrastructure.Migrations
|
||||
namespace GreenHome.Infrastructure.Data.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class AddBatteryInfoFields : Migration
|
||||
@@ -1,6 +1,6 @@
|
||||
// <auto-generated />
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using GreenHome.Infrastructure;
|
||||
using GreenHome.Infrastructure.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
@@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GreenHome.Infrastructure.Migrations
|
||||
namespace GreenHome.Infrastructure.Data.Migrations
|
||||
{
|
||||
[DbContext(typeof(GreenHomeDbContext))]
|
||||
[Migration("20260428054608_AddRelayCountToDeviceSettings")]
|
||||
@@ -1,8 +1,8 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GreenHome.Infrastructure.Migrations
|
||||
namespace GreenHome.Infrastructure.Data.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class AddRelayCountToDeviceSettings : Migration
|
||||
@@ -1,6 +1,6 @@
|
||||
// <auto-generated />
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using GreenHome.Infrastructure;
|
||||
using GreenHome.Infrastructure.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
@@ -8,7 +8,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace GreenHome.Infrastructure.Migrations
|
||||
namespace GreenHome.Infrastructure.Data.Migrations
|
||||
{
|
||||
[DbContext(typeof(GreenHomeDbContext))]
|
||||
partial class GreenHomeDbContextModelSnapshot : ModelSnapshot
|
||||
@@ -681,6 +681,54 @@ namespace GreenHome.Infrastructure.Migrations
|
||||
b.ToTable("DeviceUsers", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("GreenHome.Domain.PowerOutageLog", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<byte>("CurrentPower")
|
||||
.HasColumnType("tinyint");
|
||||
|
||||
b.Property<int>("DeviceId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("EventType")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<string>("PersianDate")
|
||||
.IsRequired()
|
||||
.HasMaxLength(10)
|
||||
.HasColumnType("nvarchar(10)");
|
||||
|
||||
b.Property<int>("PersianMonth")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("PersianYear")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<byte>("PreviousPower")
|
||||
.HasColumnType("tinyint");
|
||||
|
||||
b.Property<DateTime>("ServerTimestampUtc")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<DateTime>("TimestampUtc")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("EventType");
|
||||
|
||||
b.HasIndex("DeviceId", "TimestampUtc");
|
||||
|
||||
b.HasIndex("DeviceId", "PersianYear", "PersianMonth");
|
||||
|
||||
b.ToTable("PowerOutageLogs", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("GreenHome.Domain.ReportImage", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
@@ -1174,6 +1222,17 @@ namespace GreenHome.Infrastructure.Migrations
|
||||
b.Navigation("User");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("GreenHome.Domain.PowerOutageLog", b =>
|
||||
{
|
||||
b.HasOne("GreenHome.Domain.Device", "Device")
|
||||
.WithMany()
|
||||
.HasForeignKey("DeviceId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Device");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("GreenHome.Domain.ReportImage", b =>
|
||||
{
|
||||
b.HasOne("GreenHome.Domain.UserDailyReport", "UserDailyReport")
|
||||
70
src/GreenHome.Infrastructure/DependencyInjection.cs
Normal file
70
src/GreenHome.Infrastructure/DependencyInjection.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using GreenHome.Application;
|
||||
using GreenHome.Infrastructure.Data;
|
||||
using GreenHome.Infrastructure.Services.AI;
|
||||
using GreenHome.Infrastructure.Services.Alert;
|
||||
using GreenHome.Infrastructure.Services.Auth;
|
||||
using GreenHome.Infrastructure.Services.Checklist;
|
||||
using GreenHome.Infrastructure.Services.Device;
|
||||
using GreenHome.Infrastructure.Services.PowerOutage;
|
||||
using GreenHome.Infrastructure.Services.Report;
|
||||
using GreenHome.Infrastructure.Services.Sun;
|
||||
using GreenHome.Infrastructure.Services.Telemetry;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace GreenHome.Infrastructure;
|
||||
|
||||
/// <summary>
|
||||
/// Registers all Infrastructure-layer services with the DI container.
|
||||
/// Call this from Program.cs to keep startup clean.
|
||||
/// </summary>
|
||||
public static class DependencyInjection
|
||||
{
|
||||
public static IServiceCollection AddInfrastructure(
|
||||
this IServiceCollection services,
|
||||
IConfiguration configuration)
|
||||
{
|
||||
// Entity Framework
|
||||
services.AddDbContext<GreenHomeDbContext>(options =>
|
||||
{
|
||||
options.UseSqlServer(configuration.GetConnectionString("Default"));
|
||||
});
|
||||
|
||||
// Auth
|
||||
services.AddScoped<IAuthService, AuthService>();
|
||||
|
||||
// Alert
|
||||
services.AddScoped<IAlertService, AlertService>();
|
||||
services.AddScoped<IAlertConditionService, AlertConditionService>();
|
||||
services.AddScoped<IAlertLogService, AlertLogService>();
|
||||
|
||||
// Device
|
||||
services.AddScoped<IDeviceService, DeviceService>();
|
||||
services.AddScoped<IDeviceSettingsService, DeviceSettingsService>();
|
||||
services.AddScoped<IDeviceTokenService, DeviceTokenService>();
|
||||
services.AddScoped<IDevicePostService, DevicePostService>();
|
||||
|
||||
// Telemetry
|
||||
services.AddScoped<ITelemetryService, TelemetryService>();
|
||||
|
||||
// Reports
|
||||
services.AddScoped<IDailyReportService, DailyReportService>();
|
||||
services.AddScoped<IMonthlyReportService, MonthlyReportService>();
|
||||
services.AddScoped<IUserDailyReportService, UserDailyReportService>();
|
||||
|
||||
// Checklist
|
||||
services.AddScoped<IChecklistService, ChecklistService>();
|
||||
|
||||
// Power Outage
|
||||
services.AddScoped<IPowerOutageLogService, PowerOutageLogService>();
|
||||
|
||||
// AI
|
||||
services.AddScoped<IAIQueryService, AIQueryService>();
|
||||
|
||||
// Sun Calculator
|
||||
services.AddScoped<ISunCalculatorService, SunCalculatorService>();
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
51
src/GreenHome.Infrastructure/Extensions/PersianDateHelper.cs
Normal file
51
src/GreenHome.Infrastructure/Extensions/PersianDateHelper.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
using System.Globalization;
|
||||
|
||||
namespace GreenHome.Infrastructure.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// Helper methods for Persian (Solar Hijri) date calculations.
|
||||
/// Eliminates duplicate PersianCalendar code across services.
|
||||
/// </summary>
|
||||
public static class PersianDateHelper
|
||||
{
|
||||
private static readonly PersianCalendar PersianCalendar = new();
|
||||
|
||||
/// <summary>
|
||||
/// Extracts Persian year, month, day from a DateTime and returns the formatted Persian date string (yyyy/MM/dd).
|
||||
/// </summary>
|
||||
public static (int Year, int Month, int Day, string PersianDate) ToPersianDate(this DateTime dateTime)
|
||||
{
|
||||
var localDt = dateTime.Kind == DateTimeKind.Utc ? dateTime.ToLocalTime() : dateTime;
|
||||
var year = PersianCalendar.GetYear(localDt);
|
||||
var month = PersianCalendar.GetMonth(localDt);
|
||||
var day = PersianCalendar.GetDayOfMonth(localDt);
|
||||
var persianDate = $"{year:0000}/{month:00}/{day:00}";
|
||||
return (year, month, day, persianDate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates a Persian date string in yyyy/MM/dd format and extracts year, month, day.
|
||||
/// </summary>
|
||||
public static bool TryParsePersianDate(string? persianDate, out int year, out int month, out int day)
|
||||
{
|
||||
year = month = day = 0;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(persianDate))
|
||||
return false;
|
||||
|
||||
var parts = persianDate.Split('/');
|
||||
if (parts.Length != 3)
|
||||
return false;
|
||||
|
||||
if (!int.TryParse(parts[0], out year) || year < 1300 || year > 1500)
|
||||
return false;
|
||||
|
||||
if (!int.TryParse(parts[1], out month) || month < 1 || month > 12)
|
||||
return false;
|
||||
|
||||
if (!int.TryParse(parts[2], out day) || day < 1 || day > 31)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,38 +1,39 @@
|
||||
using GreenHome.Application;
|
||||
using GreenHome.Domain;
|
||||
using GreenHome.Infrastructure.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace GreenHome.Infrastructure;
|
||||
namespace GreenHome.Infrastructure.Services.AI;
|
||||
|
||||
public sealed class AIQueryService : IAIQueryService
|
||||
{
|
||||
private readonly GreenHomeDbContext dbContext;
|
||||
private readonly ILogger<AIQueryService> logger;
|
||||
private readonly GreenHomeDbContext _dbContext;
|
||||
private readonly ILogger<AIQueryService> _logger;
|
||||
|
||||
public AIQueryService(
|
||||
GreenHomeDbContext dbContext,
|
||||
ILogger<AIQueryService> logger)
|
||||
{
|
||||
this.dbContext = dbContext;
|
||||
this.logger = logger;
|
||||
_dbContext = dbContext;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<AIQuery> SaveQueryAsync(AIQuery query, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
dbContext.AIQueries.Add(query);
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
_dbContext.AIQueries.Add(query);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
logger.LogInformation("AI query saved: {QueryId}, Tokens: {TotalTokens}",
|
||||
_logger.LogInformation("AI query saved: {QueryId}, Tokens: {TotalTokens}",
|
||||
query.Id, query.TotalTokens);
|
||||
|
||||
return query;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Error saving AI query");
|
||||
_logger.LogError(ex, "Error saving AI query");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
@@ -42,7 +43,7 @@ public sealed class AIQueryService : IAIQueryService
|
||||
int take = 50,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await dbContext.AIQueries
|
||||
return await _dbContext.AIQueries
|
||||
.Where(q => q.DeviceId == deviceId)
|
||||
.OrderByDescending(q => q.CreatedAt)
|
||||
.Take(take)
|
||||
@@ -54,7 +55,7 @@ public sealed class AIQueryService : IAIQueryService
|
||||
int take = 50,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await dbContext.AIQueries
|
||||
return await _dbContext.AIQueries
|
||||
.Where(q => q.UserId == userId)
|
||||
.OrderByDescending(q => q.CreatedAt)
|
||||
.Take(take)
|
||||
@@ -65,7 +66,7 @@ public sealed class AIQueryService : IAIQueryService
|
||||
int deviceId,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await dbContext.AIQueries
|
||||
return await _dbContext.AIQueries
|
||||
.Where(q => q.DeviceId == deviceId)
|
||||
.SumAsync(q => q.TotalTokens, cancellationToken);
|
||||
}
|
||||
@@ -74,7 +75,7 @@ public sealed class AIQueryService : IAIQueryService
|
||||
int userId,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await dbContext.AIQueries
|
||||
return await _dbContext.AIQueries
|
||||
.Where(q => q.UserId == userId)
|
||||
.SumAsync(q => q.TotalTokens, cancellationToken);
|
||||
}
|
||||
@@ -83,7 +84,7 @@ public sealed class AIQueryService : IAIQueryService
|
||||
int take = 20,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await dbContext.AIQueries
|
||||
return await _dbContext.AIQueries
|
||||
.Include(q => q.Device)
|
||||
.Include(q => q.User)
|
||||
.OrderByDescending(q => q.CreatedAt)
|
||||
@@ -95,7 +96,7 @@ public sealed class AIQueryService : IAIQueryService
|
||||
{
|
||||
var today = DateTime.UtcNow.Date;
|
||||
|
||||
var allQueries = await dbContext.AIQueries
|
||||
var allQueries = await _dbContext.AIQueries
|
||||
.Select(q => new
|
||||
{
|
||||
q.TotalTokens,
|
||||
@@ -123,4 +124,3 @@ public sealed class AIQueryService : IAIQueryService
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,23 +1,24 @@
|
||||
using AutoMapper;
|
||||
using GreenHome.Application;
|
||||
using GreenHome.Infrastructure.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace GreenHome.Infrastructure;
|
||||
namespace GreenHome.Infrastructure.Services.Alert;
|
||||
|
||||
public sealed class AlertConditionService : IAlertConditionService
|
||||
{
|
||||
private readonly GreenHomeDbContext dbContext;
|
||||
private readonly IMapper mapper;
|
||||
private readonly GreenHomeDbContext _dbContext;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public AlertConditionService(GreenHomeDbContext dbContext, IMapper mapper)
|
||||
{
|
||||
this.dbContext = dbContext;
|
||||
this.mapper = mapper;
|
||||
_dbContext = dbContext;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<AlertConditionDto>> GetByDeviceIdAsync(int deviceId, CancellationToken cancellationToken)
|
||||
{
|
||||
var conditions = await dbContext.AlertConditions
|
||||
var conditions = await _dbContext.AlertConditions
|
||||
.Include(x => x.Device)
|
||||
.Include(x => x.Rules)
|
||||
.Where(x => x.DeviceId == deviceId)
|
||||
@@ -25,18 +26,18 @@ public sealed class AlertConditionService : IAlertConditionService
|
||||
.AsNoTracking()
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
return mapper.Map<List<AlertConditionDto>>(conditions);
|
||||
return _mapper.Map<List<AlertConditionDto>>(conditions);
|
||||
}
|
||||
|
||||
public async Task<AlertConditionDto?> GetByIdAsync(int id, CancellationToken cancellationToken)
|
||||
{
|
||||
var condition = await dbContext.AlertConditions
|
||||
var condition = await _dbContext.AlertConditions
|
||||
.Include(x => x.Device)
|
||||
.Include(x => x.Rules)
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(x => x.Id == id, cancellationToken);
|
||||
|
||||
return condition != null ? mapper.Map<AlertConditionDto>(condition) : null;
|
||||
return condition != null ? _mapper.Map<AlertConditionDto>(condition) : null;
|
||||
}
|
||||
|
||||
public async Task<int> CreateAsync(CreateAlertConditionRequest request, CancellationToken cancellationToken)
|
||||
@@ -53,29 +54,26 @@ public sealed class AlertConditionService : IAlertConditionService
|
||||
UpdatedAt = DateTime.UtcNow
|
||||
};
|
||||
|
||||
// Add rules
|
||||
var rules = mapper.Map<List<Domain.AlertRule>>(request.Rules);
|
||||
var rules = _mapper.Map<List<Domain.AlertRule>>(request.Rules);
|
||||
foreach (var rule in rules)
|
||||
{
|
||||
condition.Rules.Add(rule);
|
||||
}
|
||||
|
||||
dbContext.AlertConditions.Add(condition);
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
_dbContext.AlertConditions.Add(condition);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return condition.Id;
|
||||
}
|
||||
|
||||
public async Task UpdateAsync(UpdateAlertConditionRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var condition = await dbContext.AlertConditions
|
||||
var condition = await _dbContext.AlertConditions
|
||||
.Include(x => x.Rules)
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);
|
||||
|
||||
if (condition == null)
|
||||
{
|
||||
throw new InvalidOperationException($"Alert condition not found: {request.Id}");
|
||||
}
|
||||
|
||||
condition.NotificationType = request.NotificationType;
|
||||
condition.TimeType = request.TimeType;
|
||||
@@ -84,53 +82,48 @@ public sealed class AlertConditionService : IAlertConditionService
|
||||
condition.IsEnabled = request.IsEnabled;
|
||||
condition.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
// Remove old rules and add new ones
|
||||
dbContext.AlertRules.RemoveRange(condition.Rules);
|
||||
_dbContext.AlertRules.RemoveRange(condition.Rules);
|
||||
condition.Rules.Clear();
|
||||
|
||||
var newRules = mapper.Map<List<Domain.AlertRule>>(request.Rules);
|
||||
var newRules = _mapper.Map<List<Domain.AlertRule>>(request.Rules);
|
||||
foreach (var rule in newRules)
|
||||
{
|
||||
condition.Rules.Add(rule);
|
||||
}
|
||||
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(int id, CancellationToken cancellationToken)
|
||||
{
|
||||
var condition = await dbContext.AlertConditions
|
||||
var condition = await _dbContext.AlertConditions
|
||||
.FirstOrDefaultAsync(x => x.Id == id, cancellationToken);
|
||||
|
||||
if (condition == null)
|
||||
{
|
||||
throw new InvalidOperationException($"Alert condition not found: {id}");
|
||||
}
|
||||
|
||||
dbContext.AlertConditions.Remove(condition);
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
_dbContext.AlertConditions.Remove(condition);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<bool> ToggleEnabledAsync(int id, bool isEnabled, CancellationToken cancellationToken)
|
||||
{
|
||||
var condition = await dbContext.AlertConditions
|
||||
var condition = await _dbContext.AlertConditions
|
||||
.FirstOrDefaultAsync(x => x.Id == id, cancellationToken);
|
||||
|
||||
if (condition == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
condition.IsEnabled = isEnabled;
|
||||
condition.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<RelayConditionDto>> GetRelayConditionsAsync(int deviceId, CancellationToken cancellationToken)
|
||||
{
|
||||
var conditions = await dbContext.AlertConditions
|
||||
var conditions = await _dbContext.AlertConditions
|
||||
.Include(x => x.Rules)
|
||||
.Where(x => x.DeviceId == deviceId && (int)x.NotificationType > 0)
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
@@ -156,31 +149,23 @@ public sealed class AlertConditionService : IAlertConditionService
|
||||
|
||||
public async Task<IReadOnlyList<RelayConditionDto>> GetUpdatedRelayConditionsAsync(int deviceId, DateTime? lastUpdateDate, CancellationToken cancellationToken)
|
||||
{
|
||||
// اگر lastUpdateDate null باشه، یعنی کلاینت تا حالا هیچ دادهای نگرفته، همه رو برگردون
|
||||
if (lastUpdateDate == null)
|
||||
{
|
||||
return await GetRelayConditionsAsync(deviceId, cancellationToken);
|
||||
}
|
||||
|
||||
var hasUpdates = await dbContext.AlertConditions
|
||||
var hasUpdates = await _dbContext.AlertConditions
|
||||
.AnyAsync(x => x.DeviceId == deviceId && (int)x.NotificationType > 0 && x.UpdatedAt > lastUpdateDate, cancellationToken);
|
||||
|
||||
if (!hasUpdates)
|
||||
{
|
||||
return Array.Empty<RelayConditionDto>();
|
||||
}
|
||||
|
||||
return await GetRelayConditionsAsync(deviceId, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<bool> HasUpdatedConditionsAsync(int deviceId, DateTime? lastUpdateDate, CancellationToken cancellationToken)
|
||||
{
|
||||
if(lastUpdateDate == null) return true;
|
||||
if (lastUpdateDate == null) return true;
|
||||
|
||||
var hasUpdates = await dbContext.AlertConditions
|
||||
return await _dbContext.AlertConditions
|
||||
.AnyAsync(x => x.DeviceId == deviceId && (int)x.NotificationType > 0 && x.UpdatedAt > lastUpdateDate, cancellationToken);
|
||||
|
||||
return hasUpdates;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,18 @@
|
||||
using AutoMapper;
|
||||
using GreenHome.Application;
|
||||
using GreenHome.Infrastructure.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace GreenHome.Infrastructure;
|
||||
namespace GreenHome.Infrastructure.Services.Alert;
|
||||
|
||||
public sealed class AlertLogService : IAlertLogService
|
||||
{
|
||||
private readonly GreenHomeDbContext _context;
|
||||
private readonly GreenHomeDbContext _dbContext;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public AlertLogService(GreenHomeDbContext context, IMapper mapper)
|
||||
{
|
||||
_context = context;
|
||||
_dbContext = context;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
@@ -19,47 +20,32 @@ public sealed class AlertLogService : IAlertLogService
|
||||
AlertLogFilter filter,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var query = _context.AlertLogs
|
||||
var query = _dbContext.AlertLogs
|
||||
.Include(x => x.Device)
|
||||
.Include(x => x.User)
|
||||
.AsNoTracking()
|
||||
.AsQueryable();
|
||||
|
||||
// Apply filters
|
||||
if (filter.DeviceId.HasValue)
|
||||
{
|
||||
query = query.Where(x => x.DeviceId == filter.DeviceId.Value);
|
||||
}
|
||||
|
||||
if (filter.UserId.HasValue)
|
||||
{
|
||||
query = query.Where(x => x.UserId == filter.UserId.Value);
|
||||
}
|
||||
|
||||
if (filter.AlertType.HasValue)
|
||||
{
|
||||
query = query.Where(x => x.AlertType == filter.AlertType.Value);
|
||||
}
|
||||
|
||||
if (filter.Status.HasValue)
|
||||
{
|
||||
query = query.Where(x => x.Status == filter.Status.Value);
|
||||
}
|
||||
|
||||
if (filter.StartDate.HasValue)
|
||||
{
|
||||
query = query.Where(x => x.SentAt >= filter.StartDate.Value);
|
||||
}
|
||||
|
||||
if (filter.EndDate.HasValue)
|
||||
{
|
||||
query = query.Where(x => x.SentAt <= filter.EndDate.Value);
|
||||
}
|
||||
|
||||
// Get total count
|
||||
var totalCount = await query.CountAsync(cancellationToken);
|
||||
|
||||
// Apply pagination and ordering
|
||||
var items = await query
|
||||
.OrderByDescending(x => x.SentAt)
|
||||
.Skip((filter.Page - 1) * filter.PageSize)
|
||||
@@ -79,7 +65,7 @@ public sealed class AlertLogService : IAlertLogService
|
||||
|
||||
public async Task<AlertLogDto?> GetAlertLogByIdAsync(int id, CancellationToken cancellationToken)
|
||||
{
|
||||
var log = await _context.AlertLogs
|
||||
var log = await _dbContext.AlertLogs
|
||||
.Include(x => x.Device)
|
||||
.Include(x => x.User)
|
||||
.Include(x => x.AlertCondition)
|
||||
@@ -92,9 +78,8 @@ public sealed class AlertLogService : IAlertLogService
|
||||
public async Task<int> CreateAlertLogAsync(AlertLogDto dto, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = _mapper.Map<Domain.AlertLog>(dto);
|
||||
_context.AlertLogs.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
_dbContext.AlertLogs.Add(entity);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
return entity.Id;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,24 +1,23 @@
|
||||
using GreenHome.Application;
|
||||
using GreenHome.Domain;
|
||||
using GreenHome.Infrastructure.Data;
|
||||
using GreenHome.Sms.Ippanel;
|
||||
using GreenHome.VoiceCall.Avanak;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion.Internal;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using static GreenHome.Sms.Ippanel.IppanelSmsService;
|
||||
|
||||
namespace GreenHome.Infrastructure;
|
||||
namespace GreenHome.Infrastructure.Services.Alert;
|
||||
|
||||
public sealed class AlertService : IAlertService
|
||||
{
|
||||
private readonly GreenHomeDbContext dbContext;
|
||||
private readonly IDeviceSettingsService deviceSettingsService;
|
||||
private readonly ISmsService smsService;
|
||||
private readonly IVoiceCallService voiceCallService;
|
||||
private readonly ISunCalculatorService sunCalculatorService;
|
||||
private readonly ILogger<AlertService> logger;
|
||||
private readonly GreenHomeDbContext _dbContext;
|
||||
private readonly IDeviceSettingsService _deviceSettingsService;
|
||||
private readonly ISmsService _smsService;
|
||||
private readonly IVoiceCallService _voiceCallService;
|
||||
private readonly ISunCalculatorService _sunCalculatorService;
|
||||
private readonly IPowerOutageLogService _powerOutageLogService;
|
||||
private readonly ILogger<AlertService> _logger;
|
||||
|
||||
public AlertService(
|
||||
GreenHomeDbContext dbContext,
|
||||
@@ -26,20 +25,21 @@ public sealed class AlertService : IAlertService
|
||||
ISmsService smsService,
|
||||
IVoiceCallService voiceCallService,
|
||||
ISunCalculatorService sunCalculatorService,
|
||||
IPowerOutageLogService powerOutageLogService,
|
||||
ILogger<AlertService> logger)
|
||||
{
|
||||
this.dbContext = dbContext;
|
||||
this.deviceSettingsService = deviceSettingsService;
|
||||
this.smsService = smsService;
|
||||
this.voiceCallService = voiceCallService;
|
||||
this.sunCalculatorService = sunCalculatorService;
|
||||
this.logger = logger;
|
||||
_dbContext = dbContext;
|
||||
_deviceSettingsService = deviceSettingsService;
|
||||
_smsService = smsService;
|
||||
_voiceCallService = voiceCallService;
|
||||
_sunCalculatorService = sunCalculatorService;
|
||||
_powerOutageLogService = powerOutageLogService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<string?> CheckAndSendAlertsAsync(int deviceId, TelemetryDto telemetry, CancellationToken cancellationToken)
|
||||
public async Task<AlertCheckResult?> CheckAndSendAlertsAsync(int deviceId, TelemetryDto telemetry, CancellationToken cancellationToken)
|
||||
{
|
||||
// Get device with all users who should receive alerts
|
||||
var device = await dbContext.Devices
|
||||
var device = await _dbContext.Devices
|
||||
.Include(d => d.User)
|
||||
.Include(d => d.DeviceUsers.Where(du => du.ReceiveAlerts))
|
||||
.ThenInclude(du => du.User)
|
||||
@@ -47,105 +47,104 @@ public sealed class AlertService : IAlertService
|
||||
|
||||
if (device == null)
|
||||
{
|
||||
logger.LogWarning("Device not found: DeviceId={DeviceId}", deviceId);
|
||||
_logger.LogWarning("Device not found: DeviceId={DeviceId}", deviceId);
|
||||
return null;
|
||||
}
|
||||
|
||||
// Get device settings for location
|
||||
var settings = await deviceSettingsService.GetByDeviceIdAsync(deviceId, cancellationToken);
|
||||
var settings = await _deviceSettingsService.GetByDeviceIdAsync(deviceId, cancellationToken);
|
||||
|
||||
|
||||
var recievers = device.User.Mobile;
|
||||
recievers += "," + (settings?.SmsRecievers ?? "");
|
||||
|
||||
recievers = string.Join(",",
|
||||
recievers
|
||||
var recievers = string.Join(",",
|
||||
(device.User.Mobile + "," + (settings?.SmsRecievers ?? ""))
|
||||
.Split(',')
|
||||
.Select(x => x.Trim())
|
||||
.Where(x => x.Length > 0)
|
||||
.Distinct()
|
||||
);
|
||||
// Get all users who should receive alerts
|
||||
// var usersToAlert = device.DeviceUsers
|
||||
// .Where(du => du.ReceiveAlerts)
|
||||
// .Select(du => du.User)
|
||||
// .ToList();
|
||||
|
||||
if (recievers.Length == 0)
|
||||
{
|
||||
logger.LogInformation("No users with ReceiveAlerts enabled for device: DeviceId={DeviceId}", deviceId);
|
||||
_logger.LogInformation("No users with ReceiveAlerts enabled for device: DeviceId={DeviceId}", deviceId);
|
||||
return null;
|
||||
}
|
||||
|
||||
// Get all enabled alert conditions for this device
|
||||
var conditions = await dbContext.AlertConditions
|
||||
var conditions = await _dbContext.AlertConditions
|
||||
.Include(c => c.Rules)
|
||||
.Where(c => c.DeviceId == deviceId && c.IsEnabled)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
if (!conditions.Any())
|
||||
{
|
||||
logger.LogDebug("No enabled alert conditions for device: DeviceId={DeviceId}", deviceId);
|
||||
//return null;
|
||||
_logger.LogDebug("No enabled alert conditions for device: DeviceId={DeviceId}", deviceId);
|
||||
}
|
||||
|
||||
// Determine if it's daytime or nighttime
|
||||
bool? isDaytime = null;
|
||||
if (settings?.Latitude != null && settings.Longitude != null)
|
||||
{
|
||||
isDaytime = sunCalculatorService.IsDaytime(DateTime.UtcNow, settings.Latitude.Value, settings.Longitude.Value);
|
||||
isDaytime = _sunCalculatorService.IsDaytime(DateTime.UtcNow, settings.Latitude.Value, settings.Longitude.Value);
|
||||
}
|
||||
|
||||
List<string> messages = [];
|
||||
// Check each condition
|
||||
foreach (var condition in conditions)
|
||||
{
|
||||
// Check time type filter
|
||||
if (condition.TimeType == Domain.AlertTimeType.Day && isDaytime == false)
|
||||
{
|
||||
continue; // This condition is for daytime only, but it's nighttime
|
||||
}
|
||||
continue;
|
||||
if (condition.TimeType == Domain.AlertTimeType.Night && isDaytime == true)
|
||||
{
|
||||
continue; // This condition is for nighttime only, but it's daytime
|
||||
}
|
||||
continue;
|
||||
|
||||
// Check if all rules match (AND logic)
|
||||
var allRulesMatch = condition.Rules.All(rule => CheckRule(rule, telemetry));
|
||||
|
||||
if (allRulesMatch && condition.Rules.Count != 0)
|
||||
{
|
||||
// All rules passed, send alert if cooldown period has passed
|
||||
// await SendAlertForConditionAsync(condition, device, usersToAlert, telemetry, cancellationToken);
|
||||
AlertNotification notif = await GetAlertMessage(condition, device, device.User, telemetry, cancellationToken);
|
||||
if(notif != null && !string.IsNullOrEmpty(notif.Message))
|
||||
if (notif != null && !string.IsNullOrEmpty(notif.Message))
|
||||
{
|
||||
messages.Add(notif.Message);
|
||||
await dbContext.AlertNotifications.AddAsync(notif, cancellationToken);
|
||||
await _dbContext.AlertNotifications.AddAsync(notif, cancellationToken);
|
||||
}
|
||||
// return message;
|
||||
}
|
||||
}
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
if(telemetry.OldPower == 0 && telemetry.Power == 1)
|
||||
var result = new AlertCheckResult();
|
||||
|
||||
if (telemetry.OldPower == 0 && telemetry.Power == 1)
|
||||
{
|
||||
result.PowerStatus = "1";
|
||||
messages.Add($"برق دستگاه {device.DeviceName} متصل شد");
|
||||
await _powerOutageLogService.LogPowerChangeAsync(
|
||||
deviceId,
|
||||
telemetry.OldPower.Value,
|
||||
telemetry.Power.Value,
|
||||
telemetry.TimestampUtc ?? DateTime.UtcNow,
|
||||
cancellationToken);
|
||||
}
|
||||
else if(telemetry.OldPower == 1 && telemetry.Power == 0)
|
||||
else if (telemetry.OldPower == 1 && telemetry.Power == 0)
|
||||
{
|
||||
result.PowerStatus = "0";
|
||||
messages.Add($"برق دستگاه {device.DeviceName} قطع شد");
|
||||
await _powerOutageLogService.LogPowerChangeAsync(
|
||||
deviceId,
|
||||
telemetry.OldPower.Value,
|
||||
telemetry.Power.Value,
|
||||
telemetry.TimestampUtc ?? DateTime.UtcNow,
|
||||
cancellationToken);
|
||||
}
|
||||
|
||||
if(messages.Any())
|
||||
if (messages.Any())
|
||||
{
|
||||
return $"tt{recievers}#{string.Join("@", messages)}";
|
||||
result.Messages = messages;
|
||||
result.Recipients = recievers;
|
||||
return result;
|
||||
}
|
||||
|
||||
if (result.PowerStatus != null)
|
||||
return result;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private bool CheckRule(Domain.AlertRule rule, TelemetryDto telemetry)
|
||||
{
|
||||
// Get sensor value
|
||||
var sensorValue = rule.SensorType switch
|
||||
{
|
||||
Domain.SensorType.Temperature => telemetry.TemperatureC,
|
||||
@@ -156,7 +155,6 @@ public sealed class AlertService : IAlertService
|
||||
_ => 0m
|
||||
};
|
||||
|
||||
// Check comparison
|
||||
return rule.ComparisonType switch
|
||||
{
|
||||
Domain.ComparisonType.GreaterThan => sensorValue > rule.Value1,
|
||||
@@ -174,17 +172,15 @@ public sealed class AlertService : IAlertService
|
||||
TelemetryDto telemetry,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
// Determine cooldown based on notification type
|
||||
var cooldownMinutes = condition.NotificationType == Domain.AlertNotificationType.Call
|
||||
? condition.CallCooldownMinutes
|
||||
: condition.SmsCooldownMinutes;
|
||||
|
||||
// Build alert message once
|
||||
var message = BuildAlertMessage(condition, device.DeviceName, telemetry);
|
||||
var sentAt = DateTime.UtcNow;
|
||||
|
||||
var cooldownTime = sentAt.AddMinutes(-cooldownMinutes);
|
||||
var recentAlert = await dbContext.AlertNotifications
|
||||
var recentAlert = await _dbContext.AlertNotifications
|
||||
.Where(a => a.DeviceId == device.Id &&
|
||||
a.UserId == user.Id &&
|
||||
a.AlertConditionId == condition.Id &&
|
||||
@@ -193,7 +189,7 @@ public sealed class AlertService : IAlertService
|
||||
|
||||
if (recentAlert != null)
|
||||
{
|
||||
logger.LogInformation("Alert skipped due to cooldown: DeviceId={DeviceId}, UserId={UserId}, ConditionId={ConditionId}",
|
||||
_logger.LogInformation("Alert skipped due to cooldown: DeviceId={DeviceId}, UserId={UserId}, ConditionId={ConditionId}",
|
||||
device.Id, user.Id, condition.Id);
|
||||
return null;
|
||||
}
|
||||
@@ -205,14 +201,9 @@ public sealed class AlertService : IAlertService
|
||||
AlertConditionId = condition.Id,
|
||||
NotificationType = condition.NotificationType,
|
||||
Message = message,
|
||||
//MessageOutboxIds = messageOutboxIds,
|
||||
//ErrorMessage = errorMessage,
|
||||
SentAt = sentAt,
|
||||
//IsSent = true
|
||||
};
|
||||
|
||||
// dbContext.AlertNotifications.Add(notification);
|
||||
|
||||
return notification;
|
||||
}
|
||||
|
||||
@@ -223,21 +214,17 @@ public sealed class AlertService : IAlertService
|
||||
TelemetryDto telemetry,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
// Determine cooldown based on notification type
|
||||
var cooldownMinutes = condition.NotificationType == Domain.AlertNotificationType.Call
|
||||
? condition.CallCooldownMinutes
|
||||
: condition.SmsCooldownMinutes;
|
||||
|
||||
// Build alert message once
|
||||
var message = BuildAlertMessage(condition, device.DeviceName, telemetry);
|
||||
var sentAt = DateTime.UtcNow;
|
||||
|
||||
// Send alert to each user
|
||||
foreach (var user in usersToAlert)
|
||||
{
|
||||
// Check if alert was sent recently to this user
|
||||
var cooldownTime = sentAt.AddMinutes(-cooldownMinutes);
|
||||
var recentAlert = await dbContext.AlertNotifications
|
||||
var recentAlert = await _dbContext.AlertNotifications
|
||||
.Where(a => a.DeviceId == device.Id &&
|
||||
a.UserId == user.Id &&
|
||||
a.AlertConditionId == condition.Id &&
|
||||
@@ -246,12 +233,11 @@ public sealed class AlertService : IAlertService
|
||||
|
||||
if (recentAlert != null)
|
||||
{
|
||||
logger.LogInformation("Alert skipped due to cooldown: DeviceId={DeviceId}, UserId={UserId}, ConditionId={ConditionId}",
|
||||
_logger.LogInformation("Alert skipped due to cooldown: DeviceId={DeviceId}, UserId={UserId}, ConditionId={ConditionId}",
|
||||
device.Id, user.Id, condition.Id);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Send notification
|
||||
var startTime = DateTime.UtcNow;
|
||||
string? messageOutboxIds = null;
|
||||
string? errorMessage = null;
|
||||
@@ -263,7 +249,7 @@ public sealed class AlertService : IAlertService
|
||||
{
|
||||
(isSent, messageOutboxIds, errorMessage) = await SendSmsAlertAsync(user.Mobile, device.DeviceName, message, cancellationToken);
|
||||
}
|
||||
else // Call
|
||||
else
|
||||
{
|
||||
(isSent, messageOutboxIds, errorMessage) = await SendCallAlertAsync(user.Mobile, device.DeviceName, message, cancellationToken);
|
||||
}
|
||||
@@ -272,17 +258,14 @@ public sealed class AlertService : IAlertService
|
||||
{
|
||||
errorMessage = $"Exception: {ex.Message}";
|
||||
if (ex.InnerException != null)
|
||||
{
|
||||
errorMessage += $" | InnerException: {ex.InnerException.Message}";
|
||||
}
|
||||
isSent = false;
|
||||
logger.LogError(ex, "Failed to send alert: DeviceId={DeviceId}, UserId={UserId}, ConditionId={ConditionId}",
|
||||
_logger.LogError(ex, "Failed to send alert: DeviceId={DeviceId}, UserId={UserId}, ConditionId={ConditionId}",
|
||||
device.Id, user.Id, condition.Id);
|
||||
}
|
||||
|
||||
var processingTime = (long)(DateTime.UtcNow - startTime).TotalMilliseconds;
|
||||
|
||||
// Save notification to database (old table for backwards compatibility)
|
||||
var notification = new Domain.AlertNotification
|
||||
{
|
||||
DeviceId = device.Id,
|
||||
@@ -296,9 +279,8 @@ public sealed class AlertService : IAlertService
|
||||
IsSent = isSent
|
||||
};
|
||||
|
||||
dbContext.AlertNotifications.Add(notification);
|
||||
_dbContext.AlertNotifications.Add(notification);
|
||||
|
||||
// Log the alert
|
||||
var alertLog = new Domain.AlertLog
|
||||
{
|
||||
DeviceId = device.Id,
|
||||
@@ -314,10 +296,10 @@ public sealed class AlertService : IAlertService
|
||||
ProcessingTimeMs = processingTime
|
||||
};
|
||||
|
||||
dbContext.AlertLogs.Add(alertLog);
|
||||
_dbContext.AlertLogs.Add(alertLog);
|
||||
}
|
||||
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
private string BuildAlertMessage(Domain.AlertCondition condition, string deviceName, TelemetryDto telemetry)
|
||||
@@ -380,7 +362,7 @@ public sealed class AlertService : IAlertService
|
||||
{
|
||||
try
|
||||
{
|
||||
var smsResponse = await smsService.SendPatternSmsAsync(new PatternSmsRequest
|
||||
var smsResponse = await _smsService.SendPatternSmsAsync(new PatternSmsRequest
|
||||
{
|
||||
Recipients = [mobile],
|
||||
PatternCode = "64di3w9kb0fxvif",
|
||||
@@ -397,32 +379,28 @@ public sealed class AlertService : IAlertService
|
||||
smsResponse.Data.MessageOutboxIds.Count > 0)
|
||||
{
|
||||
var outboxIds = JsonSerializer.Serialize(smsResponse.Data.MessageOutboxIds);
|
||||
logger.LogInformation("Alert SMS sent: Mobile={Mobile}, OutboxIds={OutboxIds}", mobile, outboxIds);
|
||||
_logger.LogInformation("Alert SMS sent: Mobile={Mobile}, OutboxIds={OutboxIds}", mobile, outboxIds);
|
||||
return (true, outboxIds, null);
|
||||
}
|
||||
else
|
||||
{
|
||||
var errors = new List<string>();
|
||||
if (!string.IsNullOrWhiteSpace(smsResponse?.Meta.Message))
|
||||
{
|
||||
errors.Add(smsResponse.Meta.Message);
|
||||
}
|
||||
if (smsResponse?.Meta.Errors != null)
|
||||
{
|
||||
foreach (var error in smsResponse.Meta.Errors)
|
||||
{
|
||||
errors.Add($"{error.Key}: {string.Join(", ", error.Value)}");
|
||||
}
|
||||
}
|
||||
var errorMsg = errors.Count > 0 ? string.Join(" | ", errors) : "Unknown SMS error";
|
||||
logger.LogWarning("Alert SMS failed: Mobile={Mobile}, Error={Error}", mobile, errorMsg);
|
||||
_logger.LogWarning("Alert SMS failed: Mobile={Mobile}, Error={Error}", mobile, errorMsg);
|
||||
return (false, null, errorMsg);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var errorMsg = $"Exception: {ex.Message}";
|
||||
logger.LogError(ex, "Exception sending SMS alert: Mobile={Mobile}", mobile);
|
||||
_logger.LogError(ex, "Exception sending SMS alert: Mobile={Mobile}", mobile);
|
||||
return (false, null, errorMsg);
|
||||
}
|
||||
}
|
||||
@@ -435,38 +413,30 @@ public sealed class AlertService : IAlertService
|
||||
{
|
||||
try
|
||||
{
|
||||
// TODO: Implement voice call integration
|
||||
// For now, just log and return success
|
||||
logger.LogInformation("Voice call alert requested: Mobile={Mobile}, Message={Message}", mobile, message);
|
||||
|
||||
// Placeholder: In real implementation, call voiceCallService here
|
||||
// var callResponse = await voiceCallService.MakeCallAsync(mobile, message, cancellationToken);
|
||||
|
||||
_logger.LogInformation("Voice call alert requested: Mobile={Mobile}, Message={Message}", mobile, message);
|
||||
return (true, null, "Voice call not yet implemented");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var errorMsg = $"Exception: {ex.Message}";
|
||||
logger.LogError(ex, "Exception sending call alert: Mobile={Mobile}", mobile);
|
||||
_logger.LogError(ex, "Exception sending call alert: Mobile={Mobile}", mobile);
|
||||
return (false, null, errorMsg);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SendPowerOutageAlertAsync(int deviceId, CancellationToken cancellationToken)
|
||||
{
|
||||
// Get device with all users who should receive alerts
|
||||
var device = await dbContext.Devices
|
||||
var device = await _dbContext.Devices
|
||||
.Include(d => d.DeviceUsers.Where(du => du.ReceiveAlerts))
|
||||
.ThenInclude(du => du.User)
|
||||
.FirstOrDefaultAsync(d => d.Id == deviceId, cancellationToken);
|
||||
|
||||
if (device == null)
|
||||
{
|
||||
logger.LogWarning("Device not found for power outage alert: DeviceId={DeviceId}", deviceId);
|
||||
_logger.LogWarning("Device not found for power outage alert: DeviceId={DeviceId}", deviceId);
|
||||
throw new InvalidOperationException($"دستگاه با شناسه {deviceId} یافت نشد");
|
||||
}
|
||||
|
||||
// Get all users who should receive alerts
|
||||
var usersToAlert = device.DeviceUsers
|
||||
.Where(du => du.ReceiveAlerts)
|
||||
.Select(du => du.User)
|
||||
@@ -474,32 +444,22 @@ public sealed class AlertService : IAlertService
|
||||
|
||||
if (usersToAlert.Count == 0)
|
||||
{
|
||||
logger.LogInformation("No users with ReceiveAlerts enabled for power outage: DeviceId={DeviceId}", deviceId);
|
||||
_logger.LogInformation("No users with ReceiveAlerts enabled for power outage: DeviceId={DeviceId}", deviceId);
|
||||
return;
|
||||
}
|
||||
|
||||
var message = $"⚠️ هشدار قطع برق! دستگاه {device.DeviceName} از برق قطع شده است.";
|
||||
var sentAt = DateTime.UtcNow;
|
||||
|
||||
// Send to all users (both SMS and Call for power outage - it's critical!)
|
||||
foreach (var user in usersToAlert)
|
||||
{
|
||||
// Send SMS
|
||||
await SendPowerOutageNotificationAsync(
|
||||
device, user, message, sentAt,
|
||||
Domain.AlertNotificationType.SMS,
|
||||
cancellationToken);
|
||||
|
||||
// Send Call (important alert)
|
||||
await SendPowerOutageNotificationAsync(
|
||||
device, user, message, sentAt,
|
||||
Domain.AlertNotificationType.Call,
|
||||
cancellationToken);
|
||||
await SendPowerOutageNotificationAsync(device, user, message, sentAt, Domain.AlertNotificationType.SMS, cancellationToken);
|
||||
await SendPowerOutageNotificationAsync(device, user, message, sentAt, Domain.AlertNotificationType.Call, cancellationToken);
|
||||
}
|
||||
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
logger.LogInformation("Power outage alerts sent to {Count} users for device {DeviceId}",
|
||||
_logger.LogInformation("Power outage alerts sent to {Count} users for device {DeviceId}",
|
||||
usersToAlert.Count, deviceId);
|
||||
}
|
||||
|
||||
@@ -519,31 +479,22 @@ public sealed class AlertService : IAlertService
|
||||
try
|
||||
{
|
||||
if (notificationType == Domain.AlertNotificationType.SMS)
|
||||
{
|
||||
(isSent, messageOutboxIds, errorMessage) = await SendSmsAlertAsync(
|
||||
user.Mobile, device.DeviceName, message, cancellationToken);
|
||||
}
|
||||
(isSent, messageOutboxIds, errorMessage) = await SendSmsAlertAsync(user.Mobile, device.DeviceName, message, cancellationToken);
|
||||
else
|
||||
{
|
||||
(isSent, messageOutboxIds, errorMessage) = await SendCallAlertAsync(
|
||||
user.Mobile, device.DeviceName, message, cancellationToken);
|
||||
}
|
||||
(isSent, messageOutboxIds, errorMessage) = await SendCallAlertAsync(user.Mobile, device.DeviceName, message, cancellationToken);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
errorMessage = $"Exception: {ex.Message}";
|
||||
if (ex.InnerException != null)
|
||||
{
|
||||
errorMessage += $" | InnerException: {ex.InnerException.Message}";
|
||||
}
|
||||
isSent = false;
|
||||
logger.LogError(ex, "Failed to send power outage alert: DeviceId={DeviceId}, UserId={UserId}, Type={Type}",
|
||||
_logger.LogError(ex, "Failed to send power outage alert: DeviceId={DeviceId}, UserId={UserId}, Type={Type}",
|
||||
device.Id, user.Id, notificationType);
|
||||
}
|
||||
|
||||
var processingTime = (long)(DateTime.UtcNow - startTime).TotalMilliseconds;
|
||||
|
||||
// Save notification (old table)
|
||||
var notification = new Domain.AlertNotification
|
||||
{
|
||||
DeviceId = device.Id,
|
||||
@@ -557,9 +508,8 @@ public sealed class AlertService : IAlertService
|
||||
IsSent = isSent
|
||||
};
|
||||
|
||||
dbContext.AlertNotifications.Add(notification);
|
||||
_dbContext.AlertNotifications.Add(notification);
|
||||
|
||||
// Log the alert
|
||||
var alertLog = new Domain.AlertLog
|
||||
{
|
||||
DeviceId = device.Id,
|
||||
@@ -575,7 +525,6 @@ public sealed class AlertService : IAlertService
|
||||
ProcessingTimeMs = processingTime
|
||||
};
|
||||
|
||||
dbContext.AlertLogs.Add(alertLog);
|
||||
_dbContext.AlertLogs.Add(alertLog);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,18 @@
|
||||
using GreenHome.Application;
|
||||
using GreenHome.Infrastructure.Data;
|
||||
using GreenHome.Sms.Ippanel;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace GreenHome.Infrastructure;
|
||||
namespace GreenHome.Infrastructure.Services.Auth;
|
||||
|
||||
public sealed class AuthService : IAuthService
|
||||
{
|
||||
private readonly GreenHomeDbContext dbContext;
|
||||
private readonly ISmsService smsService;
|
||||
private readonly ILogger<AuthService> logger;
|
||||
private readonly GreenHomeDbContext _dbContext;
|
||||
private readonly ISmsService _smsService;
|
||||
private readonly ILogger<AuthService> _logger;
|
||||
private const int CodeExpirationMinutes = 5;
|
||||
private const int ResendCooldownSeconds = 120;
|
||||
|
||||
@@ -20,16 +21,15 @@ public sealed class AuthService : IAuthService
|
||||
ISmsService smsService,
|
||||
ILogger<AuthService> logger)
|
||||
{
|
||||
this.dbContext = dbContext;
|
||||
this.smsService = smsService;
|
||||
this.logger = logger;
|
||||
_dbContext = dbContext;
|
||||
_smsService = smsService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<SendCodeResponse> SendVerificationCodeAsync(
|
||||
SendCodeRequest request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
// Normalize mobile number (remove spaces, dashes, etc.)
|
||||
var mobile = NormalizeMobile(request.Mobile);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(mobile) || mobile.Length != 11 || !mobile.StartsWith("09"))
|
||||
@@ -41,11 +41,10 @@ public sealed class AuthService : IAuthService
|
||||
};
|
||||
}
|
||||
|
||||
// Check if we can resend (cooldown period)
|
||||
var canResend = await CanResendCodeAsync(mobile, cancellationToken);
|
||||
if (!canResend)
|
||||
{
|
||||
var lastCode = await dbContext.VerificationCodes
|
||||
var lastCode = await _dbContext.VerificationCodes
|
||||
.Where(v => v.Mobile == mobile && !v.IsUsed)
|
||||
.OrderByDescending(v => v.CreatedAt)
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
@@ -62,11 +61,9 @@ public sealed class AuthService : IAuthService
|
||||
}
|
||||
}
|
||||
|
||||
// Generate 4-digit code
|
||||
var code = GenerateCode();
|
||||
|
||||
// Invalidate previous unused codes for this mobile
|
||||
var previousCodes = await dbContext.VerificationCodes
|
||||
var previousCodes = await _dbContext.VerificationCodes
|
||||
.Where(v => v.Mobile == mobile && !v.IsUsed)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
@@ -76,7 +73,6 @@ public sealed class AuthService : IAuthService
|
||||
prevCode.UsedAt = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
// Create new verification code
|
||||
var verificationCode = new Domain.VerificationCode
|
||||
{
|
||||
Mobile = mobile,
|
||||
@@ -86,25 +82,23 @@ public sealed class AuthService : IAuthService
|
||||
IsUsed = false
|
||||
};
|
||||
|
||||
dbContext.VerificationCodes.Add(verificationCode);
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
_dbContext.VerificationCodes.Add(verificationCode);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
// Send SMS
|
||||
try
|
||||
{
|
||||
await smsService.SendPatternSmsAsync(new PatternSmsRequest
|
||||
await _smsService.SendPatternSmsAsync(new PatternSmsRequest
|
||||
{
|
||||
Recipients = [mobile],
|
||||
PatternCode = "ruvpjx7lajne1dx",
|
||||
Variables = new Dictionary<string, string> { { "code", code } }
|
||||
}, cancellationToken);
|
||||
|
||||
logger.LogInformation("Verification code sent to {Mobile}", mobile);
|
||||
_logger.LogInformation("Verification code sent to {Mobile}", mobile);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Failed to send SMS to {Mobile}", mobile);
|
||||
// Continue even if SMS fails (for development/testing)
|
||||
_logger.LogError(ex, "Failed to send SMS to {Mobile}", mobile);
|
||||
}
|
||||
|
||||
return new SendCodeResponse
|
||||
@@ -131,8 +125,7 @@ public sealed class AuthService : IAuthService
|
||||
};
|
||||
}
|
||||
|
||||
// Find valid verification code
|
||||
var verificationCode = await dbContext.VerificationCodes
|
||||
var verificationCode = await _dbContext.VerificationCodes
|
||||
.Where(v => v.Mobile == mobile && v.Code == code && !v.IsUsed && v.ExpiresAt > DateTime.UtcNow)
|
||||
.OrderByDescending(v => v.CreatedAt)
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
@@ -146,12 +139,10 @@ public sealed class AuthService : IAuthService
|
||||
};
|
||||
}
|
||||
|
||||
// Mark code as used
|
||||
verificationCode.IsUsed = true;
|
||||
verificationCode.UsedAt = DateTime.UtcNow;
|
||||
|
||||
// Find or create user
|
||||
var user = await dbContext.Users
|
||||
var user = await _dbContext.Users
|
||||
.FirstOrDefaultAsync(u => u.Mobile == mobile, cancellationToken);
|
||||
|
||||
if (user == null)
|
||||
@@ -162,16 +153,15 @@ public sealed class AuthService : IAuthService
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
LastLoginAt = DateTime.UtcNow
|
||||
};
|
||||
dbContext.Users.Add(user);
|
||||
_dbContext.Users.Add(user);
|
||||
}
|
||||
else
|
||||
{
|
||||
user.LastLoginAt = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
// Generate simple token (in production, use JWT)
|
||||
var token = GenerateToken(user.Id, mobile);
|
||||
|
||||
return new VerifyCodeResponse
|
||||
@@ -193,7 +183,7 @@ public sealed class AuthService : IAuthService
|
||||
public async Task<bool> CanResendCodeAsync(string mobile, CancellationToken cancellationToken)
|
||||
{
|
||||
var normalizedMobile = NormalizeMobile(mobile);
|
||||
var lastCode = await dbContext.VerificationCodes
|
||||
var lastCode = await _dbContext.VerificationCodes
|
||||
.Where(v => v.Mobile == normalizedMobile && !v.IsUsed)
|
||||
.OrderByDescending(v => v.CreatedAt)
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
@@ -213,7 +203,6 @@ public sealed class AuthService : IAuthService
|
||||
|
||||
private static string GenerateToken(int userId, string mobile)
|
||||
{
|
||||
// Simple token generation (in production, use JWT)
|
||||
var data = $"{userId}:{mobile}:{DateTime.UtcNow:yyyyMMddHHmmss}";
|
||||
var bytes = Encoding.UTF8.GetBytes(data);
|
||||
var hash = SHA256.HashData(bytes);
|
||||
@@ -225,10 +214,8 @@ public sealed class AuthService : IAuthService
|
||||
if (string.IsNullOrWhiteSpace(mobile))
|
||||
return string.Empty;
|
||||
|
||||
// Remove all non-digit characters
|
||||
var normalized = new string(mobile.Where(char.IsDigit).ToArray());
|
||||
|
||||
// Convert to standard format (09xxxxxxxxx)
|
||||
if (normalized.StartsWith("9") && normalized.Length == 10)
|
||||
normalized = "0" + normalized;
|
||||
else if (normalized.StartsWith("0098") && normalized.Length == 13)
|
||||
@@ -239,4 +226,3 @@ public sealed class AuthService : IAuthService
|
||||
return normalized;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
using AutoMapper;
|
||||
using GreenHome.Application;
|
||||
using GreenHome.Infrastructure.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace GreenHome.Infrastructure;
|
||||
namespace GreenHome.Infrastructure.Services.Checklist;
|
||||
|
||||
public sealed class ChecklistService : IChecklistService
|
||||
{
|
||||
@@ -149,4 +150,3 @@ public sealed class ChecklistService : IChecklistService
|
||||
return completion.Id;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,18 @@
|
||||
using AutoMapper;
|
||||
using GreenHome.Application;
|
||||
using GreenHome.Infrastructure.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace GreenHome.Infrastructure;
|
||||
namespace GreenHome.Infrastructure.Services.Device;
|
||||
|
||||
public sealed class DevicePostService : IDevicePostService
|
||||
{
|
||||
private readonly GreenHomeDbContext _context;
|
||||
private readonly GreenHomeDbContext _dbContext;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public DevicePostService(GreenHomeDbContext context, IMapper mapper)
|
||||
{
|
||||
_context = context;
|
||||
_dbContext = context;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
@@ -19,16 +20,14 @@ public sealed class DevicePostService : IDevicePostService
|
||||
DevicePostFilter filter,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var query = _context.DevicePosts
|
||||
var query = _dbContext.DevicePosts
|
||||
.Include(p => p.AuthorUser)
|
||||
.Include(p => p.Images)
|
||||
.AsNoTracking()
|
||||
.Where(p => p.DeviceId == filter.DeviceId);
|
||||
|
||||
if (filter.AuthorUserId.HasValue)
|
||||
{
|
||||
query = query.Where(p => p.AuthorUserId == filter.AuthorUserId.Value);
|
||||
}
|
||||
|
||||
var totalCount = await query.CountAsync(cancellationToken);
|
||||
|
||||
@@ -51,7 +50,7 @@ public sealed class DevicePostService : IDevicePostService
|
||||
|
||||
public async Task<DevicePostDto?> GetPostByIdAsync(int id, CancellationToken cancellationToken)
|
||||
{
|
||||
var post = await _context.DevicePosts
|
||||
var post = await _dbContext.DevicePosts
|
||||
.Include(p => p.AuthorUser)
|
||||
.Include(p => p.Images)
|
||||
.AsNoTracking()
|
||||
@@ -64,12 +63,9 @@ public sealed class DevicePostService : IDevicePostService
|
||||
CreateDevicePostRequest request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
// Verify user has access to device
|
||||
var hasAccess = await CanUserAccessDeviceAsync(request.AuthorUserId, request.DeviceId, cancellationToken);
|
||||
if (!hasAccess)
|
||||
{
|
||||
throw new UnauthorizedAccessException("کاربر به این دستگاه دسترسی ندارد");
|
||||
}
|
||||
|
||||
var post = new Domain.DevicePost
|
||||
{
|
||||
@@ -79,8 +75,8 @@ public sealed class DevicePostService : IDevicePostService
|
||||
CreatedAt = DateTime.UtcNow
|
||||
};
|
||||
|
||||
_context.DevicePosts.Add(post);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
_dbContext.DevicePosts.Add(post);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return post.Id;
|
||||
}
|
||||
@@ -89,33 +85,29 @@ public sealed class DevicePostService : IDevicePostService
|
||||
UpdateDevicePostRequest request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var post = await _context.DevicePosts
|
||||
var post = await _dbContext.DevicePosts
|
||||
.FirstOrDefaultAsync(p => p.Id == request.Id, cancellationToken);
|
||||
|
||||
if (post == null)
|
||||
{
|
||||
throw new InvalidOperationException($"پست با شناسه {request.Id} یافت نشد");
|
||||
}
|
||||
|
||||
post.Content = request.Content;
|
||||
post.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task DeletePostAsync(int id, CancellationToken cancellationToken)
|
||||
{
|
||||
var post = await _context.DevicePosts
|
||||
var post = await _dbContext.DevicePosts
|
||||
.Include(p => p.Images)
|
||||
.FirstOrDefaultAsync(p => p.Id == id, cancellationToken);
|
||||
|
||||
if (post == null)
|
||||
{
|
||||
throw new InvalidOperationException($"پست با شناسه {id} یافت نشد");
|
||||
}
|
||||
|
||||
_context.DevicePosts.Remove(post);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
_dbContext.DevicePosts.Remove(post);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<int> AddImageToPostAsync(
|
||||
@@ -126,13 +118,11 @@ public sealed class DevicePostService : IDevicePostService
|
||||
long fileSize,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var post = await _context.DevicePosts
|
||||
var post = await _dbContext.DevicePosts
|
||||
.FirstOrDefaultAsync(p => p.Id == postId, cancellationToken);
|
||||
|
||||
if (post == null)
|
||||
{
|
||||
throw new InvalidOperationException($"پست با شناسه {postId} یافت نشد");
|
||||
}
|
||||
|
||||
var image = new Domain.DevicePostImage
|
||||
{
|
||||
@@ -144,24 +134,22 @@ public sealed class DevicePostService : IDevicePostService
|
||||
UploadedAt = DateTime.UtcNow
|
||||
};
|
||||
|
||||
_context.DevicePostImages.Add(image);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
_dbContext.DevicePostImages.Add(image);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return image.Id;
|
||||
}
|
||||
|
||||
public async Task DeleteImageAsync(int imageId, CancellationToken cancellationToken)
|
||||
{
|
||||
var image = await _context.DevicePostImages
|
||||
var image = await _dbContext.DevicePostImages
|
||||
.FirstOrDefaultAsync(i => i.Id == imageId, cancellationToken);
|
||||
|
||||
if (image == null)
|
||||
{
|
||||
throw new InvalidOperationException($"تصویر با شناسه {imageId} یافت نشد");
|
||||
}
|
||||
|
||||
_context.DevicePostImages.Remove(image);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
_dbContext.DevicePostImages.Remove(image);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<bool> CanUserAccessDeviceAsync(
|
||||
@@ -169,17 +157,15 @@ public sealed class DevicePostService : IDevicePostService
|
||||
int deviceId,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
// Check if user is the device owner or has access through DeviceUsers
|
||||
var hasAccess = await _context.Devices
|
||||
var hasAccess = await _dbContext.Devices
|
||||
.AnyAsync(d => d.Id == deviceId && d.UserId == userId, cancellationToken);
|
||||
|
||||
if (!hasAccess)
|
||||
{
|
||||
hasAccess = await _context.DeviceUsers
|
||||
hasAccess = await _dbContext.DeviceUsers
|
||||
.AnyAsync(du => du.DeviceId == deviceId && du.UserId == userId, cancellationToken);
|
||||
}
|
||||
|
||||
return hasAccess;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,68 +1,66 @@
|
||||
using AutoMapper;
|
||||
using AutoMapper;
|
||||
using GreenHome.Application;
|
||||
using GreenHome.Infrastructure.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace GreenHome.Infrastructure;
|
||||
namespace GreenHome.Infrastructure.Services.Device;
|
||||
|
||||
public sealed class DeviceService : IDeviceService
|
||||
{
|
||||
private readonly GreenHomeDbContext dbContext;
|
||||
private readonly IMapper mapper;
|
||||
private readonly GreenHomeDbContext _dbContext;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public DeviceService(GreenHomeDbContext dbContext, IMapper mapper)
|
||||
{
|
||||
this.dbContext = dbContext;
|
||||
this.mapper = mapper;
|
||||
_dbContext = dbContext;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task<int> AddDeviceAsync(DeviceDto dto, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = mapper.Map<Domain.Device>(dto);
|
||||
dbContext.Devices.Add(entity);
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
var entity = _mapper.Map<Domain.Device>(dto);
|
||||
_dbContext.Devices.Add(entity);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
return entity.Id;
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<DeviceDto>> ListAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var items = await dbContext.Devices
|
||||
var items = await _dbContext.Devices
|
||||
.AsNoTracking()
|
||||
.Include(d => d.User)
|
||||
.OrderBy(d => d.DeviceName)
|
||||
.ToListAsync(cancellationToken);
|
||||
return mapper.Map<IReadOnlyList<DeviceDto>>(items);
|
||||
return _mapper.Map<IReadOnlyList<DeviceDto>>(items);
|
||||
}
|
||||
|
||||
public async Task<DeviceDto> GetDeviceId(string deviceName,CancellationToken cancellationToken)
|
||||
public async Task<DeviceDto> GetDeviceId(string deviceName, CancellationToken cancellationToken)
|
||||
{
|
||||
var item = await dbContext.Devices
|
||||
var item = await _dbContext.Devices
|
||||
.AsNoTracking()
|
||||
.Include(d => d.User)
|
||||
.Where(d=>d.DeviceName==deviceName)
|
||||
.Where(d => d.DeviceName == deviceName)
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
return mapper.Map<DeviceDto>(item);
|
||||
return _mapper.Map<DeviceDto>(item);
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<DeviceDto>> GetUserDevicesAsync(int userId, CancellationToken cancellationToken)
|
||||
{
|
||||
var items = await dbContext.Devices
|
||||
var items = await _dbContext.Devices
|
||||
.AsNoTracking()
|
||||
.Include(d => d.User)
|
||||
.Where(d => d.UserId == userId)
|
||||
.OrderBy(d => d.DeviceName)
|
||||
.ToListAsync(cancellationToken);
|
||||
return mapper.Map<IReadOnlyList<DeviceDto>>(items);
|
||||
return _mapper.Map<IReadOnlyList<DeviceDto>>(items);
|
||||
}
|
||||
|
||||
public async Task<PagedResult<DeviceDto>> GetDevicesAsync(DeviceFilter filter, CancellationToken cancellationToken)
|
||||
{
|
||||
if (!filter.UserId.HasValue)
|
||||
{
|
||||
throw new ArgumentException("UserId is required", nameof(filter));
|
||||
}
|
||||
|
||||
// Get user and role
|
||||
var user = await dbContext.Users
|
||||
var user = await _dbContext.Users
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(u => u.Id == filter.UserId.Value, cancellationToken);
|
||||
|
||||
@@ -77,24 +75,15 @@ public sealed class DeviceService : IDeviceService
|
||||
};
|
||||
}
|
||||
|
||||
// Build query based on role
|
||||
IQueryable<Domain.Device> query = dbContext.Devices
|
||||
IQueryable<Domain.Device> query = _dbContext.Devices
|
||||
.AsNoTracking()
|
||||
.Include(d => d.User);
|
||||
|
||||
if (user.Role == Domain.UserRole.Normal)
|
||||
{
|
||||
// Normal user: only own devices
|
||||
query = query.Where(d => d.UserId == user.Id);
|
||||
}
|
||||
else if (user.Role == Domain.UserRole.Supervisor)
|
||||
{
|
||||
// Supervisor: devices assigned to them
|
||||
query = query.Where(d => d.DeviceUsers.Any(du => du.UserId == user.Id));
|
||||
}
|
||||
// Admin: all devices (no filter)
|
||||
|
||||
// Apply search filter
|
||||
if (!string.IsNullOrWhiteSpace(filter.Search))
|
||||
{
|
||||
var searchTerm = filter.Search.Trim().ToLower();
|
||||
@@ -105,10 +94,8 @@ public sealed class DeviceService : IDeviceService
|
||||
d.Location.ToLower().Contains(searchTerm));
|
||||
}
|
||||
|
||||
// Get total count
|
||||
var totalCount = await query.CountAsync(cancellationToken);
|
||||
|
||||
// Apply pagination
|
||||
var items = await query
|
||||
.OrderBy(d => d.DeviceName)
|
||||
.Skip((filter.Page - 1) * filter.PageSize)
|
||||
@@ -117,7 +104,7 @@ public sealed class DeviceService : IDeviceService
|
||||
|
||||
return new PagedResult<DeviceDto>
|
||||
{
|
||||
Items = mapper.Map<IReadOnlyList<DeviceDto>>(items),
|
||||
Items = _mapper.Map<IReadOnlyList<DeviceDto>>(items),
|
||||
TotalCount = totalCount,
|
||||
Page = filter.Page,
|
||||
PageSize = filter.PageSize
|
||||
@@ -1,54 +1,53 @@
|
||||
using AutoMapper;
|
||||
using AutoMapper;
|
||||
using GreenHome.Application;
|
||||
using GreenHome.Infrastructure.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace GreenHome.Infrastructure;
|
||||
namespace GreenHome.Infrastructure.Services.Device;
|
||||
|
||||
public sealed class DeviceSettingsService : IDeviceSettingsService
|
||||
{
|
||||
private readonly GreenHomeDbContext dbContext;
|
||||
private readonly IMapper mapper;
|
||||
private readonly GreenHomeDbContext _dbContext;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public DeviceSettingsService(GreenHomeDbContext dbContext, IMapper mapper)
|
||||
{
|
||||
this.dbContext = dbContext;
|
||||
this.mapper = mapper;
|
||||
_dbContext = dbContext;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task<DeviceSettingsDto?> GetByDeviceIdAsync(int deviceId, CancellationToken cancellationToken)
|
||||
{
|
||||
var settings = await dbContext.DeviceSettings
|
||||
var settings = await _dbContext.DeviceSettings
|
||||
.Include(x => x.Device)
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(x => x.DeviceId == deviceId, cancellationToken);
|
||||
|
||||
return settings != null ? mapper.Map<DeviceSettingsDto>(settings) : null;
|
||||
return settings != null ? _mapper.Map<DeviceSettingsDto>(settings) : null;
|
||||
}
|
||||
|
||||
public async Task<int> CreateAsync(DeviceSettingsDto dto, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = mapper.Map<Domain.DeviceSettings>(dto);
|
||||
var entity = _mapper.Map<Domain.DeviceSettings>(dto);
|
||||
entity.CreatedAt = DateTime.UtcNow;
|
||||
entity.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
dbContext.DeviceSettings.Add(entity);
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
_dbContext.DeviceSettings.Add(entity);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
return entity.Id;
|
||||
}
|
||||
|
||||
public async Task UpdateAsync(DeviceSettingsDto dto, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await dbContext.DeviceSettings
|
||||
var entity = await _dbContext.DeviceSettings
|
||||
.FirstOrDefaultAsync(x => x.DeviceId == dto.DeviceId, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
throw new InvalidOperationException($"Device settings not found for device ID {dto.DeviceId}");
|
||||
}
|
||||
|
||||
mapper.Map(dto, entity);
|
||||
_mapper.Map(dto, entity);
|
||||
entity.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -1,32 +1,27 @@
|
||||
using GreenHome.Application;
|
||||
using GreenHome.Infrastructure.Data;
|
||||
using GreenHome.Sms.Ippanel;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace GreenHome.Infrastructure;
|
||||
namespace GreenHome.Infrastructure.Services.Device;
|
||||
|
||||
/// <summary>
|
||||
/// سرویس مدیریت توکن و تنظیمات دستگاه
|
||||
/// </summary>
|
||||
public sealed class DeviceTokenService : IDeviceTokenService
|
||||
{
|
||||
private readonly GreenHomeDbContext dbContext;
|
||||
private readonly ISmsService smsService;
|
||||
private readonly ILogger<DeviceTokenService> logger;
|
||||
private readonly GreenHomeDbContext _dbContext;
|
||||
private readonly ISmsService _smsService;
|
||||
private readonly ILogger<DeviceTokenService> _logger;
|
||||
|
||||
public DeviceTokenService(
|
||||
GreenHomeDbContext dbContext,
|
||||
ISmsService smsService,
|
||||
ILogger<DeviceTokenService> logger)
|
||||
{
|
||||
this.dbContext = dbContext;
|
||||
this.smsService = smsService;
|
||||
this.logger = logger;
|
||||
_dbContext = dbContext;
|
||||
_smsService = smsService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// دریافت فاصله زمانی آپلود بر اساس شماره تلفن یا شناسه دستگاه
|
||||
/// </summary>
|
||||
public async Task<GetUploadIntervalResponse> GetUploadIntervalAsync(
|
||||
GetUploadIntervalRequest request,
|
||||
CancellationToken cancellationToken)
|
||||
@@ -35,16 +30,15 @@ public sealed class DeviceTokenService : IDeviceTokenService
|
||||
{
|
||||
Domain.DeviceSettings? settings = null;
|
||||
|
||||
// جستجو بر اساس DeviceId یا DevicePhoneNumber
|
||||
if (request.DeviceId.HasValue)
|
||||
{
|
||||
settings = await dbContext.DeviceSettings
|
||||
settings = await _dbContext.DeviceSettings
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(ds => ds.DeviceId == request.DeviceId.Value, cancellationToken);
|
||||
}
|
||||
else if (!string.IsNullOrWhiteSpace(request.DevicePhoneNumber))
|
||||
{
|
||||
settings = await dbContext.DeviceSettings
|
||||
settings = await _dbContext.DeviceSettings
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(ds => ds.DevicePhoneNumber == request.DevicePhoneNumber, cancellationToken);
|
||||
}
|
||||
@@ -66,7 +60,7 @@ public sealed class DeviceTokenService : IDeviceTokenService
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Error getting upload interval for device");
|
||||
_logger.LogError(ex, "Error getting upload interval for device");
|
||||
return new GetUploadIntervalResponse
|
||||
{
|
||||
Success = false,
|
||||
@@ -75,17 +69,13 @@ public sealed class DeviceTokenService : IDeviceTokenService
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// درخواست توکن دستگاه (تولید و ارسال کد)
|
||||
/// </summary>
|
||||
public async Task<RequestDeviceTokenResponse> RequestDeviceTokenAsync(
|
||||
RequestDeviceTokenRequest request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
// پیدا کردن تنظیمات دستگاه بر اساس شماره تلفن
|
||||
var settings = await dbContext.DeviceSettings
|
||||
var settings = await _dbContext.DeviceSettings
|
||||
.FirstOrDefaultAsync(ds => ds.DevicePhoneNumber == request.DevicePhoneNumber, cancellationToken);
|
||||
|
||||
if (settings == null)
|
||||
@@ -97,28 +87,23 @@ public sealed class DeviceTokenService : IDeviceTokenService
|
||||
};
|
||||
}
|
||||
|
||||
// تولید کد توکن 5 رقمی
|
||||
var random = new Random();
|
||||
var tokenCode = random.Next(10000, 99999).ToString();
|
||||
|
||||
// تولید کد تایید بر اساس فرمول: (TokenCode * 7 + 12345) % 100000
|
||||
var verificationCode = ((int.Parse(tokenCode) * 7 + 12345) % 100000).ToString("D5");
|
||||
|
||||
// ذخیره کدها
|
||||
settings.TokenCode = tokenCode;
|
||||
settings.VerificationCode = verificationCode;
|
||||
settings.TokenExpiresAt = DateTime.UtcNow.AddMinutes(30); // اعتبار 10 دقیقه
|
||||
settings.TokenExpiresAt = DateTime.UtcNow.AddMinutes(30);
|
||||
settings.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
// ارسال کد توکن از طریق پیامک الگویی
|
||||
try
|
||||
{
|
||||
await smsService.SendPatternSmsAsync(new PatternSmsRequest
|
||||
await _smsService.SendPatternSmsAsync(new PatternSmsRequest
|
||||
{
|
||||
Recipients = new List<string> { request.DevicePhoneNumber },
|
||||
PatternCode = "gfukab9r0nca0pt", // TODO: کد الگوی پیامک را اینجا قرار دهید
|
||||
PatternCode = "gfukab9r0nca0pt",
|
||||
Variables = new Dictionary<string, string>
|
||||
{
|
||||
{ "tel", "09192530212" },
|
||||
@@ -128,7 +113,7 @@ public sealed class DeviceTokenService : IDeviceTokenService
|
||||
}
|
||||
catch (Exception smsEx)
|
||||
{
|
||||
logger.LogError(smsEx, "Error sending token SMS to {PhoneNumber}", request.DevicePhoneNumber);
|
||||
_logger.LogError(smsEx, "Error sending token SMS to {PhoneNumber}", request.DevicePhoneNumber);
|
||||
return new RequestDeviceTokenResponse
|
||||
{
|
||||
Success = false,
|
||||
@@ -136,7 +121,7 @@ public sealed class DeviceTokenService : IDeviceTokenService
|
||||
};
|
||||
}
|
||||
|
||||
logger.LogInformation("Token requested for device phone {PhoneNumber}", request.DevicePhoneNumber);
|
||||
_logger.LogInformation("Token requested for device phone {PhoneNumber}", request.DevicePhoneNumber);
|
||||
|
||||
return new RequestDeviceTokenResponse
|
||||
{
|
||||
@@ -147,7 +132,7 @@ public sealed class DeviceTokenService : IDeviceTokenService
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Error requesting device token");
|
||||
_logger.LogError(ex, "Error requesting device token");
|
||||
return new RequestDeviceTokenResponse
|
||||
{
|
||||
Success = false,
|
||||
@@ -156,17 +141,13 @@ public sealed class DeviceTokenService : IDeviceTokenService
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// تایید توکن دستگاه (ارسال تنظیمات)
|
||||
/// </summary>
|
||||
public async Task<VerifyDeviceTokenResponse> VerifyDeviceTokenAsync(
|
||||
VerifyDeviceTokenRequest request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
// پیدا کردن تنظیمات دستگاه
|
||||
var settings = await dbContext.DeviceSettings
|
||||
var settings = await _dbContext.DeviceSettings
|
||||
.Include(ds => ds.Device)
|
||||
.FirstOrDefaultAsync(ds => ds.DevicePhoneNumber == request.DevicePhoneNumber, cancellationToken);
|
||||
|
||||
@@ -179,7 +160,6 @@ public sealed class DeviceTokenService : IDeviceTokenService
|
||||
};
|
||||
}
|
||||
|
||||
// بررسی انقضای توکن
|
||||
if (settings.TokenExpiresAt == null || settings.TokenExpiresAt < DateTime.UtcNow)
|
||||
{
|
||||
return new VerifyDeviceTokenResponse
|
||||
@@ -189,7 +169,6 @@ public sealed class DeviceTokenService : IDeviceTokenService
|
||||
};
|
||||
}
|
||||
|
||||
// بررسی کد تایید
|
||||
if (settings.VerificationCode != request.VerificationCode)
|
||||
{
|
||||
return new VerifyDeviceTokenResponse
|
||||
@@ -198,21 +177,20 @@ public sealed class DeviceTokenService : IDeviceTokenService
|
||||
Message = "کد تایید نادرست است"
|
||||
};
|
||||
}
|
||||
Random rnd = new Random();
|
||||
// آمادهسازی پارامترها برای ارسال پیامک الگویی
|
||||
|
||||
var rnd = new Random();
|
||||
var deviceId = settings.Device.Id + "01";
|
||||
var uploadInterval = rnd.Next(1, 9) + "" + settings.UploadIntervalMin;
|
||||
var smsInterval = settings.MinimumSmsIntervalMinutes > 0 ? "1" + settings.MinimumSmsIntervalMinutes : "0" + rnd.Next(1, 9);
|
||||
var sysNumber = rnd.Next(139, 97654); // عدد رندوم SysNumber
|
||||
var simType = rnd.Next(1, 9).ToString() + ((int?)settings.SimCardType)?.ToString() ?? "1"; // مقدار عددی enum
|
||||
var sysNumber = rnd.Next(139, 97654);
|
||||
var simType = rnd.Next(1, 9).ToString() + ((int?)settings.SimCardType)?.ToString() ?? "1";
|
||||
|
||||
// ارسال تنظیمات از طریق پیامک الگویی
|
||||
try
|
||||
{
|
||||
await smsService.SendPatternSmsAsync(new PatternSmsRequest
|
||||
await _smsService.SendPatternSmsAsync(new PatternSmsRequest
|
||||
{
|
||||
Recipients = new List<string> { request.DevicePhoneNumber },
|
||||
PatternCode = "kx3kfqri7g09r02", // TODO: کد الگوی پیامک را اینجا قرار دهید
|
||||
PatternCode = "kx3kfqri7g09r02",
|
||||
Variables = new Dictionary<string, string>
|
||||
{
|
||||
{ "deviceId", deviceId },
|
||||
@@ -225,7 +203,7 @@ public sealed class DeviceTokenService : IDeviceTokenService
|
||||
}
|
||||
catch (Exception smsEx)
|
||||
{
|
||||
logger.LogError(smsEx, "Error sending settings SMS to {PhoneNumber}", request.DevicePhoneNumber);
|
||||
_logger.LogError(smsEx, "Error sending settings SMS to {PhoneNumber}", request.DevicePhoneNumber);
|
||||
return new VerifyDeviceTokenResponse
|
||||
{
|
||||
Success = false,
|
||||
@@ -233,14 +211,13 @@ public sealed class DeviceTokenService : IDeviceTokenService
|
||||
};
|
||||
}
|
||||
|
||||
// پاک کردن کدها بعد از استفاده موفق
|
||||
settings.TokenCode = null;
|
||||
settings.VerificationCode = null;
|
||||
settings.TokenExpiresAt = null;
|
||||
settings.UpdatedAt = DateTime.UtcNow;
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
logger.LogInformation("Device token verified for {PhoneNumber}", request.DevicePhoneNumber);
|
||||
_logger.LogInformation("Device token verified for {PhoneNumber}", request.DevicePhoneNumber);
|
||||
|
||||
return new VerifyDeviceTokenResponse
|
||||
{
|
||||
@@ -251,7 +228,7 @@ public sealed class DeviceTokenService : IDeviceTokenService
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Error verifying device token");
|
||||
_logger.LogError(ex, "Error verifying device token");
|
||||
return new VerifyDeviceTokenResponse
|
||||
{
|
||||
Success = false,
|
||||
@@ -260,4 +237,3 @@ public sealed class DeviceTokenService : IDeviceTokenService
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,14 +2,16 @@ using System.Diagnostics;
|
||||
using System.Text;
|
||||
using GreenHome.AI.DeepSeek;
|
||||
using GreenHome.Application;
|
||||
using GreenHome.Infrastructure.Data;
|
||||
using GreenHome.Infrastructure.Extensions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace GreenHome.Infrastructure;
|
||||
namespace GreenHome.Infrastructure.Services.Report;
|
||||
|
||||
public class DailyReportService : IDailyReportService
|
||||
public sealed class DailyReportService : IDailyReportService
|
||||
{
|
||||
private readonly GreenHomeDbContext _context;
|
||||
private readonly GreenHomeDbContext _dbContext;
|
||||
private readonly IDeepSeekService _deepSeekService;
|
||||
private readonly ILogger<DailyReportService> _logger;
|
||||
|
||||
@@ -18,7 +20,7 @@ public class DailyReportService : IDailyReportService
|
||||
IDeepSeekService deepSeekService,
|
||||
ILogger<DailyReportService> logger)
|
||||
{
|
||||
_context = context;
|
||||
_dbContext = context;
|
||||
_deepSeekService = deepSeekService;
|
||||
_logger = logger;
|
||||
}
|
||||
@@ -27,14 +29,10 @@ public class DailyReportService : IDailyReportService
|
||||
DailyReportRequest request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
// Validate Persian date format
|
||||
if (!IsValidPersianDate(request.PersianDate, out var year, out var month, out var day))
|
||||
{
|
||||
if (!PersianDateHelper.TryParsePersianDate(request.PersianDate, out var year, out var month, out var day))
|
||||
throw new ArgumentException("تاریخ شمسی باید به فرمت yyyy/MM/dd باشد", nameof(request.PersianDate));
|
||||
}
|
||||
|
||||
// Check if report already exists
|
||||
var existingReport = await _context.DailyReports
|
||||
var existingReport = await _dbContext.DailyReports
|
||||
.Include(r => r.Device)
|
||||
.FirstOrDefaultAsync(
|
||||
r => r.DeviceId == request.DeviceId && r.PersianDate == request.PersianDate,
|
||||
@@ -61,21 +59,16 @@ public class DailyReportService : IDailyReportService
|
||||
};
|
||||
}
|
||||
|
||||
// Get device info
|
||||
var device = await _context.Devices
|
||||
var device = await _dbContext.Devices
|
||||
.FirstOrDefaultAsync(d => d.Id == request.DeviceId, cancellationToken);
|
||||
|
||||
if (device == null)
|
||||
{
|
||||
throw new InvalidOperationException($"دستگاه با شناسه {request.DeviceId} یافت نشد");
|
||||
}
|
||||
|
||||
// Get device settings (including ProductType if available)
|
||||
var deviceSettings = await _context.DeviceSettings
|
||||
var deviceSettings = await _dbContext.DeviceSettings
|
||||
.FirstOrDefaultAsync(ds => ds.DeviceId == request.DeviceId, cancellationToken);
|
||||
|
||||
// Query telemetry data for the specified date
|
||||
var telemetryRecords = await _context.TelemetryRecords
|
||||
var telemetryRecords = await _dbContext.TelemetryRecords
|
||||
.Where(t => t.DeviceId == request.DeviceId && t.PersianDate == request.PersianDate)
|
||||
.OrderBy(t => t.TimestampUtc)
|
||||
.Select(t => new
|
||||
@@ -89,12 +82,9 @@ public class DailyReportService : IDailyReportService
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
if (telemetryRecords.Count == 0)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"هیچ رکوردی برای دستگاه {request.DeviceId} در تاریخ {request.PersianDate} یافت نشد");
|
||||
}
|
||||
|
||||
// Sample records: take first record from every 20 records
|
||||
var sampledRecords = telemetryRecords
|
||||
.Select((record, index) => new { record, index })
|
||||
.Where(x => x.index % 20 == 0)
|
||||
@@ -105,20 +95,17 @@ public class DailyReportService : IDailyReportService
|
||||
"تعداد {TotalCount} رکورد یافت شد. نمونهبرداری: {SampledCount} رکورد",
|
||||
telemetryRecords.Count, sampledRecords.Count);
|
||||
|
||||
// Build the data string for AI
|
||||
var dataBuilder = new StringBuilder();
|
||||
dataBuilder.AppendLine("زمان | دما (°C) | رطوبت (%) | نور (Lux) | CO (PPM)");
|
||||
dataBuilder.AppendLine("------|----------|-----------|-----------|----------");
|
||||
|
||||
foreach (var record in sampledRecords)
|
||||
{
|
||||
// Convert UTC to local time for display
|
||||
var localTime = record.TimestampUtc.AddHours(3.5); // Iran timezone (UTC+3:30)
|
||||
var localTime = record.TimestampUtc.AddHours(3.5);
|
||||
dataBuilder.AppendLine(
|
||||
$"{localTime:HH:mm:ss} | {record.TemperatureC:F1} | {record.HumidityPercent:F1} | {record.Lux:F1} | {record.GasPPM}");
|
||||
}
|
||||
|
||||
// Prepare the question for AI
|
||||
var productTypeInfo = !string.IsNullOrWhiteSpace(deviceSettings?.ProductType)
|
||||
? $" محصول کشت شده: {deviceSettings.ProductType}."
|
||||
: string.Empty;
|
||||
@@ -139,7 +126,6 @@ public class DailyReportService : IDailyReportService
|
||||
|
||||
خلاصه و مفید باش (حداکثر 300 کلمه).";
|
||||
|
||||
// Send to DeepSeek
|
||||
var stopwatch = Stopwatch.StartNew();
|
||||
ChatResponse? aiResponse;
|
||||
|
||||
@@ -172,13 +158,10 @@ public class DailyReportService : IDailyReportService
|
||||
|
||||
if (aiResponse?.Choices == null || aiResponse.Choices.Count == 0 ||
|
||||
string.IsNullOrWhiteSpace(aiResponse.Choices[0].Message?.Content))
|
||||
{
|
||||
throw new InvalidOperationException("پاسخ نامعتبر از سرویس هوش مصنوعی");
|
||||
}
|
||||
|
||||
var analysis = aiResponse.Choices[0].Message!.Content;
|
||||
|
||||
// Save the report
|
||||
var dailyReport = new Domain.DailyReport
|
||||
{
|
||||
DeviceId = request.DeviceId,
|
||||
@@ -197,8 +180,8 @@ public class DailyReportService : IDailyReportService
|
||||
CreatedAt = DateTime.UtcNow
|
||||
};
|
||||
|
||||
_context.DailyReports.Add(dailyReport);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
_dbContext.DailyReports.Add(dailyReport);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
_logger.LogInformation(
|
||||
"گزارش روزانه جدید برای دستگاه {DeviceId} و تاریخ {Date} ایجاد شد. توکن مصرف شده: {Tokens}",
|
||||
@@ -219,48 +202,20 @@ public class DailyReportService : IDailyReportService
|
||||
};
|
||||
}
|
||||
|
||||
private static bool IsValidPersianDate(string persianDate, out int year, out int month, out int day)
|
||||
{
|
||||
year = month = day = 0;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(persianDate))
|
||||
return false;
|
||||
|
||||
var parts = persianDate.Split('/');
|
||||
if (parts.Length != 3)
|
||||
return false;
|
||||
|
||||
if (!int.TryParse(parts[0], out year) || year < 1300 || year > 1500)
|
||||
return false;
|
||||
|
||||
if (!int.TryParse(parts[1], out month) || month < 1 || month > 12)
|
||||
return false;
|
||||
|
||||
if (!int.TryParse(parts[2], out day) || day < 1 || day > 31)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<DailyReportResponse> GetWeeklyAnalysisAsync(
|
||||
WeeklyAnalysisRequest request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
// Get device info
|
||||
var device = await _context.Devices
|
||||
var device = await _dbContext.Devices
|
||||
.FirstOrDefaultAsync(d => d.Id == request.DeviceId, cancellationToken);
|
||||
|
||||
if (device == null)
|
||||
{
|
||||
throw new InvalidOperationException($"دستگاه با شناسه {request.DeviceId} یافت نشد");
|
||||
}
|
||||
|
||||
// Get device settings
|
||||
var deviceSettings = await _context.DeviceSettings
|
||||
var deviceSettings = await _dbContext.DeviceSettings
|
||||
.FirstOrDefaultAsync(ds => ds.DeviceId == request.DeviceId, cancellationToken);
|
||||
|
||||
// Query telemetry data for the week
|
||||
var telemetryRecords = await _context.TelemetryRecords
|
||||
var telemetryRecords = await _dbContext.TelemetryRecords
|
||||
.Where(t => t.DeviceId == request.DeviceId &&
|
||||
string.Compare(t.PersianDate, request.StartDate) >= 0 &&
|
||||
string.Compare(t.PersianDate, request.EndDate) <= 0)
|
||||
@@ -277,12 +232,9 @@ public class DailyReportService : IDailyReportService
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
if (telemetryRecords.Count == 0)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"هیچ رکوردی برای دستگاه {request.DeviceId} در بازه {request.StartDate} تا {request.EndDate} یافت نشد");
|
||||
}
|
||||
|
||||
// Sample 1 per 100 records
|
||||
var sampledRecords = telemetryRecords
|
||||
.Select((record, index) => new { record, index })
|
||||
.Where(x => x.index % 100 == 0)
|
||||
@@ -293,7 +245,6 @@ public class DailyReportService : IDailyReportService
|
||||
"تعداد {TotalCount} رکورد یافت شد. نمونهبرداری هفتگی: {SampledCount} رکورد",
|
||||
telemetryRecords.Count, sampledRecords.Count);
|
||||
|
||||
// Build the data string
|
||||
var dataBuilder = new StringBuilder();
|
||||
dataBuilder.AppendLine("تاریخ | زمان | دما (°C) | رطوبت (%) | نور (Lux) | CO (PPM)");
|
||||
dataBuilder.AppendLine("---------|----------|----------|-----------|-----------|----------");
|
||||
@@ -305,7 +256,6 @@ public class DailyReportService : IDailyReportService
|
||||
$"{record.PersianDate} | {localTime:HH:mm} | {record.TemperatureC:F1} | {record.HumidityPercent:F1} | {record.Lux:F1} | {record.GasPPM}");
|
||||
}
|
||||
|
||||
// Prepare AI prompt
|
||||
var productTypeInfo = !string.IsNullOrWhiteSpace(deviceSettings?.ProductType)
|
||||
? $" محصول کشت شده: {deviceSettings.ProductType}."
|
||||
: string.Empty;
|
||||
@@ -330,7 +280,6 @@ public class DailyReportService : IDailyReportService
|
||||
? $"تو یک متخصص کشاورزی و گلخانه هستی که در کشت {deviceSettings.ProductType} تخصص داری و دادههای تلمتری رو تحلیل میکنی."
|
||||
: "تو یک متخصص کشاورزی و گلخانه هستی که دادههای تلمتری رو تحلیل میکنی.";
|
||||
|
||||
// Send to DeepSeek
|
||||
var stopwatch = Stopwatch.StartNew();
|
||||
var chatRequest = new ChatRequest
|
||||
{
|
||||
@@ -348,9 +297,7 @@ public class DailyReportService : IDailyReportService
|
||||
|
||||
if (aiResponse?.Choices == null || aiResponse.Choices.Count == 0 ||
|
||||
string.IsNullOrWhiteSpace(aiResponse.Choices[0].Message?.Content))
|
||||
{
|
||||
throw new InvalidOperationException("پاسخ نامعتبر از سرویس هوش مصنوعی");
|
||||
}
|
||||
|
||||
return new DailyReportResponse
|
||||
{
|
||||
@@ -371,21 +318,16 @@ public class DailyReportService : IDailyReportService
|
||||
MonthlyAnalysisRequest request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
// Get device info
|
||||
var device = await _context.Devices
|
||||
var device = await _dbContext.Devices
|
||||
.FirstOrDefaultAsync(d => d.Id == request.DeviceId, cancellationToken);
|
||||
|
||||
if (device == null)
|
||||
{
|
||||
throw new InvalidOperationException($"دستگاه با شناسه {request.DeviceId} یافت نشد");
|
||||
}
|
||||
|
||||
// Get device settings
|
||||
var deviceSettings = await _context.DeviceSettings
|
||||
var deviceSettings = await _dbContext.DeviceSettings
|
||||
.FirstOrDefaultAsync(ds => ds.DeviceId == request.DeviceId, cancellationToken);
|
||||
|
||||
// Get all daily reports for this month
|
||||
var dailyReports = await _context.DailyReports
|
||||
var dailyReports = await _dbContext.DailyReports
|
||||
.Where(dr => dr.DeviceId == request.DeviceId &&
|
||||
dr.PersianYear == request.Year &&
|
||||
dr.PersianMonth == request.Month)
|
||||
@@ -394,12 +336,9 @@ public class DailyReportService : IDailyReportService
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
if (dailyReports.Count == 0)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"هیچ تحلیل روزانهای برای دستگاه {request.DeviceId} در ماه {request.Month} سال {request.Year} یافت نشد");
|
||||
}
|
||||
|
||||
// Build summary of daily analyses
|
||||
var summaryBuilder = new StringBuilder();
|
||||
summaryBuilder.AppendLine($"تحلیلهای روزانه ماه {request.Month} سال {request.Year}:");
|
||||
summaryBuilder.AppendLine();
|
||||
@@ -413,7 +352,6 @@ public class DailyReportService : IDailyReportService
|
||||
summaryBuilder.AppendLine();
|
||||
}
|
||||
|
||||
// Prepare AI prompt
|
||||
var productTypeInfo = !string.IsNullOrWhiteSpace(deviceSettings?.ProductType)
|
||||
? $" محصول کشت شده: {deviceSettings.ProductType}."
|
||||
: string.Empty;
|
||||
@@ -439,7 +377,6 @@ public class DailyReportService : IDailyReportService
|
||||
? $"تو یک متخصص کشاورزی و گلخانه هستی که در کشت {deviceSettings.ProductType} تخصص داری. تحلیلهای روزانه رو بررسی کن و یک جمعبندی ماهانه جامع ارائه بده."
|
||||
: "تو یک متخصص کشاورزی و گلخانه هستی. تحلیلهای روزانه رو بررسی کن و یک جمعبندی ماهانه جامع ارائه بده.";
|
||||
|
||||
// Send to DeepSeek
|
||||
var stopwatch = Stopwatch.StartNew();
|
||||
var chatRequest = new ChatRequest
|
||||
{
|
||||
@@ -457,9 +394,7 @@ public class DailyReportService : IDailyReportService
|
||||
|
||||
if (aiResponse?.Choices == null || aiResponse.Choices.Count == 0 ||
|
||||
string.IsNullOrWhiteSpace(aiResponse.Choices[0].Message?.Content))
|
||||
{
|
||||
throw new InvalidOperationException("پاسخ نامعتبر از سرویس هوش مصنوعی");
|
||||
}
|
||||
|
||||
return new DailyReportResponse
|
||||
{
|
||||
@@ -476,4 +411,3 @@ public class DailyReportService : IDailyReportService
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
using GreenHome.Application;
|
||||
using GreenHome.Infrastructure.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace GreenHome.Infrastructure;
|
||||
namespace GreenHome.Infrastructure.Services.Report;
|
||||
|
||||
public sealed class MonthlyReportService : IMonthlyReportService
|
||||
{
|
||||
private readonly GreenHomeDbContext _context;
|
||||
private readonly GreenHomeDbContext _dbContext;
|
||||
|
||||
public MonthlyReportService(GreenHomeDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
_dbContext = context;
|
||||
}
|
||||
|
||||
public async Task<MonthlyReportDto> GetMonthlyReportAsync(
|
||||
@@ -18,16 +19,13 @@ public sealed class MonthlyReportService : IMonthlyReportService
|
||||
int month,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var device = await _context.Devices
|
||||
var device = await _dbContext.Devices
|
||||
.FirstOrDefaultAsync(d => d.Id == deviceId, cancellationToken);
|
||||
|
||||
if (device == null)
|
||||
{
|
||||
throw new InvalidOperationException($"دستگاه با شناسه {deviceId} یافت نشد");
|
||||
}
|
||||
|
||||
// Get alert logs for the month
|
||||
var alertLogs = await _context.AlertLogs
|
||||
var alertLogs = await _dbContext.AlertLogs
|
||||
.Where(al => al.DeviceId == deviceId &&
|
||||
al.SentAt.Year == year &&
|
||||
al.SentAt.Month == month)
|
||||
@@ -40,8 +38,7 @@ public sealed class MonthlyReportService : IMonthlyReportService
|
||||
var failedAlerts = alertLogs.Count(al => al.Status == Domain.AlertStatus.Failed);
|
||||
var powerOutageAlerts = alertLogs.Count(al => al.AlertType == Domain.AlertType.PowerOutage);
|
||||
|
||||
// Get telemetry statistics
|
||||
var telemetryData = await _context.TelemetryRecords
|
||||
var telemetryData = await _dbContext.TelemetryRecords
|
||||
.Where(t => t.DeviceId == deviceId &&
|
||||
t.PersianYear == year &&
|
||||
t.PersianMonth == month)
|
||||
@@ -58,35 +55,26 @@ public sealed class MonthlyReportService : IMonthlyReportService
|
||||
var avgGas = telemetryData.Any() ? (int)telemetryData.Average(t => t.GasPPM) : 0;
|
||||
var maxGas = telemetryData.Any() ? telemetryData.Max(t => t.GasPPM) : 0;
|
||||
|
||||
// Get user activity
|
||||
var userReportsCount = await _context.UserDailyReports
|
||||
var userReportsCount = await _dbContext.UserDailyReports
|
||||
.CountAsync(r => r.DeviceId == deviceId &&
|
||||
r.PersianYear == year &&
|
||||
r.PersianMonth == month,
|
||||
cancellationToken);
|
||||
|
||||
var checklistCompletionsCount = await _context.ChecklistCompletions
|
||||
var checklistCompletionsCount = await _dbContext.ChecklistCompletions
|
||||
.Where(cc => cc.Checklist.DeviceId == deviceId)
|
||||
.CountAsync(cc => cc.PersianDate.StartsWith($"{year}/{month:D2}"), cancellationToken);
|
||||
|
||||
var dailyAnalysesCount = await _context.DailyReports
|
||||
var dailyAnalysesCount = await _dbContext.DailyReports
|
||||
.CountAsync(dr => dr.DeviceId == deviceId &&
|
||||
dr.PersianYear == year &&
|
||||
dr.PersianMonth == month,
|
||||
cancellationToken);
|
||||
|
||||
// Generate performance summary
|
||||
var performanceSummary = GeneratePerformanceSummary(
|
||||
totalTelemetryRecords,
|
||||
totalAlerts,
|
||||
successfulAlerts,
|
||||
failedAlerts,
|
||||
avgTemp,
|
||||
avgHumidity,
|
||||
avgLux,
|
||||
avgGas,
|
||||
userReportsCount,
|
||||
checklistCompletionsCount);
|
||||
totalTelemetryRecords, totalAlerts, successfulAlerts, failedAlerts,
|
||||
avgTemp, avgHumidity, avgLux, avgGas,
|
||||
userReportsCount, checklistCompletionsCount);
|
||||
|
||||
return new MonthlyReportDto
|
||||
{
|
||||
@@ -118,17 +106,10 @@ public sealed class MonthlyReportService : IMonthlyReportService
|
||||
};
|
||||
}
|
||||
|
||||
private string GeneratePerformanceSummary(
|
||||
int totalRecords,
|
||||
int totalAlerts,
|
||||
int successfulAlerts,
|
||||
int failedAlerts,
|
||||
decimal avgTemp,
|
||||
decimal avgHumidity,
|
||||
decimal avgLux,
|
||||
int avgGas,
|
||||
int userReports,
|
||||
int checklistCompletions)
|
||||
private static string GeneratePerformanceSummary(
|
||||
int totalRecords, int totalAlerts, int successfulAlerts, int failedAlerts,
|
||||
decimal avgTemp, decimal avgHumidity, decimal avgLux, int avgGas,
|
||||
int userReports, int checklistCompletions)
|
||||
{
|
||||
var summary = new List<string>();
|
||||
|
||||
@@ -157,7 +138,6 @@ public sealed class MonthlyReportService : IMonthlyReportService
|
||||
summary.Add($" • تکمیل چکلیست: {checklistCompletions}");
|
||||
}
|
||||
|
||||
// Performance rating
|
||||
summary.Add("");
|
||||
var rating = CalculatePerformanceRating(totalRecords, failedAlerts, totalAlerts);
|
||||
summary.Add($"⭐ ارزیابی کلی: {rating}");
|
||||
@@ -165,7 +145,7 @@ public sealed class MonthlyReportService : IMonthlyReportService
|
||||
return string.Join("\n", summary);
|
||||
}
|
||||
|
||||
private string CalculatePerformanceRating(int totalRecords, int failedAlerts, int totalAlerts)
|
||||
private static string CalculatePerformanceRating(int totalRecords, int failedAlerts, int totalAlerts)
|
||||
{
|
||||
if (totalRecords == 0)
|
||||
return "بدون داده";
|
||||
@@ -182,4 +162,3 @@ public sealed class MonthlyReportService : IMonthlyReportService
|
||||
return "نیاز به بررسی 🔧";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
using AutoMapper;
|
||||
using GreenHome.Application;
|
||||
using GreenHome.Infrastructure.Data;
|
||||
using GreenHome.Infrastructure.Extensions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace GreenHome.Infrastructure;
|
||||
namespace GreenHome.Infrastructure.Services.Report;
|
||||
|
||||
public sealed class UserDailyReportService : IUserDailyReportService
|
||||
{
|
||||
@@ -87,8 +89,8 @@ public sealed class UserDailyReportService : IUserDailyReportService
|
||||
CreateUserDailyReportRequest request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
// Validate date format
|
||||
if (!IsValidPersianDate(request.PersianDate, out var year, out var month, out var day))
|
||||
// Validate Persian date format
|
||||
if (!PersianDateHelper.TryParsePersianDate(request.PersianDate, out var year, out var month, out var day))
|
||||
{
|
||||
throw new ArgumentException("تاریخ شمسی باید به فرمت yyyy/MM/dd باشد");
|
||||
}
|
||||
@@ -198,28 +200,4 @@ public sealed class UserDailyReportService : IUserDailyReportService
|
||||
_context.ReportImages.Remove(image);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
private static bool IsValidPersianDate(string persianDate, out int year, out int month, out int day)
|
||||
{
|
||||
year = month = day = 0;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(persianDate))
|
||||
return false;
|
||||
|
||||
var parts = persianDate.Split('/');
|
||||
if (parts.Length != 3)
|
||||
return false;
|
||||
|
||||
if (!int.TryParse(parts[0], out year) || year < 1300 || year > 1500)
|
||||
return false;
|
||||
|
||||
if (!int.TryParse(parts[1], out month) || month < 1 || month > 12)
|
||||
return false;
|
||||
|
||||
if (!int.TryParse(parts[2], out day) || day < 1 || day > 31)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using GreenHome.Application;
|
||||
|
||||
namespace GreenHome.Infrastructure;
|
||||
namespace GreenHome.Infrastructure.Services.Sun;
|
||||
|
||||
/// <summary>
|
||||
/// سرویس محاسبه طلوع و غروب خورشید
|
||||
@@ -94,7 +94,7 @@ public sealed class SunCalculatorService : ISunCalculatorService
|
||||
return (sunrise, sunset);
|
||||
}
|
||||
|
||||
private double CalculateJulianDay(DateTime date)
|
||||
private static double CalculateJulianDay(DateTime date)
|
||||
{
|
||||
var year = date.Year;
|
||||
var month = date.Month;
|
||||
@@ -112,14 +112,13 @@ public sealed class SunCalculatorService : ISunCalculatorService
|
||||
return Math.Floor(365.25 * (year + 4716)) + Math.Floor(30.6001 * (month + 1)) + day + b - 1524.5;
|
||||
}
|
||||
|
||||
private double ToRadians(double degrees)
|
||||
private static double ToRadians(double degrees)
|
||||
{
|
||||
return degrees * Math.PI / 180.0;
|
||||
}
|
||||
|
||||
private double ToDegrees(double radians)
|
||||
private static double ToDegrees(double radians)
|
||||
{
|
||||
return radians * 180.0 / Math.PI;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,56 +1,56 @@
|
||||
using System.Globalization;
|
||||
using AutoMapper;
|
||||
using GreenHome.Application;
|
||||
using GreenHome.Infrastructure.Data;
|
||||
using GreenHome.Infrastructure.Extensions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace GreenHome.Infrastructure;
|
||||
namespace GreenHome.Infrastructure.Services.Telemetry;
|
||||
|
||||
public sealed class TelemetryService : ITelemetryService
|
||||
{
|
||||
private readonly GreenHomeDbContext dbContext;
|
||||
private readonly IMapper mapper;
|
||||
private static readonly PersianCalendar PersianCalendar = new PersianCalendar();
|
||||
private readonly GreenHomeDbContext _dbContext;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public TelemetryService(GreenHomeDbContext dbContext, IMapper mapper)
|
||||
{
|
||||
this.dbContext = dbContext;
|
||||
this.mapper = mapper;
|
||||
_dbContext = dbContext;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task<int> AddAsync(TelemetryDto dto, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = mapper.Map<Domain.TelemetryRecord>(dto);
|
||||
var entity = _mapper.Map<Domain.TelemetryRecord>(dto);
|
||||
|
||||
if (!string.IsNullOrEmpty(dto.DeviceName))
|
||||
{
|
||||
var device = await dbContext.Devices.FirstOrDefaultAsync(d => d.DeviceName == dto.DeviceName, cancellationToken);
|
||||
var device = await _dbContext.Devices.FirstOrDefaultAsync(d => d.DeviceName == dto.DeviceName, cancellationToken);
|
||||
if (device != null)
|
||||
{
|
||||
entity.DeviceId = device.Id;
|
||||
dto.DeviceId = device.Id; // Update DTO for alert service
|
||||
dto.DeviceId = device.Id;
|
||||
}
|
||||
}
|
||||
if (dto.Id!=0)
|
||||
|
||||
if (dto.Id != 0)
|
||||
{
|
||||
var device = await dbContext.Devices.FirstOrDefaultAsync(d => d.Id == dto.Id, cancellationToken);
|
||||
var device = await _dbContext.Devices.FirstOrDefaultAsync(d => d.Id == dto.Id, cancellationToken);
|
||||
if (device != null)
|
||||
{
|
||||
entity.DeviceId = device.Id;
|
||||
}
|
||||
}
|
||||
// ذخیره زمان سرور در لحظه ثبت
|
||||
|
||||
entity.ServerTimestampUtc = DateTime.Now;
|
||||
|
||||
var dt = dto.TimestampUtc!.Value.ToLocalTime();
|
||||
var py = PersianCalendar.GetYear(dt);
|
||||
var pm = PersianCalendar.GetMonth(dt);
|
||||
var pd = PersianCalendar.GetDayOfMonth(dt);
|
||||
entity.PersianYear = py;
|
||||
entity.PersianMonth = pm;
|
||||
entity.PersianDate = $"{py:0000}/{pm:00}/{pd:00}";
|
||||
|
||||
if(entity.Lux < 0)
|
||||
if (dto.TimestampUtc.HasValue)
|
||||
{
|
||||
var lastItem = await dbContext.TelemetryRecords
|
||||
var (year, month, day, persianDate) = dto.TimestampUtc.Value.ToPersianDate();
|
||||
entity.PersianYear = year;
|
||||
entity.PersianMonth = month;
|
||||
entity.PersianDate = persianDate;
|
||||
}
|
||||
|
||||
if (entity.Lux < 0)
|
||||
{
|
||||
var lastItem = await _dbContext.TelemetryRecords
|
||||
.Where(x => x.DeviceId == entity.DeviceId)
|
||||
.OrderByDescending(x => x.TimestampUtc)
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
@@ -58,52 +58,46 @@ public sealed class TelemetryService : ITelemetryService
|
||||
entity.Lux = lastItem?.Lux ?? 0;
|
||||
}
|
||||
|
||||
dbContext.TelemetryRecords.Add(entity);
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
_dbContext.TelemetryRecords.Add(entity);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
return entity.Id;
|
||||
}
|
||||
|
||||
public async Task<PagedResult<TelemetryDto>> ListAsync(TelemetryFilter filter, CancellationToken cancellationToken)
|
||||
{
|
||||
var query = dbContext.TelemetryRecords.AsNoTracking().AsQueryable();
|
||||
if (filter.DeviceId.HasValue)
|
||||
{
|
||||
query = query.Where(x => x.DeviceId == filter.DeviceId.Value);
|
||||
}
|
||||
if (filter.StartDateUtc.HasValue)
|
||||
{
|
||||
//var start = filter.StartDateUtc.Value.Date.AddDays(1);
|
||||
var query = _dbContext.TelemetryRecords.AsNoTracking().AsQueryable();
|
||||
|
||||
if (filter.DeviceId.HasValue)
|
||||
query = query.Where(x => x.DeviceId == filter.DeviceId.Value);
|
||||
|
||||
if (filter.StartDateUtc.HasValue)
|
||||
query = query.Where(x => x.ServerTimestampUtc >= filter.StartDateUtc.Value.ToLocalTime());
|
||||
}
|
||||
|
||||
if (filter.EndDateUtc.HasValue)
|
||||
{
|
||||
query = query.Where(x => x.ServerTimestampUtc < filter.EndDateUtc.Value.ToLocalTime());
|
||||
}
|
||||
|
||||
if(filter.Page <= 0) filter.Page = 1;
|
||||
if(filter.PageSize <= 0) filter.PageSize = 1000000; // No limit
|
||||
if (filter.Page <= 0) filter.Page = 1;
|
||||
if (filter.PageSize <= 0) filter.PageSize = 1000000;
|
||||
|
||||
var total = await query.CountAsync(cancellationToken);
|
||||
var skip = (filter.Page - 1) * filter.PageSize;
|
||||
|
||||
var items = await query
|
||||
.OrderByDescending(x => x.ServerTimestampUtc)
|
||||
.Skip(skip)
|
||||
.Take(filter.PageSize)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
if(items.Any(x => x.Lux < 0))
|
||||
// Fix negative Lux values
|
||||
for (int i = 1; i < items.Count; i++)
|
||||
{
|
||||
for (int i = 1; i < items.Count; i++)
|
||||
{
|
||||
if (items[i].Lux < 0)
|
||||
items[i].Lux = items[i - 1].Lux;
|
||||
}
|
||||
if (items[i].Lux < 0)
|
||||
items[i].Lux = items[i - 1].Lux;
|
||||
}
|
||||
|
||||
return new PagedResult<TelemetryDto>
|
||||
{
|
||||
Items = mapper.Map<IReadOnlyList<TelemetryDto>>(items),
|
||||
Items = _mapper.Map<IReadOnlyList<TelemetryDto>>(items),
|
||||
TotalCount = total,
|
||||
Page = filter.Page,
|
||||
PageSize = filter.PageSize
|
||||
@@ -112,7 +106,7 @@ public sealed class TelemetryService : ITelemetryService
|
||||
|
||||
public async Task<TelemetryMinMax> GetMinMaxAsync(int deviceId, DateTime? startUtc, DateTime? endUtc, CancellationToken cancellationToken)
|
||||
{
|
||||
var query = dbContext.TelemetryRecords.AsNoTracking().Where(x => x.DeviceId == deviceId);
|
||||
var query = _dbContext.TelemetryRecords.AsNoTracking().Where(x => x.DeviceId == deviceId);
|
||||
if (startUtc.HasValue) query = query.Where(x => x.TimestampUtc >= startUtc.Value);
|
||||
if (endUtc.HasValue) query = query.Where(x => x.TimestampUtc <= endUtc.Value);
|
||||
|
||||
@@ -128,9 +122,7 @@ public sealed class TelemetryService : ITelemetryService
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
if (result.Count == 0)
|
||||
{
|
||||
return new TelemetryMinMax();
|
||||
}
|
||||
|
||||
return new TelemetryMinMax
|
||||
{
|
||||
@@ -149,7 +141,7 @@ public sealed class TelemetryService : ITelemetryService
|
||||
|
||||
public async Task<IReadOnlyList<DayCount>> GetMonthDaysAsync(int deviceId, int persianYear, int persianMonth, CancellationToken cancellationToken)
|
||||
{
|
||||
var query = dbContext.TelemetryRecords.AsNoTracking()
|
||||
var query = _dbContext.TelemetryRecords.AsNoTracking()
|
||||
.Where(x => x.DeviceId == deviceId && x.PersianYear == persianYear && x.PersianMonth == persianMonth)
|
||||
.GroupBy(x => x.PersianDate)
|
||||
.Select(g => new DayCount { PersianDate = g.Key, Count = g.Count() })
|
||||
@@ -160,7 +152,7 @@ public sealed class TelemetryService : ITelemetryService
|
||||
|
||||
public async Task<IReadOnlyList<int>> GetActiveMonthsAsync(int deviceId, int persianYear, CancellationToken cancellationToken)
|
||||
{
|
||||
var months = await dbContext.TelemetryRecords.AsNoTracking()
|
||||
var months = await _dbContext.TelemetryRecords.AsNoTracking()
|
||||
.Where(x => x.DeviceId == deviceId && x.PersianYear == persianYear)
|
||||
.Select(x => x.PersianMonth)
|
||||
.Distinct()
|
||||
@@ -1,4 +0,0 @@
|
||||
public class VoiceCallService
|
||||
{
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user