Skip to content

Commit

Permalink
The alternative view of displaying data from sensors has been changed…
Browse files Browse the repository at this point in the history
…, added app settings
  • Loading branch information
Georgy Levchenko committed Jun 27, 2021
1 parent 8a62663 commit 7f05048
Show file tree
Hide file tree
Showing 25 changed files with 1,655 additions and 171 deletions.
30 changes: 30 additions & 0 deletions SmartThermo.sln
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SmartThermo.Modules.Dialog.
EndProject
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}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SmartThermo.Modules.SettingsApplication", "src\Modules\SmartThermo.Modules.SettingsApplication\SmartThermo.Modules.SettingsApplication.csproj", "{F5819333-AE0F-477C-BED0-D03FCED73AAC}"
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
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -177,6 +181,30 @@ Global
{A80C5CCC-2D81-42E9-ABAE-F179035B814B}.Release|x64.Build.0 = Release|Any CPU
{A80C5CCC-2D81-42E9-ABAE-F179035B814B}.Release|x86.ActiveCfg = Release|Any CPU
{A80C5CCC-2D81-42E9-ABAE-F179035B814B}.Release|x86.Build.0 = Release|Any CPU
{F5819333-AE0F-477C-BED0-D03FCED73AAC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F5819333-AE0F-477C-BED0-D03FCED73AAC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F5819333-AE0F-477C-BED0-D03FCED73AAC}.Debug|x64.ActiveCfg = Debug|Any CPU
{F5819333-AE0F-477C-BED0-D03FCED73AAC}.Debug|x64.Build.0 = Debug|Any CPU
{F5819333-AE0F-477C-BED0-D03FCED73AAC}.Debug|x86.ActiveCfg = Debug|Any CPU
{F5819333-AE0F-477C-BED0-D03FCED73AAC}.Debug|x86.Build.0 = Debug|Any CPU
{F5819333-AE0F-477C-BED0-D03FCED73AAC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F5819333-AE0F-477C-BED0-D03FCED73AAC}.Release|Any CPU.Build.0 = Release|Any CPU
{F5819333-AE0F-477C-BED0-D03FCED73AAC}.Release|x64.ActiveCfg = Release|Any CPU
{F5819333-AE0F-477C-BED0-D03FCED73AAC}.Release|x64.Build.0 = Release|Any CPU
{F5819333-AE0F-477C-BED0-D03FCED73AAC}.Release|x86.ActiveCfg = Release|Any CPU
{F5819333-AE0F-477C-BED0-D03FCED73AAC}.Release|x86.Build.0 = Release|Any CPU
{5F91B1F0-DD19-491E-839B-3187BCCE41E6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5F91B1F0-DD19-491E-839B-3187BCCE41E6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5F91B1F0-DD19-491E-839B-3187BCCE41E6}.Debug|x64.ActiveCfg = Debug|Any CPU
{5F91B1F0-DD19-491E-839B-3187BCCE41E6}.Debug|x64.Build.0 = Debug|Any CPU
{5F91B1F0-DD19-491E-839B-3187BCCE41E6}.Debug|x86.ActiveCfg = Debug|Any CPU
{5F91B1F0-DD19-491E-839B-3187BCCE41E6}.Debug|x86.Build.0 = Debug|Any CPU
{5F91B1F0-DD19-491E-839B-3187BCCE41E6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5F91B1F0-DD19-491E-839B-3187BCCE41E6}.Release|Any CPU.Build.0 = Release|Any CPU
{5F91B1F0-DD19-491E-839B-3187BCCE41E6}.Release|x64.ActiveCfg = Release|Any CPU
{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
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -196,6 +224,8 @@ Global
{707F9F26-F30A-4EE6-B80C-FB4D94428467} = {87BD6B81-EF1B-478E-BBB4-A0A40B1B6119}
{E8C8E4FD-80F0-4443-904A-4BF78DCA74DE} = {87BD6B81-EF1B-478E-BBB4-A0A40B1B6119}
{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}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B511AF63-B8BD-4505-A278-DA0B0CCAA472}
Expand Down
8 changes: 7 additions & 1 deletion src/DataAccess/SmartThermo.DataAccess.Sqlite/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ public class Context : DbContext

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Setting>().HasData(new Setting { Id = 1 });
modelBuilder.Entity<Setting>().HasData(new Setting
{
Id = 1,
TimeBeforeWarning = 15,
TimeBeforeOffline = 60,
IsWriteToDatabase = true
});

