소스 검색

PRS DESKTOP - configure Desktop to use Rest Client for now

Nick-PRSDigital@bitbucket.org 2 년 전
부모
커밋
454c3ee635
1개의 변경된 파일97개의 추가작업 그리고 44개의 파일을 삭제
  1. 97 44
      prs.desktop/MainWindow.xaml.cs

+ 97 - 44
prs.desktop/MainWindow.xaml.cs

@@ -32,6 +32,7 @@ using InABox.DynamicGrid;
 using InABox.Mail;
 using InABox.Core.Reports;
 using InABox.IPC;
+using InABox.Rpc;
 using InABox.Scripting;
 using InABox.WPF;
 using NAudio.Wave;
@@ -58,7 +59,6 @@ using Pen = System.Drawing.Pen;
 using PixelFormat = System.Drawing.Imaging.PixelFormat;
 using Role = Comal.Classes.Role;
 using SortDirection = InABox.Core.SortDirection;
-using ValidationResult = InABox.Clients.ValidationResult;
 using PRSDesktop.Components.Spreadsheet;
 using InABox.Wpf.Reports;
 
@@ -75,6 +75,8 @@ namespace PRSDesktop
 
         private static PipeServer<string>? _client;
 
+        private IRpcClientTransport? _transport;
+
         private WaveIn? _audio;
         private bool _audioMuted;
         private MemoryStream? _audioStream;
@@ -140,10 +142,10 @@ namespace PRSDesktop
             NotificationsWatchDog.Tick += Notifications_Tick;
             NotificationsWatchDog.Interval = new TimeSpan(0, 2, 0);
 
-            ClientFactory.Notifications.AddHandler<Notification>(ReceiveNotification);
+            ClientFactory.PushHandlers.AddHandler<Notification>(ReceiveNotification);
             ClientFactory.RegisterMailer(EmailType.IMAP, typeof(IMAPMailer));
             ClientFactory.RegisterMailer(EmailType.Exchange, typeof(ExchangeMailer));
-            ClientFactory.OnLog += (_, message) => { Logger.Send(LogType.Information, ClientFactory.UserID, message); };
+            ClientFactory.OnLog += (type, userid, message, parameters) =>  Logger.Send(LogType.Information, ClientFactory.UserID, message, parameters);
             ClientFactory.OnRequestError += ClientFactory_OnRequestError;
 
             HotKeyManager.Initialize();
@@ -152,7 +154,8 @@ namespace PRSDesktop
             HotKeyManager.RegisterHotKey(Key.F6, ShowRecordingNotes);
             HotKeyManager.RegisterHotKey(Key.F4, ToggleRecordingAudio);
 
