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.Shared/Areas/IoTCenter/Controlls/AjaxController.cs

365 lines
13 KiB

using Cronos;
using Infrastructure.Data;
using Infrastructure.Extensions;
using IoT.Shared.Application.Domain.Entities;
using Jint;
using Jint.Native;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
namespace IoT.Shared.Areas.IoTCenter.Controlls
{
[Area("IoTCenter")]
public class AjaxController
{
protected readonly ILogger<AjaxController> _logger;
protected readonly IHttpContextAccessor _httpContext;
protected readonly IRepository<IoTProductCategory> _categoryRepo;
protected readonly IRepository<Organ> _organRepo;
protected readonly IRepository<BuildingIoTGateway> _buildingIoTGatewayRepo;
protected readonly IRepository<OrganIoTScene> _organSceneRepo;
protected readonly IRepository<IoTGateway> _nodeRepo;
protected readonly IRepository<IoTProduct> _productRepo;
protected readonly IRepository<IoTApi> _apiRepo;
protected readonly IRepository<IoTDevice> _deviceRepo;
protected readonly IRepository<IoTData> _dataRepo;
protected readonly IRepository<IoTCommand> _commandRepo;
protected readonly IRepository<IoTScene> _sceneRepo;
protected readonly IRepository<IoTSceneTigger> _sceneTiggerRepo;
public AjaxController(ILogger<AjaxController> logger,
IHttpContextAccessor httpContxt,
IRepository<IoTProductCategory> categoryRepo,
IRepository<IoTProduct> productRepo,
IRepository<Organ> organRepo,
IRepository<BuildingIoTGateway> buildingIoTGatewayRepo,
IRepository<OrganIoTScene> organSceneRepo,
IRepository<IoTGateway> nodeRepo,
IRepository<IoTScene> sceneRepo,
IRepository<IoTCommand> commandRepo,
IRepository<IoTDevice> deviceRepo,
IRepository<IoTData> dataRepo,
IRepository<IoTApi> apiRepo,
IRepository<IoTSceneTigger> sceneTiggerRepo)
{
this._logger = logger;
this._httpContext = httpContxt;
this._categoryRepo = categoryRepo;
this._productRepo = productRepo;
this._organRepo = organRepo;
this._buildingIoTGatewayRepo = buildingIoTGatewayRepo;
this._organSceneRepo = organSceneRepo;
this._nodeRepo = nodeRepo;
this._sceneRepo = sceneRepo;
this._commandRepo = commandRepo;
this._deviceRepo = deviceRepo;
this._dataRepo = dataRepo;
this._apiRepo = apiRepo;
this._sceneTiggerRepo = sceneTiggerRepo;
}
public MultiSelectList GetNodeMultiSelectList(IEnumerable<Guid> selected)
{
if (selected == null)
{
selected = new List<Guid>();
}
var list = this._nodeRepo.ReadOnlyTable()
.Select(o => new { o.Id, o.Name })
.ToList();
return new MultiSelectList(list, "Id", "Name", selected);
}
public SelectList GetCategorySelectList(Guid? selected = null)
{
var list = this._categoryRepo.ReadOnlyTable()
.Select(o => new { o.Id, Name = $"{o.Name}({o.Number})" })
.ToList();
return new SelectList(list, "Id", "Name", selected);
}
#region Organ
public SelectList GetOrganSelectList(Guid? selected = null, Guid? currentId = null)
{
var list = this._organRepo.ReadOnlyTable()
.Where(o => o.ParentId != null)
.WhereIf(currentId.HasValue, o => o.Id != currentId.Value)
.OrderBy(o => o.Parent)
.ThenBy(o => o.DisplayOrder)
.Select(o => new { o.Id, Name = $"{o.Name}({o.Number})" })
.ToList();
return new SelectList(list, "Id", "Name", selected);
}
#endregion Organ
#region Node
public SelectList GetNodeSelectList(Guid? selected = null)
{
var list = this._nodeRepo.ReadOnlyTable()
.Select(o => new { o.Id, Name = $"{o.Name}({o.Number})" })
.ToList();
return new SelectList(list, "Id", "Name", selected);
}
public SelectList GetNodeJson(Guid? selected = null)
{
return this.GetNodeSelectList(selected);
}
public SelectList GetOrganNodeSelectList(Guid parentId, Guid? selected = null)
{
var list = this._buildingIoTGatewayRepo.ReadOnlyTable()
.Where(o => o.Building.OrganId == parentId)
.Select(o => o.IoTGateway)
.Select(o => new { o.Id, Name = $"{o.Name}({o.Number})" })
.ToList();
return new SelectList(list, "Id", "Name", selected);
}
public SelectList GetOrganNodeJson(Guid parentId, Guid? selected = null)
{
return this.GetOrganNodeSelectList(parentId, selected);
}
#endregion Node
#region Scene
public SelectList GetSceneSelectList(Guid parentId, Guid? selected = null)
{
var list = this._sceneRepo.ReadOnlyTable()
.Where(o => o.NodeId == parentId)
.Include(o => o.Node)
.Select(o => new { o.Id, Name = $"{o.Node.Name}->{o.Name}" })
.ToList();
return new SelectList(list, "Id", "Name", selected);
}
public JsonResult GetSceneJson(Guid parentId, Guid? selected)
{
return new JsonResult(this.GetSceneSelectList(parentId, selected));
}
public SelectList GetOrganSceneSelectList(Guid parentId, Guid? selected = null)
{
var list = this._organSceneRepo.ReadOnlyTable()
.Where(o => o.OrganId == parentId)
.Select(o => new { o.Id, o.Name })
.ToList();
return new SelectList(list, "Id", "Name", selected);
}
public JsonResult GetOrganSceneJson(Guid parentId, Guid? selected)
{
return new JsonResult(this.GetOrganSceneSelectList(parentId, selected));
}
#endregion Scene
#region IoTTigger
public SelectList GetIoTTiggerSelectList(Guid parentId, Guid? selected = null)
{
var list = this._sceneTiggerRepo.ReadOnlyTable()
.Where(o => o.Scene.NodeId == parentId)
.Include(o => o.Scene).ThenInclude(o => o.Node)
.Select(o => new { o.Id, Name = $"{o.Scene.Node.Name}->{o.Name}" })
.ToList();
return new SelectList(list, "Id", "Name", selected);
}
public JsonResult GetIoTTiggerJson(Guid parentId, Guid? selected)
{
return new JsonResult(this.GetIoTTiggerSelectList(parentId, selected));
}
public SelectList GetGlobalIoTTiggerSelectList(Guid? selected = null)
{
var list = this._sceneTiggerRepo.ReadOnlyTable()
.Where(o => o.Scene.NodeId == null)
.Include(o => o.Scene.Node)
.Select(o => new { o.Id, o.Name })
.ToList();
return new SelectList(list, "Id", "Name", selected);
}
public JsonResult GetGlobalIoTTiggerJson(Guid? selected)
{
return new JsonResult(this.GetGlobalIoTTiggerSelectList(selected));
}
#endregion IoTTigger
public SelectList GetCommandSelectList(Guid parentId, Guid? selected = null)
{
var list = this._commandRepo.ReadOnlyTable()
.Where(o => o.Device.NodeId == parentId)
.Include(o => o.Device)
.ThenInclude(o => o.Node)
.Select(o => new { o.Id, Name = $"{o.Device.Node.Name}->{o.Name}" })
.ToList();
return new SelectList(list, "Id", "Name", selected);
}
public JsonResult GetCommandJson(Guid parentId, Guid? selected)
{
return new JsonResult(this.GetCommandSelectList(parentId, selected));
}
#region Device
public SelectList GetDeviceSelectList(Guid parentId, Guid? selected = null)
{
var list = this._deviceRepo.ReadOnlyTable()
.Where(o => o.NodeId == parentId)
.Select(o => new { o.Id, Name = $"{o.DisplayName}({o.Number})" })
.ToList();
return new SelectList(list, "Id", "Name", selected);
}
public JsonResult GetDeviceJson(Guid parentId, Guid? selected)
{
return new JsonResult(this.GetDeviceSelectList(parentId, selected));
}
public SelectList GetDeviceSelectList2(Guid parentId, Guid? selected = null)
{
var list = this._deviceRepo.ReadOnlyTable()
.Where(o => o.NodeId == parentId && o.Product.Apis.Count() > 0)
.Select(o => new { o.Id, Name = $"{o.DisplayName}({o.Number})" })
.ToList();
return new SelectList(list, "Id", "Name", selected);
}
public JsonResult GetDeviceJson2(Guid parentId, Guid? selected)
{
return new JsonResult(this.GetDeviceSelectList2(parentId, selected));
}
#endregion Device
#region Data
public SelectList GetDataSelectList(Guid parentId, Guid? selected = null)
{
var list = this._dataRepo.ReadOnlyTable()
.Where(o => !o.Hidden && o.DeviceId == parentId)
.Select(o => new { o.Id, Name = $"{o.Name}[当前数据:{o.Value})(当前状态:{o.Description}]" })
.ToList();
return new SelectList(list, "Id", "Name", selected);
}
public JsonResult GetdataJson(Guid parentId, Guid? selected)
{
return new JsonResult(this.GetDataSelectList(parentId, selected));
}
#endregion Data
#region Api
public SelectList GetProductSelectList(Guid? selected = null)
{
var list = this._productRepo.ReadOnlyTable()
.Select(o => new { o.Id, o.Name })
.ToList();
return new SelectList(list, "Id", "Name", selected);
}
public SelectList GetApiSelectList(Guid parentId, Guid? selected = null)
{
var list = this._deviceRepo.ReadOnlyTable()
.Where(o => o.Id == parentId).SelectMany(o => o.Product.Apis)
.Select(o => new { o.Id, o.Name })
.ToList();
return new SelectList(list, "Id", "Name", selected);
}
public JsonResult GetApiJson(Guid parentId, Guid? selected)
{
return new JsonResult(this.GetApiSelectList(parentId, selected));
}
public SelectList GetProductApiSelectList(Guid parentId, Guid? selected = null)
{
var list = this._apiRepo.ReadOnlyTable()
.Where(o => o.ProductId == parentId)
.Select(o => new { o.Id, o.Name })
.ToList();
return new SelectList(list, "Id", "Name", selected);
}
public JsonResult GetProductApiJson(Guid parentId, Guid? selected)
{
return new JsonResult(this.GetProductApiSelectList(parentId, selected));
}
#endregion Api
public SelectList GetCameraSelectList(string deviceNumber)
{
var list = this._deviceRepo.ReadOnlyTable()
.Where(o => o.Product.Number == "onvifcamera")
.WhereIf(!string.IsNullOrEmpty(deviceNumber), o => o.Number == deviceNumber)
.Select(o => new { Id = o.Number, Name = o.DisplayName })
.ToList();
return new SelectList(list, "Id", "Name", deviceNumber);
}
#pragma warning disable CA1822 // Mark members as static
public JsonResult CronValid(string cron)
#pragma warning restore CA1822 // Mark members as static
{
try
{
CronExpression.Parse(cron, CronFormat.IncludeSeconds);
return new JsonResult(true);
}
catch (Exception ex)
{
this._logger.LogError(ex.ToString());
return new JsonResult(ex.Message);
}
}
public JsonResult ConditionValid(string condition, Guid dataId)
{
try
{
var data = this._dataRepo.ReadOnlyTable().FirstOrDefault(o => o.Id == dataId);
var value = data.Value;
var engine = new Engine().Execute($"function valid(value){{return {condition};}}");
var result = engine.Invoke("valid", value);
if (result == JsValue.True || result == JsValue.False)
{
return new JsonResult(true);
}
else
{
return new JsonResult($"表达式运算结果无效:{result}");
}
}
catch (Exception ex)
{
ex.PrintStack();
return new JsonResult(ex.Message);
}
}
protected bool IsAjax()
{
return this._httpContext.HttpContext.Request.IsAjax();
}
}
}