Skip to content

Commit 71390bc

Browse files
committed
优化了存储结构
1 parent e0edf77 commit 71390bc

File tree

2 files changed

+17
-22
lines changed

2 files changed

+17
-22
lines changed

IoTGateway.ViewModel/BasicData/DeviceVariableVMs/SetValueVM.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ protected override void InitVM()
127127
string deviceName = dapThread.Device.DeviceName;
128128
foreach (var variable in deviceVariables)
129129
{
130-
var currentVariable = dapThread!.DeviceValues.Where(x => x.Key == variable.ID).Select(x => x.Value).FirstOrDefault();
131-
if (currentVariable is { Value: not null })
130+
var currentVariable = dapThread!.Device.DeviceVariables.FirstOrDefault(x => x.ID == variable.ID);
131+
if (currentVariable != null)
132132
{
133133
variable.DeviceName = deviceName;
134134
variable.RawValue = currentVariable.Value?.ToString();

Plugins/Plugin/DeviceThread.cs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public class DeviceThread : IDisposable
2020
private readonly string _projectId;
2121
private readonly MyMqttClient? _myMqttClient;
2222
private Interpreter? _interpreter;
23-
public Dictionary<Guid, DriverReturnValueModel> DeviceValues { get; set; } = new();
2423
internal List<MethodInfo>? Methods { get; set; }
2524
private Task? _task;
2625
private readonly DateTime _tsStartDt = new(1970, 1, 1);
@@ -102,19 +101,14 @@ public async Task CreateThread()
102101
var triggerVariables = deviceVariables.Where(x => x.IsTrigger).ToList();
103102
ReadVariables(ref triggerVariables, ref payLoadTrigger, _mqttServer);
104103

105-
var triggerValues = DeviceValues.Where(x =>
106-
triggerVariables.Select(x => x.ID).Contains(x.Key))
107-
.ToDictionary(
108-
x => Device.DeviceVariables.FirstOrDefault(y => y.ID == x.Key).Name,
109-
z => z.Value.CookedValue);
104+
var triggerValues = triggerVariables.ToDictionary(x=>x.Name,x=>x.CookedValue);
110105

111106

112107
var payLoadUnTrigger = new PayLoad() { Values = new() };
113108
//有需要上传 或者全部是非触发
114109
if (triggerValues.Values.Any(x => x is true) || !triggerVariables.Any())
115110
{
116-
117-
var variables = Device.DeviceVariables.Where(x => !triggerVariables.Select(y => y.ID).Contains(x.ID)).ToList();
111+
var variables = deviceVariables.Where(x => !triggerVariables.Select(y => y.ID).Contains(x.ID)).ToList();
118112
ReadVariables(ref variables, ref payLoadUnTrigger, _mqttServer);
119113
canPub = true;
120114
}
@@ -124,12 +118,9 @@ public async Task CreateThread()
124118
{
125119
var payLoad = new PayLoad()
126120
{
127-
Values = DeviceValues
128-
.Where(x => x.Value.StatusType == VaribaleStatusTypeEnum.Good &&
129-
deviceVariables.Where(x => x.IsUpload).Select(x => x.ID)
130-
.Contains(x.Key))
131-
.ToDictionary(kv => deviceVariables.First(x => x.ID == kv.Key).Name,
132-
kv => kv.Value.CookedValue),
121+
Values = deviceVariables
122+
.Where(x => x.StatusType == VaribaleStatusTypeEnum.Good && x.IsUpload)
123+
.ToDictionary(kv => kv.Name, kv => kv.CookedValue),
133124
DeviceStatus = payLoadTrigger.DeviceStatus
134125
};
135126
payLoad.TS = (long)(DateTime.UtcNow - _tsStartDt).TotalMilliseconds;
@@ -146,9 +137,9 @@ public async Task CreateThread()
146137

147138
}
148139

149-
//只要有读取异常且连接正常就断开
150-
if (DeviceValues
151-
.Any(x => x.Value.StatusType != VaribaleStatusTypeEnum.Good)&& Driver.IsConnected)
140+
//全部读取异常且连接正常就断开
141+
if (Device.DeviceVariables
142+
.All(x => x.StatusType != VaribaleStatusTypeEnum.Good) && Driver.IsConnected)
152143
{
153144
Driver.Close();
154145
Driver.Dispose();
@@ -247,6 +238,12 @@ item.Values[1] is bool
247238

248239
ret.VarId = item.ID;
249240

241+
item.Value = ret.Value;
242+
item.CookedValue = ret.CookedValue;
243+
item.StatusType = ret.StatusType;
244+
item.Timestamp = ret.Timestamp;
245+
item.Message = ret.Message;
246+
250247
//变化了才推送到mqttserver,用于前端展示
251248
if (JsonConvert.SerializeObject(item.Values[1]) != JsonConvert.SerializeObject(item.Values[0])|| JsonConvert.SerializeObject(item.CookedValues[1]) != JsonConvert.SerializeObject(item.CookedValues[0]))
252249
{
@@ -260,9 +257,7 @@ item.Values[1] is bool
260257
});
261258
mqttServer.InjectApplicationMessage(msgInternal);
262259
}
263-
264-
DeviceValues[item.ID] = ret;
265-
260+
266261
Thread.Sleep((int)Device.CmdPeriod);
267262
}
268263
}

0 commit comments

Comments
 (0)