HouseDebugPageView.xaml.cs 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360
  1. using ivf_tl_CustomControls;
  2. using ivf_tl_Entity.CameraEntitys;
  3. using ivf_tl_Entity.GlobalEnums;
  4. using ivf_tl_Operate.Debug;
  5. using ivf_tl_Operate.ViewModel;
  6. using ivf_tl_Operate.Windows;
  7. using Newtonsoft.Json.Linq;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Net.Sockets;
  13. using System.Runtime.ConstrainedExecution;
  14. using System.Security.Cryptography;
  15. using System.Security.Policy;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using System.Windows;
  19. using System.Windows.Controls;
  20. using System.Windows.Data;
  21. using System.Windows.Documents;
  22. using System.Windows.Input;
  23. using System.Windows.Media;
  24. using System.Windows.Media.Imaging;
  25. using System.Windows.Media.Media3D;
  26. using System.Windows.Navigation;
  27. using System.Windows.Shapes;
  28. namespace ivf_tl_Operate.View
  29. {
  30. /// <summary>
  31. /// HouseDebugPageView.xaml 的交互逻辑
  32. /// </summary>
  33. public partial class HouseDebugPageView : UserControl
  34. {
  35. //private HouseDebugPageViewModel vm { get { return (HouseDebugPageViewModel)DataContext; } }
  36. private HouseDebugPageViewModel vm = null;
  37. /// <summary>
  38. /// 实时图像打开状态
  39. /// </summary>
  40. private bool isOpen = false;
  41. /// <summary>D2-02 第二阶段:MJPEG 预览客户端(一次性,每次开预览 new 新实例)。</summary>
  42. private MjpegStreamClient _mjpeg;
  43. private object locker = new object();
  44. //#bdbdbd #bfd87d
  45. public HouseDebugPageView(HouseDebugPageViewModel houseDebugPageViewModel)
  46. {
  47. try
  48. {
  49. InitializeComponent();
  50. vm = houseDebugPageViewModel;
  51. this.DataContext = vm;
  52. Loaded += HouseDebugPageView_Loaded;
  53. }
  54. catch (Exception ex)
  55. {
  56. ExLog(ex, "HouseDebugPageView");
  57. }
  58. }
  59. #region 通用设置
  60. private void HouseDebugPageView_Loaded(object sender, RoutedEventArgs e)
  61. {
  62. vm.MessageEvent += AddMessageInfo;
  63. vm.OpenVideoEvent += OpenVideo;
  64. vm.AddPicEvent += AddPic;
  65. foreach (var item in vm.HouseList)
  66. {
  67. switch (item.houseSn)
  68. {
  69. case 1:
  70. this._houseCCD1.Text = item.ccdId.ToString();
  71. break;
  72. case 2:
  73. this._houseCCD2.Text = item.ccdId.ToString();
  74. break;
  75. case 3:
  76. this._houseCCD3.Text = item.ccdId.ToString();
  77. break;
  78. case 4:
  79. this._houseCCD4.Text = item.ccdId.ToString();
  80. break;
  81. case 5:
  82. this._houseCCD5.Text = item.ccdId.ToString();
  83. break;
  84. case 6:
  85. this._houseCCD6.Text = item.ccdId.ToString();
  86. break;
  87. case 7:
  88. this._houseCCD7.Text = item.ccdId.ToString();
  89. break;
  90. case 8:
  91. this._houseCCD8.Text = item.ccdId.ToString();
  92. break;
  93. case 9:
  94. this._houseCCD9.Text = item.ccdId.ToString();
  95. break;
  96. case 10:
  97. this._houseCCD10.Text = item.ccdId.ToString();
  98. break;
  99. }
  100. }
  101. }
  102. private void RefshInfo()
  103. {
  104. Dispatcher.Invoke(() =>
  105. {
  106. var aa = vm.houseWellSettingList.FirstOrDefault(x => x.houseSn == vm.CurrentHouseId && x.wellSn == vm.CurrentWell);
  107. if (aa == null)
  108. {
  109. this._well_TextBlock.Text = "0";
  110. this._eepromPosition.Text = "0";
  111. }
  112. else
  113. {
  114. this._well_TextBlock.Text = aa.horizontalMotorPosition.ToString();
  115. this._eepromPosition.Text = aa.eepromClearPosition.ToString();
  116. }
  117. var bb = vm.ccdPhoto.FirstOrDefault(x => x.houseSn == vm.CurrentHouseId && x.wellSn == vm.CurrentWell);
  118. if (bb == null)
  119. {
  120. this._autoFocus.Text = "0";
  121. this._ccdOne.Text = "0";
  122. }
  123. else
  124. {
  125. this._autoFocus.Text = bb.autoFocusPosition.ToString();
  126. this._ccdOne.Text = bb.clearestPosition.ToString();
  127. }
  128. });
  129. }
  130. DateTime logTime = DateTime.Now;
  131. private void AddMessageInfo(string message)
  132. {
  133. lock (locker)
  134. {
  135. logTime = DateTime.Now;
  136. message = $"{logTime.ToString("yyyy-MM-dd HH:mm:ss")}.{logTime.Millisecond}{message}";
  137. Dispatcher.Invoke(() =>
  138. {
  139. vm.MessageInfoList.Add(message);
  140. _messageList.ScrollIntoView(message);
  141. });
  142. }
  143. }
  144. private void AddPic(string fileName, int well, int focal)
  145. {
  146. Dispatcher.Invoke(() =>
  147. {
  148. if (!File.Exists(fileName))
  149. {
  150. AddMessageInfo($"图片不存在:{fileName}");
  151. return;
  152. }
  153. string imageUrl = $"file:\\{fileName}";
  154. var imageSource = AppData.Instance.ConvertHelper.StringToBitmapImage(imageUrl);
  155. if (imageSource == null)
  156. {
  157. AddMessageInfo($"图片加载失败:{fileName}");
  158. return;
  159. }
  160. Canvas canvas = new Canvas();
  161. _warpPanelPictures.Children.Add(canvas);
  162. TextBlock text = new TextBlock();
  163. Image image = new Image();
  164. canvas.Children.Add(image);
  165. canvas.Children.Add(text);
  166. canvas.Margin = new Thickness(20, 5, 20, 0);
  167. canvas.Width = 300;
  168. canvas.Height = 330;
  169. text.Text = $"well{well} - {focal}";
  170. text.FontSize = 22;
  171. text.Foreground = new SolidColorBrush(Color.FromRgb(181, 184, 189));
  172. Canvas.SetLeft(text, 80);
  173. Canvas.SetTop(text, 302);
  174. image.Width = 300;
  175. image.Height = 300;
  176. image.Source = imageSource;
  177. image.Tag = imageUrl;
  178. //image.MouseUp += (s, arg) =>
  179. //{
  180. // text.Foreground = new SolidColorBrush(Colors.Red);
  181. // PhotoListWindow win2 = new PhotoListWindow(_warpPanelPictures.Children, image, text);
  182. // win2.ShowDialog();
  183. //};
  184. });
  185. }
  186. /// <summary>
  187. /// 打开图像
  188. /// </summary>
  189. private void OpenVideo()
  190. {
  191. try
  192. {
  193. if (isOpen) { AddMessageInfo($"图像已打开"); return; }
  194. string sessionId = vm.CurrentSessionId;
  195. if (string.IsNullOrEmpty(sessionId)) { AddMessageInfo("未借用会话,无法预览(请先初始化)"); return; }
  196. // 每次开预览 new 新实例(MjpegStreamClient 一次性,不可复用)
  197. var client = new MjpegStreamClient(ivf_tl_Operate.Helpers.ControlClient.BaseUrl);
  198. _mjpeg = client;
  199. client.FrameReceived += img => Dispatcher.Invoke(() =>
  200. {
  201. if (_mjpeg != client) return; // 已被 CloseVideo 换/清,丢弃残帧(避免关画面后残帧写回)
  202. _previewImage.Source = img;
  203. });
  204. client.Stopped += reason => Dispatcher.Invoke(() =>
  205. {
  206. if (_mjpeg != client) return; // 旧实例的 Stopped 不串台到新会话
  207. isOpen = false;
  208. AddMessageInfo($"⚠ {reason}"); // 明确提示操作人员手动重开(不自动重连)
  209. });
  210. client.Start(sessionId);
  211. isOpen = true;
  212. AddMessageInfo("图像已打开(MJPEG 实时预览)");
  213. }
  214. catch (Exception ex)
  215. {
  216. ExLog(ex, "OpenVideo");
  217. AddMessageInfo($"图像打开异常:{ex.Message}");
  218. }
  219. }
  220. private void CloseVideo()
  221. {
  222. try
  223. {
  224. if (!isOpen) { AddMessageInfo($"图像未打开,无需关闭"); return; }
  225. _mjpeg?.Stop();
  226. _mjpeg = null; // 丢弃实例(一次性,下次开预览 new 新的)
  227. Dispatcher.Invoke(() => { _previewImage.Source = null; });
  228. isOpen = false;
  229. AddMessageInfo("图像已关闭");
  230. }
  231. catch (Exception ex)
  232. {
  233. ExLog(ex, "CloseVideo");
  234. AddMessageInfo($"图像关闭异常:{ex.Message}");
  235. }
  236. }
  237. private void ExLog(Exception ex, string name)
  238. {
  239. AppData.Instance.LogHelper.ExceptionLog(ex, $"HouseDebugPageView.{name}", LogEnum.RunException);
  240. }
  241. private void ShowMessageDefeat(string mess)
  242. {
  243. Dispatcher.Invoke(() =>
  244. {
  245. MessageBox.Show(mess);
  246. });
  247. //AddMessageInfo(mess);
  248. //new MessagePrompt().ShowCenterOwnerDefeat(AppData.Instance.MainWindow, mess);
  249. }
  250. private void ShowMessage(string mess)
  251. {
  252. }
  253. private void ShowMessageSuccess(string mess)
  254. {
  255. new MessagePrompt().ShowCenterOwnerSuccess(AppData.Instance.MainWindow, mess);
  256. }
  257. #endregion
  258. private void Return_Click(object sender, RoutedEventArgs e)
  259. {
  260. CloseVideo();
  261. vm.ComHouseUnit();
  262. SettingPageView settingPageView = new SettingPageView();
  263. AppData.Instance.MainWindow.LoadPage(settingPageView);
  264. }
  265. private void House_Checked(object sender, RoutedEventArgs e)
  266. {
  267. if (vm == null) return;
  268. if (!(sender is RadioButtonNoFrame radioButtonNoFrame)) return;
  269. vm.CurrentHouseId = int.Parse(radioButtonNoFrame.Tag.ToString());
  270. vm.CurrentHouse = vm.HouseList.First(x => x.houseSn == vm.CurrentHouseId);
  271. }
  272. private void CCD_Checked(object sender, RoutedEventArgs e)
  273. {
  274. if (vm == null) return;
  275. if (!(sender is RadioButtonNoFrame radioButtonNoFrame)) return;
  276. string ccdidString = radioButtonNoFrame.Tag.ToString();
  277. vm.CurrentCCDId = int.Parse(ccdidString);
  278. this._ccdId_TextBlock.Text = ccdidString;
  279. }
  280. /// <summary>
  281. /// 初始化
  282. /// </summary>
  283. /// <param name="sender"></param>
  284. /// <param name="e"></param>
  285. private async void Start_Click(object sender, RoutedEventArgs e)
  286. {
  287. try
  288. {
  289. this._house_StackPanel.IsEnabled = false;
  290. this._ccd_StackPanel.IsEnabled = false;
  291. this._startButton.IsEnabled = false;
  292. //await Task.Delay(3000 * 5);
  293. //return;
  294. await vm.ComHouseInit();
  295. RefshInfo();
  296. OpenVideo();
  297. this._button1.IsEnabled = true;
  298. this._button2.IsEnabled = true;
  299. this._button3.IsEnabled = true;
  300. this._button4.IsEnabled = true;
  301. this._button5.IsEnabled = true;
  302. this._button6.IsEnabled = true;
  303. this._button7.IsEnabled = true;
  304. this._button90.IsEnabled = true;
  305. this._button100.IsEnabled = true;
  306. this._stackPanel1.IsEnabled = true;
  307. this._stackPanel2.IsEnabled = true;
  308. this._stackPanel3.IsEnabled = true;
  309. this._stackPanel4.IsEnabled = true;
  310. }
  311. catch (Exception ex)
  312. {
  313. ExLog(ex, "Start_Click");
  314. }
  315. }
  316. /// <summary>
  317. /// 读E方
  318. /// </summary>
  319. /// <param name="sender"></param>
  320. /// <param name="e"></param>
  321. private void RedE_Click(object sender, RoutedEventArgs e)
  322. {
  323. }
  324. /// <summary>
  325. /// 读温度
  326. /// </summary>
  327. /// <param name="sender"></param>
  328. /// <param name="e"></param>
  329. private void RedTem_Click(object sender, RoutedEventArgs e)
  330. {
  331. Task.Run(() =>
  332. {
  333. vm.RedTem();
  334. });
  335. }
  336. /// <summary>
  337. /// 读压力
  338. /// </summary>
  339. /// <param name="sender"></param>
  340. /// <param name="e"></param>
  341. private void RedPre_Click(object sender, RoutedEventArgs e)
  342. {
  343. Task.Run(() =>
  344. {
  345. vm.RedPre();
  346. });
  347. }
  348. /// <summary>
  349. /// 读舱门
  350. /// </summary>
  351. /// <param name="sender"></param>
  352. /// <param name="e"></param>
  353. private void RedDoor_Click(object sender, RoutedEventArgs e)
  354. {
  355. Task.Run(() =>
  356. {
  357. vm.RedDoor();
  358. });
  359. }
  360. /// <summary>
  361. /// 开灯
  362. /// </summary>
  363. /// <param name="sender"></param>
  364. /// <param name="e"></param>
  365. private void OpenLed_Click(object sender, RoutedEventArgs e)
  366. {
  367. Task.Run(() =>
  368. {
  369. vm.OpenLed();
  370. });
  371. }
  372. /// <summary>
  373. /// 关灯
  374. /// </summary>
  375. /// <param name="sender"></param>
  376. /// <param name="e"></param>
  377. private void CloseLed_Click(object sender, RoutedEventArgs e)
  378. {
  379. Task.Run(() =>
  380. {
  381. vm.CloseLed();
  382. });
  383. }
  384. /// <summary>
  385. /// 卸载
  386. /// </summary>
  387. /// <param name="sender"></param>
  388. /// <param name="e"></param>
  389. private void End_Click(object sender, RoutedEventArgs e)
  390. {
  391. if (isOpen)
  392. {
  393. CloseVideo();
  394. }
  395. vm.IsStop = true;
  396. vm.ComHouseUnit();
  397. this._house_StackPanel.IsEnabled = true;
  398. this._ccd_StackPanel.IsEnabled = true;
  399. this._startButton.IsEnabled = true;
  400. this._button1.IsEnabled = false;
  401. this._button2.IsEnabled = false;
  402. this._button3.IsEnabled = false;
  403. this._button4.IsEnabled = false;
  404. this._button5.IsEnabled = false;
  405. this._button6.IsEnabled = false;
  406. this._button7.IsEnabled = false;
  407. this._button90.IsEnabled = false;
  408. this._button100.IsEnabled = false;
  409. this._stackPanel1.IsEnabled = false;
  410. this._stackPanel2.IsEnabled = false;
  411. this._stackPanel3.IsEnabled = false;
  412. this._stackPanel4.IsEnabled = false;
  413. }
  414. /// <summary>
  415. /// 打开进气阀
  416. /// </summary>
  417. /// <param name="sender"></param>
  418. /// <param name="e"></param>
  419. private void OpenIntake_Click(object sender, RoutedEventArgs e)
  420. {
  421. Task.Run(() =>
  422. {
  423. vm.OpenIntake();
  424. });
  425. }
  426. /// <summary>
  427. /// 关闭进气阀
  428. /// </summary>
  429. /// <param name="sender"></param>
  430. /// <param name="e"></param>
  431. private void CloseIntake_Click(object sender, RoutedEventArgs e)
  432. {
  433. Task.Run(() =>
  434. {
  435. vm.CloseIntake();
  436. });
  437. }
  438. /// <summary>
  439. /// 打开排气阀
  440. /// </summary>
  441. /// <param name="sender"></param>
  442. /// <param name="e"></param>
  443. private void OpenExhaust_Click(object sender, RoutedEventArgs e)
  444. {
  445. Task.Run(() =>
  446. {
  447. vm.OpenExhaust();
  448. });
  449. }
  450. /// <summary>
  451. /// 关闭排气阀
  452. /// </summary>
  453. /// <param name="sender"></param>
  454. /// <param name="e"></param>
  455. private void CloseExhaust_Click(object sender, RoutedEventArgs e)
  456. {
  457. Task.Run(() =>
  458. {
  459. vm.CloseExhaust();
  460. });
  461. }
  462. private void Aeration_Click(object sender, RoutedEventArgs e)
  463. {
  464. Task.Run(() =>
  465. {
  466. vm.HouseAeration();
  467. });
  468. }
  469. /// <summary>
  470. /// 写舱室进气阀时间
  471. /// </summary>
  472. /// <param name="sender"></param>
  473. /// <param name="e"></param>
  474. private void WriteOpenIntakeTime_Click(object sender, RoutedEventArgs e)
  475. {
  476. try
  477. {
  478. if (vm.CurrentHouse == null)
  479. {
  480. ShowMessageDefeat("未获取到对应的舱室信息");
  481. return;
  482. }
  483. string timeString = this._openIntakeTime_TextBox.Text.Trim();
  484. if (string.IsNullOrEmpty(timeString))
  485. {
  486. ShowMessageDefeat("请输入进气阀打开时间");
  487. return;
  488. }
  489. if (int.TryParse(timeString, out int value) && value >= 0)
  490. {
  491. Task.Run(() =>
  492. {
  493. vm.WriteOpenIntakeTime(value);
  494. if (vm.CurrentHouse != null)
  495. {
  496. vm.CurrentHouse.inletValveOpeningTime = value;
  497. vm.SetHouseInfo();
  498. }
  499. });
  500. }
  501. else
  502. {
  503. ShowMessageDefeat("进气阀打开时间只能输入大于等于0数字");
  504. return;
  505. }
  506. }
  507. catch (Exception ex)
  508. {
  509. ExLog(ex, "WriteOpenIntakeTime_Click");
  510. }
  511. }
  512. /// <summary>
  513. /// 写曝光时间
  514. /// </summary>
  515. /// <param name="sender"></param>
  516. /// <param name="e"></param>
  517. private void SetExposure_Click(object sender, RoutedEventArgs e)
  518. {
  519. try
  520. {
  521. if (vm.CurrentHouse == null)
  522. {
  523. ShowMessageDefeat("未获取到对应的舱室信息");
  524. return;
  525. }
  526. if (int.TryParse(this._exposure_TextBox.Text.Trim(), out int newExposure) && newExposure >= 0)
  527. {
  528. vm.SetExposure(newExposure);
  529. if (vm.CurrentHouse != null)
  530. {
  531. vm.CurrentHouse.ccdExposure = newExposure;
  532. vm.SetHouseInfo();
  533. }
  534. return;
  535. }
  536. ShowMessageDefeat("曝光值只能输入大于等于0数字");
  537. return;
  538. }
  539. catch (Exception ex)
  540. {
  541. ExLog(ex, "SetExposure_Click");
  542. }
  543. }
  544. /// <summary>
  545. /// 水平电机复位
  546. /// </summary>
  547. /// <param name="sender"></param>
  548. /// <param name="e"></param>
  549. private void HorizontalMotorReset_Click(object sender, RoutedEventArgs e)
  550. {
  551. Task.Run(() =>
  552. {
  553. vm.HorizontalMotorReset();
  554. RefshInfo();
  555. });
  556. }
  557. /// <summary>
  558. /// 保存当前well水平电机位置
  559. /// </summary>
  560. /// <param name="sender"></param>
  561. /// <param name="e"></param>
  562. private void SaveWellHorizontalMotor_Click(object sender, RoutedEventArgs e)
  563. {
  564. if (vm.CurrentHouse == null)
  565. {
  566. ShowMessageDefeat("未获取到对应的舱室信息");
  567. return;
  568. }
  569. Task.Run(() =>
  570. {
  571. try
  572. {
  573. vm.SaveWellHor();
  574. if (!vm.SetWellMotorPosition())
  575. {
  576. ShowMessageDefeat("数据库保存失败");
  577. return;
  578. }
  579. var aa = vm.houseWellSettingList.FirstOrDefault(x => x.houseSn == vm.CurrentHouseId && x.wellSn == vm.CurrentWell);
  580. if (aa != null)
  581. {
  582. aa.horizontalMotorPosition = vm.CurrentHor;
  583. Dispatcher.Invoke(() =>
  584. {
  585. this._well_TextBlock.Text = aa.horizontalMotorPosition.ToString();
  586. });
  587. }
  588. else
  589. {
  590. ShowMessageDefeat("未获取到well位置");
  591. return;
  592. }
  593. }
  594. catch (Exception ex)
  595. {
  596. ExLog(ex, "SaveWellHorizontalMotor_Click");
  597. }
  598. });
  599. }
  600. /// <summary>
  601. /// 水平电机正向移动
  602. /// </summary>
  603. /// <param name="sender"></param>
  604. /// <param name="e"></param>
  605. private void HorizontalMotorForward_Click(object sender, RoutedEventArgs e)
  606. {
  607. string forwardString = this._horizontalMotorForward_TextBox.Text.Trim();
  608. if (string.IsNullOrEmpty(forwardString))
  609. {
  610. MessageBox.Show("请输入正向移动脉冲");
  611. return;
  612. }
  613. if (int.TryParse(forwardString, out int value))
  614. {
  615. Task.Run(() =>
  616. {
  617. vm.HorizontalMotorForward(value);
  618. });
  619. }
  620. else
  621. {
  622. MessageBox.Show("正向移动脉冲只能输入数字");
  623. return;
  624. }
  625. }
  626. /// <summary>
  627. /// 水平电机反向运动
  628. /// </summary>
  629. /// <param name="sender"></param>
  630. /// <param name="e"></param>
  631. private void HorizontalMotorBackward_Click(object sender, RoutedEventArgs e)
  632. {
  633. string backwardString = this._horizontalMotorBackward_TextBox.Text.Trim();
  634. if (string.IsNullOrEmpty(backwardString))
  635. {
  636. MessageBox.Show("请输入反向移动脉冲");
  637. return;
  638. }
  639. if (int.TryParse(backwardString, out int value))
  640. {
  641. Task.Run(() =>
  642. {
  643. vm.HorizontalMotorBackward(value);
  644. });
  645. }
  646. else
  647. {
  648. MessageBox.Show("反向移动脉冲只能输入数字");
  649. return;
  650. }
  651. }
  652. /// <summary>
  653. /// 水平电机移动到指定Well
  654. /// </summary>
  655. /// <param name="sender"></param>
  656. /// <param name="e"></param>
  657. private void HorizontalMotorToWell_Click(object sender, RoutedEventArgs e)
  658. {
  659. string wellString = this._horizontalMotorToWell_TextBox.Text.Trim();
  660. if (string.IsNullOrEmpty(wellString))
  661. {
  662. ShowMessageDefeat("请输入目标well");
  663. return;
  664. }
  665. if (int.TryParse(wellString, out int value) && value >= 1 && value <= 16)
  666. {
  667. Task.Run(() =>
  668. {
  669. vm.HorizontalMotorToWell(value);
  670. RefshInfo();
  671. });
  672. }
  673. else
  674. {
  675. ShowMessageDefeat("目标well只能输入1-16的数字");
  676. return;
  677. }
  678. }
  679. /// <summary>
  680. /// 下一well
  681. /// </summary>
  682. /// <param name="sender"></param>
  683. /// <param name="e"></param>
  684. private void HorizontalMotorNextWell_Click(object sender, RoutedEventArgs e)
  685. {
  686. Task.Run(() =>
  687. {
  688. vm.HorizontalMotorToWell(vm.CurrentWell + 1);
  689. RefshInfo();
  690. });
  691. }
  692. /// <summary>
  693. /// 垂直电机复位
  694. /// </summary>
  695. /// <param name="sender"></param>
  696. /// <param name="e"></param>
  697. private void VerticalMotorResetWait_Click(object sender, RoutedEventArgs e)
  698. {
  699. Task.Run(() =>
  700. {
  701. vm.VerticalMotorReset();
  702. vm.CurrentFocal = 0;
  703. });
  704. }
  705. /// <summary>
  706. /// 垂直电机正向移动
  707. /// </summary>
  708. /// <param name="sender"></param>
  709. /// <param name="e"></param>
  710. private void VerticalMotorForward_Click(object sender, RoutedEventArgs e)
  711. {
  712. string forwardString = this._verticalMotorForward_TextBox.Text.Trim();
  713. if (string.IsNullOrEmpty(forwardString))
  714. {
  715. MessageBox.Show("请输入正向移动脉冲");
  716. return;
  717. }
  718. if (int.TryParse(forwardString, out int value))
  719. {
  720. Task.Run(() =>
  721. {
  722. vm.VerticalMotorForward(value);
  723. });
  724. }
  725. else
  726. {
  727. MessageBox.Show("正向移动脉冲只能输入数字");
  728. return;
  729. }
  730. }
  731. /// <summary>
  732. /// 垂直电机反向运动
  733. /// </summary>
  734. /// <param name="sender"></param>
  735. /// <param name="e"></param>
  736. private void VerticalMotorBackward_Click(object sender, RoutedEventArgs e)
  737. {
  738. string backwardString = this._verticalMotorBackward_TextBox.Text.Trim();
  739. if (string.IsNullOrEmpty(backwardString))
  740. {
  741. MessageBox.Show("请输入反向移动脉冲");
  742. return;
  743. }
  744. if (int.TryParse(backwardString, out int value))
  745. {
  746. Task.Run(() =>
  747. {
  748. vm.VerticalMotorBackward(value);
  749. });
  750. }
  751. else
  752. {
  753. MessageBox.Show("反向移动脉冲只能输入数字");
  754. return;
  755. }
  756. }
  757. /// <summary>
  758. /// 垂直电机绝对运动
  759. /// </summary>
  760. /// <param name="sender"></param>
  761. /// <param name="e"></param>
  762. private void VerticalMotorAbsolute_Click(object sender, RoutedEventArgs e)
  763. {
  764. string absoluteString = this._verticalMotorAbsolute_TextBox.Text.Trim();
  765. if (string.IsNullOrEmpty(absoluteString))
  766. {
  767. MessageBox.Show("请输入目标脉冲");
  768. return;
  769. }
  770. if (int.TryParse(absoluteString, out int value) && value >= 0 && value <= vm.tLSetting.verticalMotorPulseMax)
  771. {
  772. Task.Run(() =>
  773. {
  774. vm.VerticalMotorAbsolute(value);
  775. });
  776. }
  777. else
  778. {
  779. MessageBox.Show($"目标脉冲只能输入0-{vm.tLSetting.verticalMotorPulseMax}的数字");
  780. return;
  781. }
  782. }
  783. /// <summary>
  784. /// 保存垂直电机间隔脉冲
  785. /// </summary>
  786. /// <param name="sender"></param>
  787. /// <param name="e"></param>
  788. private void SaveVerSpacePulse_Click(object sender, RoutedEventArgs e)
  789. {
  790. try
  791. {
  792. if (vm.CurrentHouse == null)
  793. {
  794. ShowMessageDefeat("未获取到对应的舱室信息");
  795. return;
  796. }
  797. if (int.TryParse(this._verticalMotorForward_TextBox.Text.Trim(), out int newValue) && newValue > 0 && newValue <= vm.tLSetting.verticalMotorPulseMax)
  798. {
  799. Task.Run(() =>
  800. {
  801. vm.WriteOVerSpace(newValue);
  802. vm.CurrentHouse.verticalMotorSpacePulse = newValue;
  803. vm.SetHouseInfo();
  804. });
  805. }
  806. else
  807. {
  808. AddMessageInfo($"垂直电机间隔脉冲只能输入0-{vm.tLSetting.verticalMotorPulseMax}的数字");
  809. }
  810. }
  811. catch (Exception ex)
  812. {
  813. AddMessageInfo($"保存垂直电机间隔脉冲异常:{ex.Message}");
  814. ExLog(ex, "SaveVerSpacePulse_Click");
  815. }
  816. }
  817. /// <summary>
  818. /// 清空
  819. /// </summary>
  820. /// <param name="sender"></param>
  821. /// <param name="e"></param>
  822. private void Clear_Click(object sender, RoutedEventArgs e)
  823. {
  824. vm.MessageInfoList.Clear();
  825. this._warpPanelPictures.Children.Clear();
  826. }
  827. /// <summary>
  828. /// 一键电机准备
  829. /// </summary>
  830. /// <param name="sender"></param>
  831. /// <param name="e"></param>
  832. private void Motor_Click(object sender, RoutedEventArgs e)
  833. {
  834. Task.Run(() =>
  835. {
  836. vm.MototReady();
  837. });
  838. }
  839. /// <summary>
  840. /// 单张抓拍
  841. /// </summary>
  842. /// <param name="sender"></param>
  843. /// <param name="e"></param>
  844. private void GetPic_Click(object sender, RoutedEventArgs e)
  845. {
  846. try
  847. {
  848. if (!isOpen)
  849. {
  850. AddMessageInfo("开启实时图像以后才可以拍照");
  851. return;
  852. }
  853. if (vm.CurrentHouse == null)
  854. {
  855. AddMessageInfo("获取舱室信息失败");
  856. return;
  857. }
  858. string path = AppData.Instance.LogHelper.GetDeBugDanZhangDirectory(vm.CurrentHouse.houseSn, vm.CurrentWell);
  859. if (!Directory.Exists(path))
  860. {
  861. Directory.CreateDirectory(path);
  862. }
  863. string fullName = $"{path}house{vm.CurrentHouse.houseSn}_{DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-fff")}_{vm.CurrentHor}-{vm.CurrentVer}.jpg";
  864. if (!vm.SavePic(fullName)) return;
  865. AddPic(fullName, vm.CurrentWell, vm.CurrentFocal);
  866. }
  867. catch (Exception ex)
  868. {
  869. ExLog(ex, "GetPic_Click");
  870. AddMessageInfo($"单张抓拍异常:{ex.Message}");
  871. }
  872. }
  873. /// <summary>
  874. /// 水平抓拍
  875. /// </summary>
  876. /// <param name="sender"></param>
  877. /// <param name="e"></param>
  878. private void ShuiPingPic_Click(object sender, RoutedEventArgs e)
  879. {
  880. if (vm.CurrentHouse == null)
  881. {
  882. AddMessageInfo("获取舱室信息失败");
  883. return;
  884. }
  885. if (!isOpen)
  886. {
  887. AddMessageInfo("开启实时图像以后才可以拍照");
  888. return;
  889. }
  890. Task.Run(() =>
  891. {
  892. vm.ShuiPingZhuaPai();
  893. });
  894. }
  895. /// <summary>
  896. /// 结束抓图
  897. /// </summary>
  898. /// <param name="sender"></param>
  899. /// <param name="e"></param>
  900. private void EndPic_Click(object sender, RoutedEventArgs e)
  901. {
  902. vm.IsStop = true;
  903. }
  904. /// <summary>
  905. /// 清晰图层抓图
  906. /// </summary>
  907. /// <param name="sender"></param>
  908. /// <param name="e"></param>
  909. private async void AutoFocus_Click(object sender, RoutedEventArgs e)
  910. {
  911. try
  912. {
  913. if (vm.CurrentHouse == null)
  914. {
  915. ShowMessageDefeat("获取舱室信息失败");
  916. return;
  917. }
  918. if (int.TryParse(this._autoFocusNum_TextBox.Text.Trim(), out int autoFocusNum) && autoFocusNum > 0 && int.TryParse(this._autoFocus_TextBox.Text.Trim(), out int xun) && xun > 0)
  919. {
  920. this._auto_Button.IsEnabled = false;
  921. await vm.AutoFocusPic(autoFocusNum, xun);
  922. this._auto_Button.IsEnabled = true;
  923. }
  924. else
  925. {
  926. ShowMessageDefeat($"请输入正确的拍摄次数以及循环次数");
  927. }
  928. }
  929. catch (Exception ex)
  930. {
  931. ExLog(ex, "AutoFocus_Click");
  932. AddMessageInfo($"清晰图层抓图异常:{ex.Message}");
  933. }
  934. }
  935. private void CloseVideo_Click(object sender, RoutedEventArgs e)
  936. {
  937. try
  938. {
  939. CloseVideo();
  940. }
  941. catch (Exception ex)
  942. {
  943. ExLog(ex, "CloseVideo_Click");
  944. AddMessageInfo($"图像关闭异常:{ex.Message}");
  945. }
  946. }
  947. /// <summary>
  948. /// M2-05 场景A 一键全自动标定(沙盒)。对选中 well(默认 16 well)逐个跑四步标定,
  949. /// 合格绿/伪峰红实时显示,结果作出厂基准 scene=0 存档 + 写 calibration.json。
  950. /// 硬件复用调试页已借用并打开的 comBin/camera(经适配器暴露为 HAL 接口),不二次 Open/Init。
  951. /// </summary>
  952. private async void OneClickCalibrate_Click(object sender, RoutedEventArgs e)
  953. {
  954. try
  955. {
  956. if (vm.CurrentHouse == null)
  957. {
  958. ShowMessageDefeat("获取舱室信息失败,请先初始化舱室");
  959. return;
  960. }
  961. if (vm.IsCalibrating)
  962. {
  963. AddMessageInfo("[一键标定]已在标定中");
  964. return;
  965. }
  966. this._oneClickCalib_Button.IsEnabled = false;
  967. try
  968. {
  969. // 沙盒默认全 16 well 逐个跑(如需勾选子集,传入选中 well 列表即可)。
  970. await vm.OneClickCalibrate(null);
  971. }
  972. finally
  973. {
  974. this._oneClickCalib_Button.IsEnabled = true;
  975. }
  976. }
  977. catch (Exception ex)
  978. {
  979. ExLog(ex, "OneClickCalibrate_Click");
  980. AddMessageInfo($"一键标定异常:{ex.Message}");
  981. }
  982. }
  983. /// <summary>M2-05 中止一键标定(当前 well 结束后停止,已完成结果保留)。</summary>
  984. private void StopCalibrate_Click(object sender, RoutedEventArgs e)
  985. {
  986. try
  987. {
  988. vm.StopCalibrate();
  989. }
  990. catch (Exception ex)
  991. {
  992. ExLog(ex, "StopCalibrate_Click");
  993. }
  994. }
  995. /// <summary>
  996. /// M2-07 载入选定 well 当前生效的拍摄层配置(well 级覆盖优先,空则显示继承的设备级值占位),
  997. /// 回填三个手调框并触发一次预览。
  998. /// </summary>
  999. private void LoadManualTune_Click(object sender, RoutedEventArgs e)
  1000. {
  1001. try
  1002. {
  1003. if (vm.CurrentHouse == null)
  1004. {
  1005. ShowMessageDefeat("获取舱室信息失败,请先初始化舱室");
  1006. return;
  1007. }
  1008. if (vm.ManualTuneWell < 1 || vm.ManualTuneWell > 16)
  1009. {
  1010. ShowMessageDefeat("well 只能为 1-16");
  1011. return;
  1012. }
  1013. vm.LoadManualLayerSetting(vm.ManualTuneWell);
  1014. }
  1015. catch (Exception ex)
  1016. {
  1017. ExLog(ex, "LoadManualTune_Click");
  1018. }
  1019. }
  1020. /// <summary>
  1021. /// M2-07 实时预览:用该 well 标定 FocusZ + 当前手调值调 M2-02 ComputeLayerPositions 算各层 Z(关联 V-045)。
  1022. /// </summary>
  1023. private void PreviewManualTune_Click(object sender, RoutedEventArgs e)
  1024. {
  1025. try
  1026. {
  1027. if (vm.CurrentHouse == null)
  1028. {
  1029. ShowMessageDefeat("获取舱室信息失败,请先初始化舱室");
  1030. return;
  1031. }
  1032. vm.PreviewManualLayers();
  1033. }
  1034. catch (Exception ex)
  1035. {
  1036. ExLog(ex, "PreviewManualTune_Click");
  1037. }
  1038. }
  1039. /// <summary>
  1040. /// M2-07 存为该 well 默认:校验通过后写 house_well_setting well 级覆盖
  1041. /// (focus_layer_spacing_pulse/focus_layer_count + 复用 move_down_layer),留空继承设备级。
  1042. /// </summary>
  1043. private void SaveManualTune_Click(object sender, RoutedEventArgs e)
  1044. {
  1045. try
  1046. {
  1047. if (vm.CurrentHouse == null)
  1048. {
  1049. ShowMessageDefeat("获取舱室信息失败,请先初始化舱室");
  1050. return;
  1051. }
  1052. if (vm.SaveManualLayerTune())
  1053. {
  1054. ShowMessageSuccess("手调拍摄层已存为该 well 默认");
  1055. }
  1056. else
  1057. {
  1058. ShowMessageDefeat("手调拍摄层保存失败(见日志,可能为非法值或接口失败)");
  1059. }
  1060. }
  1061. catch (Exception ex)
  1062. {
  1063. ExLog(ex, "SaveManualTune_Click");
  1064. }
  1065. }
  1066. /// <summary>D2-02-T3.5 载入选定 well 当前 well 级对焦半幅覆盖值(空=继承设备级)。</summary>
  1067. private void LoadFocusRange_Click(object sender, RoutedEventArgs e)
  1068. {
  1069. try
  1070. {
  1071. if (vm.CurrentHouse == null)
  1072. {
  1073. ShowMessageDefeat("获取舱室信息失败,请先初始化舱室");
  1074. return;
  1075. }
  1076. if (vm.ManualTuneWell < 1 || vm.ManualTuneWell > 16)
  1077. {
  1078. ShowMessageDefeat("well 只能为 1-16");
  1079. return;
  1080. }
  1081. vm.LoadWellFocusRange(vm.ManualTuneWell);
  1082. }
  1083. catch (Exception ex)
  1084. {
  1085. ExLog(ex, "LoadFocusRange_Click");
  1086. }
  1087. }
  1088. /// <summary>D2-02-T3.5 保存该 well 对焦搜索半幅(写 house_well_setting well 级覆盖,留空继承设备级)。</summary>
  1089. private void SaveFocusRange_Click(object sender, RoutedEventArgs e)
  1090. {
  1091. try
  1092. {
  1093. if (vm.CurrentHouse == null)
  1094. {
  1095. ShowMessageDefeat("获取舱室信息失败,请先初始化舱室");
  1096. return;
  1097. }
  1098. if (vm.SaveWellFocusRange())
  1099. {
  1100. ShowMessageSuccess("对焦搜索半幅已保存为该 well 覆盖");
  1101. }
  1102. else
  1103. {
  1104. ShowMessageDefeat("对焦半幅保存失败(见日志,可能为非法值或接口失败)");
  1105. }
  1106. }
  1107. catch (Exception ex)
  1108. {
  1109. ExLog(ex, "SaveFocusRange_Click");
  1110. }
  1111. }
  1112. /// <summary>D2-02-T3.4 逐孔微调:对该格 well 经 control 协作单孔重标。</summary>
  1113. private async void RecalibrateWell_Click(object sender, RoutedEventArgs e)
  1114. {
  1115. try
  1116. {
  1117. if (!(sender is FrameworkElement fe) || fe.Tag == null) return;
  1118. if (!int.TryParse(fe.Tag.ToString(), out int well)) return;
  1119. await vm.RecalibrateWell(well);
  1120. }
  1121. catch (Exception ex)
  1122. {
  1123. ExLog(ex, "RecalibrateWell_Click");
  1124. AddMessageInfo($"单孔重标异常:{ex.Message}");
  1125. }
  1126. }
  1127. private void OpenVideo_Click(object sender, RoutedEventArgs e)
  1128. {
  1129. try
  1130. {
  1131. OpenVideo();
  1132. }
  1133. catch (Exception ex)
  1134. {
  1135. ExLog(ex, "OpenVideo_Click");
  1136. AddMessageInfo($"图像打开异常:{ex.Message}");
  1137. }
  1138. }
  1139. /// <summary>
  1140. /// 保存自动对焦起点
  1141. /// </summary>
  1142. /// <param name="sender"></param>
  1143. /// <param name="e"></param>
  1144. private void SaveAutoFocus_Click(object sender, RoutedEventArgs e)
  1145. {
  1146. try
  1147. {
  1148. if (vm.SetWellAutoFocus())
  1149. {
  1150. var bb = vm.ccdPhoto.FirstOrDefault(x => x.houseSn == vm.CurrentHouseId && x.wellSn == vm.CurrentWell);
  1151. if (bb != null) bb.autoFocusPosition = vm.CurrentVer;
  1152. this._autoFocus.Text = vm.CurrentVer.ToString();
  1153. AddMessageInfo($"自动对焦起点保存成功");
  1154. }
  1155. else
  1156. {
  1157. AddMessageInfo($"自动对焦起点保存失败");
  1158. }
  1159. }
  1160. catch (Exception ex)
  1161. {
  1162. ExLog(ex, "SaveAutoFocus_Click");
  1163. AddMessageInfo($"保存自动对焦起点异常:{ex.Message}");
  1164. }
  1165. }
  1166. private void _autoFocusNum_TextBox_TextChanged(object sender, TextChangedEventArgs e)
  1167. {
  1168. TextBox passwordBox = sender as TextBox;
  1169. if (passwordBox == null) return;
  1170. string ss = passwordBox.Text.Trim();
  1171. if (string.IsNullOrEmpty(ss))
  1172. {
  1173. passwordBox.Background = (VisualBrush)this.FindResource("HelpBrush2");
  1174. }
  1175. else
  1176. {
  1177. passwordBox.Background = new SolidColorBrush(Colors.White);
  1178. }
  1179. }
  1180. private void _autoFocus_TextBox_TextChanged(object sender, TextChangedEventArgs e)
  1181. {
  1182. TextBox passwordBox = sender as TextBox;
  1183. if (passwordBox == null) return;
  1184. string ss = passwordBox.Text.Trim();
  1185. if (string.IsNullOrEmpty(ss))
  1186. {
  1187. passwordBox.Background = (VisualBrush)this.FindResource("HelpBrush3");
  1188. }
  1189. else
  1190. {
  1191. passwordBox.Background = new SolidColorBrush(Colors.White);
  1192. }
  1193. }
  1194. private void Paiqi_Click(object sender, RoutedEventArgs e)
  1195. {
  1196. Task.Run(() =>
  1197. {
  1198. vm.HouseVent();
  1199. });
  1200. }
  1201. private void WriteOpenVentTime_Click(object sender, RoutedEventArgs e)
  1202. {
  1203. try
  1204. {
  1205. if (vm.CurrentHouse == null)
  1206. {
  1207. ShowMessageDefeat("未获取到对应的舱室信息");
  1208. return;
  1209. }
  1210. string timeString = this._openVentTime_TextBox.Text.Trim();
  1211. if (string.IsNullOrEmpty(timeString))
  1212. {
  1213. ShowMessageDefeat("请输入排气阀打开时间");
  1214. return;
  1215. }
  1216. if (int.TryParse(timeString, out int value) && value >= 0)
  1217. {
  1218. Task.Run(() =>
  1219. {
  1220. vm.WriteOpenVentTime(value);
  1221. //if (vm.CurrentHouse != null)
  1222. //{
  1223. // vm.CurrentHouse.inletValveOpeningTime = value;
  1224. // vm.SetHouseInfo();
  1225. //}
  1226. });
  1227. }
  1228. else
  1229. {
  1230. ShowMessageDefeat("排气阀打开时间只能输入大于等于0数字");
  1231. return;
  1232. }
  1233. }
  1234. catch (Exception ex)
  1235. {
  1236. ExLog(ex, "WriteOpenVentTime_Click");
  1237. }
  1238. }
  1239. private void ReadOpenVentTime_Click(object sender, RoutedEventArgs e)
  1240. {
  1241. Task.Run(() =>
  1242. {
  1243. vm.RedVentTime();
  1244. });
  1245. }
  1246. }
  1247. }