diff --git a/PARR.AIHIT/AIHIT/PARR.AIHIT.csproj b/PARR.AIHIT/AIHIT/PARR.AIHIT.csproj new file mode 100644 index 00000000..046f3ed5 --- /dev/null +++ b/PARR.AIHIT/AIHIT/PARR.AIHIT.csproj @@ -0,0 +1,18 @@ + + + + net7.0 + enable + enable + + + + + + + + + + + + diff --git a/PARR.AIHIT/AIHIT/Settings/SyncherSettings.cs b/PARR.AIHIT/AIHIT/Settings/SyncherSettings.cs new file mode 100644 index 00000000..b81a3050 --- /dev/null +++ b/PARR.AIHIT/AIHIT/Settings/SyncherSettings.cs @@ -0,0 +1,10 @@ +namespace PARR.AIHIT.Settings +{ + internal class SyncherSettings + { + public int OccursEvery { get; set; } + public string? IncludeAddresses { get; set; } + + public List IncludeAddressesArray => IncludeAddresses == null ? new List() : IncludeAddresses.Split(",").ToList(); + } +} diff --git a/PARR.AIHIT/AIHIT/Syncher.cs b/PARR.AIHIT/AIHIT/Syncher.cs new file mode 100644 index 00000000..450b5297 --- /dev/null +++ b/PARR.AIHIT/AIHIT/Syncher.cs @@ -0,0 +1,38 @@ +using MailKit.Search; +using Microsoft.Extensions.Logging; +using PARR.AIHIT.Settings; +using PARR.Mail.Services; + +namespace PARR.AIHIT +{ + internal class Syncher + { + private readonly ILogger logger; + private readonly IMailService mailService; + private readonly SyncherSettings syncherSettings; + + public Syncher(ILogger logger, IMailService mailService, SyncherSettings syncherSettings) + { + this.logger = logger; + this.mailService = mailService; + this.syncherSettings = syncherSettings; + } + + public async Task InvokeAsync() + { + var messages = await mailService.CheckMailAsync(SearchQuery.NotSeen); + if (!messages.Any()) + { + return; + } + //var includeAddresses = aIHITSyncherSettings.IncludeAddresses.Any() ? aIHITSyncherSettings.IncludeAddresses.Split(",") : null; + // var filtered = messages.Where(m => includeAddresses.Any(i => m.Envelope.From.Mailboxes.First().Address == i)).ToList(); + + var filtered = messages.Where(m => syncherSettings.IncludeAddressesArray.Any(i => m.Envelope.From.Mailboxes.First().Address == i)).ToList(); + foreach ( var message in filtered) + { + + } + } + } +} \ No newline at end of file diff --git a/PARR.AIHIT/AIHIT/SyncherInstaller.cs b/PARR.AIHIT/AIHIT/SyncherInstaller.cs new file mode 100644 index 00000000..290da3d2 --- /dev/null +++ b/PARR.AIHIT/AIHIT/SyncherInstaller.cs @@ -0,0 +1,23 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using PARR.AIHIT.Settings; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PARR.AIHIT +{ + public class SyncherInstaller + { + public void InstallerServices(IServiceCollection services, IConfiguration configuration) + { + var syncherSettings = new SyncherSettings(); + configuration.GetSection(nameof(SyncherSettings)).Bind(syncherSettings); + services.AddSingleton(syncherSettings); + + services.AddTransient(); + } + } +} diff --git a/PARR.DAL/Migrations/20230601014714_UpdTblHosts.Designer.cs b/PARR.DAL/Migrations/20230601014714_UpdTblHosts.Designer.cs new file mode 100644 index 00000000..920eb8bd --- /dev/null +++ b/PARR.DAL/Migrations/20230601014714_UpdTblHosts.Designer.cs @@ -0,0 +1,336 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using PARR.DAL.Context; + +#nullable disable + +namespace PARR.DAL.Migrations +{ + [DbContext(typeof(DataContext))] + [Migration("20230601014714_UpdTblHosts")] + partial class UpdTblHosts + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.5") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("PARR.DAL.Models.Host", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("APP") + .HasColumnType("text"); + + b.Property("DB") + .HasColumnType("text"); + + b.Property("DateCreated") + .HasColumnType("timestamp with time zone"); + + b.Property("DateModified") + .HasColumnType("timestamp with time zone"); + + b.Property("HostName") + .IsRequired() + .HasColumnType("text"); + + b.Property("IP") + .IsRequired() + .HasColumnType("text"); + + b.Property("LinkEK") + .HasColumnType("text"); + + b.Property("OS") + .HasColumnType("text"); + + b.Property("RegionalEK") + .HasColumnType("text"); + + b.Property("Responsible") + .HasColumnType("text"); + + b.Property("Status") + .HasColumnType("text"); + + b.Property("WorkGroup") + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Hosts"); + }); + + modelBuilder.Entity("PARR.DAL.Models.Job", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("DateCreated") + .HasColumnType("timestamp with time zone"); + + b.Property("DateModified") + .HasColumnType("timestamp with time zone"); + + b.Property("JobCategoryId") + .HasColumnType("uuid"); + + b.Property("JobModeId") + .HasColumnType("uuid"); + + b.Property("JobTypeId") + .HasColumnType("uuid"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.HasIndex("JobCategoryId"); + + b.HasIndex("JobModeId"); + + b.HasIndex("JobTypeId"); + + b.ToTable("Jobs"); + }); + + modelBuilder.Entity("PARR.DAL.Models.JobCategory", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("DateCreated") + .HasColumnType("timestamp with time zone"); + + b.Property("DateModified") + .HasColumnType("timestamp with time zone"); + + b.Property("Description") + .HasColumnType("text"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("JobCategories"); + }); + + modelBuilder.Entity("PARR.DAL.Models.JobJournal", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("DateCreated") + .HasColumnType("timestamp with time zone"); + + b.Property("DateModified") + .HasColumnType("timestamp with time zone"); + + b.Property("DateOper") + .HasColumnType("text"); + + b.Property("HostId") + .HasColumnType("uuid"); + + b.Property("Info") + .HasColumnType("text"); + + b.Property("JobId") + .HasColumnType("uuid"); + + b.Property("JobStatusId") + .HasColumnType("uuid"); + + b.Property("Other") + .HasColumnType("text"); + + b.Property("TimeOut") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("HostId"); + + b.HasIndex("JobId"); + + b.HasIndex("JobStatusId"); + + b.ToTable("JobJournals"); + }); + + modelBuilder.Entity("PARR.DAL.Models.JobMode", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("DateCreated") + .HasColumnType("timestamp with time zone"); + + b.Property("DateModified") + .HasColumnType("timestamp with time zone"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("JobModes"); + }); + + modelBuilder.Entity("PARR.DAL.Models.JobStatus", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("DateCreated") + .HasColumnType("timestamp with time zone"); + + b.Property("DateModified") + .HasColumnType("timestamp with time zone"); + + b.Property("Description") + .HasColumnType("text"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("JobStatuses"); + }); + + modelBuilder.Entity("PARR.DAL.Models.JobType", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("DateCreated") + .HasColumnType("timestamp with time zone"); + + b.Property("DateModified") + .HasColumnType("timestamp with time zone"); + + b.Property("Description") + .HasColumnType("text"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("JobTypes"); + }); + + modelBuilder.Entity("PARR.DAL.Models.Job", b => + { + b.HasOne("PARR.DAL.Models.JobCategory", "JobCategorye") + .WithMany("Jobs") + .HasForeignKey("JobCategoryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("PARR.DAL.Models.JobMode", "JobMode") + .WithMany("Jobs") + .HasForeignKey("JobModeId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("PARR.DAL.Models.JobType", "JobType") + .WithMany("Jobs") + .HasForeignKey("JobTypeId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("JobCategorye"); + + b.Navigation("JobMode"); + + b.Navigation("JobType"); + }); + + modelBuilder.Entity("PARR.DAL.Models.JobJournal", b => + { + b.HasOne("PARR.DAL.Models.Host", "Host") + .WithMany("JobJournals") + .HasForeignKey("HostId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("PARR.DAL.Models.Job", "Job") + .WithMany("Journals") + .HasForeignKey("JobId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("PARR.DAL.Models.JobStatus", "Status") + .WithMany("Journals") + .HasForeignKey("JobStatusId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Host"); + + b.Navigation("Job"); + + b.Navigation("Status"); + }); + + modelBuilder.Entity("PARR.DAL.Models.Host", b => + { + b.Navigation("JobJournals"); + }); + + modelBuilder.Entity("PARR.DAL.Models.Job", b => + { + b.Navigation("Journals"); + }); + + modelBuilder.Entity("PARR.DAL.Models.JobCategory", b => + { + b.Navigation("Jobs"); + }); + + modelBuilder.Entity("PARR.DAL.Models.JobMode", b => + { + b.Navigation("Jobs"); + }); + + modelBuilder.Entity("PARR.DAL.Models.JobStatus", b => + { + b.Navigation("Journals"); + }); + + modelBuilder.Entity("PARR.DAL.Models.JobType", b => + { + b.Navigation("Jobs"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/PARR.DAL/Migrations/20230601014714_UpdTblHosts.cs b/PARR.DAL/Migrations/20230601014714_UpdTblHosts.cs new file mode 100644 index 00000000..fd209adb --- /dev/null +++ b/PARR.DAL/Migrations/20230601014714_UpdTblHosts.cs @@ -0,0 +1,98 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace PARR.DAL.Migrations +{ + /// + public partial class UpdTblHosts : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.RenameColumn( + name: "EK", + table: "Hosts", + newName: "WorkGroup"); + + migrationBuilder.AddColumn( + name: "APP", + table: "Hosts", + type: "text", + nullable: true); + + migrationBuilder.AddColumn( + name: "DB", + table: "Hosts", + type: "text", + nullable: true); + + migrationBuilder.AddColumn( + name: "LinkEK", + table: "Hosts", + type: "text", + nullable: true); + + migrationBuilder.AddColumn( + name: "OS", + table: "Hosts", + type: "text", + nullable: true); + + migrationBuilder.AddColumn( + name: "RegionalEK", + table: "Hosts", + type: "text", + nullable: true); + + migrationBuilder.AddColumn( + name: "Responsible", + table: "Hosts", + type: "text", + nullable: true); + + migrationBuilder.AddColumn( + name: "Status", + table: "Hosts", + type: "text", + nullable: true); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "APP", + table: "Hosts"); + + migrationBuilder.DropColumn( + name: "DB", + table: "Hosts"); + + migrationBuilder.DropColumn( + name: "LinkEK", + table: "Hosts"); + + migrationBuilder.DropColumn( + name: "OS", + table: "Hosts"); + + migrationBuilder.DropColumn( + name: "RegionalEK", + table: "Hosts"); + + migrationBuilder.DropColumn( + name: "Responsible", + table: "Hosts"); + + migrationBuilder.DropColumn( + name: "Status", + table: "Hosts"); + + migrationBuilder.RenameColumn( + name: "WorkGroup", + table: "Hosts", + newName: "EK"); + } + } +} diff --git a/PARR.DAL/Migrations/DataContextModelSnapshot.cs b/PARR.DAL/Migrations/DataContextModelSnapshot.cs index 6924f21c..497c793b 100644 --- a/PARR.DAL/Migrations/DataContextModelSnapshot.cs +++ b/PARR.DAL/Migrations/DataContextModelSnapshot.cs @@ -28,15 +28,18 @@ namespace PARR.DAL.Migrations .ValueGeneratedOnAdd() .HasColumnType("uuid"); + b.Property("APP") + .HasColumnType("text"); + + b.Property("DB") + .HasColumnType("text"); + b.Property("DateCreated") .HasColumnType("timestamp with time zone"); b.Property("DateModified") .HasColumnType("timestamp with time zone"); - b.Property("EK") - .HasColumnType("text"); - b.Property("HostName") .IsRequired() .HasColumnType("text"); @@ -45,6 +48,24 @@ namespace PARR.DAL.Migrations .IsRequired() .HasColumnType("text"); + b.Property("LinkEK") + .HasColumnType("text"); + + b.Property("OS") + .HasColumnType("text"); + + b.Property("RegionalEK") + .HasColumnType("text"); + + b.Property("Responsible") + .HasColumnType("text"); + + b.Property("Status") + .HasColumnType("text"); + + b.Property("WorkGroup") + .HasColumnType("text"); + b.HasKey("Id"); b.ToTable("Hosts"); diff --git a/PARR.DAL/Models/Host.cs b/PARR.DAL/Models/Host.cs index 0db673eb..6431f52a 100644 --- a/PARR.DAL/Models/Host.cs +++ b/PARR.DAL/Models/Host.cs @@ -13,7 +13,14 @@ namespace PARR.DAL.Models public DateTimeOffset? DateModified { get; set; } public required string HostName { get; set; } public required string IP { get; set; } - public string? EK { get; set; } + public string? RegionalEK { get; set; } + public string? LinkEK { get; set; } + public string? Status { get; set; } + public string? WorkGroup { get; set; } + public string? Responsible { get; set; } + public string? OS { get; set; } + public string? DB { get; set; } + public string? APP { get; set; } diff --git a/PARR.DAL/PARR.DAL.csproj b/PARR.DAL/PARR.DAL.csproj index 74a3dffd..641b0411 100644 --- a/PARR.DAL/PARR.DAL.csproj +++ b/PARR.DAL/PARR.DAL.csproj @@ -18,7 +18,6 @@ - diff --git a/PARR.DAL/ParrDalInstaller.cs b/PARR.DAL/ParrDalInstaller.cs index 0d85ae01..b8e706fc 100644 --- a/PARR.DAL/ParrDalInstaller.cs +++ b/PARR.DAL/ParrDalInstaller.cs @@ -2,6 +2,8 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using PARR.DAL.Context; +using PARR.DAL.Services.Implementations; +using PARR.DAL.Services.Interfaces; namespace PARR.DAL { @@ -13,7 +15,7 @@ namespace PARR.DAL opt.UseNpgsql(configuration.GetConnectionString("DefaultConnection")) ); - //services.AddTransient(); + services.AddTransient(); //services.AddTransient(); //services.AddTransient(); //services.AddTransient(); diff --git a/PARR.DAL/Services/Implementations/HostService.cs b/PARR.DAL/Services/Implementations/HostService.cs new file mode 100644 index 00000000..606e6015 --- /dev/null +++ b/PARR.DAL/Services/Implementations/HostService.cs @@ -0,0 +1,24 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; +using PARR.DAL.Context; +using PARR.DAL.Models; +using PARR.DAL.Services.Abstracts; +using PARR.DAL.Services.Interfaces; + +namespace PARR.DAL.Services.Implementations +{ + internal class HostService : BaseService, IHostService + { + private readonly DataContext dataContext; + private readonly ILogger logger; + + protected override DbSet EntitySet => dataContext.Hosts; + + protected override DataContext EntitiContext => dataContext; + public HostService(DataContext dataContext, ILogger logger) : base(logger) + { + this.dataContext = dataContext; + this.logger = logger; + } + } +} diff --git a/PARR.DAL/Services/Interfaces/IHostService.cs b/PARR.DAL/Services/Interfaces/IHostService.cs new file mode 100644 index 00000000..84013a81 --- /dev/null +++ b/PARR.DAL/Services/Interfaces/IHostService.cs @@ -0,0 +1,9 @@ +using PARR.DAL.Models; +using PARR.DAL.Services.Interfaces.Base; + +namespace PARR.DAL.Services.Interfaces +{ + public interface IHostService : IBaseService + { + } +} diff --git a/PARR.Mail/PARR.Mail/MailInstaller.cs b/PARR.Mail/PARR.Mail/MailInstaller.cs new file mode 100644 index 00000000..c0fffdc0 --- /dev/null +++ b/PARR.Mail/PARR.Mail/MailInstaller.cs @@ -0,0 +1,19 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using PARR.Mail.Services; +using PARR.Mail.Settings; + +namespace PARR.Mail +{ + public static class MailInstaller + { + public static void InstallMailServices(this IServiceCollection services, IConfiguration configuration) + { + var mailSettings = new MailSettings(); + configuration.GetSection(nameof(MailSettings)).Bind(mailSettings); + services.AddSingleton(mailSettings); + + services.AddTransient(); + } + } +} diff --git a/PARR.Mail/PARR.Mail/PARR.Mail.csproj b/PARR.Mail/PARR.Mail/PARR.Mail.csproj new file mode 100644 index 00000000..65e69bd1 --- /dev/null +++ b/PARR.Mail/PARR.Mail/PARR.Mail.csproj @@ -0,0 +1,17 @@ + + + + net7.0 + enable + enable + + + + + + + + + + + diff --git a/PARR.Mail/PARR.Mail/Services/IMailService.cs b/PARR.Mail/PARR.Mail/Services/IMailService.cs new file mode 100644 index 00000000..1f06ea07 --- /dev/null +++ b/PARR.Mail/PARR.Mail/Services/IMailService.cs @@ -0,0 +1,15 @@ +using MailKit; +using MailKit.Search; +using MimeKit; + +namespace PARR.Mail.Services +{ + public interface IMailService + { + Task> CheckMailAsync(SearchQuery query); + Task GetBodyPartAsync(IMessageSummary messageSummary); + Task> GetAttachmentsAsync(IMessageSummary messageSummary); + Task GetBodyTextAsync(IMessageSummary messageSummary); + Task SetAsSeenAsync(IMessageSummary messageSummary); + } +} diff --git a/PARR.Mail/PARR.Mail/Services/MailService.cs b/PARR.Mail/PARR.Mail/Services/MailService.cs new file mode 100644 index 00000000..15e52961 --- /dev/null +++ b/PARR.Mail/PARR.Mail/Services/MailService.cs @@ -0,0 +1,85 @@ +using MailKit; +using MailKit.Net.Imap; +using MailKit.Search; +using MailKit.Security; +using Microsoft.Extensions.Logging; +using MimeKit; +using PARR.Mail.Settings; + +namespace PARR.Mail.Services +{ + internal class MailService : IMailService + { + private readonly ILogger logger; + private readonly MailSettings mailSettings; + + public MailService(ILogger logger, MailSettings mailSettings) + { + this.logger = logger; + this.mailSettings = mailSettings; + } + public async Task> CheckMailAsync(SearchQuery query) + { + using var client = await GetClient(); + + IList items = new List(); + try + { + var uids = await client.Inbox.SearchAsync(query); + items = await client.Inbox.FetchAsync(uids, MessageSummaryItems.UniqueId | MessageSummaryItems.BodyStructure | MessageSummaryItems.Envelope); + } + catch (Exception e) + { + logger.LogError(e, "Ошибка получения списка писем"); + } + return items; + } + + public async Task GetBodyPartAsync(IMessageSummary messageSummary) + { + using var client = await GetClient(); + + return await client.Inbox.GetBodyPartAsync(messageSummary.UniqueId, messageSummary.TextBody); + } + + public async Task> GetAttachmentsAsync(IMessageSummary messageSummary) + { + using var client = await GetClient(); + + List result = new List(); + foreach (var attachment in messageSummary.Attachments) + { + result.Add(await client.Inbox.GetBodyPartAsync(messageSummary.UniqueId, attachment)); + } + return result; + } + + public async Task GetBodyTextAsync(IMessageSummary messageSummary) + { + using var client = await GetClient(); + + return ((TextPart)await client.Inbox.GetBodyPartAsync(messageSummary.UniqueId, messageSummary.TextBody)).Text; + } + + public async Task SetAsSeenAsync(IMessageSummary messageSummary) + { + using var client = await GetClient(); + + await client.Inbox.AddFlagsAsync(messageSummary.UniqueId, MessageFlags.Seen, false); + } + + private async Task GetClient() + { + var client = new ImapClient(); + await client.ConnectAsync(mailSettings.Host, mailSettings.Port, SecureSocketOptions.None); + await client.AuthenticateAsync(mailSettings.UserName, mailSettings.Password); + await client.Inbox.OpenAsync(FolderAccess.ReadWrite); + + // игнорить сертификат + client.CheckCertificateRevocation = false; + client.ServerCertificateValidationCallback = (s, c, h, e) => true; + + return client; + } + } +} diff --git a/PARR.Mail/PARR.Mail/Settings/MailSettings.cs b/PARR.Mail/PARR.Mail/Settings/MailSettings.cs new file mode 100644 index 00000000..c7c6a35e --- /dev/null +++ b/PARR.Mail/PARR.Mail/Settings/MailSettings.cs @@ -0,0 +1,11 @@ +namespace PARR.Mail.Settings +{ + internal class MailSettings + { + public string Host { get; set; } + public int Port { get; set; } + public string UserName { get; set; } + public string Password { get; set; } + public bool SSL { get; set; } + } +} diff --git a/PARR.Worker/PARR.Worker/PARR.Worker.csproj b/PARR.Worker/PARR.Worker/PARR.Worker.csproj new file mode 100644 index 00000000..353e86c9 --- /dev/null +++ b/PARR.Worker/PARR.Worker/PARR.Worker.csproj @@ -0,0 +1,23 @@ + + + + net7.0 + enable + enable + dotnet-PARR.Worker-87bfa085-3c62-4b97-b25f-bdece8fc03df + + + + + + + + + + + + + + + + diff --git a/PARR.Worker/PARR.Worker/Program.cs b/PARR.Worker/PARR.Worker/Program.cs new file mode 100644 index 00000000..7cb64e85 --- /dev/null +++ b/PARR.Worker/PARR.Worker/Program.cs @@ -0,0 +1,22 @@ +using PARR.DAL; +using PARR.Mail; +using PARR.Worker; +using Serilog; + +IHost host = Host.CreateDefaultBuilder(args) + .ConfigureServices((hostContext, services) => + { + services.InstallMailServices(hostContext.Configuration); + services.InstallDalServices(hostContext.Configuration); + + services.AddHostedService(); + }) + .UseSerilog((hostContext, services, config) => + { + config + .WriteTo.Console() + .ReadFrom.Configuration(hostContext.Configuration); + }) + .Build(); + +host.Run(); diff --git a/PARR.Worker/PARR.Worker/Properties/launchSettings.json b/PARR.Worker/PARR.Worker/Properties/launchSettings.json new file mode 100644 index 00000000..a4856b40 --- /dev/null +++ b/PARR.Worker/PARR.Worker/Properties/launchSettings.json @@ -0,0 +1,11 @@ +{ + "profiles": { + "PARR.Worker": { + "commandName": "Project", + "dotnetRunMessages": true, + "environmentVariables": { + "DOTNET_ENVIRONMENT": "Development" + } + } + } +} diff --git a/PARR.Worker/PARR.Worker/Settings/SyncherSettings.cs b/PARR.Worker/PARR.Worker/Settings/SyncherSettings.cs new file mode 100644 index 00000000..12f29751 --- /dev/null +++ b/PARR.Worker/PARR.Worker/Settings/SyncherSettings.cs @@ -0,0 +1,10 @@ +namespace PARR.Worker.Settings +{ + internal class SyncherSettings + { + public int OccursEvery { get; set; } + public string? IncludeAddresses { get; set; } + + public List IncludeAddressesArray => IncludeAddresses == null ? new List() : IncludeAddresses.Split(",").ToList(); + } +} diff --git a/PARR.Worker/PARR.Worker/Syncher.cs b/PARR.Worker/PARR.Worker/Syncher.cs new file mode 100644 index 00000000..da7e364f --- /dev/null +++ b/PARR.Worker/PARR.Worker/Syncher.cs @@ -0,0 +1,38 @@ +using MailKit.Search; +using PARR.Mail.Services; +using PARR.Worker.Settings; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PARR.Worker +{ + internal class Syncher + { + private readonly ILogger logger; + private readonly IMailService mailService; + private readonly SyncherSettings aIHITSyncherSettings; + + public Syncher(ILogger logger, IMailService mailService, SyncherSettings aIHITSyncherSettings) + { + this.logger = logger; + this.mailService = mailService; + this.aIHITSyncherSettings = aIHITSyncherSettings; + } + + public async Task InvokeAsync() + { + var messages = await mailService.CheckMailAsync(SearchQuery.NotSeen); + if (!messages.Any()) + { + return; + } + //var includeAddresses = aIHITSyncherSettings.IncludeAddresses.Any() ? aIHITSyncherSettings.IncludeAddresses.Split(",") : null; + // var filtered = messages.Where(m => includeAddresses.Any(i => m.Envelope.From.Mailboxes.First().Address == i)).ToList(); + + var filtered = messages.Where(m => aIHITSyncherSettings.IncludeAddressesArray.Any(i => m.Envelope.From.Mailboxes.First().Address == i)).ToList(); + } + } +} diff --git a/PARR.Worker/PARR.Worker/Worker.cs b/PARR.Worker/PARR.Worker/Worker.cs new file mode 100644 index 00000000..358822ee --- /dev/null +++ b/PARR.Worker/PARR.Worker/Worker.cs @@ -0,0 +1,31 @@ +using Microsoft.EntityFrameworkCore; +using PARR.DAL.Services.Interfaces; + +namespace PARR.Worker +{ + public class Worker : BackgroundService + { + private readonly ILogger logger; + private readonly IHostService hostService; + + public Worker(ILogger logger, IHostService hostService) + { + this.logger = logger; + this.hostService = hostService; + } + + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + logger.LogInformation("--- Сервис запущен! ---"); + while (!stoppingToken.IsCancellationRequested) + { + var hosts = await hostService.Get().ToListAsync(); + foreach ( var host in hosts ) { logger.LogInformation($"{host.HostName}"); } + // syncher.InvokeAsync(); + //await attachmentUploader.UploadAsync(); + //logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); + await Task.Delay(60000, stoppingToken); + } + } + } +} \ No newline at end of file diff --git a/PARR.Worker/PARR.Worker/appsettings.Development.json b/PARR.Worker/PARR.Worker/appsettings.Development.json new file mode 100644 index 00000000..b2dcdb67 --- /dev/null +++ b/PARR.Worker/PARR.Worker/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/PARR.Worker/PARR.Worker/appsettings.json b/PARR.Worker/PARR.Worker/appsettings.json new file mode 100644 index 00000000..a2b4945d --- /dev/null +++ b/PARR.Worker/PARR.Worker/appsettings.json @@ -0,0 +1,38 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "Serilog": { + "MinimumLevel": { + "Default": "Debug", + "Override": { + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Debug" + } + }, + "WriteTo": [ + { + "Name": "File", + "Args": { + "path": "log/log-.txt", + "rollingInterval": "Day" + } + } + ] + }, + "MailSettings": { + "Host": "uc.dvgd.rzd", + "Password": "Robin123", + "Port": 143, + "SSL": false, + "UserName": "IVC_Robot" + }, + "AIHITSyncherSettings": { + "OccursEvery": 60000, + "IncludeAddresses": "aihit@gvc.rzd" + } + +} diff --git a/PARR_API.sln b/PARR_API.sln index cfde93c2..b594be0d 100644 --- a/PARR_API.sln +++ b/PARR_API.sln @@ -9,6 +9,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PARR.DAL", "PARR.DAL\PARR.D EndProject Project("{E53339B2-1760-4266-BCC7-CA923CBCF16C}") = "docker-compose", "docker-compose.dcproj", "{AF6BD539-34B2-4D57-AEB3-F6FDE26B98ED}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PARR.Mail", "PARR.Mail\PARR.Mail\PARR.Mail.csproj", "{228C8124-69E4-4AE0-8C33-0438E54FF1BA}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PARR.Worker", "PARR.Worker\PARR.Worker\PARR.Worker.csproj", "{D15F308C-9F63-4D69-B06E-DDA26FEE9BAB}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PARR.AIHIT", "PARR.AIHIT\AIHIT\PARR.AIHIT.csproj", "{3ED4E1CE-2AF5-4EB3-B7DF-B77CCC242B9E}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -26,6 +32,18 @@ Global {AF6BD539-34B2-4D57-AEB3-F6FDE26B98ED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {AF6BD539-34B2-4D57-AEB3-F6FDE26B98ED}.Release|Any CPU.ActiveCfg = Release|Any CPU {AF6BD539-34B2-4D57-AEB3-F6FDE26B98ED}.Release|Any CPU.Build.0 = Release|Any CPU + {228C8124-69E4-4AE0-8C33-0438E54FF1BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {228C8124-69E4-4AE0-8C33-0438E54FF1BA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {228C8124-69E4-4AE0-8C33-0438E54FF1BA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {228C8124-69E4-4AE0-8C33-0438E54FF1BA}.Release|Any CPU.Build.0 = Release|Any CPU + {D15F308C-9F63-4D69-B06E-DDA26FEE9BAB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D15F308C-9F63-4D69-B06E-DDA26FEE9BAB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D15F308C-9F63-4D69-B06E-DDA26FEE9BAB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D15F308C-9F63-4D69-B06E-DDA26FEE9BAB}.Release|Any CPU.Build.0 = Release|Any CPU + {3ED4E1CE-2AF5-4EB3-B7DF-B77CCC242B9E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3ED4E1CE-2AF5-4EB3-B7DF-B77CCC242B9E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3ED4E1CE-2AF5-4EB3-B7DF-B77CCC242B9E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3ED4E1CE-2AF5-4EB3-B7DF-B77CCC242B9E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/PARR_API/Controllers/V1/HomeController.cs b/PARR_API/Controllers/V1/HomeController.cs index 7308ff88..663235dc 100644 --- a/PARR_API/Controllers/V1/HomeController.cs +++ b/PARR_API/Controllers/V1/HomeController.cs @@ -1,12 +1,26 @@ using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using PARR.DAL.Services.Interfaces; namespace PARR.API.Controllers.V1 { public class HomeController : Controller { - public IActionResult Index() + private readonly IHostService hostService; + + public HomeController(IHostService hostService) { - return View(); + this.hostService = hostService; + } + + [HttpGet("test")] + public async Task Index() + { + var hosts = await hostService.Get().ToListAsync(); + //foreach (var host in hosts) { logger.LogInformation($"{host.HostName}"); } + //return View(); + + return Ok(hosts.Select(t => new { t.HostName, t.IP, t.Id })); } } }