Skip to content

Commit 1956702

Browse files
author
Georgy Levchenko
committed
Added ability auto connect to device.
1 parent 7f05048 commit 1956702

File tree

24 files changed

+432
-47
lines changed

24 files changed

+432
-47
lines changed

SmartThermo.sln

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SmartThermo.Modules.Setting
3939
EndProject
4040
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SmartThermo.Services.Configuration", "src\Services\SmartThermo.Services.Configuration\SmartThermo.Services.Configuration.csproj", "{5F91B1F0-DD19-491E-839B-3187BCCE41E6}"
4141
EndProject
42+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SmartThermo.Services.SerialPortObserver", "src\Services\SmartThermo.Services.SerialPortObserver\SmartThermo.Services.SerialPortObserver.csproj", "{C99966A9-E8F8-4C67-9363-365BCB4A84DD}"
43+
EndProject
4244
Global
4345
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4446
Debug|Any CPU = Debug|Any CPU
@@ -205,6 +207,18 @@ Global
205207
{5F91B1F0-DD19-491E-839B-3187BCCE41E6}.Release|x64.Build.0 = Release|Any CPU
206208
{5F91B1F0-DD19-491E-839B-3187BCCE41E6}.Release|x86.ActiveCfg = Release|Any CPU
207209
{5F91B1F0-DD19-491E-839B-3187BCCE41E6}.Release|x86.Build.0 = Release|Any CPU
210+
{C99966A9-E8F8-4C67-9363-365BCB4A84DD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
211+
{C99966A9-E8F8-4C67-9363-365BCB4A84DD}.Debug|Any CPU.Build.0 = Debug|Any CPU
212+
{C99966A9-E8F8-4C67-9363-365BCB4A84DD}.Debug|x64.ActiveCfg = Debug|Any CPU
213+
{C99966A9-E8F8-4C67-9363-365BCB4A84DD}.Debug|x64.Build.0 = Debug|Any CPU
214+
{C99966A9-E8F8-4C67-9363-365BCB4A84DD}.Debug|x86.ActiveCfg = Debug|Any CPU
215+
{C99966A9-E8F8-4C67-9363-365BCB4A84DD}.Debug|x86.Build.0 = Debug|Any CPU
216+
{C99966A9-E8F8-4C67-9363-365BCB4A84DD}.Release|Any CPU.ActiveCfg = Release|Any CPU
217+
{C99966A9-E8F8-4C67-9363-365BCB4A84DD}.Release|Any CPU.Build.0 = Release|Any CPU
218+
{C99966A9-E8F8-4C67-9363-365BCB4A84DD}.Release|x64.ActiveCfg = Release|Any CPU
219+
{C99966A9-E8F8-4C67-9363-365BCB4A84DD}.Release|x64.Build.0 = Release|Any CPU
220+
{C99966A9-E8F8-4C67-9363-365BCB4A84DD}.Release|x86.ActiveCfg = Release|Any CPU
221+
{C99966A9-E8F8-4C67-9363-365BCB4A84DD}.Release|x86.Build.0 = Release|Any CPU
208222
EndGlobalSection
209223
GlobalSection(SolutionProperties) = preSolution
210224
HideSolutionNode = FALSE
@@ -226,6 +240,7 @@ Global
226240
{A80C5CCC-2D81-42E9-ABAE-F179035B814B} = {D7FE162C-D6BC-4F89-8DF9-1AEF427FA63B}
227241
{F5819333-AE0F-477C-BED0-D03FCED73AAC} = {87BD6B81-EF1B-478E-BBB4-A0A40B1B6119}
228242
{5F91B1F0-DD19-491E-839B-3187BCCE41E6} = {D1175F74-F3B3-49F2-A1DB-74D1E10EDFBC}
243+
{C99966A9-E8F8-4C67-9363-365BCB4A84DD} = {D1175F74-F3B3-49F2-A1DB-74D1E10EDFBC}
229244
EndGlobalSection
230245
GlobalSection(ExtensibilityGlobals) = postSolution
231246
SolutionGuid = {B511AF63-B8BD-4505-A278-DA0B0CCAA472}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public class Setting : BaseModel
1616
public int TimeBeforeWarning { get; set; }
1717

1818
public int TimeBeforeOffline { get; set; }
19+
20+
public bool IsAutoConnect { get; set; }
1921

2022
public bool IsWriteToDatabase { get; set; }
2123
}
Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Prism.Regions;
1+
using System.Windows;
2+
using Prism.Regions;
23
using SmartThermo.Core;
34
using SmartThermo.Core.Mvvm;
45
using SmartThermo.Services.DeviceConnector;
@@ -8,15 +9,25 @@ namespace SmartThermo.Modules.DataViewer.ViewModels
89
{
910
public class DataViewerWindowViewModel : ViewModelBase
1011
{
12+
private readonly IDeviceConnector _deviceConnector;
13+
private readonly IRegionManager _regionManager;
14+
1115
public DataViewerWindowViewModel(IDeviceConnector deviceConnector, IRegionManager regionManager)
1216
{
13-
deviceConnector.StatusConnectChanged += (_, connect) =>
17+
_regionManager = regionManager;
18+
_deviceConnector = deviceConnector;
19+
_deviceConnector.StatusConnectChanged += OnStatusConnectChanged;
20+
}
21+
22+
private void OnStatusConnectChanged(object stage, StatusConnect args)
23+
{
24+
Application.Current.Dispatcher.Invoke(() =>
1425
{
15-
regionManager.RequestNavigate(RegionNames.DataViewerContent,
16-
deviceConnector.StatusConnect == StatusConnect.Connected
26+
_regionManager.RequestNavigate(RegionNames.DataViewerContent,
27+
_deviceConnector.StatusConnect == StatusConnect.Connected
1728
? "LoadDataViewerWindow"
1829
: "NoLoadDataViewerWindow");
19-
};
30+
});
2031
}
2132
}
2233
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,9 @@ private StatusSensor GetStatusSensor(int time)
248248
{
249249
return time switch
250250
{
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,
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,
254254
_ => throw new ArgumentOutOfRangeException(nameof(time), time, null)
255255
};
256256
}

src/Modules/SmartThermo.Modules.SettingsApplication/SmartThermo.Modules.SettingsApplication.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
</ItemGroup>
2121
<ItemGroup>
2222
<ProjectReference Include="..\..\Services\SmartThermo.Services.Configuration\SmartThermo.Services.Configuration.csproj" />
23+
<ProjectReference Include="..\..\Services\SmartThermo.Services.SerialPortObserver\SmartThermo.Services.SerialPortObserver.csproj" />
2324
<ProjectReference Include="..\..\SmartThermo.Core\SmartThermo.Core.csproj" />
2425
</ItemGroup>
2526
</Project>

src/Modules/SmartThermo.Modules.SettingsApplication/ViewModels/SettingsApplicationWindowViewModel.cs

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Prism.Regions;
22
using SmartThermo.Core.Mvvm;
33
using SmartThermo.Services.Configuration;
4+
using SmartThermo.Services.SerialPortObserver;
45

56
namespace SmartThermo.Modules.SettingsApplication.ViewModels
67
{
@@ -10,15 +11,21 @@ namespace SmartThermo.Modules.SettingsApplication.ViewModels
1011
public class SettingsApplicationWindowViewModel : RegionViewModelBase
1112
{
1213
private readonly IConfiguration _configuration;
14+
private readonly ISerialPortObserver _serialPortObserver;
15+
private bool _isLoadView = true;
1316

1417
private int _timeBeforeWarning;
1518
private int _timeBeforeOffline;
1619
private bool _isWriteToDatabase;
20+
private bool _isAutoConnect;
1721

1822
/// <inheritdoc />
19-
public SettingsApplicationWindowViewModel(IConfiguration configuration)
23+
public SettingsApplicationWindowViewModel(
24+
IConfiguration configuration,
25+
ISerialPortObserver serialPortObserver)
2026
{
2127
_configuration = configuration;
28+
_serialPortObserver = serialPortObserver;
2229
}
2330

2431
/// <summary>
@@ -27,7 +34,11 @@ public SettingsApplicationWindowViewModel(IConfiguration configuration)
2734
public int TimeBeforeWarning
2835
{
2936
get => _timeBeforeWarning;
30-
set => SetProperty(ref _timeBeforeWarning, value);
37+
set
38+
{
39+
SetProperty(ref _timeBeforeWarning, value);
40+
SaveSetting();
41+
}
3142
}
3243

3344
/// <summary>
@@ -36,7 +47,25 @@ public int TimeBeforeWarning
3647
public int TimeBeforeOffline
3748
{
3849
get => _timeBeforeOffline;
39-
set => SetProperty(ref _timeBeforeOffline, value);
50+
set
51+
{
52+
SetProperty(ref _timeBeforeOffline, value);
53+
SaveSetting();
54+
}
55+
}
56+
57+
/// <summary>
58+
/// Автоподключение к прибору.
59+
/// </summary>
60+
public bool IsAutoConnect
61+
{
62+
get => _isAutoConnect;
63+
set
64+
{
65+
SetProperty(ref _isAutoConnect, value);
66+
ChangeStatusObserver(value);
67+
SaveSetting();
68+
}
4069
}
4170

4271
/// <summary>
@@ -45,22 +74,42 @@ public int TimeBeforeOffline
4574
public bool IsWriteToDatabase
4675
{
4776
get => _isWriteToDatabase;
48-
set => SetProperty(ref _isWriteToDatabase, value);
77+
set
78+
{
79+
SetProperty(ref _isWriteToDatabase, value);
80+
SaveSetting();
81+
}
4982
}
5083

5184
/// <inheritdoc />
5285
public override void OnNavigatedTo(NavigationContext navigationContext)
5386
{
87+
_isLoadView = true;
88+
5489
TimeBeforeWarning = _configuration.TimeBeforeWarning;
5590
TimeBeforeOffline = _configuration.TimeBeforeOffline;
91+
IsAutoConnect = _configuration.IsAutoConnect;
5692
IsWriteToDatabase = _configuration.IsWriteToDatabase;
93+
94+
_isLoadView = false;
5795
}
5896

59-
/// <inheritdoc />
60-
public override void OnNavigatedFrom(NavigationContext navigationContext)
97+
private void ChangeStatusObserver(bool status)
6198
{
99+
if (status)
100+
_serialPortObserver.Start();
101+
else
102+
_serialPortObserver.Stop();
103+
}
104+
105+
private void SaveSetting()
106+
{
107+
if (_isLoadView)
108+
return;
109+
62110
_configuration.TimeBeforeWarning = _timeBeforeWarning;
63111
_configuration.TimeBeforeOffline = _timeBeforeOffline;
112+
_configuration.IsAutoConnect = _isAutoConnect;
64113
_configuration.IsWriteToDatabase = _isWriteToDatabase;
65114
_configuration.SaveChangedAsync();
66115
}

src/Modules/SmartThermo.Modules.SettingsApplication/Views/SettingsApplicationWindow.xaml

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,37 +38,47 @@
3838
<StackPanel>
3939
<TextBlock Style="{StaticResource ComboBoxHeaderStyle}" Text="Время, после которого датчик переходит в режим ожидания" />
4040
<ui:NumberBox
41-
Value="{Binding TimeBeforeWarning}"
42-
HorizontalAlignment="Left"
4341
Width="180"
4442
Margin="0,0,0,12"
43+
HorizontalAlignment="Left"
4544
LargeChange="10"
4645
Maximum="30"
4746
Minimum="2"
4847
PlaceholderText="0 сек, [2-30]"
4948
SmallChange="1"
50-
SpinButtonPlacementMode="Inline"/>
49+
SpinButtonPlacementMode="Inline"
50+
Value="{Binding TimeBeforeWarning, UpdateSourceTrigger=PropertyChanged, Delay=1000}" />
5151

5252
<TextBlock Style="{StaticResource ComboBoxHeaderStyle}" Text="Время, после которого связь с датчиком потеряна" />
5353
<ui:NumberBox
54-
Value="{Binding TimeBeforeOffline}"
55-
HorizontalAlignment="Left"
5654
Width="180"
5755
Margin="0,0,0,12"
56+
HorizontalAlignment="Left"
5857
LargeChange="10"
5958
Maximum="120"
6059
Minimum="31"
6160
PlaceholderText="0 сек, [31-120]"
6261
SmallChange="1"
63-
SpinButtonPlacementMode="Inline"/>
62+
SpinButtonPlacementMode="Inline"
63+
Value="{Binding TimeBeforeOffline, UpdateSourceTrigger=PropertyChanged, Delay=1000}" />
64+
</StackPanel>
65+
</GroupBox>
66+
<GroupBox Header="Прибор">
67+
<StackPanel>
68+
<TextBlock Style="{StaticResource ComboBoxHeaderStyle}" Text="Автоподключение к прибору" />
69+
<ui:ToggleSwitch
70+
MaxWidth="100"
71+
IsOn="{Binding IsAutoConnect}"
72+
OffContent="Выкл"
73+
OnContent="Вкл" />
6474
</StackPanel>
6575
</GroupBox>
6676
<GroupBox Header="База данных">
6777
<StackPanel>
6878
<TextBlock Style="{StaticResource ComboBoxHeaderStyle}" Text="Вести запись полученных данных" />
6979
<ui:ToggleSwitch
70-
IsOn="{Binding IsWriteToDatabase}"
7180
MaxWidth="100"
81+
IsOn="{Binding IsWriteToDatabase}"
7282
OffContent="Выкл"
7383
OnContent="Вкл" />
7484
</StackPanel>

src/Services/SmartThermo.Services.Configuration/Configuration.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class Configuration : IConfiguration
1212
/// </summary>
1313
public Configuration()
1414
{
15-
InitValue();
15+
FillValue();
1616
}
1717

1818
/// <inheritdoc />
@@ -21,6 +21,9 @@ public Configuration()
2121
/// <inheritdoc />
2222
public int TimeBeforeOffline { get; set; }
2323

24+
/// <inheritdoc />
25+
public bool IsAutoConnect { get; set; }
26+
2427
/// <inheritdoc />
2528
public bool IsWriteToDatabase { get; set; }
2629

@@ -34,20 +37,22 @@ public async Task SaveChangedAsync()
3437
var data = context.Settings.First();
3538
data.TimeBeforeWarning = TimeBeforeWarning;
3639
data.TimeBeforeOffline = TimeBeforeOffline;
40+
data.IsAutoConnect = IsAutoConnect;
3741
data.IsWriteToDatabase = IsWriteToDatabase;
3842

3943
return context.SaveChanges();
4044
});
4145
await Task.WhenAll(saveDataTask);
4246
}
4347

44-
private void InitValue()
48+
private void FillValue()
4549
{
4650
using var context = new Context();
4751
var data = context.Settings.First();
4852

4953
TimeBeforeWarning = data.TimeBeforeWarning;
5054
TimeBeforeOffline = data.TimeBeforeOffline;
55+
IsAutoConnect = data.IsAutoConnect;
5156
IsWriteToDatabase = data.IsWriteToDatabase;
5257
}
5358
}

src/Services/SmartThermo.Services.Configuration/IConfiguration.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ public interface IConfiguration
1717
/// </summary>
1818
public int TimeBeforeOffline { get; set; }
1919

20+
/// <summary>
21+
/// Автоподключение к прибору.
22+
/// </summary>
23+
public bool IsAutoConnect { get; set; }
24+
2025
/// <summary>
2126
/// Вести запись полученных данных.
2227
/// </summary>
@@ -25,7 +30,6 @@ public interface IConfiguration
2530
/// <summary>
2631
/// Асинхронно сохраняет все изменения, внесенные в IConfiguration, в основную базу данных.
2732
/// </summary>
28-
/// <returns></returns>
2933
Task SaveChangedAsync();
3034
}
3135
}

0 commit comments

Comments
 (0)