-            DatabaseType = App.DatabaseSettings.DatabaseType;
+            var settings = App.DatabaseSettings;
+            DatabaseType = settings.DatabaseType;
             switch (DatabaseType)
             {
                 case DatabaseType.Standalone:
@@ -161,29 +164,30 @@ namespace PRSDesktop
                     DbFactory.Logo = App.DatabaseSettings.Logo;
                     break;
                 
-                case DatabaseType.Networked:
-                    
-                    //var url = App.DatabaseSettings.URLs.FirstOrDefault() ?? "localhost:8000";
-                    //ClientFactory.SetClientType(typeof(RPCClient<>), Platform.Wpf, CoreUtils.GetVersion(), () => new RPCClientSocketTransport(url));
-                    
-                    var url = RestClient<User>.Ping(App.DatabaseSettings.URLs);
-                    if(url is null)
-                    {
-                        MessageBox.Show("Server is unavailable!");
-                        return;
-                    }
-                    else
-                    {
-                        ClientFactory.SetClientType(typeof(RestClient<>), Platform.Wpf, CoreUtils.GetVersion(), url, true);
-                    }
+                case DatabaseType.Networked:                    
+                    //new RPC stuff (temporary disabled - for enabling when RPC is ready)
+                    //_transport = new RpcClientSocketTransport(App.DatabaseSettings.URLs);
+                    //_transport.OnClose += TransportConnectionLost;
+                    //_transport.Connect();
+                    //ClientFactory.SetClientType(typeof(RpcClient<>), Platform.Wpf, CoreUtils.GetVersion(), _transport );
+
+                    var url = RestClient<User>.Ping(App.DatabaseSettings.URLs, out DatabaseInfo info);
+                    ClientFactory.SetClientType(typeof(RestClient<>), Platform.Wpf, CoreUtils.GetVersion(), 
+                        url, true);
                     break;
                 
                 case DatabaseType.Local:
-                    //var pipe = DatabaseServerProperties.GetPipeName(App.DatabaseSettings.LocalServerName);
-                    //ClientFactory.SetClientType(typeof(RPCClient<>), Platform.Wpf, CoreUtils.GetVersion(),
-                    //    () => new RPCClientPipeTransport(pipe));
-                     ClientFactory.SetClientType(typeof(IPCClient<>), Platform.Wpf, CoreUtils.GetVersion(), 
-                          DatabaseServerProperties.GetPipeName(App.DatabaseSettings.LocalServerName));
+                    //new RPC stuff (temporary disabled - for enabling when RPC is ready)
+                    //var pipename = DatabaseServerProperties.GetPipeName(App.DatabaseSettings.LocalServerName);
+                    //_transport = new RpcClientPipeTransport(pipename);
+                    //_transport.OnClose += TransportConnectionLost;
+                    //_transport.Connect();
+                    //ClientFactory.SetClientType(typeof(RpcClient<>), Platform.Wpf, CoreUtils.GetVersion(),
+                    //    _transport );
+
+
+                    ClientFactory.SetClientType(typeof(IPCClient<>), Platform.Wpf, CoreUtils.GetVersion(), 
+                           DatabaseServerProperties.GetPipeName(App.DatabaseSettings.LocalServerName));
                     break;
             }
 
@@ -214,8 +218,8 @@ namespace PRSDesktop
             CheckForUpdates();
 
             Exception? startupException = null;
-            Exception? loginException = null;
-            ValidationResult? loginStatus = null;
+            //Exception? loginException = null;
+            ValidationStatus? loginStatus = null;
 
             Progress.ShowModal("Loading PRS", progress =>
             {
@@ -311,10 +315,10 @@ namespace PRSDesktop
 
             if (startupException != null)
                 MessageBox.Show(startupException.Message);
-            else if (loginException != null)
-                MessageBox.Show(loginException.Message);
+            //else if (loginException != null)
+            //    MessageBox.Show(loginException.Message);
 
-            if (DoLogin(App.DatabaseSettings.Autologin) == ValidationResult.VALID)
+            if (DoLogin(App.DatabaseSettings.Autologin) == ValidationStatus.VALID)
             {
                 AfterLogin();
             }
@@ -330,7 +334,7 @@ namespace PRSDesktop
 
             Progress.ShowModal("Starting Up", progress =>
             {
-                if (loginStatus == ValidationResult.VALID && DatabaseType == DatabaseType.Standalone)
+                if (loginStatus == ValidationStatus.VALID && DatabaseType == DatabaseType.Standalone)
                 {
                     progress.Report("Starting Scheduler");
                     scheduler.Start();
@@ -338,6 +342,34 @@ namespace PRSDesktop
             });
         }
 
+        private void TransportConnectionLost(IRpcTransport transport, RpcTransportCloseArgs e)
+        {
+            if ((transport is IRpcClientTransport client))
+            {
+                Dispatcher.Invoke(() => {
+                    Progress.ShowModal("Reconnecting", (progress) =>
+                    {
+                        DateTime lost = DateTime.Now;
+                        while (!client.IsConnected())
+                        {
+                            progress.Report($"Connection lost - ({(DateTime.Now - lost):hh\\:mm})");
+                            try
+                            {
+                                Logger.Send(LogType.Error,ClientFactory.UserID,"Reconnecting - ({0:hh\\:mm})",DateTime.Now - lost);
+                                client.Connect();
+                                if (client.IsConnected())
+                                    ClientFactory.Validate(ClientFactory.SessionID);
+                            }
+                            catch (System.Exception e1)
+                            {
+                                Logger.Send(LogType.Error,ClientFactory.UserID,$"Reconnect Failed: {e1.Message}");
+                            }
+                        }
+                    });
+                });
+            }
+        }
+        
         private bool _loggingOut = false;
 
         private void ClientFactory_OnRequestError(RequestException e)
@@ -1427,9 +1459,9 @@ namespace PRSDesktop
 
         #region Login Management
 
-        private ValidationResult? DoLogin(bool autoLogin)
+        private ValidationStatus? DoLogin(bool autoLogin)
         {
-            ValidationResult? result = null;
+            ValidationStatus? result = null;
             if (autoLogin)
             {
                 if (App.DatabaseSettings.LoginType == LoginType.UserID)
@@ -1444,22 +1476,22 @@ namespace PRSDesktop
                         MessageBox.Show("Error connecting to server.\nPlease check the server URL and port number.");
                         result = null;
                     }
-                    if (result == ValidationResult.INVALID)
+                    if (result == ValidationStatus.INVALID)
                     {
                         MessageBox.Show("Unable to Login with User ID: " + App.DatabaseSettings.UserID);
                     }
                 }
             }
-            if (result != ValidationResult.VALID)
+            if (result != ValidationStatus.VALID)
             {
-                var login = new PinLogin(CoreUtils.GetVersion(), result ?? ValidationResult.INVALID);
+                var login = new PinLogin(CoreUtils.GetVersion(), result ?? ValidationStatus.INVALID);
                 if (login.ShowDialog() == true)
                 {
-                    result = ValidationResult.VALID;
+                    result = ValidationStatus.VALID;
                 }
             }
 
-            return result ?? ValidationResult.INVALID;
+            return result ?? ValidationStatus.INVALID;
         }
 
         /// <summary>
