using System; using System.Drawing; using System.Net.Http; using System.Text.RegularExpressions; using Application.Models; using Infrastructure.Extensions; using Microsoft.AspNetCore.Mvc; using Swashbuckle.AspNetCore.Annotations; namespace WinService.Controllers { public class HomeController : Controller { private readonly WindowsService _ws; private readonly IHttpClientFactory _httpClientFactory; public HomeController(WindowsService ws, IHttpClientFactory httpClientFactory) { this._ws = ws; this._httpClientFactory = httpClientFactory; } public IActionResult Index(string id, string hw) { ViewBag.Title = "PC监控服务"; var model = this._ws.GetModel(); this._ws.Update(model); return View(model); } [HttpGet] [Route("/pc/api")] [ApiExplorerSettings(IgnoreApi = true)] public string Api() { var url = new UriBuilder { Host = Request.Host.Host, Port = Request.Host.Port ?? 80, Path = "/swagger/v1/swagger.json" }.ToString(); var hc = this._httpClientFactory.CreateClient(); var result = hc.GetStringAsync(url).Result; return result; } [HttpGet] [Route("/pc/22shutdown")] [SwaggerOperation("关机")] public ApiResponse Shutdown() { var model = this._ws.GetModel(); model.IsOnline = false; this._ws.Update(model); this._ws.Shutdown(); return ApiResponse.AsyncSuccess(); } [HttpGet] [Route("/pc/21boot")] [SwaggerOperation("开机")] public ApiResponse Boot() { return ApiResponse.AsyncSuccess(); } [HttpGet] [Route("/pc/23shot")] [SwaggerOperation("截屏")] public ApiResponse ScreenShot() { try { var videoModeDescription = WindowsService.Search("select VideoModeDescription from Win32_VideoController ")["VideoModeDescription"].ToString(); var width = int.Parse(Regex.Match(videoModeDescription, @"^\d+(?=\s)").Value); var height = int.Parse(Regex.Match(videoModeDescription, @"(?<=\sx\s)\d+(?=\sx)").Value); using (var bitmap = new Bitmap(width, height)) { var gc = Graphics.FromImage(bitmap); gc.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(width, height)); byte[] byteArray = bitmap.ToJpeg(); return ApiResponse.Success($"data:image/jpeg;base64,{Convert.ToBase64String(byteArray)}", 1); } } catch (Exception ex) { return ApiResponse.Error(ex.Message); } } } }