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 Swashbuckle.AspNetCore.Annotations; using System; using System.Linq; using System.Net.Http; using System.Threading.Tasks; namespace FBeeService.Controllers { public class HomeController : Controller { private readonly IRepository _gatewayRepo; private readonly IRepository _deviceRepo; private readonly IHttpClientFactory _httpClientFactory; private readonly DeviceService _deviceService; public HomeController(IHttpClientFactory httpClientFactory, DeviceService deviceService, IRepository gatewayRepo, IRepository deviceRepo) { this._gatewayRepo = gatewayRepo; this._deviceRepo = deviceRepo; this._httpClientFactory = httpClientFactory; this._deviceService = deviceService; } #region ui public IActionResult Index() { return View(this._gatewayRepo.ReadOnlyTable().ToList()); } public IActionResult Gateway(string sn) { return View(this._deviceRepo.ReadOnlyTable().Where(o => o.Sn == sn).ToList()); } #endregion ui #region api [SwaggerOperation("查询所有网关")] [HttpGet] [Route("/api/refresh")] public ApiResponse Refresh() { try { Task.Run(() => { this._deviceService.Refresh(); }); } catch (Exception ex) { ex.PrintStack(); return ApiResponse.Error(ex.Message); } return ApiResponse.AsyncSuccess(); } [SwaggerOperation("查询网关信息")] [HttpGet] [Route("/api/x9d")] public ApiResponse X9d(string sn) { try { this._deviceService.X9d(sn); } catch (Exception ex) { ex.PrintStack(); return ApiResponse.Error(ex.Message); } return ApiResponse.AsyncSuccess(); } [SwaggerOperation("查询所有设备")] [HttpGet] [Route("/api/x81")] public ApiResponse X81(string sn) { try { this._deviceService.X81(sn); } catch (Exception ex) { ex.PrintStack(); return ApiResponse.Error(ex.Message); } return ApiResponse.AsyncSuccess(); } [SwaggerOperation("删除指定设备")] [HttpGet] [Route("/api/x95")] public ApiResponse X95(string sn, string ieee) { try { this._deviceService.X95(sn, ieee); } catch (Exception ex) { ex.PrintStack(); return ApiResponse.Error(ex.Message); } return ApiResponse.AsyncSuccess(); } [ApiExplorerSettings(IgnoreApi = true)] [SwaggerOperation("设备开关")] [HttpGet] [Route("/api/x82")] public ApiResponse X82(string sn, string ieee, byte status) { try { this._deviceService.X82(sn, ieee, status); } catch (Exception ex) { ex.PrintStack(); return ApiResponse.Error(ex.Message); } return ApiResponse.AsyncSuccess(); } #endregion api #region smart plug [HttpGet] [Route("/smartplug/api")] [SwaggerOperation("API")] [ApiExplorerSettings(IgnoreApi = true)] public string Switch2Api() { return GetApiJson("/smartplug/"); } [SwaggerOperation("开")] [HttpGet] [Route("/smartplug/21on")] public ApiResponse SmartPlugOn([SwaggerParameter("设备Id")]string id) { try { this._deviceService.X82(null, id, 1); } 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.X82(null, id, 0); } catch (Exception ex) { ex.PrintStack(); return ApiResponse.Error(ex.Message); } return ApiResponse.AsyncSuccess(); } #endregion smart plug #region tools 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 tools } }