Skip to content

Commit 7f05048

Browse files
author
Georgy Levchenko
committed
The alternative view of displaying data from sensors has been changed, added app settings
1 parent 8a62663 commit 7f05048

File tree

25 files changed

+1655
-171
lines changed

25 files changed

+1655
-171
lines changed

SmartThermo.sln

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SmartThermo.Modules.Dialog.
3535
EndProject
3636
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SmartThermo.SmartThermo.Core.Tests", "tests\SmartThermo.SmartThermo.Core.Tests\SmartThermo.SmartThermo.Core.Tests.csproj", "{A80C5CCC-2D81-42E9-ABAE-F179035B814B}"
3737
EndProject
38+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SmartThermo.Modules.SettingsApplication", "src\Modules\SmartThermo.Modules.SettingsApplication\SmartThermo.Modules.SettingsApplication.csproj", "{F5819333-AE0F-477C-BED0-D03FCED73AAC}"
39+
EndProject
40+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SmartThermo.Services.Configuration", "src\Services\SmartThermo.Services.Configuration\SmartThermo.Services.Configuration.csproj", "{5F91B1F0-DD19-491E-839B-3187BCCE41E6}"
41+
EndProject
3842
Global
3943
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4044
Debug|Any CPU = Debug|Any CPU
@@ -177,6 +181,30 @@ Global
177181
{A80C5CCC-2D81-42E9-ABAE-F179035B814B}.Release|x64.Build.0 = Release|Any CPU
178182
{A80C5CCC-2D81-42E9-ABAE-F179035B814B}.Release|x86.ActiveCfg = Release|Any CPU
179183
{A80C5CCC-2D81-42E9-ABAE-F179035B814B}.Release|x86.Build.0 = Release|Any CPU
184+
{F5819333-AE0F-477C-BED0-D03FCED73AAC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
185+
{F5819333-AE0F-477C-BED0-D03FCED73AAC}.Debug|Any CPU.Build.0 = Debug|Any CPU
186+
{F5819333-AE0F-477C-BED0-D03FCED73AAC}.Debug|x64.ActiveCfg = Debug|Any CPU
187+
{F5819333-AE0F-477C-BED0-D03FCED73AAC}.Debug|x64.Build.0 = Debug|Any CPU
188+
{F5819333-AE0F-477C-BED0-D03FCED73AAC}.Debug|x86.ActiveCfg = Debug|Any CPU
189+
{F5819333-AE0F-477C-BED0-D03FCED73AAC}.Debug|x86.Build.0 = Debug|Any CPU
190+
{F5819333-AE0F-477C-BED0-D03FCED73AAC}.Release|Any CPU.ActiveCfg = Release|Any CPU
191+
{F5819333-AE0F-477C-BED0-D03FCED73AAC}.Release|Any CPU.Build.0 = Release|Any CPU
192+
{F5819333-AE0F-477C-BED0-D03FCED73AAC}.Release|x64.ActiveCfg = Release|Any CPU
193+
{F5819333-AE0F-477C-BED0-D03FCED73AAC}.Release|x64.Build.0 = Release|Any CPU
194+
{F5819333-AE0F-477C-BED0-D03FCED73AAC}.Release|x86.ActiveCfg = Release|Any CPU
195+
{F5819333-AE0F-477C-BED0-D03FCED73AAC}.Release|x86.Build.0 = Release|Any CPU
196+
{5F91B1F0-DD19-491E-839B-3187BCCE41E6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
197+
{5F91B1F0-DD19-491E-839B-3187BCCE41E6}.Debug|Any CPU.Build.0 = Debug|Any CPU
198+
{5F91B1F0-DD19-491E-839B-3187BCCE41E6}.Debug|x64.ActiveCfg = Debug|Any CPU
199+
{5F91B1F0-DD19-491E-839B-3187BCCE41E6}.Debug|x64.Build.0 = Debug|Any CPU
200+
{5F91B1F0-DD19-491E-839B-3187BCCE41E6}.Debug|x86.ActiveCfg = Debug|Any CPU
201+
{5F91B1F0-DD19-491E-839B-3187BCCE41E6}.Debug|x86.Build.0 = Debug|Any CPU
202+
{5F91B1F0-DD19-491E-839B-3187BCCE41E6}.Release|Any CPU.ActiveCfg = Release|Any CPU
203+
{5F91B1F0-DD19-491E-839B-3187BCCE41E6}.Release|Any CPU.Build.0 = Release|Any CPU
204+
{5F91B1F0-DD19-491E-839B-3187BCCE41E6}.Release|x64.ActiveCfg = Release|Any CPU
205+
{5F91B1F0-DD19-491E-839B-3187BCCE41E6}.Release|x64.Build.0 = Release|Any CPU
206+
{5F91B1F0-DD19-491E-839B-3187BCCE41E6}.Release|x86.ActiveCfg = Release|Any CPU
207+
{5F91B1F0-DD19-491E-839B-3187BCCE41E6}.Release|x86.Build.0 = Release|Any CPU
180208
EndGlobalSection
181209
GlobalSection(SolutionProperties) = preSolution
182210
HideSolutionNode = FALSE
@@ -196,6 +224,8 @@ Global
196224
{707F9F26-F30A-4EE6-B80C-FB4D94428467} = {87BD6B81-EF1B-478E-BBB4-A0A40B1B6119}
197225
{E8C8E4FD-80F0-4443-904A-4BF78DCA74DE} = {87BD6B81-EF1B-478E-BBB4-A0A40B1B6119}
198226
{A80C5CCC-2D81-42E9-ABAE-F179035B814B} = {D7FE162C-D6BC-4F89-8DF9-1AEF427FA63B}
227+
{F5819333-AE0F-477C-BED0-D03FCED73AAC} = {87BD6B81-EF1B-478E-BBB4-A0A40B1B6119}
228+
{5F91B1F0-DD19-491E-839B-3187BCCE41E6} = {D1175F74-F3B3-49F2-A1DB-74D1E10EDFBC}
199229
EndGlobalSection
200230
GlobalSection(ExtensibilityGlobals) = postSolution
201231
SolutionGuid = {B511AF63-B8BD-4505-A278-DA0B0CCAA472}

src/DataAccess/SmartThermo.DataAccess.Sqlite/Context.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ public class Context : DbContext
1717

1818
protected override void OnModelCreating(ModelBuilder modelBuilder)
1919
{
20-
modelBuilder.Entity<Setting>().HasData(new Setting { Id = 1 });
20+
modelBuilder.Entity<Setting>().HasData(new Setting
21+
{
22+
Id = 1,
23+
TimeBeforeWarning = 15,
24+
TimeBeforeOffline = 60,
25+
IsWriteToDatabase = true
26+
});
2127

2228
modelBuilder.Entity<SelectMode>().HasData(
2329
new SelectMode { Id = 1, SettingId = 1, Stage = false },

src/DataAccess/SmartThermo.DataAccess.Sqlite/Models/Setting.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,11 @@ public class Setting : BaseModel
1212
public string PortNameSelected { get; set; }
1313

1414
public string BaudRateSelected { get; set; }
15+
16+
public int TimeBeforeWarning { get; set; }
17+
18+
public int TimeBeforeOffline { get; set; }
19+
20+
public bool IsWriteToDatabase { get; set; }
1521
}
1622
}

src/Modules/SmartThermo.Modules.Analytics/ViewModels/AnalyticsWindowViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public AnalyticsWindowViewModel(INotifications notifications, IDialogService dia
135135
_notifications = notifications;
136136
_dialogService = dialogService;
137137

138-
_timer = new Timer((state) => _isUpdateChart = false, 0, Timeout.Infinite, Timeout.Infinite);
138+
_timer = new Timer(state => _isUpdateChart = false, 0, Timeout.Infinite, Timeout.Infinite);
139139

140140
SelectCountRecord = Enumerable.Range(0, 6)
141141
.Select(x => new ItemDescriptor<int>($"{Math.Pow(10, x + 2)} записей", x))

src/Modules/SmartThermo.Modules.Analytics/Views/AnalyticsWindow.xaml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@
139139
Grid.Column="1"
140140
Margin="0,16,0,0"
141141
Background="{DynamicResource SystemControlBackgroundBaseMediumLowBrush}" />
142-
<Grid Grid.Column="2" Margin="16,8,0,8">
142+
<Grid Grid.Column="2" Margin="16,8,0,0">
143143
<ui:SimpleStackPanel Spacing="16">
144144
<GroupBox Padding="0,-8,0,0" Header="Сессии">
145145
<ui:SimpleStackPanel Orientation="Vertical" Spacing="8">
@@ -214,10 +214,7 @@
214214
</StackPanel>
215215
</ui:SimpleStackPanel>
216216
</GroupBox>
217-
<Button
218-
Margin="0,8,0,0"
219-
HorizontalAlignment="Right"
220-
Command="{Binding GetSensorDataCommand}">
217+
<Button HorizontalAlignment="Right" Command="{Binding GetSensorDataCommand}">
221218
<ui:SimpleStackPanel Orientation="Horizontal" Spacing="8">
222219

223220
<ui:PathIcon

src/Modules/SmartThermo.Modules.DataViewer/Models/MeasureData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace SmartThermo.Modules.DataViewer.Models
44
{
5-
public class MeasureData
5+
public struct MeasureData
66
{
77
public DateTime DateTime { get; set; }
88

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using SmartThermo.Core.Enums;
2+
3+
namespace SmartThermo.Modules.DataViewer.Models
4+
{
5+
public struct SensorsData
6+
{
7+
public int? Temperature { get; set; }
8+
9+
public StatusSensor StatusSensor { get; set; }
10+
}
11+
}

src/Modules/SmartThermo.Modules.DataViewer/Models/SensorsEther.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace SmartThermo.Modules.DataViewer.Models
22
{
3-
public class SensorsEther
3+
public struct SensorsEther
44
{
55
public int Id { get; set; }
66

src/Modules/SmartThermo.Modules.DataViewer/SmartThermo.Modules.DataViewer.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
</ItemGroup>
1212
<ItemGroup>
1313
<ProjectReference Include="..\..\DataAccess\SmartThermo.DataAccess.Sqlite\SmartThermo.DataAccess.Sqlite.csproj" />
14+
<ProjectReference Include="..\..\Services\SmartThermo.Services.Configuration\SmartThermo.Services.Configuration.csproj" />
1415
<ProjectReference Include="..\..\SmartThermo.Core\SmartThermo.Core.csproj" />
1516
<ProjectReference Include="..\SmartThermo.Modules.Dialog.SettingsDevice\SmartThermo.Modules.Dialog.SettingsDevice.csproj" />
1617
</ItemGroup>

src/Modules/SmartThermo.Modules.DataViewer/ViewModels/Represent/LoadDataViewerWindowViewModel.cs

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
using System.Windows;
2222
using System.Windows.Data;
2323
using System.Windows.Threading;
24+
using SmartThermo.Core.Enums;
25+
using SmartThermo.Services.Configuration;
2426

2527
namespace SmartThermo.Modules.DataViewer.ViewModels.Represent
2628
{
@@ -37,6 +39,7 @@ public class LoadDataViewerWindowViewModel : ViewModelBase, IRegionMemberLifetim
3739

3840
private static readonly object Lock = new object();
3941

42+
private readonly IConfiguration _configuration;
4043
private readonly IDeviceConnector _deviceConnector;
4144
private readonly List<int> _groupSensorId = new List<int>();
4245
private ObservableCollection<LimitRelay> _limitRelayItems = new ObservableCollection<LimitRelay>();
@@ -47,7 +50,7 @@ public class LoadDataViewerWindowViewModel : ViewModelBase, IRegionMemberLifetim
4750
private List<double> _axisYMax = new List<double>();
4851
private List<double> _axisYMin = new List<double>();
4952
private List<bool> _selectMode = new List<bool>();
50-
private List<int?> _temperature = new List<int?>();
53+
private List<SensorsData> _sensorsData = new List<SensorsData>();
5154

5255
#endregion
5356

@@ -105,10 +108,10 @@ public List<bool> SelectMode
105108
set => SetProperty(ref _selectMode, value);
106109
}
107110

108-
public List<int?> Temperature
111+
public List<SensorsData> SensorsData
109112
{
110-
get => _temperature;
111-
set => SetProperty(ref _temperature, value);
113+
get => _sensorsData;
114+
set => SetProperty(ref _sensorsData, value);
112115
}
113116

114117
public DelegateCommand<int?> ChangeSelectModeCommand { get; }
@@ -121,8 +124,12 @@ public List<int?> Temperature
121124

122125
#region Constructor
123126

124-
public LoadDataViewerWindowViewModel(IDeviceConnector deviceConnector, INotifications notifications)
127+
public LoadDataViewerWindowViewModel(
128+
IConfiguration configuration,
129+
IDeviceConnector deviceConnector,
130+
INotifications notifications)
125131
{
132+
_configuration = configuration;
126133
_deviceConnector = deviceConnector;
127134
_deviceConnector.SettingDeviceChanged += DeviceConnector_SettingDeviceChanged;
128135
_deviceConnector.StatusConnectChanged += DeviceConnector_StatusConnectChanged;
@@ -195,24 +202,27 @@ private void DeviceConnector_SettingDeviceChanged(object sender, SettingDeviceEv
195202

196203
private void DeviceConnector_RegistersRequested(object sender, List<SensorInfoEventArgs> sensorData)
197204
{
198-
var sensorEhterItems = sensorData.Where(x => x.IsAir)
205+
var sensorEtherItems = sensorData.Where(x => x.IsAir)
199206
.Select(x => new SensorsEther
200207
{
201208
Id = x.Number,
202209
Time = x.TimeLastBroadcast
203210
})
204211
.ToList();
205212
SensorsEtherItems.Clear();
206-
SensorsEtherItems.AddRange(sensorEhterItems);
207-
208-
_temperature.Clear();
209-
_temperature.AddRange(sensorData
210-
.Select(x => x.IsAir ? (int?)x.Temperature : null)
213+
SensorsEtherItems.AddRange(sensorEtherItems);
214+
215+
_sensorsData.Clear();
216+
_sensorsData.AddRange(sensorData
217+
.Select(x => new SensorsData
218+
{
219+
Temperature = x.IsAir ? (int?)x.Temperature : null,
220+
StatusSensor = x.IsAir ? GetStatusSensor(x.TimeLastBroadcast) : StatusSensor.Offline,
221+
})
211222
.ToList());
212-
RaisePropertyChanged(nameof(Temperature));
223+
RaisePropertyChanged(nameof(SensorsData));
213224

214225
var now = DateTime.Now.Round(TimeSpan.FromSeconds(1));
215-
// Вызов диспетчера требуется для корректной работы отрисовки графика при переключении окон.
216226
Application.Current?.Dispatcher?.InvokeAsync(() =>
217227
{
218228
for (var i = 0; i < 36; i++)
@@ -230,7 +240,19 @@ private void DeviceConnector_RegistersRequested(object sender, List<SensorInfoEv
230240
foreach (var item in ChartValues.Where(item => item.Count > 25))
231241
item.RemoveAt(0);
232242

233-
SaveDataToDatabaseAsync(now, sensorData);
243+
if (_configuration.IsWriteToDatabase)
244+
SaveDataToDatabaseAsync(now, sensorData);
245+
}
246+
247+
private StatusSensor GetStatusSensor(int time)
248+
{
249+
return time switch
250+
{
251+
var n when (n < _configuration.TimeBeforeWarning) => StatusSensor.Online,
252+
var n when (n >= _configuration.TimeBeforeWarning) => StatusSensor.Wait,
253+
var n when (n >= _configuration.TimeBeforeOffline) => StatusSensor.Offline,
254+
_ => throw new ArgumentOutOfRangeException(nameof(time), time, null)
255+
};
234256
}
235257

236258
private async void SaveDataToDatabaseAsync(DateTime time, IReadOnlyList<SensorInfoEventArgs> sensorData)
@@ -268,8 +290,12 @@ private void InitCharts()
268290
ChartValues.AddRange(Enumerable.Range(0, 36)
269291
.Select(x => new ChartValues<MeasureData>())
270292
.ToList());
271-
Temperature.AddRange(Enumerable.Range(0,36)
272-
.Select(x => default(int?))
293+
SensorsData.AddRange(Enumerable.Range(0,36)
294+
.Select(x => new SensorsData()
295+
{
296+
Temperature = default ,
297+
StatusSensor = StatusSensor.Offline
298+
})
273299
.ToList());
274300
LimitRelayItems.AddRange(Enumerable.Range(0, 6)
275301
.Select(x => new LimitRelay())
@@ -283,38 +309,8 @@ private void InitCharts()
283309
SetAxisXLimits(DateTime.Now);
284310
SetAxisYLimits();
285311
SetRelayLimits();
286-
287-
//LoadTestDataAsync();
288312
}
289-
290-
//private async Task LoadTestDataAsync()
291-
//{
292-
// await Task.Delay(1000);
293-
// var random = new Random();
294-
295-
// var myDates = new DateTime[1000_000];
296-
// for (var i = 0; i < 1000_000; i++)
297-
// myDates[i] = DateTime.Now.AddSeconds(i);
298-
299-
// await using var context = new Context();
300-
// var result = Enumerable.Range(0, 1000_000)
301-
// .Select((x, index) => new SensorInformation
302-
// {
303-
// Id = index + 1,
304-
// Value1 = (int)(120 + 5 * Math.Cos(index * 0.0001d) + random.Next(0, 1)),
305-
// Value2 = (int)(100 + 5 * Math.Sin(index * 0.001d) + random.Next(0, 3)),
306-
// Value3 = (int)(80 + 5 * Math.Cos(index * 0.001d) + random.Next(0, 4)),
307-
// Value4 = (int)(60 + 5 * Math.Cos(index * 0.0003d) + random.Next(0, 2)),
308-
// Value5 = (int)(40 + 5 * Math.Asin(index * 0.00001d) + random.Next(0, 1)),
309-
// Value6 = (int)(20 + 5 * Math.Acos(index * 0.0001d) + random.Next(0, 2)),
310-
// DataTime = myDates[index].Round(TimeSpan.FromSeconds(1)),
311-
// SensorGroupId = _groupSensorId[0]
312-
// }).ToList();
313-
314-
// await context.SensorInformations.AddRangeAsync(result);
315-
// await context.SaveChangesAsync();
316-
//}
317-
313+
318314
private void GetSelectMode()
319315
{
320316
using var context = new Context();
@@ -348,7 +344,8 @@ private void SetDataFromSetting(ushort data)
348344
: 0;
349345

350346
LimitRelayItems[i].TemperatureThreshold1 = data.IsBitSet(i)
351-
? _deviceConnector.SettingDevice.TemperatureThreshold[0] - LimitRelayItems[i].HysteresisThreshold1 / 2d
347+
? _deviceConnector.SettingDevice.TemperatureThreshold[0] -
348+
LimitRelayItems[i].HysteresisThreshold1 / 2d
352349
: 0;
353350
}
354351
break;
@@ -358,11 +355,13 @@ private void SetDataFromSetting(ushort data)
358355
for (var i = 0; i < LimitRelayItems.Count; i++)
359356
{
360357
LimitRelayItems[i].HysteresisThreshold2 = data.IsBitSet(i)
361-
? ((_deviceConnector.SettingDevice.TemperatureHysteresis & 0b1111_1111_0000_0000) >> 8) * 2d
358+
? ((_deviceConnector.SettingDevice.TemperatureHysteresis & 0b1111_1111_0000_0000) >>
359+
8) * 2d
362360
: 0;
363361

364362
LimitRelayItems[i].TemperatureThreshold2 = data.IsBitSet(i)
365-
? _deviceConnector.SettingDevice.TemperatureThreshold[1] - LimitRelayItems[i].HysteresisThreshold2 / 2d
363+
? _deviceConnector.SettingDevice.TemperatureThreshold[1] -
364+
LimitRelayItems[i].HysteresisThreshold2 / 2d
366365
: 0;
367366
}
368367
break;

0 commit comments

Comments
 (0)