Skip to content

Commit

Permalink
[AB#38] Wpf project add options class.
Browse files Browse the repository at this point in the history
  • Loading branch information
hcoona committed Dec 15, 2019
1 parent 3fd3a7a commit 6747578
Show file tree
Hide file tree
Showing 18 changed files with 256 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -336,13 +336,17 @@ private PlcFrame CreateGetRunningParameterResponseFrame()

private PlcFrame CreateGetAlarmResponseFrame()
{
byte[] responseContent = new byte[0x06];
byte[] responseContent = new byte[0x0A];
responseContent[0] = (byte)(this.Alarm.LowFlowRate ? 0x01 : 0x00);
responseContent[1] = (byte)(this.Alarm.HighHeaterPressure ? 0x01 : 0x00);
responseContent[2] = (byte)(this.Alarm.LowHeaterPressure ? 0x01 : 0x00);
responseContent[3] = (byte)(this.Alarm.NoPower ? 0x01 : 0x00);
responseContent[4] = (byte)(this.Alarm.HeaterOverloadedBroken ? 0x01 : 0x00);
responseContent[5] = (byte)(this.Alarm.ElectricalHeaterBroken ? 0x01 : 0x00);
responseContent[6] = (byte)(this.Alarm.NoWater ? 0x01 : 0x00);
responseContent[7] = (byte)(this.Alarm.HighVoltage ? 0x01 : 0x00);
responseContent[8] = (byte)(this.Alarm.LowVoltage ? 0x01 : 0x00);
responseContent[9] = (byte)(this.Alarm.EmergencyStopped ? 0x01 : 0x00);

return PlcFrame.Create(
PlcMessageType.GetAlarmResponse,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Windows;
using GeothermalResearchInstitute.v2;
using GeothermalResearchInstitute.Wpf.Modules;
using GeothermalResearchInstitute.Wpf.Options;
using GeothermalResearchInstitute.Wpf.ViewModels;
using GeothermalResearchInstitute.Wpf.Views;
using Grpc.Core;
Expand All @@ -16,6 +17,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Unity;
Expand Down Expand Up @@ -70,14 +72,19 @@ protected override void OnStartup(StartupEventArgs e)
})
.ConfigureServices((context, builder) =>
{
IHostEnvironment env = context.HostingEnvironment;
IConfiguration config = context.Configuration;

builder.Configure<CoreOptions>(config.GetSection("core"));

builder.AddSingleton(serviceProvider =>
{
return new GrpcLoggerAdapter(
serviceProvider.GetRequiredService<ILoggerFactory>(),
serviceProvider.GetRequiredService<ILogger<ClientBase>>());
});

if (context.HostingEnvironment.IsDevelopment())
if (env.IsDevelopment())
{
#if DEBUG
builder.AddSingleton<
Expand All @@ -87,14 +94,17 @@ protected override void OnStartup(StartupEventArgs e)
}
else
{
string hostname = context.Configuration.GetValue<string>("core:server.hostname");
int port = context.Configuration.GetValue<int>("core:server.port");

var channel = new Channel(hostname, port, ChannelCredentials.Insecure);
var deviceServiceClient = new DeviceService.DeviceServiceClient(channel);

builder.AddSingleton(channel);
builder.AddSingleton(deviceServiceClient);
builder.AddSingleton(provider =>
{
IOptions<CoreOptions> coreOptions =
provider.GetRequiredService<IOptions<CoreOptions>>();
return new Channel(
coreOptions.Value.ServerGrpcAddress,
coreOptions.Value.ServerGrpcPort,
ChannelCredentials.Insecure);
});
builder.AddSingleton(provider =>
new DeviceService.DeviceServiceClient(provider.GetRequiredService<Channel>()));
}
})
.Build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using GeothermalResearchInstitute.v2;
using GeothermalResearchInstitute.Wpf.Common;

namespace GeothermalResearchInstitute.Wpf.ViewModels
namespace GeothermalResearchInstitute.Wpf.Common
{
public static class ProtoUtils
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// <copyright file="CoreOptions.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>

namespace GeothermalResearchInstitute.Wpf.Options
{
public class CoreOptions
{
public string ServerGrpcAddress { get; set; }

public int ServerGrpcPort { get; set; }

public int DefaultReadTimeoutMillis { get; set; }

public int DefaultWriteTimeoutMillis { get; set; }

public int DefaultPageSize { get; set; } = 50;

public int DefaultRefreshIntervalMillis { get; set; } = 1000;

public int MaxErrorToleranceNum { get; set; } = 5;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,26 @@
using System.Threading.Tasks;
using GeothermalResearchInstitute.v2;
using GeothermalResearchInstitute.Wpf.Common;
using GeothermalResearchInstitute.Wpf.Options;
using Grpc.Core;
using Microsoft.Extensions.Options;
using Prism.Mvvm;

namespace GeothermalResearchInstitute.Wpf.ViewModels
{
public class DeviceAlarmHistoryViewModel : BindableBase
{
private readonly v2.DeviceService.DeviceServiceClient client;
private readonly IOptions<CoreOptions> coreOptions;
private readonly DeviceService.DeviceServiceClient client;
private ViewModelContext viewModelContext;
private string nextPageToken = null;
private bool noMore = false;

public DeviceAlarmHistoryViewModel(v2.DeviceService.DeviceServiceClient client)
public DeviceAlarmHistoryViewModel(
IOptions<CoreOptions> coreOptions,
DeviceService.DeviceServiceClient client)
{
this.coreOptions = coreOptions ?? throw new ArgumentNullException(nameof(coreOptions));
this.client = client ?? throw new ArgumentNullException(nameof(client));
}

Expand All @@ -43,7 +49,7 @@ public async Task LoadAsync()
var request = new ListAlarmChangesRequest
{
DeviceId = this.ViewModelContext.SelectedDevice.Id,
PageSize = 50,
PageSize = this.coreOptions.Value.DefaultPageSize,
};

if (!string.IsNullOrEmpty(this.nextPageToken))
Expand All @@ -55,7 +61,7 @@ public async Task LoadAsync()
{
ListAlarmChangesResponse response = await this.client.ListAlarmChangesAsync(
request,
deadline: DateTime.UtcNow.AddSeconds(5));
deadline: DateTime.UtcNow.AddMilliseconds(this.coreOptions.Value.DefaultReadTimeoutMillis));
this.AlarmChanges.AddRange(response.AlarmChanges);
this.nextPageToken = response.NextPageToken;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
using System.Windows.Threading;
using GeothermalResearchInstitute.v2;
using GeothermalResearchInstitute.Wpf.Common;
using GeothermalResearchInstitute.Wpf.Options;
using Google.Protobuf.WellKnownTypes;
using Grpc.Core;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
Expand All @@ -19,21 +21,25 @@ namespace GeothermalResearchInstitute.Wpf.ViewModels
{
public class DeviceControlViewModel : BindableBase, IRegionMemberLifetime, INavigationAware
{
private readonly DeviceService.DeviceServiceClient client;
private readonly ILogger<DeviceControlViewModel> logger;
private readonly IOptions<CoreOptions> coreOptions;
private readonly DeviceService.DeviceServiceClient client;
private readonly DispatcherTimer timer;
private ViewModelContext viewModelContext;
private Switch deviceSwitch;
private Metric metric;

public DeviceControlViewModel(
DeviceService.DeviceServiceClient client,
ILogger<DeviceControlViewModel> logger)
ILogger<DeviceControlViewModel> logger,
IOptions<CoreOptions> coreOptions,
DeviceService.DeviceServiceClient client)
{
this.client = client ?? throw new ArgumentNullException(nameof(client));
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
this.coreOptions = coreOptions ?? throw new ArgumentNullException(nameof(coreOptions));
this.client = client ?? throw new ArgumentNullException(nameof(client));

this.timer = new DispatcherTimer(
TimeSpan.FromSeconds(1),
TimeSpan.FromMilliseconds(this.coreOptions.Value.DefaultRefreshIntervalMillis),
DispatcherPriority.DataBind,
this.Timer_Tick,
Dispatcher.CurrentDispatcher);
Expand Down Expand Up @@ -102,7 +108,7 @@ public async Task LoadMetricAsync()
{
DeviceId = this.ViewModelContext.SelectedDevice.Id,
},
deadline: DateTime.UtcNow.AddMilliseconds(500));
deadline: DateTime.UtcNow.AddMilliseconds(this.coreOptions.Value.DefaultReadTimeoutMillis));
}
catch (RpcException e)
{
Expand All @@ -122,7 +128,7 @@ public async Task LoadSwitchAsync()
{
DeviceId = this.ViewModelContext.SelectedDevice.Id,
},
deadline: DateTime.UtcNow.AddMilliseconds(500));
deadline: DateTime.UtcNow.AddMilliseconds(this.coreOptions.Value.DefaultReadTimeoutMillis));
this.Switch = response;
}
catch (RpcException e)
Expand Down Expand Up @@ -153,7 +159,7 @@ private async void ExecuteSwitchOnClickCommand(string fieldMask)
Switch = updatingSwitch,
UpdateMask = updatingMask,
},
deadline: DateTime.UtcNow.AddMilliseconds(500));
deadline: DateTime.UtcNow.AddMilliseconds(this.coreOptions.Value.DefaultWriteTimeoutMillis));
}
catch (RpcException e)
{
Expand All @@ -175,7 +181,7 @@ private async void ExecuteSwitchOffClickCommand(string fieldMask)
Switch = updatingSwitch,
UpdateMask = updatingMask,
},
deadline: DateTime.UtcNow.AddMilliseconds(500));
deadline: DateTime.UtcNow.AddMilliseconds(this.coreOptions.Value.DefaultWriteTimeoutMillis));
}
catch (RpcException e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
using System.Windows;
using GeothermalResearchInstitute.v2;
using GeothermalResearchInstitute.Wpf.Common;
using GeothermalResearchInstitute.Wpf.Options;
using GeothermalResearchInstitute.Wpf.Views;
using Grpc.Core;
using Microsoft.Extensions.Options;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
Expand All @@ -19,14 +22,20 @@ namespace GeothermalResearchInstitute.Wpf.ViewModels
{
public class DeviceListViewModel : BindableBase
{
private readonly IOptions<CoreOptions> coreOptions;
private readonly IRegionManager regionManager;
private readonly DeviceService.DeviceServiceClient client;
private ViewModelContext viewModelContext;

public DeviceListViewModel(IRegionManager regionManager, DeviceService.DeviceServiceClient client)
public DeviceListViewModel(
IOptions<CoreOptions> coreOptions,
IRegionManager regionManager,
DeviceService.DeviceServiceClient client)
{
this.coreOptions = coreOptions ?? throw new ArgumentNullException(nameof(coreOptions));
this.regionManager = regionManager ?? throw new ArgumentNullException(nameof(regionManager));
this.client = client ?? throw new ArgumentNullException(nameof(client));

this.ConfirmCommand = new DelegateCommand(this.ExecuteConfirmCommand);
}

Expand Down Expand Up @@ -55,12 +64,19 @@ public Device SelectedDevice

public async Task LoadDevicesAsync()
{
ListDevicesResponse response = await this.client.ListDevicesAsync(
new ListDevicesRequest(),
deadline: DateTime.UtcNow.AddMilliseconds(500));
this.Devices.Clear();
this.Devices.AddRange(response.Devices);
this.SelectedDevice = this.Devices.FirstOrDefault();
try
{
ListDevicesResponse response = await this.client.ListDevicesAsync(
new ListDevicesRequest(),
deadline: DateTime.UtcNow.AddMilliseconds(this.coreOptions.Value.DefaultReadTimeoutMillis));
this.Devices.Clear();
this.Devices.AddRange(response.Devices);
this.SelectedDevice = this.Devices.FirstOrDefault();
}
catch (RpcException e)
{
e.ShowMessageBox();
}
}

private void ExecuteConfirmCommand()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
using System;
using System.Windows.Threading;
using GeothermalResearchInstitute.v2;
using GeothermalResearchInstitute.Wpf.Options;
using Grpc.Core;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Prism.Mvvm;
using Prism.Regions;

Expand All @@ -17,19 +19,27 @@ namespace GeothermalResearchInstitute.Wpf.ViewModels
"Performance", "CA1822", Justification = "ViewModel.")]
public class DeviceMetricBoardViewModel : BindableBase, IRegionMemberLifetime, INavigationAware
{
private readonly DeviceService.DeviceServiceClient client;
private readonly ILogger<DeviceMetricBoardViewModel> logger;
private readonly IOptions<CoreOptions> coreOptions;
private readonly DeviceService.DeviceServiceClient client;
private readonly DispatcherTimer timer;
private ViewModelContext viewModelContext;
private Metric metric;

public DeviceMetricBoardViewModel(
DeviceService.DeviceServiceClient client,
ILogger<DeviceMetricBoardViewModel> logger)
ILogger<DeviceMetricBoardViewModel> logger,
IOptions<CoreOptions> coreOptions,
DeviceService.DeviceServiceClient client)
{
this.client = client ?? throw new ArgumentNullException(nameof(client));
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
this.timer = new DispatcherTimer(TimeSpan.FromSeconds(1), DispatcherPriority.DataBind, this.Timer_Tick, Dispatcher.CurrentDispatcher);
this.coreOptions = coreOptions ?? throw new ArgumentNullException(nameof(coreOptions));
this.client = client ?? throw new ArgumentNullException(nameof(client));

this.timer = new DispatcherTimer(
TimeSpan.FromMilliseconds(this.coreOptions.Value.DefaultRefreshIntervalMillis),
DispatcherPriority.DataBind,
this.Timer_Tick,
Dispatcher.CurrentDispatcher);
}

public ViewModelContext ViewModelContext
Expand Down Expand Up @@ -107,7 +117,7 @@ private async void Timer_Tick(object sender, EventArgs e)
{
DeviceId = this.ViewModelContext.SelectedDevice.Id,
},
deadline: DateTime.UtcNow.AddSeconds(1));
deadline: DateTime.UtcNow.AddMilliseconds(this.coreOptions.Value.DefaultReadTimeoutMillis));
}
catch (RpcException ex)
{
Expand Down
Loading

0 comments on commit 6747578

Please sign in to comment.