从前端调用rpc修改变量值

This commit is contained in:
iioter 2022-05-09 23:57:46 +08:00
parent 21dfb6f737
commit 9fede82d4e
14 changed files with 249 additions and 110 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -20,7 +20,7 @@ namespace IoTGateway.ViewModel.BasicData.DeviceVMs
{ {
try try
{ {
var device = DC.Set<Device>().Where(x => x.ID == Guid.Parse(FC["id"].ToString())).Include(x => x.Parent).Include(x=>x.DeviceConfigs).Include(x => x.Driver).FirstOrDefault(); var device = DC.Set<Device>().Where(x => x.ID == Guid.Parse(FC["id"].ToString())).Include(x => x.Parent).Include(x => x.DeviceVariables).Include(x => x.DeviceConfigs).Include(x => x.Driver).FirstOrDefault();
if (device == null) if (device == null)
= "复制失败,找不到设备"; = "复制失败,找不到设备";

View File

@ -46,7 +46,8 @@ namespace IoTGateway.ViewModel.BasicData.DeviceVMs
if(a.DeviceTypeEnum== DeviceTypeEnum.Device) if(a.DeviceTypeEnum== DeviceTypeEnum.Device)
return "true"; return "true";
return "false"; return "false";
}),this.MakeGridHeader(x=>"attribute").SetHide().SetFormat((a,b)=>{ }),
this.MakeGridHeader(x=>"attribute").SetHide().SetFormat((a,b)=>{
if(a.DeviceTypeEnum== DeviceTypeEnum.Device) if(a.DeviceTypeEnum== DeviceTypeEnum.Device)
return "true"; return "true";
return "false"; return "false";

View File

@ -21,6 +21,7 @@ namespace IoTGateway.ViewModel.BasicData.DeviceVariableVMs
{ {
return new List<GridAction> return new List<GridAction>
{ {
this.MakeAction("DeviceVariable","SetValue","写入值","写入值", GridActionParameterTypesEnum.SingleId,"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.Create, Localizer["Sys.Create"],"BasicData", dialogWidth: 800),
this.MakeStandardAction("DeviceVariable", GridActionStandardTypesEnum.Edit, Localizer["Sys.Edit"], "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), this.MakeStandardAction("DeviceVariable", GridActionStandardTypesEnum.Delete, Localizer["Sys.Delete"], "BasicData", dialogWidth: 800),
@ -79,6 +80,11 @@ namespace IoTGateway.ViewModel.BasicData.DeviceVariableVMs
this.MakeGridHeader(x=> "detail").SetHide().SetFormat((a,b)=>{ this.MakeGridHeader(x=> "detail").SetHide().SetFormat((a,b)=>{
return "false"; return "false";
}), }),
this.MakeGridHeader(x=>"setValue").SetHide().SetFormat((a,b)=>{
if(a.Device.AutoStart== true)
return "true";
return "false";
}),
this.MakeGridHeaderAction(width: 115) this.MakeGridHeaderAction(width: 115)
}; };
} }
@ -103,7 +109,7 @@ namespace IoTGateway.ViewModel.BasicData.DeviceVariableVMs
if (Searcher.DeviceId != null) if (Searcher.DeviceId != null)
IoTBackgroundService.VariableSelectDeviceId = Searcher.DeviceId; IoTBackgroundService.VariableSelectDeviceId = Searcher.DeviceId;
var query = DC.Set<DeviceVariable>() var query = DC.Set<DeviceVariable>().Include(x => x.Device)
.CheckContain(Searcher.Name, x => x.Name) .CheckContain(Searcher.Name, x => x.Name)
.CheckContain(Searcher.Method, x => x.Method) .CheckContain(Searcher.Method, x => x.Method)
.CheckContain(Searcher.DeviceAddress, x => x.DeviceAddress) .CheckContain(Searcher.DeviceAddress, x => x.DeviceAddress)
@ -121,6 +127,7 @@ namespace IoTGateway.ViewModel.BasicData.DeviceVariableVMs
Expressions = x.Expressions, Expressions = x.Expressions,
ProtectType = x.ProtectType, ProtectType = x.ProtectType,
DeviceName_view = x.Device.DeviceName, DeviceName_view = x.Device.DeviceName,
Device = x.Device
}) })
.OrderBy(x => x.DeviceName_view).ThenBy(x => x.DeviceAddress); .OrderBy(x => x.DeviceName_view).ThenBy(x => x.DeviceAddress);
return query; return query;

View File

@ -28,16 +28,19 @@ namespace IoTGateway.ViewModel.BasicData.DeviceVariableVMs
.OrderBy(x => x.Parent.Index).ThenBy(x => x.Parent.DeviceName) .OrderBy(x => x.Parent.Index).ThenBy(x => x.Parent.DeviceName)
.OrderBy(x => x.Index).ThenBy(x => x.DeviceName) .OrderBy(x => x.Index).ThenBy(x => x.DeviceName)
.GetSelectListItems(Wtm, y => y.DeviceName); .GetSelectListItems(Wtm, y => y.DeviceName);
var deviceService = Wtm.ServiceProvider.GetService(typeof(DeviceService)) as DeviceService;
if (Entity.DeviceId != null) if (Entity.DeviceId != null)
{ {
var deviceService = Wtm.ServiceProvider.GetService(typeof(DeviceService)) as DeviceService;
AllMethods = deviceService.GetDriverMethods(Entity.DeviceId); AllMethods = deviceService.GetDriverMethods(Entity.DeviceId);
var DapThread = deviceService.DeviceThreads.Where(x => x._device.ID == Entity.DeviceId).FirstOrDefault();
} }
else if (IoTBackgroundService.ConfigSelectDeviceId != null) else if (IoTBackgroundService.VariableSelectDeviceId != null)
{ {
Entity.DeviceId = IoTBackgroundService.ConfigSelectDeviceId; Entity.DeviceId = IoTBackgroundService.VariableSelectDeviceId;
AllMethods = deviceService.GetDriverMethods(Entity.DeviceId);
} }
if (AllMethods.Count() > 0)
AllMethods[0].Selected = true;
} }
public override void DoAdd() public override void DoAdd()
@ -55,9 +58,9 @@ namespace IoTGateway.ViewModel.BasicData.DeviceVariableVMs
public override void DoDelete() public override void DoDelete()
{ {
//先获取id //先获取id
var id= UpdateDevices.FC2Guids(FC); var id = UpdateDevices.FC2Guids(FC);
var deviceId = DC.Set<DeviceVariable>().Where(x => id.Contains(x.ID)).Select(x=>x.DeviceId).FirstOrDefault(); var deviceId = DC.Set<DeviceVariable>().Where(x => id.Contains(x.ID)).Select(x => x.DeviceId).FirstOrDefault();
FC["Entity.DeviceId"] =(StringValues)deviceId.ToString(); FC["Entity.DeviceId"] = (StringValues)deviceId.ToString();
base.DoDelete(); base.DoDelete();
UpdateVaribale(); UpdateVaribale();
} }

