-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GeothermalResearchInstitute] Add DeviceMetricHistoryExportView.
- Loading branch information
Showing
11 changed files
with
295 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
...nstitute/GeothermalResearchInstitute.Wpf/ViewModels/DeviceMetricHistoryExportViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// <copyright file="DeviceMetricHistoryExportViewModel.cs" company="Shuai Zhang"> | ||
// Copyright Shuai Zhang. All rights reserved. | ||
// Licensed under the GPLv3 license. See LICENSE file in the project root for full license information. | ||
// </copyright> | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Windows.Forms; | ||
using GeothermalResearchInstitute.v2; | ||
using Google.Protobuf.WellKnownTypes; | ||
using Prism.Commands; | ||
using Prism.Mvvm; | ||
|
||
namespace GeothermalResearchInstitute.Wpf.ViewModels | ||
{ | ||
[System.Diagnostics.CodeAnalysis.SuppressMessage( | ||
"Performance", "CA1822", Justification = "ViewModel.")] | ||
public class DeviceMetricHistoryExportViewModel : BindableBase | ||
{ | ||
private static readonly TimeSpan[] CandidateExportTimeSpans = new TimeSpan[] | ||
{ | ||
TimeSpan.FromSeconds(1), | ||
TimeSpan.FromSeconds(5), | ||
TimeSpan.FromSeconds(15), | ||
}; | ||
|
||
private readonly DeviceService.DeviceServiceClient client; | ||
private ViewModelContext viewModelContext; | ||
private DateTime startDateTime = DateTime.Now; | ||
private DateTime endDateTime = DateTime.Now; | ||
private TimeSpan selectedTimeSpan = CandidateExportTimeSpans[0]; | ||
|
||
public DeviceMetricHistoryExportViewModel(DeviceService.DeviceServiceClient client) | ||
{ | ||
this.client = client ?? throw new ArgumentNullException(nameof(client)); | ||
this.ExportCommand = new DelegateCommand(this.ExecuteExport, this.CanExport); | ||
} | ||
|
||
public ICollection<TimeSpan> ExportTimeSpans => CandidateExportTimeSpans; | ||
|
||
public ViewModelContext ViewModelContext | ||
{ | ||
get => this.viewModelContext; | ||
set => this.SetProperty(ref this.viewModelContext, value); | ||
} | ||
|
||
public DateTime StartDateTime | ||
{ | ||
get => this.startDateTime; | ||
set => this.SetProperty(ref this.startDateTime, value); | ||
} | ||
|
||
public DateTime EndDateTime | ||
{ | ||
get => this.endDateTime; | ||
set => this.SetProperty(ref this.endDateTime, value); | ||
} | ||
|
||
public TimeSpan SelectedTimeSpan | ||
{ | ||
get => this.selectedTimeSpan; | ||
set => this.SetProperty(ref this.selectedTimeSpan, value); | ||
} | ||
|
||
public DelegateCommand ExportCommand { get; } | ||
|
||
private bool CanExport() => this.StartDateTime < this.EndDateTime; | ||
|
||
private async void ExecuteExport() | ||
{ | ||
var metrics = new List<Metric>(); | ||
string nextPageToken = null; | ||
while (true) | ||
{ | ||
var request = new ListMetricsRequest | ||
{ | ||
DeviceId = this.ViewModelContext.SelectedDevice.Id, | ||
StartTime = this.StartDateTime.ToUniversalTime().ToTimestamp(), | ||
EndTime = this.EndDateTime.ToUniversalTime().ToTimestamp(), | ||
PageSize = 200, | ||
}; | ||
if (nextPageToken != null) | ||
{ | ||
request.PageToken = nextPageToken; | ||
} | ||
|
||
ListMetricsResponse response = await this.client.ListMetricsAsync( | ||
request, | ||
deadline: DateTime.Now.AddSeconds(5)); | ||
nextPageToken = response.NextPageToken; | ||
|
||
if (response.Metrics.Count == 0) | ||
{ | ||
break; | ||
} | ||
|
||
if (response.Metrics.Any(m => m.CreateTime.ToDateTimeOffset() < this.StartDateTime)) | ||
{ | ||
metrics.AddRange(response.Metrics.Where( | ||
m => m.CreateTime.ToDateTimeOffset() >= this.StartDateTime)); | ||
break; | ||
} | ||
else | ||
{ | ||
metrics.AddRange(response.Metrics); | ||
} | ||
} | ||
|
||
using var saveFileDialog = new SaveFileDialog | ||
{ | ||
Filter = "逗号分隔文件(*.csv)|*.csv", | ||
AddExtension = true, | ||
}; | ||
|
||
if (saveFileDialog.ShowDialog() == DialogResult.OK) | ||
{ | ||
using var sw = new StreamWriter( | ||
File.Open(saveFileDialog.FileName, FileMode.Create, FileAccess.Write, FileShare.Read), | ||
Encoding.UTF8); | ||
|
||
// TODO: Write it. | ||
foreach (var m in metrics) | ||
{ | ||
await sw.WriteLineAsync(m.ToString()).ConfigureAwait(true); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
...esearchInstitute/GeothermalResearchInstitute.Wpf/Views/DeviceMetricHistoryExportView.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<UserControl | ||
x:Class="GeothermalResearchInstitute.Wpf.Views.DeviceMetricHistoryExportView" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:local="clr-namespace:GeothermalResearchInstitute.Wpf.Views" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:prism="http://prismlibrary.com/" | ||
xmlns:xwtk="clr-namespace:Xceed.Wpf.Toolkit;assembly=Xceed.Wpf.Toolkit" | ||
d:DesignHeight="360" | ||
d:DesignWidth="600" | ||
prism:ViewModelLocator.AutoWireViewModel="True" | ||
Loaded="DeviceMetricHistoryExportView_Loaded" | ||
mc:Ignorable="d"> | ||
<Grid> | ||
<Grid.ColumnDefinitions> | ||
<ColumnDefinition Width="*" /> | ||
<ColumnDefinition Width="3*" /> | ||
<ColumnDefinition Width="*" /> | ||
</Grid.ColumnDefinitions> | ||
<Grid.RowDefinitions> | ||
<RowDefinition Height="*" /> | ||
<RowDefinition Height="3*" /> | ||
<RowDefinition Height="*" /> | ||
</Grid.RowDefinitions> | ||
<DockPanel Grid.Row="1" Grid.Column="1"> | ||
<Button | ||
HorizontalAlignment="Right" | ||
Command="{Binding ExportCommand}" | ||
Content="确定" | ||
DockPanel.Dock="Bottom" | ||
IsDefault="True" | ||
Style="{StaticResource ButtonBaseStyle}" /> | ||
<Grid> | ||
<Grid.RowDefinitions> | ||
<RowDefinition /> | ||
<RowDefinition /> | ||
<RowDefinition /> | ||
</Grid.RowDefinitions> | ||
<Grid.ColumnDefinitions> | ||
<ColumnDefinition /> | ||
<ColumnDefinition /> | ||
</Grid.ColumnDefinitions> | ||
<Label | ||
Grid.Row="0" | ||
Grid.Column="0" | ||
VerticalAlignment="Center" | ||
Content="起始时间:" /> | ||
<xwtk:DateTimePicker | ||
Grid.Row="0" | ||
Grid.Column="1" | ||
VerticalAlignment="Center" | ||
ShowButtonSpinner="False" | ||
Value="{Binding StartDateTime}" /> | ||
<Label | ||
Grid.Row="1" | ||
Grid.Column="0" | ||
VerticalAlignment="Center" | ||
Content="结束时间:" /> | ||
<xwtk:DateTimePicker | ||
Grid.Row="1" | ||
Grid.Column="1" | ||
VerticalAlignment="Center" | ||
ShowButtonSpinner="False" | ||
Value="{Binding EndDateTime}" /> | ||
<Label | ||
Grid.Row="2" | ||
Grid.Column="0" | ||
VerticalAlignment="Center" | ||
Content="时间间隔:" /> | ||
<ComboBox | ||
Grid.Row="2" | ||
Grid.Column="1" | ||
VerticalAlignment="Center" | ||
ItemsSource="{Binding ExportTimeSpans}" | ||
SelectedItem="{Binding SelectedTimeSpan}"> | ||
<ComboBox.ItemTemplate> | ||
<DataTemplate> | ||
<StackPanel Orientation="Horizontal"> | ||
<TextBlock Text="{Binding Path=TotalSeconds, StringFormat={}{0:# 秒}}" /> | ||
</StackPanel> | ||
</DataTemplate> | ||
</ComboBox.ItemTemplate> | ||
</ComboBox> | ||
</Grid> | ||
</DockPanel> | ||
</Grid> | ||
</UserControl> |
39 changes: 39 additions & 0 deletions
39
...archInstitute/GeothermalResearchInstitute.Wpf/Views/DeviceMetricHistoryExportView.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// <copyright file="DeviceMetricHistoryExportView.xaml.cs" company="Shuai Zhang"> | ||
// Copyright Shuai Zhang. All rights reserved. | ||
// Licensed under the GPLv3 license. See LICENSE file in the project root for full license information. | ||
// </copyright> | ||
|
||
using System.Windows; | ||
using System.Windows.Controls; | ||
using GeothermalResearchInstitute.Wpf.ViewModels; | ||
using Prism.Common; | ||
using Prism.Regions; | ||
|
||
namespace GeothermalResearchInstitute.Wpf.Views | ||
{ | ||
public partial class DeviceMetricHistoryExportView : UserControl | ||
{ | ||
public DeviceMetricHistoryExportView() | ||
{ | ||
this.InitializeComponent(); | ||
RegionContext.GetObservableContext(this).PropertyChanged += this.RegionContext_PropertyChanged; | ||
} | ||
|
||
private DeviceMetricHistoryExportViewModel ViewModel => this.DataContext as DeviceMetricHistoryExportViewModel; | ||
|
||
private void RegionContext_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) | ||
{ | ||
var context = (ObservableObject<object>)sender; | ||
var viewModelContext = (ViewModelContext)context.Value; | ||
this.ViewModel.ViewModelContext = viewModelContext; | ||
} | ||
|
||
private void DeviceMetricHistoryExportView_Loaded(object sender, RoutedEventArgs e) | ||
{ | ||
ViewModelContext viewModelContext = this.ViewModel.ViewModelContext; | ||
viewModelContext.UserBarVisibility = Visibility.Visible; | ||
viewModelContext.BannerVisibility = Visibility.Visible; | ||
viewModelContext.Title = "导出历史数据"; // TODO: From resource. | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.