Beispiel #1
0
        protected override void OnStartup(StartupEventArgs e)
        {
            RenderOptions.ProcessRenderMode = RenderMode.SoftwareOnly;
            VirtualRoot.AddCmdPath <ShowFileDownloaderCommand>(action: message => {
                UIThread.Execute(() => {
                    FileDownloader.ShowWindow(message.DownloadFileUrl, message.FileTitle, message.DownloadComplete);
                });
            }, location: this.GetType());
            VirtualRoot.AddCmdPath <UpgradeCommand>(action: message => {
                AppStatic.Upgrade(message.FileName, message.Callback);
            }, location: this.GetType());
            try {
                appMutex = new Mutex(true, s_appPipName, out createdNew);
            }
            catch (Exception) {
                createdNew = false;
            }

            if (createdNew)
            {
                this.ShutdownMode = ShutdownMode.OnExplicitShutdown;
                NotiCenterWindow.Instance.ShowWindow();
                LoginWindow.Login(() => {
                    bool isInnerIp = Net.IpUtil.IsInnerIp(NTMinerRegistry.GetControlCenterHost());
                    if (isInnerIp)
                    {
                        NTMinerServices.NTMinerServicesUtil.RunNTMinerServices(() => {
                            Init();
                        });
                    }
                    else
                    {
                        Init();
                    }
                });
            }
            else
            {
                try {
                    _appViewFactory.ShowMainWindow(this, MinerServer.NTMinerAppType.MinerStudio);
                }
                catch (Exception) {
                    DialogWindow.ShowSoftDialog(new DialogWindowViewModel(
                                                    message: "另一个NTMiner正在运行,请手动结束正在运行的NTMiner进程后再次尝试。",
                                                    title: "alert",
                                                    icon: "Icon_Error"));
                    Process currentProcess = Process.GetCurrentProcess();
                    NTMiner.Windows.TaskKill.KillOtherProcess(currentProcess);
                }
            }
            base.OnStartup(e);
        }
Beispiel #2
0
 protected override void OnStartup(StartupEventArgs e)
 {
     RenderOptions.ProcessRenderMode = RenderMode.SoftwareOnly;
     // 之所以提前到这里是因为升级之前可能需要下载升级器,下载升级器时需要下载器
     VirtualRoot.AddCmdPath <ShowFileDownloaderCommand>(action: message => {
         FileDownloader.ShowWindow(message.DownloadFileUrl, message.FileTitle, message.DownloadComplete);
     }, location: this.GetType());
     VirtualRoot.AddCmdPath <UpgradeCommand>(action: message => {
         AppStatic.Upgrade(message.FileName, message.Callback);
     }, location: this.GetType());
     createdNew = AppUtil.GetMutex(NTKeyword.MinerStudioAppMutex);
     if (createdNew)
     {
         this.ShutdownMode = ShutdownMode.OnExplicitShutdown;
         // 因为登录窗口会用到VirtualRoot.Out,而Out的延迟自动关闭消息会用到倒计时
         VirtualRoot.StartTimer(new WpfTimer());
         NotiCenterWindow.Instance.ShowWindow();
         LoginWindow.Login(() => {
             bool isInnerIp = Net.IpUtil.IsInnerIp(NTMinerRegistry.GetControlCenterHost());
             if (isInnerIp)
             {
                 NTMinerServices.NTMinerServicesUtil.RunNTMinerServices(() => {
                     Init();
                 });
             }
             else
             {
                 Init();
             }
         });
     }
     else
     {
         try {
             _appViewFactory.ShowMainWindow(this, NTMinerAppType.MinerStudio);
         }
         catch (Exception) {
             DialogWindow.ShowSoftDialog(new DialogWindowViewModel(
                                             message: "另一个群控客户端正在运行但唤醒失败,请重试。",
                                             title: "错误",
                                             icon: "Icon_Error"));
             Process currentProcess = Process.GetCurrentProcess();
             NTMiner.Windows.TaskKill.KillOtherProcess(currentProcess);
         }
     }
     base.OnStartup(e);
 }
