diff --git a/IoTGateway.Model/DeviceVariable.cs b/IoTGateway.Model/DeviceVariable.cs index 26b9d7d..350621c 100644 --- a/IoTGateway.Model/DeviceVariable.cs +++ b/IoTGateway.Model/DeviceVariable.cs @@ -28,6 +28,7 @@ namespace IoTGateway.Model [Display(Name = "权限")] public ProtectTypeEnum ProtectType { get; set; } + [Newtonsoft.Json.JsonIgnore] public Device Device { get; set; } [Display(Name = "设备")] public Guid? DeviceId { get; set; } diff --git a/IoTGateway.ViewModel/BasicData/DeviceVariableVMs/DeviceVariableListVM.cs b/IoTGateway.ViewModel/BasicData/DeviceVariableVMs/DeviceVariableListVM.cs index 3d655b9..d15e0d0 100644 --- a/IoTGateway.ViewModel/BasicData/DeviceVariableVMs/DeviceVariableListVM.cs +++ b/IoTGateway.ViewModel/BasicData/DeviceVariableVMs/DeviceVariableListVM.cs @@ -21,7 +21,7 @@ namespace IoTGateway.ViewModel.BasicData.DeviceVariableVMs { return new List { - this.MakeAction("DeviceVariable","SetValue","写入值","写入值", GridActionParameterTypesEnum.SingleId,"BasicData",600).SetIconCls("_wtmicon _wtmicon-xiayibu").SetHideOnToolBar(false).SetShowInRow(false).SetBindVisiableColName("setValue"), + this.MakeAction("DeviceVariable","SetValue","写入值","写入值", GridActionParameterTypesEnum.MultiIds,"BasicData",600).SetIconCls("_wtmicon _wtmicon-xiayibu").SetHideOnToolBar(false).SetShowInRow(false).SetBindVisiableColName("setValue"), this.MakeStandardAction("DeviceVariable", GridActionStandardTypesEnum.Create, Localizer["Sys.Create"],"BasicData", dialogWidth: 800), this.MakeStandardAction("DeviceVariable", GridActionStandardTypesEnum.Edit, Localizer["Sys.Edit"], "BasicData", dialogWidth: 800), this.MakeStandardAction("DeviceVariable", GridActionStandardTypesEnum.Delete, Localizer["Sys.Delete"], "BasicData", dialogWidth: 800), diff --git a/IoTGateway.ViewModel/BasicData/DeviceVariableVMs/SetValueVM.cs b/IoTGateway.ViewModel/BasicData/DeviceVariableVMs/SetValueVM.cs index 4724a0d..05d506b 100644 --- a/IoTGateway.ViewModel/BasicData/DeviceVariableVMs/SetValueVM.cs +++ b/IoTGateway.ViewModel/BasicData/DeviceVariableVMs/SetValueVM.cs @@ -7,80 +7,139 @@ using WalkingTec.Mvvm.Core; using WalkingTec.Mvvm.Core.Extensions; using IoTGateway.Model; using System.ComponentModel.DataAnnotations; +using Microsoft.Extensions.Primitives; +using System.Runtime.Intrinsics.Arm; +using Newtonsoft.Json; namespace IoTGateway.ViewModel.BasicData.DeviceVariableVMs { public class SetValueVM : BaseVM { - public string 设备名 { get; set; } - public string 变量名 { get; set; } - public string 类型 { get; set; } - public string 当前原始值 { get; set; } - public string 当前计算值 { get; set; } - public string 状态 { get; set; } - public string 设定原始值 { get; set; } + public List SetValues { get; set; } = new(); public string 设置结果 { get; set; } public void Set() { try { - var variable = DC.Set().Where(x => x.ID == Guid.Parse(FC["id"].ToString())).AsNoTracking().Include(x => x.Device).FirstOrDefault(); - 设备名 = variable.Device.DeviceName; - 变量名 = variable.Name; - 类型 = variable.DataType.GetEnumDisplayName(); + var ids = (StringValues)FC["setValue.ID[]"]; + var values= (StringValues)FC["setValue.SetRawValue[]"]; + Dictionary kv = new(0); + for (int i = 0; i < ids.Count; i++) + { + kv[ids[i]]=values[i]; + } + + + var setValues = JsonConvert.DeserializeObject>( + JsonConvert.SerializeObject(DC.Set() + .Where(x => ids.Contains(x.ID.ToString().ToLower())).AsNoTracking() + .OrderBy(x => x.DeviceId).ToList())); var deviceService = Wtm.ServiceProvider.GetService(typeof(DeviceService)) as DeviceService; - var dapThread = deviceService.DeviceThreads.Where(x => x.Device.ID == variable.DeviceId).FirstOrDefault(); - if (dapThread?.DeviceValues != null && dapThread.DeviceValues.ContainsKey(variable.ID)) - { - 当前原始值 = dapThread.DeviceValues[variable.ID].Value?.ToString(); - 当前计算值 = dapThread.DeviceValues[variable.ID].CookedValue?.ToString(); - 状态 = dapThread.DeviceValues[variable.ID].StatusType.ToString(); - } - - if (variable == null || variable.Device == null || dapThread == null) - 设置结果 = "设置失败,找不到设备(变量)"; - - else - { - PluginInterface.RpcRequest request = new PluginInterface.RpcRequest() + if (setValues != null) + foreach (var deviceVariables in setValues.GroupBy(x => x.DeviceId)) { - RequestId = Guid.NewGuid().ToString(), - DeviceName = variable.Device.DeviceName, - Method = "write", - Params = new Dictionary() { { variable.Name, 设定原始值 } } - }; - dapThread.MyMqttClient_OnExcRpc(this, request); - 设置结果 = "设置成功"; - } + if (deviceService != null) + { + var dapThread = + deviceService.DeviceThreads.FirstOrDefault(x => + x.Device.ID == deviceVariables.Key); + + if (dapThread != null) + { + string deviceName = dapThread.Device.DeviceName; + foreach (var variable in deviceVariables) + { + if (dapThread.DeviceValues.ContainsKey(variable.ID)) + { + variable.DeviceName = deviceName; + variable.RawValue = dapThread.DeviceValues[variable.ID].Value?.ToString(); + variable.Value = dapThread.DeviceValues[variable.ID].CookedValue?.ToString(); + variable.Status = dapThread.DeviceValues[variable.ID].StatusType.ToString(); + variable.SetRawValue = kv[variable.ID.ToString()]; + } + } + + PluginInterface.RpcRequest request = new PluginInterface.RpcRequest() + { + RequestId = Guid.NewGuid().ToString(), + DeviceName = deviceName, + Method = "write", + Params = deviceVariables.ToDictionary(x => x.Name, x => x.SetRawValue) + }; + dapThread.MyMqttClient_OnExcRpc(this, request); + } + + } + } + 设置结果 = "设置成功"; } catch (Exception ex) { - 设置结果 = $"设置失败,{ex}"; } } protected override void InitVM() { - var variable = DC.Set().Where(x => x.ID == Guid.Parse(FC["id"].ToString())).AsNoTracking().Include(x => x.Device).FirstOrDefault(); - 设备名 = variable.Device.DeviceName; - 变量名 = variable.Name; - 类型 = variable.DataType.GetEnumDisplayName(); + StringValues ids; + if (FC.ContainsKey("setValue.ID[]")) + ids = (StringValues)FC["setValue.ID[]"]; + else + ids = (StringValues)FC["Ids[]"]; + + var setValues = JsonConvert.DeserializeObject>( + JsonConvert.SerializeObject(DC.Set() + .Where(x => ids.Contains(x.ID.ToString().ToLower())).AsNoTracking() + .OrderBy(x => x.DeviceId).ToList())); var deviceService = Wtm.ServiceProvider.GetService(typeof(DeviceService)) as DeviceService; - var dapThread = deviceService.DeviceThreads.Where(x => x.Device.ID == variable.DeviceId).FirstOrDefault(); - if (dapThread?.DeviceValues != null && dapThread.DeviceValues.ContainsKey(variable.ID)) - { - 当前原始值 = dapThread.DeviceValues[variable.ID].Value?.ToString(); - 当前计算值 = dapThread.DeviceValues[variable.ID].CookedValue?.ToString(); - 状态 = dapThread.DeviceValues[variable.ID].StatusType.ToString(); - } + if (setValues != null) + foreach (var deviceVariables in setValues.GroupBy(x => x.DeviceId)) + { + if (deviceService != null) + { + var dapThread = + deviceService.DeviceThreads.FirstOrDefault(x => + x.Device.ID == deviceVariables.Key); + if (dapThread != null) + { + string deviceName = dapThread.Device.DeviceName; + foreach (var variable in deviceVariables) + { + if (dapThread.DeviceValues.ContainsKey(variable.ID)) + { + variable.DeviceName = deviceName; + variable.RawValue = dapThread.DeviceValues[variable.ID].Value?.ToString(); + variable.Value = dapThread.DeviceValues[variable.ID].CookedValue?.ToString(); + variable.Status = dapThread.DeviceValues[variable.ID].StatusType.ToString(); + } + } + } + + } + } + + SetValues = setValues; base.InitVM(); } } + + public class SetValue : DeviceVariable + { + [Display(Name = "设备名")] + public string DeviceName { get; set; } + [Display(Name = "设定原值")] + public object SetRawValue { get; set; } + [Display(Name = "原值")] + public string RawValue { get; set; } + [Display(Name = "计算值")] + public string Value { get; set; } + [Display(Name = "状态")] + public string Status { get; set; } + } } diff --git a/IoTGateway/Areas/BasicData/Controllers/DeviceVariableController.cs b/IoTGateway/Areas/BasicData/Controllers/DeviceVariableController.cs index b727f98..0414edf 100644 --- a/IoTGateway/Areas/BasicData/Controllers/DeviceVariableController.cs +++ b/IoTGateway/Areas/BasicData/Controllers/DeviceVariableController.cs @@ -5,6 +5,7 @@ using WalkingTec.Mvvm.Core; using WalkingTec.Mvvm.Mvc; using WalkingTec.Mvvm.Core.Extensions; using IoTGateway.ViewModel.BasicData.DeviceVariableVMs; +using Opc.Ua.Security.Certificates; namespace IoTGateway.Controllers { @@ -216,16 +217,21 @@ namespace IoTGateway.Controllers } #region 下发写入 [ActionDescription("下发写入")] - public ActionResult SetValue() + [HttpPost] + public ActionResult SetValue(string[] IDs) { - var vm = Wtm.CreateVM(); + var vm = Wtm.CreateVM(Ids: IDs); return PartialView(vm); } [HttpPost] [ActionDescription("下发写入")] - public ActionResult SetValue(SetValueVM vm) + public ActionResult DoSetValue() { + var ids = Request.Form["setValue.ID[]"].ToArray(); + var values = Request.Form["setValue.SetRawValue[]"].ToArray(); + + var vm = Wtm.CreateVM(Ids: ids); if (!ModelState.IsValid) { return PartialView(vm); diff --git a/IoTGateway/Areas/BasicData/Views/DeviceVariable/Indexbak.cshtml b/IoTGateway/Areas/BasicData/Views/DeviceVariable/Indexbak.cshtml deleted file mode 100644 index 8edf80b..0000000 --- a/IoTGateway/Areas/BasicData/Views/DeviceVariable/Indexbak.cshtml +++ /dev/null @@ -1,505 +0,0 @@ -@model IoTGateway.ViewModel.BasicData.DeviceVariableVMs.DeviceVariableListVM -@inject IStringLocalizer Localizer; -@using Newtonsoft.Json - -
-
-
-
-
-
-
-
-
-

- 搜索条件 -
- 搜索 - -
-

-
- - -
- -
- -
- -
- -
-
- -
- - - - -
- -
- -
- -
-
- -
- - -
- -
- -
- -
-
- -
- - -
- -
- -
- -
-
- -
- - -
- -
- -
- -
-
- -
- - - -
- -
-
-
- -
- - -
- - - - - - - -
- -
- - - - - - - - - - - - - - \ No newline at end of file diff --git a/IoTGateway/Areas/BasicData/Views/DeviceVariable/SetValue.cshtml b/IoTGateway/Areas/BasicData/Views/DeviceVariable/SetValue.cshtml index 671bff7..0f38028 100644 --- a/IoTGateway/Areas/BasicData/Views/DeviceVariable/SetValue.cshtml +++ b/IoTGateway/Areas/BasicData/Views/DeviceVariable/SetValue.cshtml @@ -1,17 +1,26 @@ -@model IoTGateway.ViewModel.BasicData.DeviceVariableVMs.SetValueVM +@using IoTGateway.ViewModel.BasicData.DeviceVariableVMs +@model IoTGateway.ViewModel.BasicData.DeviceVariableVMs.SetValueVM @inject IStringLocalizer Localizer; - + - @Model.设备名/@Model.变量名 - 当前原始值:@Model.当前原始值======>当前计算值:@Model.当前计算值 - 类型:@Model.类型======状态:@Model.状态 - + @if (@Model.SetValues.Any()) + { + foreach (var setValue in @Model.SetValues) + { +
+ @setValue.DeviceName/@setValue.Name:【原值:@setValue.RawValue】=>【计算值:@setValue.Value】 +
+ +
+ + } + }
- +
diff --git a/IoTGateway/iotgateway.db b/IoTGateway/iotgateway.db index f66e24b..5727705 100644 Binary files a/IoTGateway/iotgateway.db and b/IoTGateway/iotgateway.db differ