Skip to content

Commit

Permalink
Added ability auto connect to device.
Browse files Browse the repository at this point in the history
  • Loading branch information
Georgy Levchenko committed Jun 29, 2021
1 parent 7f05048 commit 1956702
Show file tree
Hide file tree
Showing 24 changed files with 432 additions and 47 deletions.
15 changes: 15 additions & 0 deletions SmartThermo.sln
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SmartThermo.Modules.Setting
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SmartThermo.Services.Configuration", "src\Services\SmartThermo.Services.Configuration\SmartThermo.Services.Configuration.csproj", "{5F91B1F0-DD19-491E-839B-3187BCCE41E6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SmartThermo.Services.SerialPortObserver", "src\Services\SmartThermo.Services.SerialPortObserver\SmartThermo.Services.SerialPortObserver.csproj", "{C99966A9-E8F8-4C67-9363-365BCB4A84DD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -205,6 +207,18 @@ Global
{5F91B1F0-DD19-491E-839B-3187BCCE41E6}.Release|x64.Build.0 = Release|Any CPU
{5F91B1F0-DD19-491E-839B-3187BCCE41E6}.Release|x86.ActiveCfg = Release|Any CPU
{5F91B1F0-DD19-491E-839B-3187BCCE41E6}.Release|x86.Build.0 = Release|Any CPU
{C99966A9-E8F8-4C67-9363-365BCB4A84DD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C99966A9-E8F8-4C67-9363-365BCB4A84DD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C99966A9-E8F8-4C67-9363-365BCB4A84DD}.Debug|x64.ActiveCfg = Debug|Any CPU
{C99966A9-E8F8-4C67-9363-365BCB4A84DD}.Debug|x64.Build.0 = Debug|Any CPU
{C99966A9-E8F8-4C67-9363-365BCB4A84DD}.Debug|x86.ActiveCfg = Debug|Any CPU
{C99966A9-E8F8-4C67-9363-365BCB4A84DD}.Debug|x86.Build.0 = Debug|Any CPU
{C99966A9-E8F8-4C67-9363-365BCB4A84DD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C99966A9-E8F8-4C67-9363-365BCB4A84DD}.Release|Any CPU.Build.0 = Release|Any CPU
{C99966A9-E8F8-4C67-9363-365BCB4A84DD}.Release|x64.ActiveCfg = Release|Any CPU
{C99966A9-E8F8-4C67-9363-365BCB4A84DD}.Release|x64.Build.0 = Release|Any CPU
{C99966A9-E8F8-4C67-9363-365BCB4A84DD}.Release|x86.ActiveCfg = Release|Any CPU
{C99966A9-E8F8-4C67-9363-365BCB4A84DD}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -226,6 +240,7 @@ Global
{A80C5CCC-2D81-42E9-ABAE-F179035B814B} = {D7FE162C-D6BC-4F89-8DF9-1AEF427FA63B}
{F5819333-AE0F-477C-BED0-D03FCED73AAC} = {87BD6B81-EF1B-478E-BBB4-A0A40B1B6119}
{5F91B1F0-DD19-491E-839B-3187BCCE41E6} = {D1175F74-F3B3-49F2-A1DB-74D1E10EDFBC}
{C99966A9-E8F8-4C67-9363-365BCB4A84DD} = {D1175F74-F3B3-49F2-A1DB-74D1E10EDFBC}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B511AF63-B8BD-4505-A278-DA0B0CCAA472}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class Setting : BaseModel
public int TimeBeforeWarning { get; set; }

public int TimeBeforeOffline { get; set; }

public bool IsAutoConnect { get; set; }

public bool IsWriteToDatabase { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Prism.Regions;
using System.Windows;
using Prism.Regions;
using SmartThermo.Core;
using SmartThermo.Core.Mvvm;
using SmartThermo.Services.DeviceConnector;
Expand All @@ -8,15 +9,25 @@ namespace SmartThermo.Modules.DataViewer.ViewModels
{
public class DataViewerWindowViewModel : ViewModelBase
{
private readonly IDeviceConnector _deviceConnector;
private readonly IRegionManager _regionManager;

public DataViewerWindowViewModel(IDeviceConnector deviceConnector, IRegionManager regionManager)
{
deviceConnector.StatusConnectChanged += (_, connect) =>
_regionManager = regionManager;
_deviceConnector = deviceConnector;
_deviceConnector.StatusConnectChanged += OnStatusConnectChanged;
}

private void OnStatusConnectChanged(object stage, StatusConnect args)
{
Application.Current.Dispatcher.Invoke(() =>
{
regionManager.RequestNavigate(RegionNames.DataViewerContent,
deviceConnector.StatusConnect == StatusConnect.Connected
_regionManager.RequestNavigate(RegionNames.DataViewerContent,
_deviceConnector.StatusConnect == StatusConnect.Connected
? "LoadDataViewerWindow"
: "NoLoadDataViewerWindow");
};
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,9 @@ private StatusSensor GetStatusSensor(int time)
{
return time switch
{
var n when (n < _configuration.TimeBeforeWarning) => StatusSensor.Online,
var n when (n >= _configuration.TimeBeforeWarning) => StatusSensor.Wait,
var n when (n >= _configuration.TimeBeforeOffline) => StatusSensor.Offline,
var n when n < _configuration.TimeBeforeWarning => StatusSensor.Online,
var n when n >= _configuration.TimeBeforeWarning => StatusSensor.Wait,
var n when n >= _configuration.TimeBeforeOffline => StatusSensor.Offline,
_ => throw new ArgumentOutOfRangeException(nameof(time), time, null)
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Services\SmartThermo.Services.Configuration\SmartThermo.Services.Configuration.csproj" />
<ProjectReference Include="..\..\Services\SmartThermo.Services.SerialPortObserver\SmartThermo.Services.SerialPortObserver.csproj" />
<ProjectReference Include="..\..\SmartThermo.Core\SmartThermo.Core.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Prism.Regions;
using SmartThermo.Core.Mvvm;
using SmartThermo.Services.Configuration;
using SmartThermo.Services.SerialPortObserver;

namespace SmartThermo.Modules.SettingsApplication.ViewModels
{
Expand All @@ -10,15 +11,21 @@ namespace SmartThermo.Modules.SettingsApplication.ViewModels
public class SettingsApplicationWindowViewModel : RegionViewModelBase
{
private readonly IConfiguration _configuration;
private readonly ISerialPortObserver _serialPortObserver;
private bool _isLoadView = true;

private int _timeBeforeWarning;
private int _timeBeforeOffline;
private bool _isWriteToDatabase;
private bool _isAutoConnect;

/// <inheritdoc />
public SettingsApplicationWindowViewModel(IConfiguration configuration)
public SettingsApplicationWindowViewModel(
IConfiguration configuration,
ISerialPortObserver serialPortObserver)
{
_configuration = configuration;
_serialPortObserver = serialPortObserver;
}

/// <summary>
Expand All @@ -27,7 +34,11 @@ public SettingsApplicationWindowViewModel(IConfiguration configuration)
public int TimeBeforeWarning
{
get => _timeBeforeWarning;
set => SetProperty(ref _timeBeforeWarning, value);
set
{
SetProperty(ref _timeBeforeWarning, value);
SaveSetting();
}
}

/// <summary>
Expand All @@ -36,7 +47,25 @@ public int TimeBeforeWarning
public int TimeBeforeOffline
{
get => _timeBeforeOffline;
set => SetProperty(ref _timeBeforeOffline, value);
set
{
SetProperty(ref _timeBeforeOffline, value);
SaveSetting();
}
}

/// <summary>
/// Автоподключение к прибору.
/// </summary>
public bool IsAutoConnect
{
get => _isAutoConnect;
set
{
SetProperty(ref _isAutoConnect, value);
ChangeStatusObserver(value);
SaveSetting();
}
}

/// <summary>
Expand All @@ -45,22 +74,42 @@ public int TimeBeforeOffline
public bool IsWriteToDatabase
{
get => _isWriteToDatabase;
set => SetProperty(ref _isWriteToDatabase, value);
set
{
SetProperty(ref _isWriteToDatabase, value);
SaveSetting();
}
}

/// <inheritdoc />
public override void OnNavigatedTo(NavigationContext navigationContext)
{
_isLoadView = true;

TimeBeforeWarning = _configuration.TimeBeforeWarning;
TimeBeforeOffline = _configuration.TimeBeforeOffline;
IsAutoConnect = _configuration.IsAutoConnect;
IsWriteToDatabase = _configuration.IsWriteToDatabase;

_isLoadView = false;
}

/// <inheritdoc />
public override void OnNavigatedFrom(NavigationContext navigationContext)
private void ChangeStatusObserver(bool status)
{
if (status)
_serialPortObserver.Start();
else
_serialPortObserver.Stop();
}

private void SaveSetting()
{
if (_isLoadView)
return;

_configuration.TimeBeforeWarning = _timeBeforeWarning;
_configuration.TimeBeforeOffline = _timeBeforeOffline;
_configuration.IsAutoConnect = _isAutoConnect;
_configuration.IsWriteToDatabase = _isWriteToDatabase;
_configuration.SaveChangedAsync();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,37 +38,47 @@
<StackPanel>
<TextBlock Style="{StaticResource ComboBoxHeaderStyle}" Text="Время, после которого датчик переходит в режим ожидания" />
<ui:NumberBox
Value="{Binding TimeBeforeWarning}"
HorizontalAlignment="Left"
Width="180"
Margin="0,0,0,12"
HorizontalAlignment="Left"
LargeChange="10"
Maximum="30"
Minimum="2"
PlaceholderText="0 сек, [2-30]"
SmallChange="1"
SpinButtonPlacementMode="Inline"/>
SpinButtonPlacementMode="Inline"
Value="{Binding TimeBeforeWarning, UpdateSourceTrigger=PropertyChanged, Delay=1000}" />

<TextBlock Style="{StaticResource ComboBoxHeaderStyle}" Text="Время, после которого связь с датчиком потеряна" />
<ui:NumberBox
Value="{Binding TimeBeforeOffline}"
HorizontalAlignment="Left"
Width="180"
Margin="0,0,0,12"
HorizontalAlignment="Left"
LargeChange="10"
Maximum="120"
Minimum="31"
PlaceholderText="0 сек, [31-120]"
SmallChange="1"
SpinButtonPlacementMode="Inline"/>
SpinButtonPlacementMode="Inline"
Value="{Binding TimeBeforeOffline, UpdateSourceTrigger=PropertyChanged, Delay=1000}" />
</StackPanel>
</GroupBox>
<GroupBox Header="Прибор">
<StackPanel>
<TextBlock Style="{StaticResource ComboBoxHeaderStyle}" Text="Автоподключение к прибору" />
<ui:ToggleSwitch
MaxWidth="100"
IsOn="{Binding IsAutoConnect}"
OffContent="Выкл"
OnContent="Вкл" />
</StackPanel>
</GroupBox>
<GroupBox Header="База данных">
<StackPanel>
<TextBlock Style="{StaticResource ComboBoxHeaderStyle}" Text="Вести запись полученных данных" />
<ui:ToggleSwitch
IsOn="{Binding IsWriteToDatabase}"
MaxWidth="100"
IsOn="{Binding IsWriteToDatabase}"
OffContent="Выкл"
OnContent="Вкл" />
</StackPanel>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class Configuration : IConfiguration
/// </summary>
public Configuration()
{
InitValue();
FillValue();
}

/// <inheritdoc />
Expand All @@ -21,6 +21,9 @@ public Configuration()
/// <inheritdoc />
public int TimeBeforeOffline { get; set; }

/// <inheritdoc />
public bool IsAutoConnect { get; set; }

/// <inheritdoc />
public bool IsWriteToDatabase { get; set; }

Expand All @@ -34,20 +37,22 @@ public async Task SaveChangedAsync()
var data = context.Settings.First();
data.TimeBeforeWarning = TimeBeforeWarning;
data.TimeBeforeOffline = TimeBeforeOffline;
data.IsAutoConnect = IsAutoConnect;
data.IsWriteToDatabase = IsWriteToDatabase;

return context.SaveChanges();
});
await Task.WhenAll(saveDataTask);
}

private void InitValue()
private void FillValue()
{
using var context = new Context();
var data = context.Settings.First();

TimeBeforeWarning = data.TimeBeforeWarning;
TimeBeforeOffline = data.TimeBeforeOffline;
IsAutoConnect = data.IsAutoConnect;
IsWriteToDatabase = data.IsWriteToDatabase;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public interface IConfiguration
/// </summary>
public int TimeBeforeOffline { get; set; }

/// <summary>
/// Автоподключение к прибору.
/// </summary>
public bool IsAutoConnect { get; set; }

/// <summary>
/// Вести запись полученных данных.
/// </summary>
Expand All @@ -25,7 +30,6 @@ public interface IConfiguration
/// <summary>
/// Асинхронно сохраняет все изменения, внесенные в IConfiguration, в основную базу данных.
/// </summary>
/// <returns></returns>
Task SaveChangedAsync();
}
}
Loading

0 comments on commit 1956702

Please sign in to comment.