View File

@ -0,0 +1,86 @@
using Microsoft.EntityFrameworkCore;
using Plugin;
using System;
using System.Collections.Generic;
using System.Linq;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using IoTGateway.Model;
using System.ComponentModel.DataAnnotations;
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 string { get; set; }
public void Set()
{
try
{
var variable = DC.Set<DeviceVariable>().Where(x => x.ID == Guid.Parse(FC["id"].ToString())).AsNoTracking().Include(x => x.Device).FirstOrDefault();
= variable.Device.DeviceName;
= variable.Name;
= variable.DataType.GetEnumDisplayName();
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()
{
RequestId = Guid.NewGuid().ToString(),
DeviceName = variable.Device.DeviceName,
Method = "write",
Params = new Dictionary<string, object>() { { variable.Name, } }
};
dapThread.MyMqttClient_OnExcRpc(this, request);
= "设置成功";
}
}
catch (Exception ex)
{
= $"设置失败,{ex}";
}
}
protected override void InitVM()
{
var variable = DC.Set<DeviceVariable>().Where(x => x.ID == Guid.Parse(FC["id"].ToString())).AsNoTracking().Include(x => x.Device).FirstOrDefault();
= variable.Device.DeviceName;
= variable.Name;
= variable.DataType.GetEnumDisplayName();
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();
}
base.InitVM();
}
}
}