Beispiel #3
0
 private static void PostAsync <T>(string controller, string action, object param, Action <T, Exception> callback) where T : class
 {
     Task.Factory.StartNew(() => {
         try {
             string serverHost = NTMinerRegistry.GetControlCenterHost();
             using (HttpClient client = new HttpClient()) {
                 Task <HttpResponseMessage> message =
                     client.PostAsJsonAsync($"http://{serverHost}:{Consts.ControlCenterPort}/api/{controller}/{action}", param);
                 T response = message.Result.Content.ReadAsAsync <T>().Result;
                 callback?.Invoke(response, null);
             }
         }
         catch (Exception e) {
             callback?.Invoke(null, e);
         }
     });
 }
Beispiel #4
0
 private static T Post <T>(string controller, string action, object param, int?timeout = null) where T : class
 {
     try {
         string serverHost = NTMinerRegistry.GetControlCenterHost();
         using (HttpClient client = new HttpClient()) {
             if (timeout.HasValue)
             {
                 client.Timeout = TimeSpan.FromMilliseconds(timeout.Value);
             }
             Task <HttpResponseMessage> message = client.PostAsJsonAsync($"http://{serverHost}:{Consts.ControlCenterPort}/api/{controller}/{action}", param);
             T response = message.Result.Content.ReadAsAsync <T>().Result;
             return(response);
         }
     }
     catch (Exception e) {
         Logger.ErrorDebugLine(e);
         return(null);
     }
 }