modelBuilder.Entity<SelectMode>().HasData(
new SelectMode { Id = 1, SettingId = 1, Stage = false },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,11 @@ public class Setting : BaseModel
public string PortNameSelected { get; set; }

public string BaudRateSelected { get; set; }

public int TimeBeforeWarning { get; set; }

public int TimeBeforeOffline { get; set; }

public bool IsWriteToDatabase { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public AnalyticsWindowViewModel(INotifications notifications, IDialogService dia
_notifications = notifications;
_dialogService = dialogService;

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

SelectCountRecord = Enumerable.Range(0, 6)
.Select(x => new ItemDescriptor<int>($"{Math.Pow(10, x + 2)} записей", x))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
Grid.Column="1"
Margin="0,16,0,0"
Background="{DynamicResource SystemControlBackgroundBaseMediumLowBrush}" />
<Grid Grid.Column="2" Margin="16,8,0,8">
<Grid Grid.Column="2" Margin="16,8,0,0">
<ui:SimpleStackPanel Spacing="16">
<GroupBox Padding="0,-8,0,0" Header="Сессии">
<ui:SimpleStackPanel Orientation="Vertical" Spacing="8">
Expand Down Expand Up @@ -214,10 +214,7 @@
</StackPanel>
</ui:SimpleStackPanel>
</GroupBox>
<Button
Margin="0,8,0,0"
HorizontalAlignment="Right"
Command="{Binding GetSensorDataCommand}">
<Button HorizontalAlignment="Right" Command="{Binding GetSensorDataCommand}">
<ui:SimpleStackPanel Orientation="Horizontal" Spacing="8">

<ui:PathIcon
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace SmartThermo.Modules.DataViewer.Models
{
public class MeasureData
public struct MeasureData
{
public DateTime DateTime { get; set; }

Expand Down
11 changes: 11 additions & 0 deletions src/Modules/SmartThermo.Modules.DataViewer/Models/SensorsData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using SmartThermo.Core.Enums;

namespace SmartThermo.Modules.DataViewer.Models
{
public struct SensorsData
{
public int? Temperature { get; set; }

public StatusSensor StatusSensor { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace SmartThermo.Modules.DataViewer.Models
{
public class SensorsEther
public struct SensorsEther
{
public int Id { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\DataAccess\SmartThermo.DataAccess.Sqlite\SmartThermo.DataAccess.Sqlite.csproj" />
<ProjectReference Include="..\..\Services\SmartThermo.Services.Configuration\SmartThermo.Services.Configuration.csproj" />
<ProjectReference Include="..\..\SmartThermo.Core\SmartThermo.Core.csproj" />
<ProjectReference Include="..\SmartThermo.Modules.Dialog.SettingsDevice\SmartThermo.Modules.Dialog.SettingsDevice.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
using System.Windows;
using System.Windows.Data;
using System.Windows.Threading;
using SmartThermo.Core.Enums;
using SmartThermo.Services.Configuration;

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

private static readonly object Lock = new object();

private readonly IConfiguration _configuration;
private readonly IDeviceConnector _deviceConnector;
private readonly List<int> _groupSensorId = new List<int>();
private ObservableCollection<LimitRelay> _limitRelayItems = new ObservableCollection<LimitRelay>();
Expand All @@ -47,7 +50,7 @@ public class LoadDataViewerWindowViewModel : ViewModelBase, IRegionMemberLifetim
private List<double> _axisYMax = new List<double>();
private List<double> _axisYMin = new List<double>();
private List<bool> _selectMode = new List<bool>();
private List<int?> _temperature = new List<int?>();
private List<SensorsData> _sensorsData = new List<SensorsData>();

#endregion

Expand Down Expand Up @@ -105,10 +108,10 @@ public List<bool> SelectMode
set => SetProperty(ref _selectMode, value);
}

public List<int?> Temperature
public List<SensorsData> SensorsData
{
get => _temperature;
set => SetProperty(ref _temperature, value);
get => _sensorsData;
set => SetProperty(ref _sensorsData, value);
}

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

#region Constructor

public LoadDataViewerWindowViewModel(IDeviceConnector deviceConnector, INotifications notifications)
public LoadDataViewerWindowViewModel(
IConfiguration configuration,
IDeviceConnector deviceConnector,
INotifications notifications)
{
_configuration = configuration;
_deviceConnector = deviceConnector;
_deviceConnector.SettingDeviceChanged += DeviceConnector_SettingDeviceChanged;
_deviceConnector.StatusConnectChanged += DeviceConnector_StatusConnectChanged;
Expand Down Expand Up @@ -195,24 +202,27 @@ private void DeviceConnector_SettingDeviceChanged(object sender, SettingDeviceEv

private void DeviceConnector_RegistersRequested(object sender, List<SensorInfoEventArgs> sensorData)
{
var sensorEhterItems = sensorData.Where(x => x.IsAir)
var sensorEtherItems = sensorData.Where(x => x.IsAir)
.Select(x => new SensorsEther
{
Id = x.Number,
Time = x.TimeLastBroadcast
})
.ToList();
SensorsEtherItems.Clear();
SensorsEtherItems.AddRange(sensorEhterItems);

_temperature.Clear();
_temperature.AddRange(sensorData
.Select(x => x.IsAir ? (int?)x.Temperature : null)
SensorsEtherItems.AddRange(sensorEtherItems);

_sensorsData.Clear();
_sensorsData.AddRange(sensorData
.Select(x => new SensorsData
{
Temperature = x.IsAir ? (int?)x.Temperature : null,
StatusSensor = x.IsAir ? GetStatusSensor(x.TimeLastBroadcast) : StatusSensor.Offline,
})
.ToList());
RaisePropertyChanged(nameof(Temperature));
RaisePropertyChanged(nameof(SensorsData));

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

SaveDataToDatabaseAsync(now, sensorData);
if (_configuration.IsWriteToDatabase)
SaveDataToDatabaseAsync(now, sensorData);
}

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,
_ => throw new ArgumentOutOfRangeException(nameof(time), time, null)
};
}

private async void SaveDataToDatabaseAsync(DateTime time, IReadOnlyList<SensorInfoEventArgs> sensorData)
Expand Down Expand Up @@ -268,8 +290,12 @@ private void InitCharts()
ChartValues.AddRange(Enumerable.Range(0, 36)
.Select(x => new ChartValues<MeasureData>())
.ToList());
Temperature.AddRange(Enumerable.Range(0,36)
.Select(x => default(int?))
SensorsData.AddRange(Enumerable.Range(0,36)
.Select(x => new SensorsData()
{
Temperature = default ,
StatusSensor = StatusSensor.Offline
})
.ToList());
LimitRelayItems.AddRange(Enumerable.Range(0, 6)
.Select(x => new LimitRelay())
Expand All @@ -283,38 +309,8 @@ private void InitCharts()
SetAxisXLimits(DateTime.Now);
SetAxisYLimits();
SetRelayLimits();

//LoadTestDataAsync();
}

//private async Task LoadTestDataAsync()
//{
// await Task.Delay(1000);
// var random = new Random();

// var myDates = new DateTime[1000_000];
// for (var i = 0; i < 1000_000; i++)
// myDates[i] = DateTime.Now.AddSeconds(i);

// await using var context = new Context();
// var result = Enumerable.Range(0, 1000_000)
// .Select((x, index) => new SensorInformation
// {
// Id = index + 1,
// Value1 = (int)(120 + 5 * Math.Cos(index * 0.0001d) + random.Next(0, 1)),
// Value2 = (int)(100 + 5 * Math.Sin(index * 0.001d) + random.Next(0, 3)),
// Value3 = (int)(80 + 5 * Math.Cos(index * 0.001d) + random.Next(0, 4)),
// Value4 = (int)(60 + 5 * Math.Cos(index * 0.0003d) + random.Next(0, 2)),
// Value5 = (int)(40 + 5 * Math.Asin(index * 0.00001d) + random.Next(0, 1)),
// Value6 = (int)(20 + 5 * Math.Acos(index * 0.0001d) + random.Next(0, 2)),
// DataTime = myDates[index].Round(TimeSpan.FromSeconds(1)),
// SensorGroupId = _groupSensorId[0]
// }).ToList();

// await context.SensorInformations.AddRangeAsync(result);
// await context.SaveChangesAsync();
//}


private void GetSelectMode()
{
using var context = new Context();
Expand Down Expand Up @@ -348,7 +344,8 @@ private void SetDataFromSetting(ushort data)
: 0;

LimitRelayItems[i].TemperatureThreshold1 = data.IsBitSet(i)
? _deviceConnector.SettingDevice.TemperatureThreshold[0] - LimitRelayItems[i].HysteresisThreshold1 / 2d
? _deviceConnector.SettingDevice.TemperatureThreshold[0] -
LimitRelayItems[i].HysteresisThreshold1 / 2d
: 0;
}
break;
Expand All @@ -358,11 +355,13 @@ private void SetDataFromSetting(ushort data)
for (var i = 0; i < LimitRelayItems.Count; i++)
{
LimitRelayItems[i].HysteresisThreshold2 = data.IsBitSet(i)
? ((_deviceConnector.SettingDevice.TemperatureHysteresis & 0b1111_1111_0000_0000) >> 8) * 2d
? ((_deviceConnector.SettingDevice.TemperatureHysteresis & 0b1111_1111_0000_0000) >>
8) * 2d
: 0;

LimitRelayItems[i].TemperatureThreshold2 = data.IsBitSet(i)
? _deviceConnector.SettingDevice.TemperatureThreshold[1] - LimitRelayItems[i].HysteresisThreshold2 / 2d
? _deviceConnector.SettingDevice.TemperatureThreshold[1] -
LimitRelayItems[i].HysteresisThreshold2 / 2d
: 0;
}
break;
Expand Down
Loading

0 comments on commit 7f05048

Please sign in to comment.