View File

@ -214,6 +214,28 @@ namespace IoTGateway.Controllers
{ {
return vm.GetExportData(); return vm.GetExportData();
} }
#region
[ActionDescription("下发写入")]
public ActionResult SetValue()
{
var vm = Wtm.CreateVM<SetValueVM>();
return PartialView(vm);
}
[HttpPost]
[ActionDescription("下发写入")]
public ActionResult SetValue(SetValueVM vm)
{
if (!ModelState.IsValid)
{
return PartialView(vm);
}
else
{
vm.Set();
return FFResult().CloseDialog().RefreshGrid().Alert($"{vm.设置结果}");
}
}
#endregion
} }
} }

View File

@ -0,0 +1,17 @@
@model IoTGateway.ViewModel.BasicData.DeviceVariableVMs.SetValueVM
@inject IStringLocalizer<Program> Localizer;
<wt:form vm="@Model">
<wt:row items-per-row="ItemsPerRowEnum.One">
<wt:quote>@Model.设备名/@Model.变量名</wt:quote>
<wt:quote>当前原始值:@Model.当前原始值======>当前计算值:@Model.当前计算值</wt:quote>
<wt:quote>类型:@Model.类型======状态:@Model.状态</wt:quote>
<wt:textbox field=设定原始值 />
</wt:row>
<wt:row align="AlignEnum.Right">
<wt:submitbutton text="下发" />
<wt:closebutton text="取消" />
</wt:row>
</wt:form>

Binary file not shown.

View File

@ -162,7 +162,7 @@ namespace Plugin
_myMqttClient.DeviceDisconnected(_device.DeviceName); _myMqttClient.DeviceDisconnected(_device.DeviceName);
} }
private void MyMqttClient_OnExcRpc(object? sender, RpcRequest e) public void MyMqttClient_OnExcRpc(object? sender, RpcRequest e)
{ {
if (e.DeviceName == _device.DeviceName) if (e.DeviceName == _device.DeviceName)
{ {

View File

@ -178,6 +178,8 @@ namespace Plugin
try try
{ {
tBRpcRequest = JsonConvert.DeserializeObject<TBRpcRequest>(e.ApplicationMessage.ConvertPayloadToString()); tBRpcRequest = JsonConvert.DeserializeObject<TBRpcRequest>(e.ApplicationMessage.ConvertPayloadToString());
if(!string.IsNullOrWhiteSpace(tBRpcRequest.RequestData.Method))
{
OnExcRpc?.Invoke(Client, new RpcRequest() OnExcRpc?.Invoke(Client, new RpcRequest()
{ {
Method = tBRpcRequest.RequestData.Method, Method = tBRpcRequest.RequestData.Method,
@ -186,6 +188,7 @@ namespace Plugin
Params = tBRpcRequest.RequestData.Params Params = tBRpcRequest.RequestData.Params
}); });
} }
}
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError($"ReceiveTbRpc:Topic:{e.ApplicationMessage.Topic},Payload:{e.ApplicationMessage.ConvertPayloadToString()}", ex); _logger.LogError($"ReceiveTbRpc:Topic:{e.ApplicationMessage.Topic},Payload:{e.ApplicationMessage.ConvertPayloadToString()}", ex);