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.
299 lines
11 KiB
299 lines
11 KiB
using Application.Domain.Entities;
|
|
using Cronos;
|
|
using CSScriptLib;
|
|
using Infrastructure.Data;
|
|
using Infrastructure.Extensions;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Logging;
|
|
using System;
|
|
using System.Linq;
|
|
|
|
namespace IoT.Shared.Areas.Admin.Controlls
|
|
{
|
|
[Area("Admin")]
|
|
public class AjaxController
|
|
{
|
|
private readonly ILogger<AjaxController> _logger;
|
|
private readonly IRepository<Category> _categoryRepo;
|
|
private readonly IRepository<Node> _nodeRepo;
|
|
private readonly IRepository<Product> _productRepo;
|
|
private readonly IRepository<Api> _apiRepo;
|
|
private readonly IRepository<Device> _deviceRepo;
|
|
private readonly IRepository<Data> _dataRepo;
|
|
private readonly IRepository<Command> _commandRepo;
|
|
private readonly IRepository<Scene> _sceneRepo;
|
|
private readonly IRepository<IoTTimer> _iotTimerRepo;
|
|
private readonly IRepository<IoTTigger> _iotTiggerRepo;
|
|
|
|
public AjaxController(ILogger<AjaxController> logger,
|
|
IRepository<Category> categoryRepo,
|
|
IRepository<Product> productRepo,
|
|
IRepository<Node> nodeRepo,
|
|
IRepository<Scene> sceneRepo,
|
|
IRepository<Command> commandRepo,
|
|
IRepository<Device> deviceRepo,
|
|
IRepository<Data> dataRepo,
|
|
IRepository<Api> apiRepo,
|
|
IRepository<IoTTimer> iotTimerRepo,
|
|
IRepository<IoTTigger> iotTiggerRepo)
|
|
{
|
|
this._logger = logger;
|
|
this._categoryRepo = categoryRepo;
|
|
this._productRepo = productRepo;
|
|
this._nodeRepo = nodeRepo;
|
|
this._sceneRepo = sceneRepo;
|
|
this._commandRepo = commandRepo;
|
|
this._deviceRepo = deviceRepo;
|
|
this._dataRepo = dataRepo;
|
|
this._apiRepo = apiRepo;
|
|
this._iotTimerRepo = iotTimerRepo;
|
|
this._iotTiggerRepo = iotTiggerRepo;
|
|
}
|
|
|
|
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 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);
|
|
}
|
|
|
|
#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 GetGlobalSceneSelectList(Guid? selected = null)
|
|
{
|
|
var list = this._sceneRepo.ReadOnlyTable()
|
|
.Where(o => o.NodeId == null)
|
|
.Include(o => o.Node)
|
|
.Select(o => new { o.Id, o.Name })
|
|
.ToList();
|
|
return new SelectList(list, "Id", "Name", selected);
|
|
}
|
|
|
|
public JsonResult GetGlobalSceneJson(Guid? selected)
|
|
{
|
|
return new JsonResult(this.GetGlobalSceneSelectList(selected));
|
|
}
|
|
|
|
#endregion Scene
|
|
|
|
#region IoTTimer
|
|
|
|
public SelectList GetIoTTimerSelectList(Guid parentId, Guid? selected = null)
|
|
{
|
|
var list = this._iotTimerRepo.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 GetIoTTimerJson(Guid parentId, Guid? selected)
|
|
{
|
|
return new JsonResult(this.GetIoTTimerSelectList(parentId, selected));
|
|
}
|
|
|
|
public SelectList GetGlobalIoTTimerSelectList(Guid? selected = null)
|
|
{
|
|
var list = this._iotTimerRepo.ReadOnlyTable()
|
|
.Where(o => o.NodeId == null)
|
|
.Include(o => o.Node)
|
|
.Select(o => new { o.Id, o.Name })
|
|
.ToList();
|
|
return new SelectList(list, "Id", "Name", selected);
|
|
}
|
|
|
|
public JsonResult GetGlobalIoTTimerJson(Guid? selected)
|
|
{
|
|
return new JsonResult(this.GetGlobalIoTTimerSelectList(selected));
|
|
}
|
|
|
|
#endregion IoTTimer
|
|
|
|
#region IoTTigger
|
|
|
|
public SelectList GetIoTTiggerSelectList(Guid parentId, Guid? selected = null)
|
|
{
|
|
var list = this._iotTiggerRepo.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 GetIoTTiggerJson(Guid parentId, Guid? selected)
|
|
{
|
|
return new JsonResult(this.GetIoTTiggerSelectList(parentId, selected));
|
|
}
|
|
|
|
public SelectList GetGlobalIoTTiggerSelectList(Guid? selected = null)
|
|
{
|
|
var list = this._iotTiggerRepo.ReadOnlyTable()
|
|
.Where(o => o.NodeId == null)
|
|
.Include(o => o.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));
|
|
}
|
|
|
|
#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));
|
|
}
|
|
|
|
#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);
|
|
}
|
|
|
|
public JsonResult CronValid(string cron)
|
|
{
|
|
try
|
|
{
|
|
CronExpression.Parse(cron, CronFormat.IncludeSeconds);
|
|
return new JsonResult(true);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex.PrintStack();
|
|
return new JsonResult(ex.Message);
|
|
}
|
|
}
|
|
|
|
public JsonResult ConditionValid(string condition, Guid dataId)
|
|
{
|
|
try
|
|
{
|
|
var data = this._dataRepo.ReadOnlyTable().FirstOrDefault(o => o.Id == dataId);
|
|
var methodText = $"bool Valid(string name,string key,{data.Type.ToString().ToLower()} value,string description){{ return {condition};}}";
|
|
Console.WriteLine(methodText);
|
|
dynamic method = CSScript.Evaluator.LoadMethod(methodText);
|
|
dynamic value = data.GetValue();
|
|
var result = method.Valid(data.Name, data.Key, value, data.Description);
|
|
Console.WriteLine(result);
|
|
return new JsonResult(true);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex.PrintStack();
|
|
return new JsonResult(ex.Message);
|
|
}
|
|
}
|
|
}
|
|
} |