MainWindow.xaml.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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. }
  88. private void MainWindow_Loaded(object sender, RoutedEventArgs e)
  89. {
  90. if(LicenseMana.mLicenseModel != null)
  91. {
  92. if (LicenseMana.mLicenseModel.IsPermanent)
  93. {
  94. this.Title = $"{this.Title} (永久注册)";
  95. }
  96. else
  97. {
  98. this.Title = $"{this.Title} (L)";
  99. }
  100. }
  101. else
  102. {
  103. this.Title = $"{this.Title} (未注册)";
  104. }
  105. //控制lblInstanceIndex背景色的颜色
  106. int colorIndex = InstanceIndex % 3;
  107. if(colorIndex == 1)
  108. {
  109. lblInstanceIndex.Background = Brushes.Red;
  110. }
  111. else if (colorIndex == 2)
  112. {
  113. lblInstanceIndex.Background = Brushes.Green;
  114. }
  115. else
  116. {
  117. lblInstanceIndex.Background = Brushes.Blue;
  118. }
  119. lblInstanceIndex.Content = $"第{InstanceIndex}个";
  120. }
  121. private void BtnConfig_Click(object sender, RoutedEventArgs e)
  122. {
  123. EditConfigDlg dialog = new EditConfigDlg()
  124. {
  125. Owner = this,
  126. WindowStartupLocation = WindowStartupLocation.CenterOwner
  127. };
  128. if(dialog.ShowDialog() == true)
  129. {
  130. }
  131. //ResetButtonForegrounds();
  132. //txtConfig.Foreground = Brushes.DarkGreen;
  133. //string key = "ConfigPage";
  134. //// 检查字典中是否已有该页面
  135. //if (pageCache.ContainsKey(key))
  136. //{
  137. // ContentArea.Content = pageCache[key]; // 如果存在,直接显示该页面
  138. //}
  139. //else
  140. //{
  141. // var page = new UCConfig(); //UCSingleGrid(); // 新建页面
  142. // //pageCache[key] = page; // 缓存页面
  143. // ContentArea.Content = page; // 显示新页面
  144. //}
  145. }
  146. private void BtnSetStation_Click(object sender, RoutedEventArgs e)
  147. {
  148. ResetButtonForegrounds();
  149. txtSetStation.Foreground = Brushes.DarkGreen;
  150. string key = "SetStation";
  151. // 检查字典中是否已有该页面
  152. if (pageCache.ContainsKey(key))
  153. {
  154. ContentArea.Content = pageCache[key]; // 如果存在,直接显示该页面
  155. }
  156. else
  157. {
  158. var page = new UCStationMain(); // 新建页面
  159. //pageCache[key] = page; // 缓存页面
  160. ContentArea.Content = page; // 显示新页面
  161. }
  162. }
  163. //单张图片的处理
  164. private void BtnSingleImage_Click(object sender, RoutedEventArgs e)
  165. {
  166. ResetButtonForegrounds();
  167. // 将被点击的按钮前景色设置为DarkGreen
  168. txtSingleImage.Foreground = SelectedForeground; //"#2196F3"
  169. //Brushes.DarkGreen;
  170. string key = "SingleImagePage";
  171. // 检查字典中是否已有该页面
  172. if (pageCache.ContainsKey(key))
  173. {
  174. ContentArea.Content = pageCache[key]; // 如果存在,直接显示该页面
  175. }
  176. else
  177. {
  178. var page = new UCSingleMain(); //UCSingleGrid(); // 新建页面
  179. pageCache[key] = page; // 缓存页面
  180. ContentArea.Content = page; // 显示新页面
  181. }
  182. }
  183. private void BtnStandImport_Click(object sender,RoutedEventArgs e)
  184. {
  185. ResetButtonForegrounds();
  186. // 将被点击的按钮前景色设置为DarkGreen
  187. txtStandImport.Foreground = Brushes.DarkGreen;
  188. string key = "StandImportPage";
  189. // 检查字典中是否已有该页面
  190. if (pageCache.ContainsKey(key))
  191. {
  192. ContentArea.Content = pageCache[key]; // 如果存在,直接显示该页面
  193. }
  194. else
  195. {
  196. var page = new UCStandMain(); // 新建页面
  197. pageCache[key] = page; // 缓存页面
  198. ContentArea.Content = page; // 显示新页面
  199. }
  200. }
  201. //批量图片的处理
  202. private void BtnPatchImage_Click(object sender, RoutedEventArgs e)
  203. {
  204. ResetButtonForegrounds();
  205. // 将被点击的按钮前景色设置为DarkGreen
  206. txtPatchImage.Foreground = Brushes.DarkGreen;
  207. string key = "PatchImagePage";
  208. // 检查字典中是否已有该页面
  209. if (pageCache.ContainsKey(key))
  210. {
  211. ContentArea.Content = pageCache[key]; // 如果存在,直接显示该页面
  212. }
  213. else
  214. {
  215. var page = new UCPatchMain();
  216. pageCache[key] = page; // 缓存页面
  217. ContentArea.Content = page; // 显示新页面
  218. }
  219. }
  220. private void BtnPatchCompare_Click(object sender, RoutedEventArgs e)
  221. {
  222. ResetButtonForegrounds();
  223. // 将被点击的按钮前景色设置为DarkGreen
  224. txtCompareImage.Foreground = Brushes.DarkGreen;
  225. string key = "CompareImagePage";
  226. // 检查字典中是否已有该页面
  227. if (pageCache.ContainsKey(key))
  228. {
  229. ContentArea.Content = pageCache[key]; // 如果存在,直接显示该页面
  230. }
  231. else
  232. {
  233. var page = new UCCompMain();
  234. pageCache[key] = page; // 缓存页面
  235. ContentArea.Content = page; // 显示新页面
  236. }
  237. }
  238. private void ResetButtonForegrounds()
  239. {
  240. //txtConfig.Foreground = Brushes.Black;
  241. txtSingleImage.Foreground = Brushes.Black;
  242. txtStandImport.Foreground = Brushes.Black;
  243. txtPatchImage.Foreground = Brushes.Black;
  244. txtCompareImage.Foreground = Brushes.Black;
  245. txtSetStation.Foreground = Brushes.Black;
  246. }
  247. private void BtnAbout_Click(object sender, RoutedEventArgs e)
  248. {
  249. AboutWindow aboutWindow = new AboutWindow()
  250. {
  251. Owner = Application.Current.MainWindow
  252. };
  253. aboutWindow.ShowDialog(); // 显示为模态窗口
  254. }
  255. private void BtnCheckUpgrade_Click(object sender, RoutedEventArgs e)
  256. {
  257. WaitWindow waitWindow = null;
  258. try
  259. {
  260. string titleInfo = "正在检查升级信息,请稍后...";
  261. waitWindow = new WaitWindow(titleInfo)
  262. {
  263. Owner = Application.Current.MainWindow,
  264. WindowStartupLocation = WindowStartupLocation.CenterOwner
  265. };
  266. waitWindow.Show();
  267. UpgradeModel upgradeModel = UpgradeHelper.ReadUpdateTxt();
  268. //waitWindow.Close();
  269. //获取当前的版本与versionCode比较
  270. string currentVersion = UpgradeHelper.GetCurrentVersion();
  271. // 获取服务器返回的 versionCode(假设是字符串 "1.0.0.6")
  272. string serverVersionCode = upgradeModel.VersionCode;
  273. if (UpgradeHelper.IsNewVersionAvailable(currentVersion, serverVersionCode))
  274. {
  275. //MessageBox.Show($"发现新版本({serverVersionCode})。请升级应用。", "升级提示",
  276. // MessageBoxButton.OK, MessageBoxImage.Information);
  277. WndUpdateAsk updateAsk = new WndUpdateAsk(upgradeModel)
  278. {
  279. Owner = Application.Current.MainWindow,
  280. WindowStartupLocation = WindowStartupLocation.CenterOwner
  281. };
  282. updateAsk.ShowDialog();
  283. }
  284. else
  285. {
  286. MessageBox.Show("当前已经是最新版本。", "无升级", MessageBoxButton.OK, MessageBoxImage.Information);
  287. }
  288. }
  289. catch(Exception ex)
  290. {
  291. MessageBox.Show(ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  292. }
  293. if(waitWindow != null)
  294. {
  295. waitWindow.Close();
  296. }
  297. }
  298. //注册
  299. private void BtnReg_Click(object sender, RoutedEventArgs e)
  300. {
  301. RegisterDlg registerDlg = new RegisterDlg
  302. {
  303. Owner = this,
  304. WindowStartupLocation = WindowStartupLocation.CenterOwner
  305. };
  306. if (registerDlg.ShowDialog() == true)
  307. {
  308. }
  309. }
  310. private void Overlay_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  311. {
  312. // 如果覆盖层可见且用户点击了它,则隐藏覆盖层并关闭对话框
  313. //if (this.overlay.Visibility == Visibility.Visible)
  314. //{
  315. // this.overlay.Visibility = Visibility.Collapsed;
  316. // // 尝试关闭与覆盖层关联的对话框
  317. // foreach (Window window in Application.Current.Windows)
  318. // {
  319. // if (window != this && window.IsActive)
  320. // {
  321. // window.Close();
  322. // break;
  323. // }
  324. // }
  325. //}
  326. }
  327. private void Window_PreviewMouseDown(object sender, MouseButtonEventArgs e)
  328. {
  329. // 查找所有打开的窗口
  330. foreach (Window child in Application.Current.Windows)
  331. {
  332. // 检查是否是你想要的窗口
  333. if (child is imgWindow || child is EditStandValueDlg || child is LogViewerWindow)
  334. {
  335. child.Close();
  336. }
  337. }
  338. }
  339. private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
  340. {
  341. // 当按下 Esc 键时关闭对话框
  342. if (e.Key == Key.Escape)
  343. {
  344. foreach (Window child in Application.Current.Windows)
  345. {
  346. // 检查是否是你想要的窗口
  347. if (child is imgWindow || child is EditStandValueDlg || child is LogViewerWindow)
  348. {
  349. child.Close();
  350. }
  351. }
  352. }
  353. }
  354. public object GetActivityUC()
  355. {
  356. return ContentArea.Content;
  357. }
  358. private async void BtnFreeSpace_Click(object sender, RoutedEventArgs e)
  359. {
  360. string titleInfo = "正在释放数据库空间占用,请稍候...";
  361. WaitWindow waitWindow = new WaitWindow(titleInfo)
  362. {
  363. Owner = Application.Current.MainWindow,
  364. WindowStartupLocation = WindowStartupLocation.CenterOwner
  365. };
  366. waitWindow.Show();
  367. //DBStand.FreeDatabase();
  368. try
  369. {
  370. await Task.Run(() =>
  371. {
  372. DBStand.FreeDatabase();
  373. });
  374. }
  375. catch (Exception ex)
  376. {
  377. MessageBox.Show(Application.Current.MainWindow, $"失败:{ex.Message}", "错误",
  378. MessageBoxButton.OK, MessageBoxImage.Error);
  379. }
  380. finally
  381. {
  382. //关闭等待窗口
  383. waitWindow.Close();
  384. }
  385. MessageBox.Show(this, "已释放", "提示",MessageBoxButton.OK, MessageBoxImage.Information);
  386. }
  387. private void ChkRealSendAILog_Checked(object sender, RoutedEventArgs e)
  388. {
  389. FaRun.SendAiLogEnable = true;
  390. }
  391. private void ChkRealSendAILog_Unchecked(object sender, RoutedEventArgs e)
  392. {
  393. FaRun.SendAiLogEnable = false;
  394. }
  395. private void ChkRealRecordAILog_Checked(object sender, RoutedEventArgs e)
  396. {
  397. FaRun.RecordRealLogEnable = true;
  398. }
  399. private void ChkRealRecordAILog_Unchecked(object sender, RoutedEventArgs e)
  400. {
  401. FaRun.RecordRealLogEnable = false;
  402. }
  403. private void BtnViewRealLog_Click(object sender, RoutedEventArgs e)
  404. {
  405. ViewRealLogDlg dialog = new ViewRealLogDlg()
  406. {
  407. Owner = this,
  408. WindowStartupLocation = WindowStartupLocation.CenterOwner
  409. };
  410. dialog.ShowDialog();
  411. }
  412. //临时修改Json文件
  413. private void BtnUpdateJson_Click(object sender, RoutedEventArgs e)
  414. {
  415. string standJsonFile = @"D:\RT102_AI\mv2_json\goog_20250408120027.json";
  416. string updateJsonFile = @"D:\RT102_AI\mv2_json\all_20250401142632.json";
  417. string standJson = File.ReadAllText(standJsonFile);
  418. string updateJson = File.ReadAllText(updateJsonFile);
  419. List<TStandDetail> standList = JsonConvert.DeserializeObject<List<TStandDetail>>(standJson) ?? new List<TStandDetail>();
  420. List<TStandDetail> updateList = JsonConvert.DeserializeObject<List<TStandDetail>>(updateJson) ?? new List<TStandDetail>();
  421. foreach(TStandDetail stand in standList)
  422. {
  423. foreach(TStandDetail update in updateList)
  424. {
  425. if (stand.SrcImage.Equals(update.SrcImage))
  426. {
  427. update.StandValue = stand.StandValue;
  428. update.ENumCount = stand.ENumCount;
  429. update.ELastUnit = stand.ELastUnit;
  430. update.MeterType = stand.MeterType;
  431. update.BrightVal = stand.BrightVal;
  432. update.FlowRate = stand.FlowRate;
  433. update.DialRegion = stand.DialRegion;
  434. update.NumCount = stand.NumCount;
  435. update.IndCount = stand.IndCount;
  436. update.FeatureRegion = stand.FeatureRegion;
  437. update.LastUnit = stand.LastUnit;
  438. update.LastValue = stand.LastValue;
  439. update.LastTime = stand.LastTime;
  440. break;
  441. }
  442. }
  443. }
  444. string fileName = @"D:\RT102_AI\mv2_json\all_20250401142632_update.json";
  445. string json = JsonConvert.SerializeObject(updateList, Formatting.Indented); // 格式化 JSON
  446. File.WriteAllText(fileName, json);
  447. MessageBox.Show("修改完成");
  448. }
  449. //----------------------------------------------------------
  450. }
  451. }