From 24ef29449e13fe60b88085838e6ff0e79759a88d Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Thu, 13 Jan 2022 12:05:34 +0200 Subject: [PATCH] wake machine from API by machineId --- notes/nugets.txt | 2 +- .../CommandHandlers/WakeMachineHandler.cs | 35 +++++++++++++++++++ .../NetworkResurrector.Api.Application.csproj | 2 +- .../Repositories/NetworkRepository.cs | 10 ++++++ .../Repositories/INetworkRepository.cs | 1 + .../Commands/PingMachine.cs | 1 + .../Commands/ShutdownMachine.cs | 1 + .../Commands/WakeMachine.cs | 1 + .../Events/MachineWaked.cs | 6 +--- src/api/NetworkResurrector.Api/Startup.cs | 7 ++-- .../NetworkResurrector.Api/appsettings.json | 3 ++ .../NetworkResurrector.Server.Wrapper.csproj | 1 + .../Services/IResurrectorService.cs | 8 +++-- 13 files changed, 67 insertions(+), 11 deletions(-) create mode 100644 src/api/NetworkResurrector.Api.Application/CommandHandlers/WakeMachineHandler.cs diff --git a/notes/nugets.txt b/notes/nugets.txt index 1062055..c1b96e4 100644 --- a/notes/nugets.txt +++ b/notes/nugets.txt @@ -5,5 +5,5 @@ dotnet pack /*--include-symbols*/ # Server: dotnet nuget push NetworkResurrector.Server.PublishedLanguage.1.0.3.nupkg -k ***REMOVED*** -s https://toodle.ddns.net/public-nuget-server/nuget -dotnet nuget push NetworkResurrector.Server.Wrapper.1.0.3.nupkg -k ***REMOVED*** -s https://toodle.ddns.net/public-nuget-server/nuget +dotnet nuget push NetworkResurrector.Server.Wrapper.1.0.3.1.nupkg -k ***REMOVED*** -s https://toodle.ddns.net/public-nuget-server/nuget ####################################################################################################################################################### \ No newline at end of file diff --git a/src/api/NetworkResurrector.Api.Application/CommandHandlers/WakeMachineHandler.cs b/src/api/NetworkResurrector.Api.Application/CommandHandlers/WakeMachineHandler.cs new file mode 100644 index 0000000..f621fb3 --- /dev/null +++ b/src/api/NetworkResurrector.Api.Application/CommandHandlers/WakeMachineHandler.cs @@ -0,0 +1,35 @@ +using MediatR; +using Microsoft.Extensions.Logging; +using NetworkResurrector.Api.Domain.Repositories; +using NetworkResurrector.Api.PublishedLanguage.Commands; +using NetworkResurrector.Api.PublishedLanguage.Events; +using NetworkResurrector.Server.Wrapper.Services; +using System.Threading; +using System.Threading.Tasks; + +namespace NetworkResurrector.Api.Application.CommandHandlers +{ + public class WakeMachineHandler : IRequestHandler + { + private readonly ILogger _logger; + private readonly IResurrectorService _resurrectorService; + private readonly INetworkRepository _repository; + + public WakeMachineHandler(ILogger logger, IResurrectorService resurrectorService, INetworkRepository repository) + { + _logger=logger; + _resurrectorService=resurrectorService; + _repository=repository; + } + + public async Task Handle(WakeMachine command, CancellationToken cancellationToken) + { + var machine = await _repository.GetMachine(command.MachineId); + + //log activity + var wakeResult = await _resurrectorService.Wake(machine.MACAddress); + var result = new MachineWaked(wakeResult.Success, wakeResult.Status); + return result; + } + } +} diff --git a/src/api/NetworkResurrector.Api.Application/NetworkResurrector.Api.Application.csproj b/src/api/NetworkResurrector.Api.Application/NetworkResurrector.Api.Application.csproj index 9530e74..c902918 100644 --- a/src/api/NetworkResurrector.Api.Application/NetworkResurrector.Api.Application.csproj +++ b/src/api/NetworkResurrector.Api.Application/NetworkResurrector.Api.Application.csproj @@ -11,7 +11,7 @@ - + diff --git a/src/api/NetworkResurrector.Api.Domain.Data/Repositories/NetworkRepository.cs b/src/api/NetworkResurrector.Api.Domain.Data/Repositories/NetworkRepository.cs index c3949c9..1280b9a 100644 --- a/src/api/NetworkResurrector.Api.Domain.Data/Repositories/NetworkRepository.cs +++ b/src/api/NetworkResurrector.Api.Domain.Data/Repositories/NetworkRepository.cs @@ -2,6 +2,7 @@ using NetworkResurrector.Api.Domain.Data.DbContexts; using NetworkResurrector.Api.Domain.Entities; using NetworkResurrector.Api.Domain.Repositories; +using System; using System.Threading.Tasks; namespace NetworkResurrector.Api.Domain.Data.Repositories @@ -19,5 +20,14 @@ namespace NetworkResurrector.Api.Domain.Data.Repositories { return _dbContext.Machines.ToArrayAsync(); } + + public async Task GetMachine(int machineId) + { + var machine = await _dbContext.Machines.FirstOrDefaultAsync(z => z.MachineId == machineId); + if (machine == null) + throw new Exception($"Machine with id {machineId} not found"); + + return machine; + } } } diff --git a/src/api/NetworkResurrector.Api.Domain/Repositories/INetworkRepository.cs b/src/api/NetworkResurrector.Api.Domain/Repositories/INetworkRepository.cs index ba99ded..29ea5db 100644 --- a/src/api/NetworkResurrector.Api.Domain/Repositories/INetworkRepository.cs +++ b/src/api/NetworkResurrector.Api.Domain/Repositories/INetworkRepository.cs @@ -6,5 +6,6 @@ namespace NetworkResurrector.Api.Domain.Repositories public interface INetworkRepository { Task GetMachines(); + Task GetMachine(int machineId); } } diff --git a/src/api/NetworkResurrector.Api.PublishedLanguage/Commands/PingMachine.cs b/src/api/NetworkResurrector.Api.PublishedLanguage/Commands/PingMachine.cs index d66d22d..3860ba4 100644 --- a/src/api/NetworkResurrector.Api.PublishedLanguage/Commands/PingMachine.cs +++ b/src/api/NetworkResurrector.Api.PublishedLanguage/Commands/PingMachine.cs @@ -5,5 +5,6 @@ namespace NetworkResurrector.Api.PublishedLanguage.Commands { public class PingMachine : Command { + public int MachineId { get; set; } } } diff --git a/src/api/NetworkResurrector.Api.PublishedLanguage/Commands/ShutdownMachine.cs b/src/api/NetworkResurrector.Api.PublishedLanguage/Commands/ShutdownMachine.cs index c5cda32..dede1fd 100644 --- a/src/api/NetworkResurrector.Api.PublishedLanguage/Commands/ShutdownMachine.cs +++ b/src/api/NetworkResurrector.Api.PublishedLanguage/Commands/ShutdownMachine.cs @@ -5,5 +5,6 @@ namespace NetworkResurrector.Api.PublishedLanguage.Commands { public class ShutdownMachine : Command { + public int MachineId { get; set; } } } diff --git a/src/api/NetworkResurrector.Api.PublishedLanguage/Commands/WakeMachine.cs b/src/api/NetworkResurrector.Api.PublishedLanguage/Commands/WakeMachine.cs index 8c05b54..20bc3a6 100644 --- a/src/api/NetworkResurrector.Api.PublishedLanguage/Commands/WakeMachine.cs +++ b/src/api/NetworkResurrector.Api.PublishedLanguage/Commands/WakeMachine.cs @@ -5,5 +5,6 @@ namespace NetworkResurrector.Api.PublishedLanguage.Commands { public class WakeMachine : Command { + public int MachineId { get; set; } } } diff --git a/src/api/NetworkResurrector.Api.PublishedLanguage/Events/MachineWaked.cs b/src/api/NetworkResurrector.Api.PublishedLanguage/Events/MachineWaked.cs index bbc8f95..e85346b 100644 --- a/src/api/NetworkResurrector.Api.PublishedLanguage/Events/MachineWaked.cs +++ b/src/api/NetworkResurrector.Api.PublishedLanguage/Events/MachineWaked.cs @@ -1,8 +1,4 @@ namespace NetworkResurrector.Api.PublishedLanguage.Events { - public class MachineWaked - { - public bool Success { get; set; } - public string Status { get; set; } - } + public record MachineWaked(bool Success, string Status); } diff --git a/src/api/NetworkResurrector.Api/Startup.cs b/src/api/NetworkResurrector.Api/Startup.cs index 198419d..b6e062b 100644 --- a/src/api/NetworkResurrector.Api/Startup.cs +++ b/src/api/NetworkResurrector.Api/Startup.cs @@ -11,6 +11,7 @@ using NDB.Extensions.Swagger.Constants; using NDB.Security.Authentication.Identity; using NetworkResurrector.Api.Application; using NetworkResurrector.Api.Domain.Data; +using NetworkResurrector.Server.Wrapper; using Newtonsoft.Json; using System.Reflection; @@ -40,12 +41,14 @@ namespace NetworkResurrector.Api services.AddScoped(typeof(IPipelineBehavior<,>), typeof(RequestPostProcessorBehavior<,>)); // AutoMapper - services.AddAutoMapper( - typeof(Application.Mappings.MappingProfile).Assembly); + services.AddAutoMapper(typeof(Application.Mappings.MappingProfile).Assembly); // Swagger services.AddSwagger("NetworkResurrector API", AuthorizationType.InhouseIdentity); + // Add network resurrector server services + services.UseResurrectorServices(_configuration.GetSection("NetworkResurrectorServer")["BaseAddress"]); + // Data access services.AddDataAccess(); diff --git a/src/api/NetworkResurrector.Api/appsettings.json b/src/api/NetworkResurrector.Api/appsettings.json index ee90213..b600f54 100644 --- a/src/api/NetworkResurrector.Api/appsettings.json +++ b/src/api/NetworkResurrector.Api/appsettings.json @@ -15,5 +15,8 @@ "IdentityServer": { //"BaseAddress": "http://localhost:5063/" "BaseAddress": "https://toodle.ddns.net/identity-server-api/" + }, + "NetworkResurrectorServer": { + "BaseAddress": "http://localhost:5062" } } diff --git a/src/server/NetworkResurrector.Server.Wrapper/NetworkResurrector.Server.Wrapper.csproj b/src/server/NetworkResurrector.Server.Wrapper/NetworkResurrector.Server.Wrapper.csproj index 399fc26..9fcfdc5 100644 --- a/src/server/NetworkResurrector.Server.Wrapper/NetworkResurrector.Server.Wrapper.csproj +++ b/src/server/NetworkResurrector.Server.Wrapper/NetworkResurrector.Server.Wrapper.csproj @@ -6,6 +6,7 @@ https://dev.azure.com/tstanciu94/NetworkResurrector https://dev.azure.com/tstanciu94/NetworkResurrector Git + 1.0.3.1 diff --git a/src/server/NetworkResurrector.Server.Wrapper/Services/IResurrectorService.cs b/src/server/NetworkResurrector.Server.Wrapper/Services/IResurrectorService.cs index 6e2ef2a..920667f 100644 --- a/src/server/NetworkResurrector.Server.Wrapper/Services/IResurrectorService.cs +++ b/src/server/NetworkResurrector.Server.Wrapper/Services/IResurrectorService.cs @@ -1,7 +1,11 @@ -namespace NetworkResurrector.Server.Wrapper.Services +using NetworkResurrector.Server.PublishedLanguage.Events; +using System.Threading.Tasks; + +namespace NetworkResurrector.Server.Wrapper.Services { public interface IResurrectorService { - + Task Wake(string macAddress); + Task Ping(string ipAddressOrMachineName); } } \ No newline at end of file