using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Reflection; using System.Windows.Controls; using System.Windows; using PdmSwPlugin.Common.Entity; using System.Threading.Tasks; using VersionControl; using PdmSwPlugin.Common.Util.Http; using PdmSwPlugin.Common.Entity.System; using PdmSwPlugin.Commmon.Util.UI; using PdmSwPlugin.Common.Util; using PdmSwPlugin.Common.Interface; using SolidWorks.Interop.sldworks; using System.Windows.Threading; using SuperSocket.SocketBase; using SuperSocket.SocketBase.Protocol; using PdmSwPlugin.Main.Tcp; using SolidWorks.Interop.swconst; using System.Windows.Interop; namespace PdmSwPlugin.Main { /// /// MainControl.xaml 的交互逻辑 /// UI 相关 /// public partial class MainControl : UserControl, ISwAppSetter { private TcpServer tcpServer; public void InitTcpServer() { tcpServer = new TcpServer(); tcpServer.NewRequestReceived += new RequestHandler(HandleRequest); if (!tcpServer.Setup(5678)) { Logger.Error("Tcp Server Setup Error."); } if (!tcpServer.Start()) { Logger.Error("Tcp Server Start Error."); } } public void StopTcpServer() { tcpServer?.Stop(); tcpServer = null; } public void HandleRequest(TcpSession session, StringRequestInfo info) { byte[] msg = new byte[1]; if (PdmUser.LoginUser != null) { // 登录返回1 msg[0] = 1; } else { // 没登录返回0 msg[0] = 0; } session.Send(msg, 0, 1); } #region 检查更新相关 /// /// 检查更新 /// public async void CheckUpdate() { // 主进程ID,自动更新时要Kill int mainProcessId = Process.GetCurrentProcess().Id; try { Dictionary datas = new Dictionary { { "params", new List> { new Dictionary { {"appId", PluginConst.AppId}, {"currentVersion", PluginConst.Version}, } } } }; Result> result = await HttpClientCreator.PostAsyncAction>("plugin/openApi/checkUpdate", datas); List updates = result.HandleResult(); if (updates != null && updates.Count > 0) { PluginInfo update = updates[0]; if (update.required) { DoUpdateNotRequired(mainProcessId); } else { DoUpdateNotRequired(mainProcessId); } } } catch (Exception e) { Logger.Error("检查更新错误!", e); this.Error("检查更新错误!"); } } /// /// 非必要更新 /// /// SoliWorks进程ID private void DoUpdateNotRequired(int mainProcessId) { string updaterPath = "AutoUpdater\\AutoUpdater.exe"; string exePath = new DirectoryInfo(Assembly.GetExecutingAssembly().Location).Parent.FullName; string exeFileName = $"{exePath}\\{updaterPath}"; //if (SwApp.SendMsgToUser2("检测到插件更新,是否立刻更新?(请注意保存当前工作)", (int)swMessageBoxIcon_e.swMbInformation, (int)swMessageBoxBtn_e.swMbYesNo) // == (int)swMessageBoxResult_e.swMbHitYes) //{ // Process updaterProcess = new Process // { // StartInfo = new ProcessStartInfo // { // FileName = exeFileName, // Arguments = mainProcessId + "" // } // }; // updaterProcess.Start(); //} // 非必要更新 new Task(() => { MessageBoxResult dr = MessageBox.Show("检测到插件更新,是否立刻更新?(请注意保存当前工作)", "插件更新", MessageBoxButton.OKCancel, MessageBoxImage.Question); if (dr == MessageBoxResult.OK) { Process updaterProcess = new Process { StartInfo = new ProcessStartInfo { FileName = exeFileName, Arguments = mainProcessId + "" } }; updaterProcess.Start(); } }).Start(); } /// /// 当前版本写入注册表 /// private void WriteVersionToReg() { try { // 加载完先写版本注册表,保证版本统一,版本写入失败提示并退出 VersionUtil.SetPluginVersion(PluginConst.AppId, PluginConst.Version); } catch (Exception ex) { this.Error("插件版本写入失败!"); Logger.Error("Plugin version write to reg failed!", ex); } } /// /// 更新自动更新程序 /// public void UpdateAutoUpdater() { try { string dllPath = new DirectoryInfo(Assembly.GetExecutingAssembly().Location).Parent.FullName; string waitingPath = Path.Combine(dllPath, "WaitingUpdate"); if (Directory.Exists(waitingPath)) { // 如果存在WaitingUpdate文件夹,把这个文件夹重命名为AutoUpdater string targetPath = Path.Combine(dllPath, "AutoUpdater"); string delPath = Path.Combine(dllPath, "AutoUpdaterBak"); Directory.Move(targetPath, delPath); Directory.Move(waitingPath, targetPath); Directory.Delete(delPath, true); } } catch (Exception ex) { this.Error($"更新自动更新程序失败!{ex.StackTrace}"); Logger.Error("Update AutoUpdater failed.", ex); } } #endregion /// /// 加载插件主Tab /// public void LoginSuccess() { // 自动登录成功 clientCreator = new HttpClientCreator(); if (mainTab == null) { mainTab = new MainTab(Logout); mainTab.SetSwApp(SwApp); } AddPlugin(); mainBorder.Child = mainTab; if (mainTab.tabControl.Items.Count > 0) { TabItem tabItem = mainTab.tabControl.Items[0] as TabItem; tabItem.Focus(); (tabItem.Content as UIElement).Focus(); } } public void Logout() { try { // 删除缓存bin文件 string binPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData), "HengXin", "PdmSwPlugin", "bin"); File.Delete(binPath); } catch (Exception ex) { //SwApp.SendMsgToUser2("用户缓存清理失败!", (int)swMessageBoxIcon_e.swMbWarning, (int)swMessageBoxBtn_e.swMbOk); Logger.Error("Delete User Bin File Failed.", ex); } try { // 清除UserControl事件 DeActiveHandler(); } catch (Exception ex) { SwApp.SendMsgToUser2("发生未知异常,详情请见日志!", (int)swMessageBoxIcon_e.swMbWarning, (int)swMessageBoxBtn_e.swMbOk); Logger.Error("DeActiveHandler Failed.", ex); } mainTab.tabControl.Items.Clear(); PdmUser.SetLoginUser(null); mainBorder.Child = new LoginControl(LoginSuccess); } public void MainControl_Loaded(object sender, RoutedEventArgs e) { // 加载完先把版本写入注册表 WriteVersionToReg(); // 然后更新AutoUpdater UpdateAutoUpdater(); // 最后检查更新 CheckUpdate(); mainBorder.Child = new LoginControl(LoginSuccess); } public void SetSwApp(SldWorks SwApp) { this.SwApp = SwApp; } private void self_Unloaded(object sender, RoutedEventArgs e) { // StopTcpServer(); } } }