优化了存储结构
This commit is contained in:
parent
e0edf777dc
commit
71390bc3a6
@ -127,8 +127,8 @@ namespace IoTGateway.ViewModel.BasicData.DeviceVariableVMs
|
|||||||
string deviceName = dapThread.Device.DeviceName;
|
string deviceName = dapThread.Device.DeviceName;
|
||||||
foreach (var variable in deviceVariables)
|
foreach (var variable in deviceVariables)
|
||||||
{
|
{
|
||||||
var currentVariable = dapThread!.DeviceValues.Where(x => x.Key == variable.ID).Select(x => x.Value).FirstOrDefault();
|
var currentVariable = dapThread!.Device.DeviceVariables.FirstOrDefault(x => x.ID == variable.ID);
|
||||||
if (currentVariable is { Value: not null })
|
if (currentVariable != null)
|
||||||
{
|
{
|
||||||
variable.DeviceName = deviceName;
|
variable.DeviceName = deviceName;
|
||||||
variable.RawValue = currentVariable.Value?.ToString();
|
variable.RawValue = currentVariable.Value?.ToString();
|
||||||
|
@ -20,7 +20,6 @@ namespace Plugin
|
|||||||
private readonly string _projectId;
|
private readonly string _projectId;
|
||||||
private readonly MyMqttClient? _myMqttClient;
|
private readonly MyMqttClient? _myMqttClient;
|
||||||
private Interpreter? _interpreter;
|
private Interpreter? _interpreter;
|
||||||
public Dictionary<Guid, DriverReturnValueModel> DeviceValues { get; set; } = new();
|
|
||||||
internal List<MethodInfo>? Methods { get; set; }
|
internal List<MethodInfo>? Methods { get; set; }
|
||||||
private Task? _task;
|
private Task? _task;
|
||||||
private readonly DateTime _tsStartDt = new(1970, 1, 1);
|
private readonly DateTime _tsStartDt = new(1970, 1, 1);
|
||||||
@ -102,19 +101,14 @@ namespace Plugin
|
|||||||
var triggerVariables = deviceVariables.Where(x => x.IsTrigger).ToList();
|
var triggerVariables = deviceVariables.Where(x => x.IsTrigger).ToList();
|
||||||
ReadVariables(ref triggerVariables, ref payLoadTrigger, _mqttServer);
|
ReadVariables(ref triggerVariables, ref payLoadTrigger, _mqttServer);
|
||||||
|
|
||||||
var triggerValues = DeviceValues.Where(x =>
|
var triggerValues = triggerVariables.ToDictionary(x=>x.Name,x=>x.CookedValue);
|
||||||
triggerVariables.Select(x => x.ID).Contains(x.Key))
|
|
||||||
.ToDictionary(
|
|
||||||
x => Device.DeviceVariables.FirstOrDefault(y => y.ID == x.Key).Name,
|
|
||||||
z => z.Value.CookedValue);
|
|
||||||
|
|
||||||
|
|
||||||
var payLoadUnTrigger = new PayLoad() { Values = new() };
|
var payLoadUnTrigger = new PayLoad() { Values = new() };
|
||||||
//有需要上传 或者全部是非触发
|
//有需要上传 或者全部是非触发
|
||||||
if (triggerValues.Values.Any(x => x is true) || !triggerVariables.Any())
|
if (triggerValues.Values.Any(x => x is true) || !triggerVariables.Any())
|
||||||
{
|
{
|
||||||
|
var variables = deviceVariables.Where(x => !triggerVariables.Select(y => y.ID).Contains(x.ID)).ToList();
|
||||||
var variables = Device.DeviceVariables.Where(x => !triggerVariables.Select(y => y.ID).Contains(x.ID)).ToList();
|
|
||||||
ReadVariables(ref variables, ref payLoadUnTrigger, _mqttServer);
|
ReadVariables(ref variables, ref payLoadUnTrigger, _mqttServer);
|
||||||
canPub = true;
|
canPub = true;
|
||||||
}
|
}
|
||||||
@ -124,12 +118,9 @@ namespace Plugin
|
|||||||
{
|
{
|
||||||
var payLoad = new PayLoad()
|
var payLoad = new PayLoad()
|
||||||
{
|
{
|
||||||
Values = DeviceValues
|
Values = deviceVariables
|
||||||
.Where(x => x.Value.StatusType == VaribaleStatusTypeEnum.Good &&
|
.Where(x => x.StatusType == VaribaleStatusTypeEnum.Good && x.IsUpload)
|
||||||
deviceVariables.Where(x => x.IsUpload).Select(x => x.ID)
|
.ToDictionary(kv => kv.Name, kv => kv.CookedValue),
|
||||||
.Contains(x.Key))
|
|
||||||
.ToDictionary(kv => deviceVariables.First(x => x.ID == kv.Key).Name,
|
|
||||||
kv => kv.Value.CookedValue),
|
|
||||||
DeviceStatus = payLoadTrigger.DeviceStatus
|
DeviceStatus = payLoadTrigger.DeviceStatus
|
||||||
};
|
};
|
||||||
payLoad.TS = (long)(DateTime.UtcNow - _tsStartDt).TotalMilliseconds;
|
payLoad.TS = (long)(DateTime.UtcNow - _tsStartDt).TotalMilliseconds;
|
||||||
@ -146,9 +137,9 @@ namespace Plugin
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//只要有读取异常且连接正常就断开
|
//全部读取异常且连接正常就断开
|
||||||
if (DeviceValues
|
if (Device.DeviceVariables
|
||||||
.Any(x => x.Value.StatusType != VaribaleStatusTypeEnum.Good)&& Driver.IsConnected)
|
.All(x => x.StatusType != VaribaleStatusTypeEnum.Good) && Driver.IsConnected)
|
||||||
{
|
{
|
||||||
Driver.Close();
|
Driver.Close();
|
||||||
Driver.Dispose();
|
Driver.Dispose();
|
||||||
@ -247,6 +238,12 @@ namespace Plugin
|
|||||||
|
|
||||||
ret.VarId = item.ID;
|
ret.VarId = item.ID;
|
||||||
|
|
||||||
|
item.Value = ret.Value;
|
||||||
|
item.CookedValue = ret.CookedValue;
|
||||||
|
item.StatusType = ret.StatusType;
|
||||||
|
item.Timestamp = ret.Timestamp;
|
||||||
|
item.Message = ret.Message;
|
||||||
|
|
||||||
//变化了才推送到mqttserver,用于前端展示
|
//变化了才推送到mqttserver,用于前端展示
|
||||||
if (JsonConvert.SerializeObject(item.Values[1]) != JsonConvert.SerializeObject(item.Values[0])|| JsonConvert.SerializeObject(item.CookedValues[1]) != JsonConvert.SerializeObject(item.CookedValues[0]))
|
if (JsonConvert.SerializeObject(item.Values[1]) != JsonConvert.SerializeObject(item.Values[0])|| JsonConvert.SerializeObject(item.CookedValues[1]) != JsonConvert.SerializeObject(item.CookedValues[0]))
|
||||||
{
|
{
|
||||||
@ -260,9 +257,7 @@ namespace Plugin
|
|||||||
});
|
});
|
||||||
mqttServer.InjectApplicationMessage(msgInternal);
|
mqttServer.InjectApplicationMessage(msgInternal);
|
||||||
}
|
}
|
||||||
|
|
||||||
DeviceValues[item.ID] = ret;
|
|
||||||
|
|
||||||
Thread.Sleep((int)Device.CmdPeriod);
|
Thread.Sleep((int)Device.CmdPeriod);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user