You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iot/projects/IoT/IoTServices/FBeeService/Controllers/HomeController.cs

360 lines
10 KiB

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.ComponentModel.DataAnnotations;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
namespace FBeeService.Controllers
{
public class HomeController : Controller
{
private readonly IRepository<Gateway> _gatewayRepo;
private readonly IRepository<FBeeDevice> _deviceRepo;
private readonly IHttpClientFactory _httpClientFactory;
private readonly DeviceService _deviceService;
public HomeController(IHttpClientFactory httpClientFactory,
DeviceService deviceService,
IRepository<Gateway> gatewayRepo,
IRepository<FBeeDevice> deviceRepo)
{
this._gatewayRepo = gatewayRepo;
this._deviceRepo = deviceRepo;
this._httpClientFactory = httpClientFactory;
this._deviceService = deviceService;
}
#region ui
public IActionResult Index()
{
return View(this._gatewayRepo.ReadOnlyTable().OrderBy(o => o.Sn).ToList());
}
public IActionResult Gateway(string sn)
{
var list = this._deviceRepo.ReadOnlyTable()
.Where(o => o.Sn == sn)
.OrderBy(o => o.DeviceId).ThenBy(o => o.ZoneType).ThenBy(o => o.Name)
.ToList();
return View(list);
}
#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();
}
[ApiExplorerSettings(IgnoreApi = true)]
[SwaggerOperation("设置亮度")]
[HttpGet]
[Route("/api/x83")]
public ApiResponse X83(string sn, string ieee, [Range(0, 255)]byte brightness)
{
try
{
this._deviceService.X83(sn, ieee, brightness);
}
catch (Exception ex)
{
ex.PrintStack();
return ApiResponse.Error(ex.Message);
}
return ApiResponse.AsyncSuccess();
}
[ApiExplorerSettings(IgnoreApi = true)]
[SwaggerOperation("设置颜色")]
[HttpGet]
[Route("/api/x84")]
public ApiResponse X84(string sn, string ieee, [Range(0, 255)]byte hue, [Range(0, 255)]byte saturation)
{
try
{
this._deviceService.X84(sn, ieee, hue, saturation);
}
catch (Exception ex)
{
ex.PrintStack();
return ApiResponse.Error(ex.Message);
}
return ApiResponse.AsyncSuccess();
}
//[ApiExplorerSettings(IgnoreApi = true)]
//[SwaggerOperation("设备开关")]
//[HttpGet]
//[Route("/api/xa7x05")]
//public ApiResponse XA7X05(string sn, string ieee)
//{
// try
// {
// this._deviceService.XA7X05(sn, ieee);
// }
// 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 colorlight
[HttpGet]
[Route("/colorlight/api")]
[SwaggerOperation("API")]
[ApiExplorerSettings(IgnoreApi = true)]
public string ColorLightApi()
{
return GetApiJson("/colorlight/");
}
[SwaggerOperation("调节亮度")]
[HttpGet]
[Route("/colorlight/23setbrightness")]
public ApiResponse SetBrightness([SwaggerParameter("网关Id")]string sn, [SwaggerParameter("设备Id")]string id, [Range(0, 255)]int brightness)
{
try
{
this._deviceService.X83(sn, id, (byte)brightness);
}
catch (Exception ex)
{
ex.PrintStack();
return ApiResponse.Error(ex.Message);
}
return ApiResponse.AsyncSuccess();
}
[SwaggerOperation("调节亮度")]
[HttpGet]
[Route("/colorlight/24setcolor")]
public ApiResponse SetColor([SwaggerParameter("网关Id")]string sn, [SwaggerParameter("设备Id")]string id, [Range(0, 255)]int hue, [Range(0, 255)]int saturation)
{
try
{
this._deviceService.X84(sn, id, (byte)hue, (byte)saturation);
}
catch (Exception ex)
{
ex.PrintStack();
return ApiResponse.Error(ex.Message);
}
return ApiResponse.AsyncSuccess();
}
[SwaggerOperation("调节色温")]
[HttpGet]
[Route("/colorlight/25setcolortemperature")]
public ApiResponse SetColorTemperature([SwaggerParameter("网关Id")]string sn, [SwaggerParameter("设备Id")]string id, [Range(2700, 6500)]int colorTemperature)
{
try
{
this._deviceService.XA8(sn, id, (ushort)colorTemperature);
}
catch (Exception ex)
{
ex.PrintStack();
return ApiResponse.Error(ex.Message);
}
return ApiResponse.AsyncSuccess();
}
#endregion colorlight
#region IR
[SwaggerOperation("版本")]
[HttpGet]
[Route("/ir/version")]
public ApiResponse Version([SwaggerParameter("网关Id")]string sn, [SwaggerParameter("设备Id")]string id)
{
try
{
this._deviceService.XA70080(sn, id);
}
catch (Exception ex)
{
ex.PrintStack();
return ApiResponse.Error(ex.Message);
}
return ApiResponse.AsyncSuccess();
}
#endregion IR
#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
}
}