@@ -1526,6 +1558,7 @@ namespace PRSDesktop
 
         private void LoadCurrentEmployee()
         {
+            
             var me = new Client<Employee>().Query(
                 new Filter<Employee>(x => x.UserLink.ID).IsEqualTo(ClientFactory.UserGuid),
                 new Columns<Employee>(x => x.ID).Add(x => x.Email).Add(x=>x.Name)
@@ -1597,7 +1630,7 @@ namespace PRSDesktop
                 MessageBox.Show(message);
             }
 
-            if (DoLogin(false) == ValidationResult.VALID)
+            if (DoLogin(false) == ValidationStatus.VALID)
             {
                 AfterLogin();
             }
@@ -2746,8 +2779,30 @@ namespace PRSDesktop
             return _timesheets.Rows.Any(r => r.Get<TimeSheet, DateTime>(c => c.Date).Date < DateTime.Today);
         }
 
+        private void ShutDownTransport()
+        {
+            if (_transport != null)
+            {
+                _transport.OnClose -= TransportConnectionLost;
+                if (_transport.IsConnected())
+                    _transport.Disconnect();
+                _transport = null;
+            }
+        }
+        
         private void Window_Unloaded(object sender, RoutedEventArgs e)
         {
+            ShutDownTransport();
+        }
+        
+        
+        private void RibbonWindow_Closed(object sender, EventArgs e)
+        {
+            
+            ShutDownTransport();
+            
+            DisconnectRecorderNotes();
+            Application.Current.Shutdown();
         }
 
         //private bool _closingFromSystemMenu = false;
@@ -2776,6 +2831,9 @@ namespace PRSDesktop
             if (!CoreUtils.GetVersion().Equals("???"))
                 if (station.ID != Guid.Empty)
                     ExecuteLogout();
+
+            ShutDownTransport();
+
         }
 
         private void FinalizeAutoTimesheet()
@@ -2803,11 +2861,6 @@ namespace PRSDesktop
         }
 
 
-        private void RibbonWindow_Closed(object sender, EventArgs e)
-        {
-            DisconnectRecorderNotes();
-            Application.Current.Shutdown();
-        }
 
         #region Notifications + Heartbeat
 
@@ -4098,7 +4151,7 @@ namespace PRSDesktop
 
         private void LoginButton_OnClick(object sender, RoutedEventArgs e)
         {
-            if (DoLogin(false) == ValidationResult.VALID)
+            if (DoLogin(false) == ValidationStatus.VALID)
                 AfterLogin();
         }