MainWindow.xaml.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. using MeterVision.Config;
  2. using MeterVision.db;
  3. using MeterVision.Dlg;
  4. using MeterVision.FreeAi;
  5. using MeterVision.Patch;
  6. using MeterVision.Single;
  7. using MeterVision.Stand;
  8. using MeterVision.Station;
  9. using MeterVision.Upgrade;
  10. using MeterVision.Util;
  11. using Newtonsoft.Json;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.ComponentModel;
  15. using System.IO;
  16. using System.Linq;
  17. using System.Runtime.InteropServices;
  18. using System.Text;
  19. using System.Threading.Tasks;
  20. using System.Windows;
  21. using System.Windows.Controls;
  22. using System.Windows.Data;
  23. using System.Windows.Documents;
  24. using System.Windows.Input;
  25. using System.Windows.Media;
  26. using System.Windows.Media.Imaging;
  27. using System.Windows.Navigation;
  28. using System.Windows.Shapes;
  29. namespace MeterVision
  30. {
  31. /// <summary>
  32. /// MainWindow.xaml 的交互逻辑
  33. /// </summary>
  34. public partial class MainWindow : Window
  35. {
  36. //全局硬件码
  37. public static string MachineCode;
  38. //绑定ConfigItem的属性就可以完成绑定操作
  39. //public event PropertyChangedEventHandler PropertyChanged;
  40. //protected void OnPropertyChanged(string propertyName)
  41. //{
  42. // PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  43. //}
  44. //private ConfigItem _configItem;
  45. //public ConfigItem mConfigItem
  46. //{
  47. // get => _configItem;
  48. // set
  49. // {
  50. // _configItem = value;
  51. // //确保通知属性更改
  52. // //OnPropertyChanged(nameof(mConfigItem));
  53. // }
  54. //}
  55. [DllImport("kernel32.dll", SetLastError = true)]
  56. static extern IntPtr AddDllDirectory(string NewDirectory);
  57. [DllImport("kernel32.dll")]
  58. static extern bool SetDefaultDllDirectories(uint DirectoryFlags);
  59. const uint LOAD_LIBRARY_SEARCH_DEFAULT_DIRS = 0x00001000;
  60. //--------------------------------------------
  61. private SolidColorBrush SelectedForeground;
  62. public CfginiItem CurConfigItem { get; set; }
  63. //字典用于缓存已加载的页面
  64. private Dictionary<string, object> pageCache = new Dictionary<string, object>();
  65. //第几个实例
  66. public int InstanceIndex { get; set; }
  67. public MainWindow()
  68. {
  69. InitializeComponent();
  70. SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
  71. //AddDllDirectory(AppDomain.CurrentDomain.BaseDirectory + "dlls");
  72. AddDllDirectory(AppDomain.CurrentDomain.BaseDirectory);
  73. this.Title = $"{this.Title} V{UpgradeHelper.GetCurrentVersion()}";
  74. SelectedForeground = Brushes.DarkGreen;
  75. //new SolidColorBrush((Color)ColorConverter.ConvertFromString("#2196F3"));
  76. ResetButtonForegrounds();
  77. CurConfigItem = CfginiItem.GetConfigItem(); //ConfigItem.GetConfigItem();
  78. this.DataContext = this;
  79. this.Loaded += MainWindow_Loaded;
  80. UpdateDatabase();
  81. }
  82. //更新数据库(因为有添加的字段)
  83. private void UpdateDatabase()
  84. {
  85. DBPatch.UpdateTPatchDetailSchema();
  86. DBSingle.UpdateTSingleDetailSchema();
  87. DBPatch.UpdateTPatchDetail2Schema();
  88. DBPatch.UpdateTPatchSchema();
  89. DBPatch.RefreshV_PatchView();
  90. }
  91. private void MainWindow_Loaded(object sender, RoutedEventArgs e)
  92. {
  93. //20250725 暂时不用
  94. /*if(LicenseMana.mLicenseModel != null)
  95. {
  96. if (LicenseMana.mLicenseModel.IsPermanent)
  97. {
  98. this.Title = $"{this.Title} (永久注册)";
  99. }
  100. else
  101. {
  102. this.Title = $"{this.Title} (L)";
  103. }
  104. }
  105. else
  106. {
  107. this.Title = $"{this.Title} (未注册)";
  108. }*/
  109. //控制lblInstanceIndex背景色的颜色
  110. int colorIndex = InstanceIndex % 3;
  111. if(colorIndex == 1)
  112. {
  113. lblInstanceIndex.Background = Brushes.Red;
  114. }
  115. else if (colorIndex == 2)
  116. {
  117. lblInstanceIndex.Background = Brushes.Green;
  118. }
  119. else
  120. {
  121. lblInstanceIndex.Background = Brushes.Blue;
  122. }
  123. lblInstanceIndex.Content = $"第{InstanceIndex}个";
  124. }
  125. private void BtnConfig_Click(object sender, RoutedEventArgs e)
  126. {
  127. EditConfigDlg dialog = new EditConfigDlg()
  128. {
  129. Owner = this,
  130. WindowStartupLocation = WindowStartupLocation.CenterOwner
  131. };
  132. if(dialog.ShowDialog() == true)
  133. {
  134. }
  135. //ResetButtonForegrounds();
  136. //txtConfig.Foreground = Brushes.DarkGreen;
  137. //string key = "ConfigPage";
  138. //// 检查字典中是否已有该页面
  139. //if (pageCache.ContainsKey(key))
  140. //{
  141. // ContentArea.Content = pageCache[key]; // 如果存在,直接显示该页面
  142. //}
  143. //else
  144. //{
  145. // var page = new UCConfig(); //UCSingleGrid(); // 新建页面
  146. // //pageCache[key] = page; // 缓存页面
  147. // ContentArea.Content = page; // 显示新页面
  148. //}
  149. }
  150. //接口设置
  151. private void BtnCfgInterface_Click(object sender, RoutedEventArgs e)
  152. {
  153. EditInterfaceDlg dialog = new EditInterfaceDlg()
  154. {
  155. Owner = this,
  156. WindowStartupLocation = WindowStartupLocation.CenterOwner
  157. };
  158. if (dialog.ShowDialog() == true)
  159. {
  160. }
  161. }
  162. private void BtnSetStation_Click(object sender, RoutedEventArgs e)
  163. {
  164. ResetButtonForegrounds();
  165. txtSetStation.Foreground = Brushes.DarkGreen;
  166. string key = "SetStation";
  167. // 检查字典中是否已有该页面
  168. if (pageCache.ContainsKey(key))
  169. {
  170. ContentArea.Content = pageCache[key]; // 如果存在,直接显示该页面
  171. }
  172. else
  173. {
  174. var page = new UCStationMain(); // 新建页面
  175. //pageCache[key] = page; // 缓存页面
  176. ContentArea.Content = page; // 显示新页面
  177. }
  178. }
  179. //单张图片的处理
  180. private void BtnSingleImage_Click(object sender, RoutedEventArgs e)
  181. {
  182. ResetButtonForegrounds();
  183. // 将被点击的按钮前景色设置为DarkGreen
  184. txtSingleImage.Foreground = SelectedForeground; //"#2196F3"
  185. //Brushes.DarkGreen;
  186. string key = "SingleImagePage";
  187. // 检查字典中是否已有该页面
  188. if (pageCache.ContainsKey(key))
  189. {
  190. ContentArea.Content = pageCache[key]; // 如果存在,直接显示该页面
  191. }
  192. else
  193. {
  194. var page = new UCSingleMain(); //UCSingleGrid(); // 新建页面
  195. pageCache[key] = page; // 缓存页面
  196. ContentArea.Content = page; // 显示新页面
  197. }
  198. }
  199. private void BtnStandImport_Click(object sender,RoutedEventArgs e)
  200. {
  201. ResetButtonForegrounds();
  202. // 将被点击的按钮前景色设置为DarkGreen
  203. txtStandImport.Foreground = Brushes.DarkGreen;
  204. string key = "StandImportPage";
  205. // 检查字典中是否已有该页面
  206. if (pageCache.ContainsKey(key))
  207. {
  208. ContentArea.Content = pageCache[key]; // 如果存在,直接显示该页面
  209. }
  210. else
  211. {
  212. var page = new UCStandMain(); // 新建页面
  213. pageCache[key] = page; // 缓存页面
  214. ContentArea.Content = page; // 显示新页面
  215. }
  216. }
  217. //批量图片的处理
  218. private void BtnPatchImage_Click(object sender, RoutedEventArgs e)
  219. {
  220. ResetButtonForegrounds();
  221. // 将被点击的按钮前景色设置为DarkGreen
  222. txtPatchImage.Foreground = Brushes.DarkGreen;
  223. string key = "PatchImagePage";
  224. // 检查字典中是否已有该页面
  225. if (pageCache.ContainsKey(key))
  226. {
  227. ContentArea.Content = pageCache[key]; // 如果存在,直接显示该页面
  228. }
  229. else
  230. {
  231. var page = new UCPatchMain();
  232. pageCache[key] = page; // 缓存页面
  233. ContentArea.Content = page; // 显示新页面
  234. }
  235. }
  236. private void BtnPatchCompare_Click(object sender, RoutedEventArgs e)
  237. {
  238. ResetButtonForegrounds();
  239. // 将被点击的按钮前景色设置为DarkGreen
  240. txtCompareImage.Foreground = Brushes.DarkGreen;
  241. string key = "CompareImagePage";
  242. // 检查字典中是否已有该页面
  243. if (pageCache.ContainsKey(key))
  244. {
  245. ContentArea.Content = pageCache[key]; // 如果存在,直接显示该页面
  246. }
  247. else
  248. {
  249. var page = new UCCompMain();
  250. pageCache[key] = page; // 缓存页面
  251. ContentArea.Content = page; // 显示新页面
  252. }
  253. }
  254. private void ResetButtonForegrounds()
  255. {
  256. //txtConfig.Foreground = Brushes.Black;
  257. txtSingleImage.Foreground = Brushes.Black;
  258. txtStandImport.Foreground = Brushes.Black;
  259. txtPatchImage.Foreground = Brushes.Black;
  260. txtCompareImage.Foreground = Brushes.Black;
  261. txtSetStation.Foreground = Brushes.Black;
  262. }
  263. private void BtnAbout_Click(object sender, RoutedEventArgs e)
  264. {
  265. AboutWindow aboutWindow = new AboutWindow()
  266. {
  267. Owner = Application.Current.MainWindow
  268. };
  269. aboutWindow.ShowDialog(); // 显示为模态窗口
  270. }
  271. private void BtnCheckUpgrade_Click(object sender, RoutedEventArgs e)
  272. {
  273. WaitWindow waitWindow = null;
  274. try
  275. {
  276. string titleInfo = "正在检查升级信息,请稍后...";
  277. waitWindow = new WaitWindow(titleInfo)
  278. {
  279. Owner = Application.Current.MainWindow,
  280. WindowStartupLocation = WindowStartupLocation.CenterOwner
  281. };
  282. waitWindow.Show();
  283. UpgradeModel upgradeModel = UpgradeHelper.ReadUpdateTxt();
  284. //waitWindow.Close();
  285. //获取当前的版本与versionCode比较
  286. string currentVersion = UpgradeHelper.GetCurrentVersion();
  287. // 获取服务器返回的 versionCode(假设是字符串 "1.0.0.6")
  288. string serverVersionCode = upgradeModel.VersionCode;
  289. if (UpgradeHelper.IsNewVersionAvailable(currentVersion, serverVersionCode))
  290. {
  291. //MessageBox.Show($"发现新版本({serverVersionCode})。请升级应用。", "升级提示",
  292. // MessageBoxButton.OK, MessageBoxImage.Information);
  293. WndUpdateAsk updateAsk = new WndUpdateAsk(upgradeModel)
  294. {
  295. Owner = Application.Current.MainWindow,
  296. WindowStartupLocation = WindowStartupLocation.CenterOwner
  297. };
  298. updateAsk.ShowDialog();
  299. }
  300. else
  301. {
  302. MessageBox.Show("当前已经是最新版本。", "无升级", MessageBoxButton.OK, MessageBoxImage.Information);
  303. }
  304. }
  305. catch(Exception ex)
  306. {
  307. MessageBox.Show(ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  308. }
  309. if(waitWindow != null)
  310. {
  311. waitWindow.Close();
  312. }
  313. }
  314. //注册
  315. private void BtnReg_Click(object sender, RoutedEventArgs e)
  316. {
  317. RegisterDlg registerDlg = new RegisterDlg
  318. {
  319. Owner = this,
  320. WindowStartupLocation = WindowStartupLocation.CenterOwner
  321. };
  322. if (registerDlg.ShowDialog() == true)
  323. {
  324. }
  325. }
  326. private void Overlay_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  327. {
  328. // 如果覆盖层可见且用户点击了它,则隐藏覆盖层并关闭对话框
  329. //if (this.overlay.Visibility == Visibility.Visible)
  330. //{
  331. // this.overlay.Visibility = Visibility.Collapsed;
  332. // // 尝试关闭与覆盖层关联的对话框
  333. // foreach (Window window in Application.Current.Windows)
  334. // {
  335. // if (window != this && window.IsActive)
  336. // {
  337. // window.Close();
  338. // break;
  339. // }
  340. // }
  341. //}
  342. }
  343. private void Window_PreviewMouseDown(object sender, MouseButtonEventArgs e)
  344. {
  345. // 查找所有打开的窗口
  346. foreach (Window child in Application.Current.Windows)
  347. {
  348. // 检查是否是你想要的窗口
  349. if (child is imgWindow || child is EditStandValueDlg || child is LogViewerWindow)
  350. {
  351. child.Close();
  352. }
  353. }
  354. }
  355. private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
  356. {
  357. // 当按下 Esc 键时关闭对话框
  358. if (e.Key == Key.Escape)
  359. {
  360. foreach (Window child in Application.Current.Windows)
  361. {
  362. // 检查是否是你想要的窗口
  363. if (child is imgWindow || child is EditStandValueDlg || child is LogViewerWindow)
  364. {
  365. child.Close();
  366. }
  367. }
  368. }
  369. }
  370. public object GetActivityUC()
  371. {
  372. return ContentArea.Content;
  373. }
  374. private async void BtnFreeSpace_Click(object sender, RoutedEventArgs e)
  375. {
  376. string titleInfo = "正在释放数据库空间占用,请稍候...";
  377. WaitWindow waitWindow = new WaitWindow(titleInfo)
  378. {
  379. Owner = Application.Current.MainWindow,
  380. WindowStartupLocation = WindowStartupLocation.CenterOwner
  381. };
  382. waitWindow.Show();
  383. //DBStand.FreeDatabase();
  384. try
  385. {
  386. await Task.Run(() =>
  387. {
  388. DBStand.FreeDatabase();
  389. });
  390. }
  391. catch (Exception ex)
  392. {
  393. MessageBox.Show(Application.Current.MainWindow, $"失败:{ex.Message}", "错误",
  394. MessageBoxButton.OK, MessageBoxImage.Error);
  395. }
  396. finally
  397. {
  398. //关闭等待窗口
  399. waitWindow.Close();
  400. }
  401. MessageBox.Show(this, "已释放", "提示",MessageBoxButton.OK, MessageBoxImage.Information);
  402. }
  403. private void ChkRealSendAILog_Checked(object sender, RoutedEventArgs e)
  404. {
  405. FaRun.SendAiLogEnable = true;
  406. }
  407. private void ChkRealSendAILog_Unchecked(object sender, RoutedEventArgs e)
  408. {
  409. FaRun.SendAiLogEnable = false;
  410. }
  411. private void ChkRealRecordAILog_Checked(object sender, RoutedEventArgs e)
  412. {
  413. FaRun.RecordRealLogEnable = true;
  414. }
  415. private void ChkRealRecordAILog_Unchecked(object sender, RoutedEventArgs e)
  416. {
  417. FaRun.RecordRealLogEnable = false;
  418. }
  419. private void BtnViewRealLog_Click(object sender, RoutedEventArgs e)
  420. {
  421. ViewRealLogDlg dialog = new ViewRealLogDlg()
  422. {
  423. Owner = this,
  424. WindowStartupLocation = WindowStartupLocation.CenterOwner
  425. };
  426. dialog.ShowDialog();
  427. }
  428. //临时修改Json文件
  429. private void BtnUpdateJson_Click(object sender, RoutedEventArgs e)
  430. {
  431. string standJsonFile = @"D:\RT102_AI\mv2_json\goog_20250408120027.json";
  432. string updateJsonFile = @"D:\RT102_AI\mv2_json\all_20250401142632.json";
  433. string standJson = File.ReadAllText(standJsonFile);
  434. string updateJson = File.ReadAllText(updateJsonFile);
  435. List<TStandDetail> standList = JsonConvert.DeserializeObject<List<TStandDetail>>(standJson) ?? new List<TStandDetail>();
  436. List<TStandDetail> updateList = JsonConvert.DeserializeObject<List<TStandDetail>>(updateJson) ?? new List<TStandDetail>();
  437. foreach(TStandDetail stand in standList)
  438. {
  439. foreach(TStandDetail update in updateList)
  440. {
  441. if (stand.SrcImage.Equals(update.SrcImage))
  442. {
  443. update.StandValue = stand.StandValue;
  444. update.ENumCount = stand.ENumCount;
  445. update.ELastUnit = stand.ELastUnit;
  446. update.MeterType = stand.MeterType;
  447. update.BrightVal = stand.BrightVal;
  448. update.FlowRate = stand.FlowRate;
  449. update.DialRegion = stand.DialRegion;
  450. update.NumCount = stand.NumCount;
  451. update.IndCount = stand.IndCount;
  452. update.FeatureRegion = stand.FeatureRegion;
  453. update.LastUnit = stand.LastUnit;
  454. update.LastValue = stand.LastValue;
  455. update.LastTime = stand.LastTime;
  456. break;
  457. }
  458. }
  459. }
  460. string fileName = @"D:\RT102_AI\mv2_json\all_20250401142632_update.json";
  461. string json = JsonConvert.SerializeObject(updateList, Formatting.Indented); // 格式化 JSON
  462. File.WriteAllText(fileName, json);
  463. MessageBox.Show("修改完成");
  464. }
  465. //----------------------------------------------------------
  466. }
  467. }