iotgateway/IoTGateway.ViewModel/BasicData/DeviceVariableVMs/SetValueVM.cs

163 lines
6.7 KiB
C#
Raw Normal View History

2022-05-09 15:57:46 +00:00
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;
2022-08-21 14:01:32 +00:00
using Microsoft.Extensions.Primitives;
using System.Runtime.Intrinsics.Arm;
using Newtonsoft.Json;
2022-05-09 15:57:46 +00:00
namespace IoTGateway.ViewModel.BasicData.DeviceVariableVMs
{
public class SetValueVM : BaseVM
{
2022-08-21 14:01:32 +00:00
public List<SetValue> SetValues { get; set; } = new();
2022-05-09 15:57:46 +00:00
public string { get; set; }
public void Set()
{
try
{
StringValues ids, values;
2022-08-22 04:46:01 +00:00
if (FC.ContainsKey("setValue.ID[]"))
{
ids = (StringValues)FC["setValue.ID[]"];
values = (StringValues)FC["setValue.SetRawValue[]"];
}
else
{
ids = (StringValues)FC["setValue.ID"];
values = (StringValues)FC["setValue.SetRawValue"];
}
2022-08-21 14:01:32 +00:00
Dictionary<string, string> kv = new(0);
for (int i = 0; i < ids.Count; i++)
2022-05-09 15:57:46 +00:00
{
kv[ids[i]] = values[i];
2022-05-09 15:57:46 +00:00
}
2022-08-21 14:01:32 +00:00
var setValues = JsonConvert.DeserializeObject<List<SetValue>>(
JsonConvert.SerializeObject(DC.Set<DeviceVariable>()
.Where(x => ids.Contains(x.ID.ToString().ToLower())).AsNoTracking()
.OrderBy(x => x.DeviceId).ToList()));
var deviceService = Wtm.ServiceProvider.GetService(typeof(DeviceService)) as DeviceService;
if (setValues != null)
foreach (var deviceVariables in setValues.GroupBy(x => x.DeviceId))
2022-05-09 15:57:46 +00:00
{
2022-08-21 14:01:32 +00:00
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)
{
var currentVariable = dapThread!.Device.DeviceVariables.FirstOrDefault(x => x.ID == variable.ID);
if (currentVariable != null)
2022-08-21 14:01:32 +00:00
{
variable.DeviceName = deviceName + (!string.IsNullOrEmpty(variable.Alias)
? $"->{variable.Alias}"
: "");
variable.RawValue = currentVariable.Value?.ToString();
variable.Value = currentVariable.CookedValue?.ToString();
variable.Status = currentVariable.StatusType.ToString();
2022-08-21 14:01:32 +00:00
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);
}
2022-08-21 14:01:32 +00:00
}
}
= "下发完成请在Rpc日志页面查看结果";
2022-05-09 15:57:46 +00:00
}
catch (Exception ex)
{
= $"设置失败,{ex}";
}
}
protected override void InitVM()
{
2022-08-21 14:01:32 +00:00
StringValues ids;
if (FC.ContainsKey("setValue.ID[]"))
ids = (StringValues)FC["setValue.ID[]"];
2022-08-22 04:46:01 +00:00
else if (FC.ContainsKey("setValue.ID"))
ids = (StringValues)FC["setValue.ID"];
2022-08-21 14:01:32 +00:00
else
ids = (StringValues)FC["Ids[]"];
var setValues = JsonConvert.DeserializeObject<List<SetValue>>(
JsonConvert.SerializeObject(DC.Set<DeviceVariable>()
.Where(x => ids.Contains(x.ID.ToString().ToLower())).AsNoTracking()
.OrderBy(x => x.DeviceId).ToList()));
2022-05-09 15:57:46 +00:00
var deviceService = Wtm.ServiceProvider.GetService(typeof(DeviceService)) as DeviceService;
2022-08-21 14:01:32 +00:00
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)
{
2024-06-01 15:29:31 +00:00
var currentVariable = dapThread!.Device.DeviceVariables.FirstOrDefault(x => x.ID == variable.ID);
if (currentVariable != null)
2022-08-21 14:01:32 +00:00
{
variable.DeviceName = deviceName;
variable.RawValue = currentVariable.Value?.ToString();
variable.Value = currentVariable.CookedValue?.ToString();
variable.Status = currentVariable.StatusType.ToString();
2022-08-21 14:01:32 +00:00
}
}
}
2022-05-09 15:57:46 +00:00
2022-08-21 14:01:32 +00:00
}
}
SetValues = setValues;
2022-05-09 15:57:46 +00:00
base.InitVM();
}
}
2022-08-21 14:01:32 +00:00
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; }
}
2022-05-09 15:57:46 +00:00
}