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
|
|
|
|
|
{
|
2022-08-22 04:46:01 +00:00
|
|
|
|
StringValues ids , values;
|
|
|
|
|
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
|
|
|
|
{
|
2022-08-21 14:01:32 +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)
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
设置结果 = "设置成功";
|
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)
|
|
|
|
|
{
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
|
}
|