diff --git a/Common/Common.sln b/Common/Common.sln index fe22063d..54ffff44 100644 --- a/Common/Common.sln +++ b/Common/Common.sln @@ -822,6 +822,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "TempPE", "TempPE", "{91CA98 EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Common.GUI", "Ui\Common.GUI\Common.GUI.csproj", "{25A3BC33-563A-41EC-A1CB-4C5F520DDD53}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "LaaPackages", "LaaPackages", "{2E31C385-4FFC-473E-9C00-D7DC48140704}" +EndProject Global GlobalSection(SharedMSBuildProjectFiles) = preSolution Utils\DateTimeServerShared\DateTimeServerShared.projitems*{4806d89a-fbfb-41bc-aaa7-2242444bf55a}*SharedItemsImports = 4 diff --git a/Common/Hardware/Common.Hardware.Ports/SerialPort.cs b/Common/Hardware/Common.Hardware.Ports/SerialPort.cs index 27b2300c..ae05a9dc 100644 --- a/Common/Hardware/Common.Hardware.Ports/SerialPort.cs +++ b/Common/Hardware/Common.Hardware.Ports/SerialPort.cs @@ -21,13 +21,14 @@ this.COMPort = name; this.readAwaiter = new ManualResetEventSlim(); this.closeAwaiter = new ManualResetEventSlim(); - var sp = new SysSerialPort(this.COMPort, baudRate, parity, dataBits, stopBits); - serialPorts.AddOrUpdate( - key: this.COMPort, - addValue: sp, - updateValueFactory: (_1, _2) => sp); + serialPorts.AddOrUpdate(this.COMPort, sp, (key, value) => + { + value?.Close(); + + return sp; + }); } public event Action Connected; @@ -36,9 +37,7 @@ public string COMPort { get; } - public bool IsOpen - => serialPorts.TryGetValue(this.COMPort, out var sp) - && sp.IsOpen; + public bool IsOpen => serialPorts.TryGetValue(this.COMPort, out var sp) && sp.IsOpen; public void Close() { @@ -47,7 +46,7 @@ try { sp.Close(); - this.closeAwaiter.Wait(3000); + this.closeAwaiter.Wait(5000); this.closeAwaiter.Reset(); sp.DataReceived -= this.SP_DataReceived; @@ -82,7 +81,7 @@ public IEnumerable ReadBytes() { - var sp = this.GetOpenSP(); + var sp = this.GetOpenedSP(); var size = sp.ReadBufferSize; var buffer = new byte[size]; var length = 0; @@ -109,12 +108,14 @@ yield return buffer; } + + sp.Close(); } } public IEnumerable ReadLines() { - var sp = this.GetOpenSP(); + var sp = this.GetOpenedSP(); var text = default(string); using (sp) @@ -139,15 +140,17 @@ } } - public void RemoveConnection() + public void Remove() { - this.Close(); - serialPorts.TryRemove(this.COMPort, out _); + if (serialPorts.TryRemove(this.COMPort, out var sp)) + { + sp.Close(); + } } public void Write(byte[] bytes) { - var sp = this.GetOpenSP(); + var sp = this.GetOpenedSP(); try { @@ -161,7 +164,7 @@ public void Write(string text) { - var sp = this.GetOpenSP(); + var sp = this.GetOpenedSP(); try { @@ -173,7 +176,7 @@ } } - private SysSerialPort GetOpenSP() + private SysSerialPort GetOpenedSP() { if (!serialPorts.TryGetValue(this.COMPort, out var sp)) { @@ -182,6 +185,7 @@ if (!sp.IsOpen) { + this.Close(); this.Open(); } diff --git a/Common/Hardware/Common.Hardware.SIRT/SIRTBroadcaster.Internal.cs b/Common/Hardware/Common.Hardware.SIRT/SIRTBroadcaster.Internal.cs index 2fe77f9c..da60c062 100644 --- a/Common/Hardware/Common.Hardware.SIRT/SIRTBroadcaster.Internal.cs +++ b/Common/Hardware/Common.Hardware.SIRT/SIRTBroadcaster.Internal.cs @@ -115,6 +115,8 @@ private void SerialPort_Connected() { + this.IsConnected = true; + this.cancellationTokenSource = new CancellationTokenSource(); this.cancellationToken = this.cancellationTokenSource.Token; @@ -130,6 +132,9 @@ private void SerialPort_Disconnected() { + this.IsActivated = false; + this.IsConnected = false; + try { this.cancellationTokenSource?.Cancel(); @@ -158,6 +163,8 @@ private void StartProcessingTasksAsync() { + this.IsActivated = true; + this.processingTask = Task.Factory.StartNew(() => { var manualResetEventSlim = new ManualResetEventSlim(); diff --git a/Common/Hardware/Common.Hardware.SIRT/SIRTBroadcaster.Public.cs b/Common/Hardware/Common.Hardware.SIRT/SIRTBroadcaster.Public.cs index 208178a9..8d58f0bf 100644 --- a/Common/Hardware/Common.Hardware.SIRT/SIRTBroadcaster.Public.cs +++ b/Common/Hardware/Common.Hardware.SIRT/SIRTBroadcaster.Public.cs @@ -28,6 +28,10 @@ public int Frequency { get; protected set; } + public bool IsConnected { get; protected set; } + + public bool IsActivated { get; protected set; } + public void Close() { this.serialPort.Close(); @@ -84,6 +88,6 @@ } public void RemoveConnection() - => this.serialPort.RemoveConnection(); + => this.serialPort.Remove(); } } diff --git a/Common/Hardware/Common.Hardware.SIRT/SIRTFactory.cs b/Common/Hardware/Common.Hardware.SIRT/SIRTFactory.cs index ecfd1cb0..7429bfb9 100644 --- a/Common/Hardware/Common.Hardware.SIRT/SIRTFactory.cs +++ b/Common/Hardware/Common.Hardware.SIRT/SIRTFactory.cs @@ -7,84 +7,95 @@ public class SIRTFactory { - private readonly ConcurrentDictionary connections + private readonly ConcurrentDictionary broadcasters = new ConcurrentDictionary(); public event Action Logging; public void CloseConnection(string portName) { - if (this.connections.TryGetValue(portName, out var connection)) + if (this.broadcasters.TryGetValue(portName, out var broadcaster)) { - connection.Close(); + broadcaster.Logging -= this.NotifyMessage; + broadcaster.Close(); } } public void CloseConnections() { - foreach (var connection in this.GetConnections()) + foreach (var broadcaster in this.GetConnections()) { - connection.Close(); + broadcaster.Logging -= this.NotifyMessage; + broadcaster.Close(); } } public void DequeueTasks(uint address) { - foreach (var connection in this.GetConnections()) + foreach (var broadcaster in this.GetConnections()) { - connection.DequeueTasks(address); + broadcaster.DequeueTasks(address); } } public void OpenConnections() { - foreach (var connection in this.GetConnections()) + foreach (var broadcaster in this.GetConnections()) { - connection.Open(); + broadcaster.Logging += this.NotifyMessage; + broadcaster.Open(); } } public void ConnectSIRT(string portName, Action callback) { - if (!this.connections.TryGetValue(portName, out var broadcaster)) + if (!this.broadcasters.TryGetValue(portName, out var broadcaster)) { broadcaster = new SIRTBroadcaster(portName); - broadcaster.Logging += message => this.Logging?.Invoke(message); + broadcaster.Logging += this.NotifyMessage; - if (!this.connections.TryAdd(portName, broadcaster)) + if (!this.broadcasters.TryAdd(portName, broadcaster)) { broadcaster = null; } } callback(broadcaster); - broadcaster?.Close(); - broadcaster?.Open(); + + if (broadcaster?.IsActivated == false) + { + broadcaster?.Close(); + broadcaster?.Open(); + } } public void EnqueueTask(SIRTTask task) { - foreach (var connection in this.GetConnections()) + foreach (var broadcaster in this.GetConnections()) { - connection.EnqueueTask(task); + broadcaster.EnqueueTask(task); } } public IEnumerable GetPortNames() - => this.connections.Keys.ToArray(); + => this.broadcasters.Keys.ToArray(); public void RemoveConnections() { - foreach (var key in this.connections.Keys.ToArray()) + foreach (var key in this.broadcasters.Keys.ToArray()) { - if (this.connections.TryRemove(key, out var broadcaster)) + if (this.broadcasters.TryRemove(key, out var broadcaster)) { + broadcaster.Logging -= this.NotifyMessage; broadcaster.RemoveConnection(); } } } + private void NotifyMessage(string message) + => this.Logging?.Invoke(message); + private IEnumerable GetConnections() - => this.connections.Values.ToArray(); + => this.broadcasters.Values.ToArray(); } } diff --git a/Common/Ui/Common.GUI/App.config b/Common/Ui/Common.GUI/App.config index ba675aa2..5314b623 100644 --- a/Common/Ui/Common.GUI/App.config +++ b/Common/Ui/Common.GUI/App.config @@ -4,6 +4,6 @@ - + \ No newline at end of file diff --git a/Common/Ui/Common.GUI/Common.GUI.csproj b/Common/Ui/Common.GUI/Common.GUI.csproj index fdb416c2..cd8a0ef5 100644 --- a/Common/Ui/Common.GUI/Common.GUI.csproj +++ b/Common/Ui/Common.GUI/Common.GUI.csproj @@ -79,11 +79,20 @@ SIRTFactory.xaml + + GridLayout.xaml + + + + ControlsPage.xaml + + AutomaticInitialize.xaml + ManualProgrammingPage.xaml @@ -91,10 +100,13 @@ + + + Designer MSBuild:Compile @@ -103,6 +115,10 @@ Designer MSBuild:Compile + + Designer + MSBuild:Compile + Designer MSBuild:Compile @@ -115,6 +131,10 @@ MSBuild:Compile Designer + + Designer + MSBuild:Compile + Designer MSBuild:Compile @@ -156,6 +176,10 @@ MSBuild:Compile Designer + + Designer + MSBuild:Compile + Designer MSBuild:Compile @@ -201,7 +225,6 @@ - diff --git a/Common/Ui/Common.GUI/Controls/Eregister/SIRTFactory.xaml b/Common/Ui/Common.GUI/Controls/Eregister/SIRTFactory.xaml index 1a3f32e6..30cfc289 100644 --- a/Common/Ui/Common.GUI/Controls/Eregister/SIRTFactory.xaml +++ b/Common/Ui/Common.GUI/Controls/Eregister/SIRTFactory.xaml @@ -1,14 +1,112 @@  + x:Class="Common.GUI.Controls.Eregister.SIRTFactory" + xmlns:ERVM="clr-namespace:Common.GUI.ViewModels.Eregister" + DataContext="{Binding SIRTFactoryVM, Source={StaticResource Services}}"> - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - diff --git a/Common/Ui/Common.GUI/Controls/GridLayout.xaml b/Common/Ui/Common.GUI/Controls/GridLayout.xaml new file mode 100644 index 00000000..aae4c6b5 --- /dev/null +++ b/Common/Ui/Common.GUI/Controls/GridLayout.xaml @@ -0,0 +1,4 @@ + + diff --git a/Common/Ui/Common.GUI/Controls/GridLayout.xaml.cs b/Common/Ui/Common.GUI/Controls/GridLayout.xaml.cs new file mode 100644 index 00000000..9b06e795 --- /dev/null +++ b/Common/Ui/Common.GUI/Controls/GridLayout.xaml.cs @@ -0,0 +1,45 @@ +namespace Common.GUI.Controls +{ + using System.Windows; + using System.Windows.Controls; + + public partial class GridLayout : Grid + { + private static readonly DependencyProperty RowsCountProperty = DependencyProperty.Register( + name: nameof(RowsCount) + , propertyType: typeof(int) + , ownerType: typeof(GridLayout) + , typeMetadata: new FrameworkPropertyMetadata( + defaultValue: 0 + , propertyChangedCallback: RowsCountChanged)); + + public GridLayout() => this.InitializeComponent(); + + public int RowsCount + { + get => this.GetValue(RowsCountProperty) is int rowsCount ? rowsCount : 0; + set => this.SetValue(RowsCountProperty, value); + } + + private static void RowsCountChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args) + { + if (dependencyObject is GridLayout gridLayout) + { + if (args.NewValue is int newRowsCount) + { + var rowDefinitions = gridLayout.RowDefinitions; + + while (rowDefinitions.Count > newRowsCount) + { + rowDefinitions.RemoveAt(rowDefinitions.Count - 1); + } + + while (rowDefinitions.Count < newRowsCount) + { + rowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); + } + } + } + } + } +} diff --git a/Common/Ui/Common.GUI/Controls/MainMenu.xaml b/Common/Ui/Common.GUI/Controls/MainMenu.xaml index 30cb3dc1..04988015 100644 --- a/Common/Ui/Common.GUI/Controls/MainMenu.xaml +++ b/Common/Ui/Common.GUI/Controls/MainMenu.xaml @@ -4,554 +4,6 @@ Background="{DynamicResource primary}" BorderThickness="0"> - - M 0,0 L 3.5,4 L 7,0 Z - M 0,4 L 3.5,0 L 7,4 Z - M 0,0 L 4,3.5 L 0,7 Z - F1 M 10.0,1.2 L 4.7,9.1 L 4.5,9.1 L 0,5.2 L 1.3,3.5 L 4.3,6.1L 8.3,0 L 10.0,1.2 Z - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -594,5 +46,9 @@ CommandParameter="Pages/Eregister/ManualProgrammingPage.xaml" Header="Manual Programming" Style="{StaticResource MenuItemStyle}" /> + diff --git a/Common/Ui/Common.GUI/Models/Base/Appsettings.cs b/Common/Ui/Common.GUI/Models/Base/Appsettings.cs new file mode 100644 index 00000000..b7dfa015 --- /dev/null +++ b/Common/Ui/Common.GUI/Models/Base/Appsettings.cs @@ -0,0 +1,31 @@ +namespace Common.GUI.Models.Base +{ + using System; + using System.Configuration; + using System.Linq; + + public class Appsettings + { + private static readonly string dll = typeof(App).Assembly.Location; + + public static string GetOrCreateSetting(string settingKey) + { + if (string.IsNullOrWhiteSpace(settingKey)) + { + return default(string); + } + + var configuration = ConfigurationManager.OpenExeConfiguration(dll); + var settings = configuration.AppSettings.Settings; + + if (!settings.AllKeys.Any(x => x.Equals(settingKey, StringComparison.OrdinalIgnoreCase))) + { + settings.Add(settingKey, string.Empty); + configuration.Save(ConfigurationSaveMode.Modified); + ConfigurationManager.RefreshSection("appSettings"); + } + + return settings[settingKey].Value; + } + } +} diff --git a/Common/Ui/Common.GUI/Models/Base/Arguments.cs b/Common/Ui/Common.GUI/Models/Base/Arguments.cs index 0f4cc7e5..a9627fb7 100644 --- a/Common/Ui/Common.GUI/Models/Base/Arguments.cs +++ b/Common/Ui/Common.GUI/Models/Base/Arguments.cs @@ -6,8 +6,6 @@ { private object[] values = Array.Empty(); - public object[] Values => this.values; - internal void Reset() => Array.Resize(ref this.values, 0); @@ -25,5 +23,15 @@ Array.Resize(ref this.values, argsLength); Array.Copy(arguments, 0, this.values, 0, argsLength); } + + internal T Get(int i, T @default = default(T)) + { + if ((this.values?.Length ?? 0) > i && this.values[i] is T value) + { + return value; + } + + return @default; + } } } diff --git a/Common/Ui/Common.GUI/Models/Data/EregisterDbContext.cs b/Common/Ui/Common.GUI/Models/Data/EregisterDbContext.cs new file mode 100644 index 00000000..a9010b92 --- /dev/null +++ b/Common/Ui/Common.GUI/Models/Data/EregisterDbContext.cs @@ -0,0 +1,7 @@ +namespace Common.GUI.Models.Data +{ + public class EregisterDbContext + { + + } +} diff --git a/Common/Ui/Common.GUI/Models/Services.cs b/Common/Ui/Common.GUI/Models/Services.cs index ebdc5c0a..a9fb4d61 100644 --- a/Common/Ui/Common.GUI/Models/Services.cs +++ b/Common/Ui/Common.GUI/Models/Services.cs @@ -3,6 +3,7 @@ using Common.GUI.Models.Base; using Common.GUI.ViewModels.Eregister; using Common.Hardware.SIRT; + using Common.Hardware.WaterMeter.eRegister; using Microsoft.Extensions.DependencyInjection; @@ -16,6 +17,10 @@ public EregisterManualProgrammingVM EregisterManualProgrammingVM => serviceProvider.GetService(); + public AutomaticInitializeVM AutomaticInitializeVM => serviceProvider.GetService(); + + public SIRTFactoryVM SIRTFactoryVM => serviceProvider.GetService(); + private void App_Exit(object sender, ExitEventArgs args) { App.Current.Exit -= this.App_Exit; @@ -24,10 +29,13 @@ } private static ServiceProvider ConfigureServices() - => new ServiceCollection() + => new ServiceCollection() .AddSingleton() .AddSingleton() + .AddSingleton() .AddSingleton() + .AddSingleton() + .AddSingleton() .BuildServiceProvider(); public static T GetService() diff --git a/Common/Ui/Common.GUI/Pages/Eregister/AutomaticInitialize.xaml b/Common/Ui/Common.GUI/Pages/Eregister/AutomaticInitialize.xaml new file mode 100644 index 00000000..6d66de9c --- /dev/null +++ b/Common/Ui/Common.GUI/Pages/Eregister/AutomaticInitialize.xaml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + diff --git a/Common/Ui/Common.GUI/Pages/Eregister/AutomaticInitialize.xaml.cs b/Common/Ui/Common.GUI/Pages/Eregister/AutomaticInitialize.xaml.cs new file mode 100644 index 00000000..fcbfa90d --- /dev/null +++ b/Common/Ui/Common.GUI/Pages/Eregister/AutomaticInitialize.xaml.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace Common.GUI.Pages.Eregister +{ + /// + /// Interaction logic for AutomaticInitialize.xaml + /// + public partial class AutomaticInitialize : Page + { + public AutomaticInitialize() + { + InitializeComponent(); + } + } +} diff --git a/Common/Ui/Common.GUI/Pages/MainPage.xaml b/Common/Ui/Common.GUI/Pages/MainPage.xaml index 5b4822ef..26cecad7 100644 --- a/Common/Ui/Common.GUI/Pages/MainPage.xaml +++ b/Common/Ui/Common.GUI/Pages/MainPage.xaml @@ -2,9 +2,8 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Controls="clr-namespace:Common.GUI.Controls" + xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero2" FontFamily="Verdana"> - - diff --git a/Common/Ui/Common.GUI/Theme/Combobox.xaml b/Common/Ui/Common.GUI/Theme/Combobox.xaml new file mode 100644 index 00000000..9b330fc9 --- /dev/null +++ b/Common/Ui/Common.GUI/Theme/Combobox.xaml @@ -0,0 +1,161 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Common/Ui/Common.GUI/Theme/Icons.xaml b/Common/Ui/Common.GUI/Theme/Icons.xaml index ec297b51..60cfc618 100644 --- a/Common/Ui/Common.GUI/Theme/Icons.xaml +++ b/Common/Ui/Common.GUI/Theme/Icons.xaml @@ -24,6 +24,10 @@ FillRule="evenodd" Figures="M4.646 1.646a.5.5 0 0 1 .708 0l6 6a.5.5 0 0 1 0 .708l-6 6a.5.5 0 0 1-.708-.708L10.293 8 4.646 2.354a.5.5 0 0 1 0-.708"/> + + @@ -38,4 +42,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/Common/Ui/Common.GUI/Theme/Styles.xaml b/Common/Ui/Common.GUI/Theme/Styles.xaml index fb685e5b..a8416ee1 100644 --- a/Common/Ui/Common.GUI/Theme/Styles.xaml +++ b/Common/Ui/Common.GUI/Theme/Styles.xaml @@ -315,9 +315,9 @@ Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True"> + RecognizesAccessKey="True" + SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" + VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> @@ -358,13 +358,13 @@ - + - + + + + M 0,0 L 3.5,4 L 7,0 Z + M 0,4 L 3.5,0 L 7,4 Z + M 0,0 L 4,3.5 L 0,7 Z + F1 M 10.0,1.2 L 4.7,9.1 L 4.5,9.1 L 0,5.2 L 1.3,3.5 L 4.3,6.1L 8.3,0 L 10.0,1.2 Z + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Common/Ui/Common.GUI/ViewModels/Base/GridItemVM[T].cs b/Common/Ui/Common.GUI/ViewModels/Base/GridItemVM[T].cs new file mode 100644 index 00000000..c48e2700 --- /dev/null +++ b/Common/Ui/Common.GUI/ViewModels/Base/GridItemVM[T].cs @@ -0,0 +1,33 @@ +namespace Common.GUI.ViewModels.Base +{ + public class GridItemVM : VM where T : GridItemVM + { + private int column; + public int Column + { + get => this.column; + set => this.SetValue(x => x.column = value); + } + + private int columnSpan = 1; + public int ColumnSpan + { + get => this.columnSpan; + set => this.SetValue(x => x.columnSpan = value); + } + + private int row; + public int Row + { + get => this.row; + set => this.SetValue(x => x.row = value); + } + + private int rowSpan = 1; + public int RowSpan + { + get => this.rowSpan; + set => this.SetValue(x => x.rowSpan = value); + } + } +} diff --git a/Common/Ui/Common.GUI/ViewModels/Base/VM.cs b/Common/Ui/Common.GUI/ViewModels/Base/VM.cs index 53841fb9..a71470b9 100644 --- a/Common/Ui/Common.GUI/ViewModels/Base/VM.cs +++ b/Common/Ui/Common.GUI/ViewModels/Base/VM.cs @@ -1,7 +1,5 @@ namespace Common.GUI.ViewModels.Base { - using Common.GUI.Models.Base; - using System; using System.ComponentModel; using System.Runtime.CompilerServices; @@ -15,6 +13,12 @@ } + protected void InvokeOnMainThread(Action action) + => App.Current.Dispatcher.Invoke(action); + + protected void InvokeOnMainThreadAsync(Action action) + => App.Current.Dispatcher.InvokeAsync(action); + protected void NotifyPropertyChanged([CallerMemberName] string member = "") => this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(member)); diff --git a/Common/Ui/Common.GUI/ViewModels/Eregister/AutomaticInitializeVM.cs b/Common/Ui/Common.GUI/ViewModels/Eregister/AutomaticInitializeVM.cs new file mode 100644 index 00000000..0fed4dfd --- /dev/null +++ b/Common/Ui/Common.GUI/ViewModels/Eregister/AutomaticInitializeVM.cs @@ -0,0 +1,30 @@ +namespace Common.GUI.ViewModels.Eregister +{ + using Common.GUI.Models.Base; + using Common.GUI.ViewModels.Base; + using Common.Hardware.WaterMeter.eRegister; + + public class AutomaticInitializeVM : ArgsVM + { + private readonly EregisterFactory eregisterFactory; + private readonly int sharedId; + + public AutomaticInitializeVM(Arguments arguments, EregisterFactory eregisterFactory) : base(arguments) + { + this.eregisterFactory = eregisterFactory; + this.sharedId = arguments.Get(0, 1); + + this.InitializeVM(); + } + + public override void Dispose() + { + + } + + private void InitializeVM() + { + + } + } +} diff --git a/Common/Ui/Common.GUI/ViewModels/Eregister/SIRTFactoryVM.cs b/Common/Ui/Common.GUI/ViewModels/Eregister/SIRTFactoryVM.cs new file mode 100644 index 00000000..5298394d --- /dev/null +++ b/Common/Ui/Common.GUI/ViewModels/Eregister/SIRTFactoryVM.cs @@ -0,0 +1,79 @@ +namespace Common.GUI.ViewModels.Eregister +{ + using Common.GUI.Models.Base; + using Common.GUI.ViewModels.Base; + using Common.Hardware.SIRT; + + using System.Collections.ObjectModel; + using System.Text.RegularExpressions; + using System.Threading.Tasks; + using System.Windows.Input; + + public class SIRTFactoryVM : VM + { + private readonly SIRTFactory sirtFactory; + + public SIRTFactoryVM(SIRTFactory sirtFactory) + { + this.sirtFactory = sirtFactory; + this.RefreshConnections = new Command(this.RefreshConnectionsHandler); + this.DisconnectConnections = new Command(this.DisconnectConnectionsHandler); + + this.InitializeVM(); + } + + public ICommand RefreshConnections { get; } + + public ICommand DisconnectConnections { get; } + + public ObservableCollection Sirts { get; } + = new ObservableCollection(); + + public override void Dispose() + { + this.Sirts.Clear(); + this.sirtFactory.RemoveConnections(); + } + + private void InitializeVM() + => Task.Factory.StartNew(this.InitializeSirtBroadcaster); + + private void InitializeSirtBroadcaster() + { + var comportsList = Appsettings.GetOrCreateSetting("SIRT.ComPorts"); + var comportsMatches = Regex.Matches($"{comportsList}", "[Cc][Oo][Mm][0-9]+"); + + foreach (Match match in comportsMatches) + { + if (match.Success) + { + var comport = match.Value.ToUpper(); + + this.sirtFactory.ConnectSIRT(comport, broadcaster => + { + this.InvokeOnMainThreadAsync(() => + { + this.Sirts.Add(new SIRTVM(broadcaster, this.Sirts.Count)); + }); + }); + } + } + } + + private void DisconnectConnectionsHandler() + { + this.Sirts.Clear(); + + Task.Factory.StartNew(this.sirtFactory.RemoveConnections); + } + + private void RefreshConnectionsHandler() + { + this.Sirts.Clear(); + + Task.Factory + .StartNew(this.sirtFactory.RemoveConnections) + .ContinueWith(_ => this.InitializeSirtBroadcaster()); + } + } +} diff --git a/Common/Ui/Common.GUI/ViewModels/Eregister/SIRTVM.cs b/Common/Ui/Common.GUI/ViewModels/Eregister/SIRTVM.cs new file mode 100644 index 00000000..190c187e --- /dev/null +++ b/Common/Ui/Common.GUI/ViewModels/Eregister/SIRTVM.cs @@ -0,0 +1,81 @@ +namespace Common.GUI.ViewModels.Eregister +{ + using Common.GUI.ViewModels.Base; + using Common.Hardware.SIRT; + + public class SIRTVM : GridItemVM + { + private SIRTBroadcaster broadcaster; + + public SIRTVM(SIRTBroadcaster broadcaster, int row) + { + this.Row = row; + this.IsConnecting = true; + this.IsConnectedAndActive = false; + this.broadcaster = broadcaster; + this.broadcaster.Activated += this.Broadcaster_Activated; + this.broadcaster.Connected += this.Broadcaster_Connected; + this.broadcaster.Disconnected += this.Broadcaster_Disconnected; + } + + private bool? isConnecting; + public bool? IsConnecting + { + get => this.isConnecting; + set => this.SetValue(x => x.isConnecting = value); + } + + private bool? isConnectedAndActive; + public bool? IsConnectedAndActive + { + get => this.isConnectedAndActive; + set => this.SetValue(x => x.isConnectedAndActive = value); + } + + private string hexId; + public string HexId + { + get => this.hexId; + set => this.SetValue(x => x.hexId = value); + } + + private string comPort; + public string ComPort + { + get => this.comPort; + set => this.SetValue(x => x.comPort = value); + } + + private int? frequency; + public int? Frequency + { + get => this.frequency; + set => this.SetValue(x => x.frequency = value); + } + + private void Broadcaster_Activated() + { + this.IsConnectedAndActive = true; + this.HexId = this.broadcaster.HexId; + this.ComPort = this.broadcaster.COMPort; + this.Frequency = this.broadcaster.Frequency; + } + + private void Broadcaster_Connected() + { + this.IsConnecting = false; + this.IsConnectedAndActive = null; + this.HexId = default(string); + this.ComPort = this.broadcaster.COMPort; + this.Frequency = default(int?); + } + + private void Broadcaster_Disconnected() + { + this.IsConnectedAndActive = false; + this.HexId = default(string); + this.ComPort = default(string); + this.Frequency = default(int?); + } + } +} \ No newline at end of file