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.
183 lines
6.6 KiB
183 lines
6.6 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.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace IoT.Shared.Areas.IoTCenter.Controlls
|
|
{
|
|
public class AjaxBaseController
|
|
{
|
|
protected readonly IServiceProvider _services;
|
|
|
|
protected readonly ILogger<AjaxBaseController> _logger;
|
|
|
|
protected readonly IHttpContextAccessor _httpContext;
|
|
|
|
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;
|
|
|
|
public AjaxBaseController(IServiceProvider services, ILogger<AjaxBaseController> logger)
|
|
{
|
|
this._services = services;
|
|
this._logger = logger;
|
|
}
|
|
|
|
public JsonResult GetIoTProduct(Guid? selected)
|
|
{
|
|
using var scope = this._services.CreateScope();
|
|
var repo = scope.ServiceProvider.GetRequiredService<IRepository<IoTProduct>>();
|
|
var list = repo.ReadOnlyTable()
|
|
.Select(o => new { o.Id, o.Name })
|
|
.ToList();
|
|
return new JsonResult(new SelectList(list, "Id", "Name", selected));
|
|
}
|
|
|
|
public JsonResult GetIoTApi(Guid parentId, Guid? selected = null)
|
|
{
|
|
using var scope = this._services.CreateScope();
|
|
var repo = scope.ServiceProvider.GetRequiredService<IRepository<IoTApi>>();
|
|
var list = repo.ReadOnlyTable()
|
|
.Where(o => o.ProductId == parentId)
|
|
.Select(o => new { o.Id, o.Name })
|
|
.ToList();
|
|
return new JsonResult(new SelectList(list, "Id", "Name", selected));
|
|
}
|
|
|
|
public JsonResult GetIoTGateway(Guid? selected = null)
|
|
{
|
|
using var scope = this._services.CreateScope();
|
|
var repo = scope.ServiceProvider.GetRequiredService<IRepository<IoTGateway>>();
|
|
var list = repo.ReadOnlyTable()
|
|
.Select(o => new { o.Id, Name = $"[{o.Number}]{o.Name}" })
|
|
.ToList();
|
|
return new JsonResult(new SelectList(list, "Id", "Name", selected));
|
|
}
|
|
|
|
public JsonResult GetIoTDevice(Guid parentId, Guid? selected = null)
|
|
{
|
|
using var scope = this._services.CreateScope();
|
|
var repo = scope.ServiceProvider.GetRequiredService<IRepository<IoTDevice>>();
|
|
var list = repo.ReadOnlyTable()
|
|
.Where(o => o.IoTGatewayId == parentId)
|
|
.Select(o => new { o.Id, o.Name })
|
|
.ToList();
|
|
return new JsonResult(new SelectList(list, "Id", "Name", selected));
|
|
}
|
|
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
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 GetDeviceSelectList2(Guid parentId, Guid? selected = null)
|
|
{
|
|
var list = this._deviceRepo.ReadOnlyTable()
|
|
.Where(o => o.IoTGatewayId == 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));
|
|
}
|
|
|
|
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));
|
|
}
|
|
|
|
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));
|
|
}
|
|
|
|
#pragma warning disable CA1822 // Mark members as static
|
|
public JsonResult CronValid(string cron)
|
|
{
|
|
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();
|
|
}
|
|
}
|
|
}
|