using System; using System.Linq; using System.Net.Http; using System.Threading.Tasks; using Application.Domain.Entities; using Application.Models; using Infrastructure.Data; using Infrastructure.Extensions; using Microsoft.AspNetCore.Mvc; using Swashbuckle.AspNetCore.Annotations; namespace ONVIFService.Controllers { public class HomeController : Controller { private readonly IRepository _cameraRepo; private readonly IHttpClientFactory _httpClientFactory; private readonly OnvifService _onvifService; public HomeController(IHttpClientFactory httpClientFactory, OnvifService onvifService, IRepository cameraRepo) { this._cameraRepo = cameraRepo; this._httpClientFactory = httpClientFactory; this._onvifService = onvifService; } public IActionResult Index() { Task.Run(() => { try { this._onvifService.Search(); this._onvifService.Notify(); } catch (Exception ex) { ex.PrintStack(); } }); var model = this._cameraRepo.ReadOnlyTable().ToList(); return View(model); } public IActionResult Test() { var model = this._cameraRepo.ReadOnlyTable().ToList(); return View(model); } [HttpGet] [ApiExplorerSettings(IgnoreApi = true)] public FileResult Image(string id) { return File(this._onvifService.ScreenShot(id), "image/jpeg"); } [HttpGet] [Route("/camera/api")] [SwaggerOperation("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("/camera/20shot")] [SwaggerOperation("截屏")] public ApiResponse ScreenShot([SwaggerParameter("设备Id")]string id) { return ApiResponse.Success($"data:image/jpeg;base64,{Convert.ToBase64String(this._onvifService.ScreenShot(id))}", 1); } [SwaggerOperation("放大")] [HttpGet] [Route("/camera/21zoomin")] public ApiResponse ZoomIn([SwaggerParameter("设备Id")]string id) { try { this._onvifService.ZoomIn(id, 0.5f); } catch (Exception ex) { ex.PrintStack(); return ApiResponse.Error(ex.Message); } return ApiResponse.AsyncSuccess(); } [SwaggerOperation("缩小")] [HttpGet] [Route("/camera/22zoomout")] public ApiResponse ZoomOut([SwaggerParameter("设备Id")]string id) { try { this._onvifService.ZoomIn(id, -0.5f); } catch (Exception ex) { ex.PrintStack(); return ApiResponse.Error(ex.Message); } return ApiResponse.AsyncSuccess(); } [SwaggerOperation("停止缩放")] [HttpGet] [Route("/camera/23stopzoom")] public ApiResponse StopZoom([SwaggerParameter("设备Id")]string id) { try { this._onvifService.StopZoom(id); } catch (Exception ex) { ex.PrintStack(); return ApiResponse.Error(ex.Message); } return ApiResponse.AsyncSuccess(); } [SwaggerOperation("向左旋转")] [HttpGet] [Route("/camera/24left")] public ApiResponse Left([SwaggerParameter("设备Id")]string id) { try { this._onvifService.Left(id, 0.5f); } catch (Exception ex) { ex.PrintStack(); return ApiResponse.Error(ex.Message); } return ApiResponse.AsyncSuccess(); } [SwaggerOperation("向右旋转")] [HttpGet] [Route("/camera/25right")] public ApiResponse Right([SwaggerParameter("设备Id")]string id) { try { this._onvifService.Right(id, 0.5f); } catch (Exception ex) { ex.PrintStack(); return ApiResponse.Error(ex.Message); } return ApiResponse.AsyncSuccess(); } [SwaggerOperation("向上旋转")] [HttpGet] [Route("/camera/26up")] public ApiResponse Up([SwaggerParameter("设备Id")]string id) { try { this._onvifService.Up(id, 0.5f); } catch (Exception ex) { ex.PrintStack(); return ApiResponse.Error(ex.Message); } return ApiResponse.AsyncSuccess(); } [SwaggerOperation("向下旋转")] [HttpGet] [Route("/camera/27down")] public ApiResponse Down([SwaggerParameter("设备Id")]string id) { try { this._onvifService.Down(id, 0.5f); } catch (Exception ex) { ex.PrintStack(); return ApiResponse.Error(ex.Message); } return ApiResponse.AsyncSuccess(); } [SwaggerOperation("停止转动")] [HttpGet] [Route("/camera/28stopturn")] public ApiResponse StopTurn([SwaggerParameter("设备Id")]string id) { try { this._onvifService.StopTurn(id); } catch (Exception ex) { ex.PrintStack(); return ApiResponse.Error(ex.Message); } return ApiResponse.AsyncSuccess(); } [ApiExplorerSettings(IgnoreApi = true)] public IActionResult Restart(string id) { try { this._onvifService.Remove(id); this._onvifService.Add(id); return Json("success"); } catch (Exception ex) { return Json(ex.Message); } } } }