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 Newtonsoft.Json; using Newtonsoft.Json.Linq; using Polly; using SPService.Applicaiton.Models; using Swashbuckle.AspNetCore.Annotations; namespace FBeeService.Controllers { public class HomeController : Controller { private readonly IRepository _repo; private readonly IHttpClientFactory _httpClientFactory; private readonly DeviceService _deviceService; public HomeController(IHttpClientFactory httpClientFactory, DeviceService deviceService, IRepository repo) { this._repo = repo; this._httpClientFactory = httpClientFactory; this._deviceService = deviceService; } public IActionResult Index() { Task.Run(() => { this._deviceService.Notify(); }); return View(this._repo.ReadOnlyTable().ToList()); } [HttpGet] [Route("/smartplug/api")] [SwaggerOperation("API")] [ApiExplorerSettings(IgnoreApi = true)] public string Switch2Api() { return GetApiJson("/smartplug/"); } #region api [SwaggerOperation("开")] [HttpGet] [Route("/smartplug/21on")] public ApiResponse SmartPlugOn([SwaggerParameter("设备Id")]string id) { try { this._deviceService.SmartPlugOn(id); } catch (Exception ex) { ex.PrintStack(); return ApiResponse.Error(ex.Message); } return ApiResponse.AsyncSuccess(); } [SwaggerOperation("关")] [HttpGet] [Route("/smartplug/22off")] public ApiResponse SmartPlugOff([SwaggerParameter("设备Id")]string id) { try { this._deviceService.SmartPlugOff(id); } catch (Exception ex) { ex.PrintStack(); return ApiResponse.Error(ex.Message); } return ApiResponse.AsyncSuccess(); } [HttpGet] [Route("/socket1/api")] [SwaggerOperation("API")] [ApiExplorerSettings(IgnoreApi = true)] public string Socket1Api() { return GetApiJson("/socket1/"); } [HttpGet] [Route("/socket2/api")] [SwaggerOperation("API")] [ApiExplorerSettings(IgnoreApi = true)] public string Socket2Api() { return GetApiJson("/socket2/"); } private string GetApiJson(string prefix) { 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; var json = JsonConvert.DeserializeObject(result) as JObject; var paths = json.Properties().FirstOrDefault(o => o.Name == "paths").Value as JObject; var names = paths.Properties().Select(o => o.Name).ToList(); foreach (var item in names) { if (!item.StartsWith(prefix)) { paths.Remove(item); } } var realResult = JsonConvert.SerializeObject(json); return realResult; } #endregion api [SwaggerOperation("全开")] [HttpGet] [Route("/switch2/21on")] public ApiResponse On([SwaggerParameter("设备Id")]string id) { try { this._deviceService.Switch2AllOn(id); } catch (Exception ex) { ex.PrintStack(); return ApiResponse.Error(ex.Message); } return ApiResponse.AsyncSuccess(); } [SwaggerOperation("全关")] [HttpGet] [Route("/switch2/22off")] public ApiResponse Off([SwaggerParameter("设备Id")]string id) { try { this._deviceService.Switch2AllOff(id); } catch (Exception ex) { ex.PrintStack(); return ApiResponse.Error(ex.Message); } return ApiResponse.AsyncSuccess(); } [SwaggerOperation("L1开")] [HttpGet] [Route("/switch2/23l1on")] public ApiResponse L1On([SwaggerParameter("设备Id")]string id) { try { this._deviceService.Switch2L1On(id); } catch (Exception ex) { ex.PrintStack(); return ApiResponse.Error(ex.Message); } return ApiResponse.AsyncSuccess(); } [SwaggerOperation("L1关")] [HttpGet] [Route("/switch2/24l1off")] public ApiResponse L1Off([SwaggerParameter("设备Id")]string id) { try { this._deviceService.Switch2L1Off(id); } catch (Exception ex) { ex.PrintStack(); return ApiResponse.Error(ex.Message); } return ApiResponse.AsyncSuccess(); } [SwaggerOperation("L2开")] [HttpGet] [Route("/switch2/25l2on")] public ApiResponse L2On([SwaggerParameter("设备Id")]string id) { try { this._deviceService.Switch2L2On(id); } catch (Exception ex) { ex.PrintStack(); return ApiResponse.Error(ex.Message); } return ApiResponse.AsyncSuccess(); } [SwaggerOperation("L2关")] [HttpGet] [Route("/switch2/26l2off")] public ApiResponse L2Off([SwaggerParameter("设备Id")]string id) { try { this._deviceService.Switch2L2Off(id); } catch (Exception ex) { ex.PrintStack(); return ApiResponse.Error(ex.Message); } return ApiResponse.AsyncSuccess(); } [SwaggerOperation("开")] [HttpGet] [Route("/socket1/21on")] public ApiResponse Socket1On([SwaggerParameter("设备Id")]string id) { try { this._deviceService.SocketOn(id); } catch (Exception ex) { ex.PrintStack(); return ApiResponse.Error(ex.Message); } return ApiResponse.AsyncSuccess(); } [SwaggerOperation("关")] [HttpGet] [Route("/socket1/22off")] public ApiResponse Socket1Off([SwaggerParameter("设备Id")]string id) { try { this._deviceService.SocketOff(id); } catch (Exception ex) { ex.PrintStack(); return ApiResponse.Error(ex.Message); } return ApiResponse.AsyncSuccess(); } [SwaggerOperation("开")] [HttpGet] [Route("/socket2/21on")] public ApiResponse Socket2On([SwaggerParameter("设备Id")]string id) { try { this._deviceService.SocketOn(id); } catch (Exception ex) { ex.PrintStack(); return ApiResponse.Error(ex.Message); } return ApiResponse.AsyncSuccess(); } [SwaggerOperation("关")] [HttpGet] [Route("/socket2/22off")] public ApiResponse Socket2Off([SwaggerParameter("设备Id")]string id) { try { this._deviceService.SocketOff(id); } catch (Exception ex) { ex.PrintStack(); return ApiResponse.Error(ex.Message); } return ApiResponse.AsyncSuccess(); } } }