Beispiel #5
0
        private static void GetAsync <T>(string controller, string action, Dictionary <string, string> param, Action <T, Exception> callback)
        {
            Task.Factory.StartNew(() => {
                try {
                    string serverHost = NTMinerRegistry.GetControlCenterHost();
                    using (HttpClient client = RpcRoot.Create()) {
                        string queryString = string.Empty;
                        if (param != null && param.Count != 0)
                        {
                            queryString = "?" + string.Join("&", param.Select(a => a.Key + "=" + a.Value));
                        }

                        Task <HttpResponseMessage> message =
                            client.GetAsync($"http://{serverHost}:{NTKeyword.ControlCenterPort.ToString()}/api/{controller}/{action}{queryString}");
                        T response = message.Result.Content.ReadAsAsync <T>().Result;
                        callback?.Invoke(response, null);
                    }
                }
                catch (Exception e) {
                    callback?.Invoke(default, e);
Beispiel #6
0
 private static void PostAsync <T>(string controller, string action, Dictionary <string, string> query, object param, Action <T, Exception> callback) where T : class
 {
     Task.Factory.StartNew(() => {
         try {
             string queryString = string.Empty;
             if (query != null && query.Count != 0)
             {
                 queryString = "?" + string.Join("&", query.Select(a => a.Key + "=" + a.Value));
             }
             string serverHost = NTMinerRegistry.GetControlCenterHost();
             using (HttpClient client = new HttpClient()) {
                 Task <HttpResponseMessage> getHttpResponse = client.PostAsJsonAsync($"http://{serverHost}:{NTKeyword.ControlCenterPort}/api/{controller}/{action}{queryString}", param);
                 T response = getHttpResponse.Result.Content.ReadAsAsync <T>().Result;
                 callback?.Invoke(response, null);
             }
         }
         catch (Exception e) {
             callback?.Invoke(null, e);
         }
     });
 }
Beispiel #7
0
 private static T Post <T>(string controller, string action, Dictionary <string, string> query, object param, int?timeout = null) where T : class
 {
     try {
         string queryString = string.Empty;
         if (query != null && query.Count != 0)
         {
             queryString = "?" + string.Join("&", query.Select(a => a.Key + "=" + a.Value));
         }
         string serverHost = NTMinerRegistry.GetControlCenterHost();
         using (HttpClient client = RpcRoot.Create()) {
             if (timeout.HasValue)
             {
                 client.Timeout = TimeSpan.FromMilliseconds(timeout.Value);
             }
             Task <HttpResponseMessage> getHttpResponse = client.PostAsJsonAsync($"http://{serverHost}:{NTKeyword.ControlCenterPort.ToString()}/api/{controller}/{action}{queryString}", param);
             T response = getHttpResponse.Result.Content.ReadAsAsync <T>().Result;
             return(response);
         }
     }
     catch (Exception e) {
         Logger.ErrorDebugLine(e);
         return(null);
     }
 }
Beispiel #8
0
        protected override void OnStartup(StartupEventArgs e)
        {
            RenderOptions.ProcessRenderMode = RenderMode.SoftwareOnly;
            try {
                appMutex = new Mutex(true, s_appPipName, out createdNew);
            }
            catch (Exception) {
                createdNew = false;
            }

            if (createdNew)
            {
                Vms.AppStatic.IsMinerClient = false;
                SplashWindow splashWindow = new SplashWindow();
                splashWindow.Show();
                NotiCenterWindow.Instance.Show();
                NTMinerRoot.AppName = "开源矿工群控客户端";
                NTMinerServices.NTMinerServicesUtil.RunNTMinerServices();
                NTMinerRoot.Current.Init(() => {
                    NTMinerRoot.KernelDownloader = new KernelDownloader();
                    UIThread.Execute(() => {
                        splashWindow?.Close();
                        bool?result = true;
                        if (Ip.Util.IsInnerIp(NTMinerRegistry.GetControlCenterHost()))
                        {
                            SingleUser.LoginName = "innerip";
                            SingleUser.SetPasswordSha1("123");
                            result = true;
                        }
                        else if (string.IsNullOrEmpty(SingleUser.LoginName) || string.IsNullOrEmpty(SingleUser.PasswordSha1))
                        {
                            LoginWindow loginWindow = new LoginWindow();
                            result = loginWindow.ShowDialog();
                        }
                        if (result.HasValue && result.Value)
                        {
                            ChartsWindow.ShowWindow();
                            System.Drawing.Icon icon = new System.Drawing.Icon(GetResourceStream(new Uri("pack://application:,,,/MinerStudio;component/logo.ico")).Stream);
                            AppHelper.NotifyIcon     = ExtendedNotifyIcon.Create(icon, "群控客户端", isMinerStudio: true);
                            #region 处理显示主界面命令
                            VirtualRoot.Window <ShowMainWindowCommand>("处理显示主界面命令", LogEnum.None,
                                                                       action: message => {
                                Dispatcher.Invoke((ThreadStart)ChartsWindow.ShowWindow);
                            });
                            #endregion
                            HttpServer.Start($"http://localhost:{WebApiConst.MinerStudioPort}");
                            AppHelper.RemoteDesktop = MsRdpRemoteDesktop.OpenRemoteDesktop;
                        }
                    });
                });
                VirtualRoot.Window <CloseNTMinerCommand>("处理关闭群控客户端命令", LogEnum.UserConsole,
                                                         action: message => {
                    UIThread.Execute(() => {
                        if (MainWindow != null)
                        {
                            MainWindow.Close();
                        }
                        Shutdown();
                        Environment.Exit(0);
                    });
                });
            }
            else
            {
                try {
                    AppHelper.ShowMainWindow(this, MinerServer.NTMinerAppType.MinerStudio);
                }
                catch (Exception) {
                    DialogWindow.ShowDialog(message: "另一个NTMiner正在运行,请手动结束正在运行的NTMiner进程后再次尝试。", title: "alert", icon: "Icon_Error");
                    Process   currentProcess = Process.GetCurrentProcess();
                    Process[] processes      = Process.GetProcessesByName(currentProcess.ProcessName);
                    foreach (var process in processes)
                    {
                        if (process.Id != currentProcess.Id)
                        {
                            NTMiner.Windows.TaskKill.Kill(process.Id);
                        }
                    }
                }
            }
            base.OnStartup(e);
        }
Beispiel #9
0
        protected override void OnStartup(StartupEventArgs e)
        {
            RenderOptions.ProcessRenderMode = RenderMode.SoftwareOnly;
            VirtualRoot.Window <UpgradeCommand>(LogEnum.DevConsole,
                                                action: message => {
                AppStatic.Upgrade(message.FileName, message.Callback);
            });
            try {
                appMutex = new Mutex(true, s_appPipName, out createdNew);
            }
            catch (Exception) {
                createdNew = false;
            }

            if (createdNew)
            {
                this.ShutdownMode = ShutdownMode.OnExplicitShutdown;
                NTMinerRoot.SetIsMinerClient(false);
                NotiCenterWindow.Instance.Show();
                LoginWindow loginWindow = new LoginWindow();
                var         result      = loginWindow.ShowDialog();
                if (result.HasValue && result.Value)
                {
                    bool isInnerIp = Ip.Util.IsInnerIp(NTMinerRegistry.GetControlCenterHost());
                    if (isInnerIp)
                    {
                        NTMinerServices.NTMinerServicesUtil.RunNTMinerServices(() => {
                            Init();
                        });
                    }
                    else
                    {
                        Init();
                    }
                }
                VirtualRoot.Window <CloseNTMinerCommand>("处理关闭群控客户端命令", LogEnum.UserConsole,
                                                         action: message => {
                    UIThread.Execute(() => {
                        try {
                            if (MainWindow != null)
                            {
                                MainWindow.Close();
                            }
                            Shutdown();
                        }
                        catch (Exception ex) {
                            Logger.ErrorDebugLine(ex);
                            Environment.Exit(0);
                        }
                    });
                });
            }
            else
            {
                try {
                    _appViewFactory.ShowMainWindow(this, MinerServer.NTMinerAppType.MinerStudio);
                }
                catch (Exception) {
                    DialogWindow.ShowDialog(message: "另一个NTMiner正在运行,请手动结束正在运行的NTMiner进程后再次尝试。", title: "alert", icon: "Icon_Error");
                    Process currentProcess = Process.GetCurrentProcess();
                    NTMiner.Windows.TaskKill.KillOtherProcess(currentProcess);
                }
            }
            base.OnStartup(e);
        }
Beispiel #10
0
        protected override void OnStartup(StartupEventArgs e)
        {
            RenderOptions.ProcessRenderMode = RenderMode.SoftwareOnly;
            VirtualRoot.BuildCmdPath <ShowFileDownloaderCommand>(action: message => {
                UIThread.Execute(() => {
                    FileDownloader.ShowWindow(message.DownloadFileUrl, message.FileTitle, message.DownloadComplete);
                });
            });
            VirtualRoot.BuildCmdPath <UpgradeCommand>(action: message => {
                AppStatic.Upgrade(message.FileName, message.Callback);
            });
            try {
                appMutex = new Mutex(true, s_appPipName, out createdNew);
            }
            catch (Exception) {
                createdNew = false;
            }

            if (createdNew)
            {
                this.ShutdownMode = ShutdownMode.OnExplicitShutdown;
                NotiCenterWindow.ShowWindow();
                LoginWindow.Login(() => {
                    bool isInnerIp = Net.Util.IsInnerIp(NTMinerRegistry.GetControlCenterHost());
                    if (isInnerIp)
                    {
                        NTMinerServices.NTMinerServicesUtil.RunNTMinerServices(() => {
                            Init();
                        });
                    }
                    else
                    {
                        Init();
                    }
                });
                VirtualRoot.BuildCmdPath <CloseNTMinerCommand>(action: message => {
                    // 不能推迟这个日志记录的时机,因为推迟会有windows异常日志
                    VirtualRoot.ThisLocalWarn(nameof(NTMinerRoot), $"退出{VirtualRoot.AppName}。原因:{message.Reason}");
                    UIThread.Execute(() => {
                        try {
                            Shutdown();
                        }
                        catch (Exception ex) {
                            Logger.ErrorDebugLine(ex);
                            Environment.Exit(0);
                        }
                    });
                });
            }
            else
            {
                try {
                    _appViewFactory.ShowMainWindow(this, MinerServer.NTMinerAppType.MinerStudio);
                }
                catch (Exception) {
                    DialogWindow.ShowSoftDialog(new DialogWindowViewModel(
                                                    message: "另一个NTMiner正在运行,请手动结束正在运行的NTMiner进程后再次尝试。",
                                                    title: "alert",
                                                    icon: "Icon_Error"));
                    Process currentProcess = Process.GetCurrentProcess();
                    NTMiner.Windows.TaskKill.KillOtherProcess(currentProcess);
                }
            }
            base.OnStartup(e);
        }