HouseDebugPageView.xaml.cs 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357
  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(async () => await vm.RedTem());
  332. }
  333. /// <summary>
  334. /// 读压力
  335. /// </summary>
  336. /// <param name="sender"></param>
  337. /// <param name="e"></param>
  338. private void RedPre_Click(object sender, RoutedEventArgs e)
  339. {
  340. Task.Run(async () =>
  341. {
  342. await vm.RedPre();
  343. });
  344. }
  345. /// <summary>
  346. /// 读舱门
  347. /// </summary>
  348. /// <param name="sender"></param>
  349. /// <param name="e"></param>
  350. private void RedDoor_Click(object sender, RoutedEventArgs e)
  351. {
  352. Task.Run(async () =>
  353. {
  354. await vm.RedDoor();
  355. });
  356. }
  357. /// <summary>
  358. /// 开灯
  359. /// </summary>
  360. /// <param name="sender"></param>
  361. /// <param name="e"></param>
  362. private void OpenLed_Click(object sender, RoutedEventArgs e)
  363. {
  364. Task.Run(async () =>
  365. {
  366. await vm.OpenLed();
  367. });
  368. }
  369. /// <summary>
  370. /// 关灯
  371. /// </summary>
  372. /// <param name="sender"></param>
  373. /// <param name="e"></param>
  374. private void CloseLed_Click(object sender, RoutedEventArgs e)
  375. {
  376. Task.Run(async () =>
  377. {
  378. await vm.CloseLed();
  379. });
  380. }
  381. /// <summary>
  382. /// 卸载
  383. /// </summary>
  384. /// <param name="sender"></param>
  385. /// <param name="e"></param>
  386. private void End_Click(object sender, RoutedEventArgs e)
  387. {
  388. if (isOpen)
  389. {
  390. CloseVideo();
  391. }
  392. vm.IsStop = true;
  393. vm.ComHouseUnit();
  394. this._house_StackPanel.IsEnabled = true;
  395. this._ccd_StackPanel.IsEnabled = true;
  396. this._startButton.IsEnabled = true;
  397. this._button1.IsEnabled = false;
  398. this._button2.IsEnabled = false;
  399. this._button3.IsEnabled = false;
  400. this._button4.IsEnabled = false;
  401. this._button5.IsEnabled = false;
  402. this._button6.IsEnabled = false;
  403. this._button7.IsEnabled = false;
  404. this._button90.IsEnabled = false;
  405. this._button100.IsEnabled = false;
  406. this._stackPanel1.IsEnabled = false;
  407. this._stackPanel2.IsEnabled = false;
  408. this._stackPanel3.IsEnabled = false;
  409. this._stackPanel4.IsEnabled = false;
  410. }
  411. /// <summary>
  412. /// 打开进气阀
  413. /// </summary>
  414. /// <param name="sender"></param>
  415. /// <param name="e"></param>
  416. private void OpenIntake_Click(object sender, RoutedEventArgs e)
  417. {
  418. Task.Run(async () =>
  419. {
  420. await vm.OpenIntake();
  421. });
  422. }
  423. /// <summary>
  424. /// 关闭进气阀
  425. /// </summary>
  426. /// <param name="sender"></param>
  427. /// <param name="e"></param>
  428. private void CloseIntake_Click(object sender, RoutedEventArgs e)
  429. {
  430. Task.Run(async () =>
  431. {
  432. await vm.CloseIntake();
  433. });
  434. }
  435. /// <summary>
  436. /// 打开排气阀
  437. /// </summary>
  438. /// <param name="sender"></param>
  439. /// <param name="e"></param>
  440. private void OpenExhaust_Click(object sender, RoutedEventArgs e)
  441. {
  442. Task.Run(async () =>
  443. {
  444. await vm.OpenExhaust();
  445. });
  446. }
  447. /// <summary>
  448. /// 关闭排气阀
  449. /// </summary>
  450. /// <param name="sender"></param>
  451. /// <param name="e"></param>
  452. private void CloseExhaust_Click(object sender, RoutedEventArgs e)
  453. {
  454. Task.Run(async () =>
  455. {
  456. await vm.CloseExhaust();
  457. });
  458. }
  459. private void Aeration_Click(object sender, RoutedEventArgs e)
  460. {
  461. Task.Run(async () =>
  462. {
  463. await vm.HouseAeration();
  464. });
  465. }
  466. /// <summary>
  467. /// 写舱室进气阀时间
  468. /// </summary>
  469. /// <param name="sender"></param>
  470. /// <param name="e"></param>
  471. private void WriteOpenIntakeTime_Click(object sender, RoutedEventArgs e)
  472. {
  473. try
  474. {
  475. if (vm.CurrentHouse == null)
  476. {
  477. ShowMessageDefeat("未获取到对应的舱室信息");
  478. return;
  479. }
  480. string timeString = this._openIntakeTime_TextBox.Text.Trim();
  481. if (string.IsNullOrEmpty(timeString))
  482. {
  483. ShowMessageDefeat("请输入进气阀打开时间");
  484. return;
  485. }
  486. if (int.TryParse(timeString, out int value) && value >= 0)
  487. {
  488. Task.Run(async () =>
  489. {
  490. await vm.WriteOpenIntakeTime(value);
  491. if (vm.CurrentHouse != null)
  492. {
  493. vm.CurrentHouse.inletValveOpeningTime = value;
  494. vm.SetHouseInfo();
  495. }
  496. });
  497. }
  498. else
  499. {
  500. ShowMessageDefeat("进气阀打开时间只能输入大于等于0数字");
  501. return;
  502. }
  503. }
  504. catch (Exception ex)
  505. {
  506. ExLog(ex, "WriteOpenIntakeTime_Click");
  507. }
  508. }
  509. /// <summary>
  510. /// 写曝光时间
  511. /// </summary>
  512. /// <param name="sender"></param>
  513. /// <param name="e"></param>
  514. private async void SetExposure_Click(object sender, RoutedEventArgs e)
  515. {
  516. try
  517. {
  518. if (vm.CurrentHouse == null)
  519. {
  520. ShowMessageDefeat("未获取到对应的舱室信息");
  521. return;
  522. }
  523. if (int.TryParse(this._exposure_TextBox.Text.Trim(), out int newExposure) && newExposure >= 0)
  524. {
  525. await vm.SetExposure(newExposure);
  526. if (vm.CurrentHouse != null)
  527. {
  528. vm.CurrentHouse.ccdExposure = newExposure;
  529. vm.SetHouseInfo();
  530. }
  531. return;
  532. }
  533. ShowMessageDefeat("曝光值只能输入大于等于0数字");
  534. return;
  535. }
  536. catch (Exception ex)
  537. {
  538. ExLog(ex, "SetExposure_Click");
  539. }
  540. }
  541. /// <summary>
  542. /// 水平电机复位
  543. /// </summary>
  544. /// <param name="sender"></param>
  545. /// <param name="e"></param>
  546. private void HorizontalMotorReset_Click(object sender, RoutedEventArgs e)
  547. {
  548. Task.Run(async () =>
  549. {
  550. await vm.HorizontalMotorReset();
  551. RefshInfo();
  552. });
  553. }
  554. /// <summary>
  555. /// 保存当前well水平电机位置
  556. /// </summary>
  557. /// <param name="sender"></param>
  558. /// <param name="e"></param>
  559. private void SaveWellHorizontalMotor_Click(object sender, RoutedEventArgs e)
  560. {
  561. if (vm.CurrentHouse == null)
  562. {
  563. ShowMessageDefeat("未获取到对应的舱室信息");
  564. return;
  565. }
  566. Task.Run(async () =>
  567. {
  568. try
  569. {
  570. await vm.SaveWellHor();
  571. if (!vm.SetWellMotorPosition())
  572. {
  573. ShowMessageDefeat("数据库保存失败");
  574. return;
  575. }
  576. var aa = vm.houseWellSettingList.FirstOrDefault(x => x.houseSn == vm.CurrentHouseId && x.wellSn == vm.CurrentWell);
  577. if (aa != null)
  578. {
  579. aa.horizontalMotorPosition = vm.CurrentHor;
  580. Dispatcher.Invoke(() =>
  581. {
  582. this._well_TextBlock.Text = aa.horizontalMotorPosition.ToString();
  583. });
  584. }
  585. else
  586. {
  587. ShowMessageDefeat("未获取到well位置");
  588. return;
  589. }
  590. }
  591. catch (Exception ex)
  592. {
  593. ExLog(ex, "SaveWellHorizontalMotor_Click");
  594. }
  595. });
  596. }
  597. /// <summary>
  598. /// 水平电机正向移动
  599. /// </summary>
  600. /// <param name="sender"></param>
  601. /// <param name="e"></param>
  602. private void HorizontalMotorForward_Click(object sender, RoutedEventArgs e)
  603. {
  604. string forwardString = this._horizontalMotorForward_TextBox.Text.Trim();
  605. if (string.IsNullOrEmpty(forwardString))
  606. {
  607. MessageBox.Show("请输入正向移动脉冲");
  608. return;
  609. }
  610. if (int.TryParse(forwardString, out int value))
  611. {
  612. Task.Run(async () =>
  613. {
  614. await vm.HorizontalMotorForward(value);
  615. });
  616. }
  617. else
  618. {
  619. MessageBox.Show("正向移动脉冲只能输入数字");
  620. return;
  621. }
  622. }
  623. /// <summary>
  624. /// 水平电机反向运动
  625. /// </summary>
  626. /// <param name="sender"></param>
  627. /// <param name="e"></param>
  628. private void HorizontalMotorBackward_Click(object sender, RoutedEventArgs e)
  629. {
  630. string backwardString = this._horizontalMotorBackward_TextBox.Text.Trim();
  631. if (string.IsNullOrEmpty(backwardString))
  632. {
  633. MessageBox.Show("请输入反向移动脉冲");
  634. return;
  635. }
  636. if (int.TryParse(backwardString, out int value))
  637. {
  638. Task.Run(async () =>
  639. {
  640. await vm.HorizontalMotorBackward(value);
  641. });
  642. }
  643. else
  644. {
  645. MessageBox.Show("反向移动脉冲只能输入数字");
  646. return;
  647. }
  648. }
  649. /// <summary>
  650. /// 水平电机移动到指定Well
  651. /// </summary>
  652. /// <param name="sender"></param>
  653. /// <param name="e"></param>
  654. private void HorizontalMotorToWell_Click(object sender, RoutedEventArgs e)
  655. {
  656. string wellString = this._horizontalMotorToWell_TextBox.Text.Trim();
  657. if (string.IsNullOrEmpty(wellString))
  658. {
  659. ShowMessageDefeat("请输入目标well");
  660. return;
  661. }
  662. if (int.TryParse(wellString, out int value) && value >= 1 && value <= 16)
  663. {
  664. Task.Run(async () =>
  665. {
  666. await vm.HorizontalMotorToWell(value);
  667. RefshInfo();
  668. });
  669. }
  670. else
  671. {
  672. ShowMessageDefeat("目标well只能输入1-16的数字");
  673. return;
  674. }
  675. }
  676. /// <summary>
  677. /// 下一well
  678. /// </summary>
  679. /// <param name="sender"></param>
  680. /// <param name="e"></param>
  681. private void HorizontalMotorNextWell_Click(object sender, RoutedEventArgs e)
  682. {
  683. Task.Run(async () =>
  684. {
  685. await vm.HorizontalMotorToWell(vm.CurrentWell + 1);
  686. RefshInfo();
  687. });
  688. }
  689. /// <summary>
  690. /// 垂直电机复位
  691. /// </summary>
  692. /// <param name="sender"></param>
  693. /// <param name="e"></param>
  694. private void VerticalMotorResetWait_Click(object sender, RoutedEventArgs e)
  695. {
  696. Task.Run(async () =>
  697. {
  698. await vm.VerticalMotorReset();
  699. vm.CurrentFocal = 0;
  700. });
  701. }
  702. /// <summary>
  703. /// 垂直电机正向移动
  704. /// </summary>
  705. /// <param name="sender"></param>
  706. /// <param name="e"></param>
  707. private void VerticalMotorForward_Click(object sender, RoutedEventArgs e)
  708. {
  709. string forwardString = this._verticalMotorForward_TextBox.Text.Trim();
  710. if (string.IsNullOrEmpty(forwardString))
  711. {
  712. MessageBox.Show("请输入正向移动脉冲");
  713. return;
  714. }
  715. if (int.TryParse(forwardString, out int value))
  716. {
  717. Task.Run(async () =>
  718. {
  719. await vm.VerticalMotorForward(value);
  720. });
  721. }
  722. else
  723. {
  724. MessageBox.Show("正向移动脉冲只能输入数字");
  725. return;
  726. }
  727. }
  728. /// <summary>
  729. /// 垂直电机反向运动
  730. /// </summary>
  731. /// <param name="sender"></param>
  732. /// <param name="e"></param>
  733. private void VerticalMotorBackward_Click(object sender, RoutedEventArgs e)
  734. {
  735. string backwardString = this._verticalMotorBackward_TextBox.Text.Trim();
  736. if (string.IsNullOrEmpty(backwardString))
  737. {
  738. MessageBox.Show("请输入反向移动脉冲");
  739. return;
  740. }
  741. if (int.TryParse(backwardString, out int value))
  742. {
  743. Task.Run(async () =>
  744. {
  745. await vm.VerticalMotorBackward(value);
  746. });
  747. }
  748. else
  749. {
  750. MessageBox.Show("反向移动脉冲只能输入数字");
  751. return;
  752. }
  753. }
  754. /// <summary>
  755. /// 垂直电机绝对运动
  756. /// </summary>
  757. /// <param name="sender"></param>
  758. /// <param name="e"></param>
  759. private void VerticalMotorAbsolute_Click(object sender, RoutedEventArgs e)
  760. {
  761. string absoluteString = this._verticalMotorAbsolute_TextBox.Text.Trim();
  762. if (string.IsNullOrEmpty(absoluteString))
  763. {
  764. MessageBox.Show("请输入目标脉冲");
  765. return;
  766. }
  767. if (int.TryParse(absoluteString, out int value) && value >= 0 && value <= vm.tLSetting.verticalMotorPulseMax)
  768. {
  769. Task.Run(async () =>
  770. {
  771. await vm.VerticalMotorAbsolute(value);
  772. });
  773. }
  774. else
  775. {
  776. MessageBox.Show($"目标脉冲只能输入0-{vm.tLSetting.verticalMotorPulseMax}的数字");
  777. return;
  778. }
  779. }
  780. /// <summary>
  781. /// 保存垂直电机间隔脉冲
  782. /// </summary>
  783. /// <param name="sender"></param>
  784. /// <param name="e"></param>
  785. private void SaveVerSpacePulse_Click(object sender, RoutedEventArgs e)
  786. {
  787. try
  788. {
  789. if (vm.CurrentHouse == null)
  790. {
  791. ShowMessageDefeat("未获取到对应的舱室信息");
  792. return;
  793. }
  794. if (int.TryParse(this._verticalMotorForward_TextBox.Text.Trim(), out int newValue) && newValue > 0 && newValue <= vm.tLSetting.verticalMotorPulseMax)
  795. {
  796. Task.Run(async () =>
  797. {
  798. await vm.WriteOVerSpace(newValue);
  799. vm.CurrentHouse.verticalMotorSpacePulse = newValue;
  800. vm.SetHouseInfo();
  801. });
  802. }
  803. else
  804. {
  805. AddMessageInfo($"垂直电机间隔脉冲只能输入0-{vm.tLSetting.verticalMotorPulseMax}的数字");
  806. }
  807. }
  808. catch (Exception ex)
  809. {
  810. AddMessageInfo($"保存垂直电机间隔脉冲异常:{ex.Message}");
  811. ExLog(ex, "SaveVerSpacePulse_Click");
  812. }
  813. }
  814. /// <summary>
  815. /// 清空
  816. /// </summary>
  817. /// <param name="sender"></param>
  818. /// <param name="e"></param>
  819. private void Clear_Click(object sender, RoutedEventArgs e)
  820. {
  821. vm.MessageInfoList.Clear();
  822. this._warpPanelPictures.Children.Clear();
  823. }
  824. /// <summary>
  825. /// 一键电机准备
  826. /// </summary>
  827. /// <param name="sender"></param>
  828. /// <param name="e"></param>
  829. private void Motor_Click(object sender, RoutedEventArgs e)
  830. {
  831. Task.Run(async () =>
  832. {
  833. await vm.MototReady();
  834. });
  835. }
  836. /// <summary>
  837. /// 单张抓拍
  838. /// </summary>
  839. /// <param name="sender"></param>
  840. /// <param name="e"></param>
  841. private void GetPic_Click(object sender, RoutedEventArgs e)
  842. {
  843. try
  844. {
  845. if (!isOpen)
  846. {
  847. AddMessageInfo("开启实时图像以后才可以拍照");
  848. return;
  849. }
  850. if (vm.CurrentHouse == null)
  851. {
  852. AddMessageInfo("获取舱室信息失败");
  853. return;
  854. }
  855. string path = AppData.Instance.LogHelper.GetDeBugDanZhangDirectory(vm.CurrentHouse.houseSn, vm.CurrentWell);
  856. if (!Directory.Exists(path))
  857. {
  858. Directory.CreateDirectory(path);
  859. }
  860. string fullName = $"{path}house{vm.CurrentHouse.houseSn}_{DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-fff")}_{vm.CurrentHor}-{vm.CurrentVer}.jpg";
  861. if (!vm.SavePic(fullName)) return;
  862. AddPic(fullName, vm.CurrentWell, vm.CurrentFocal);
  863. }
  864. catch (Exception ex)
  865. {
  866. ExLog(ex, "GetPic_Click");
  867. AddMessageInfo($"单张抓拍异常:{ex.Message}");
  868. }
  869. }
  870. /// <summary>
  871. /// 水平抓拍
  872. /// </summary>
  873. /// <param name="sender"></param>
  874. /// <param name="e"></param>
  875. private void ShuiPingPic_Click(object sender, RoutedEventArgs e)
  876. {
  877. if (vm.CurrentHouse == null)
  878. {
  879. AddMessageInfo("获取舱室信息失败");
  880. return;
  881. }
  882. if (!isOpen)
  883. {
  884. AddMessageInfo("开启实时图像以后才可以拍照");
  885. return;
  886. }
  887. Task.Run(() =>
  888. {
  889. vm.ShuiPingZhuaPai();
  890. });
  891. }
  892. /// <summary>
  893. /// 结束抓图
  894. /// </summary>
  895. /// <param name="sender"></param>
  896. /// <param name="e"></param>
  897. private void EndPic_Click(object sender, RoutedEventArgs e)
  898. {
  899. vm.IsStop = true;
  900. }
  901. /// <summary>
  902. /// 清晰图层抓图
  903. /// </summary>
  904. /// <param name="sender"></param>
  905. /// <param name="e"></param>
  906. private async void AutoFocus_Click(object sender, RoutedEventArgs e)
  907. {
  908. try
  909. {
  910. if (vm.CurrentHouse == null)
  911. {
  912. ShowMessageDefeat("获取舱室信息失败");
  913. return;
  914. }
  915. 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)
  916. {
  917. this._auto_Button.IsEnabled = false;
  918. await vm.AutoFocusPic(autoFocusNum, xun);
  919. this._auto_Button.IsEnabled = true;
  920. }
  921. else
  922. {
  923. ShowMessageDefeat($"请输入正确的拍摄次数以及循环次数");
  924. }
  925. }
  926. catch (Exception ex)
  927. {
  928. ExLog(ex, "AutoFocus_Click");
  929. AddMessageInfo($"清晰图层抓图异常:{ex.Message}");
  930. }
  931. }
  932. private void CloseVideo_Click(object sender, RoutedEventArgs e)
  933. {
  934. try
  935. {
  936. CloseVideo();
  937. }
  938. catch (Exception ex)
  939. {
  940. ExLog(ex, "CloseVideo_Click");
  941. AddMessageInfo($"图像关闭异常:{ex.Message}");
  942. }
  943. }
  944. /// <summary>
  945. /// M2-05 场景A 一键全自动标定(沙盒)。对选中 well(默认 16 well)逐个跑四步标定,
  946. /// 合格绿/伪峰红实时显示,结果作出厂基准 scene=0 存档 + 写 calibration.json。
  947. /// 硬件复用调试页已借用并打开的 comBin/camera(经适配器暴露为 HAL 接口),不二次 Open/Init。
  948. /// </summary>
  949. private async void OneClickCalibrate_Click(object sender, RoutedEventArgs e)
  950. {
  951. try
  952. {
  953. if (vm.CurrentHouse == null)
  954. {
  955. ShowMessageDefeat("获取舱室信息失败,请先初始化舱室");
  956. return;
  957. }
  958. if (vm.IsCalibrating)
  959. {
  960. AddMessageInfo("[一键标定]已在标定中");
  961. return;
  962. }
  963. this._oneClickCalib_Button.IsEnabled = false;
  964. try
  965. {
  966. // 沙盒默认全 16 well 逐个跑(如需勾选子集,传入选中 well 列表即可)。
  967. await vm.OneClickCalibrate(null);
  968. }
  969. finally
  970. {
  971. this._oneClickCalib_Button.IsEnabled = true;
  972. }
  973. }
  974. catch (Exception ex)
  975. {
  976. ExLog(ex, "OneClickCalibrate_Click");
  977. AddMessageInfo($"一键标定异常:{ex.Message}");
  978. }
  979. }
  980. /// <summary>M2-05 中止一键标定(当前 well 结束后停止,已完成结果保留)。</summary>
  981. private void StopCalibrate_Click(object sender, RoutedEventArgs e)
  982. {
  983. try
  984. {
  985. vm.StopCalibrate();
  986. }
  987. catch (Exception ex)
  988. {
  989. ExLog(ex, "StopCalibrate_Click");
  990. }
  991. }
  992. /// <summary>
  993. /// M2-07 载入选定 well 当前生效的拍摄层配置(well 级覆盖优先,空则显示继承的设备级值占位),
  994. /// 回填三个手调框并触发一次预览。
  995. /// </summary>
  996. private void LoadManualTune_Click(object sender, RoutedEventArgs e)
  997. {
  998. try
  999. {
  1000. if (vm.CurrentHouse == null)
  1001. {
  1002. ShowMessageDefeat("获取舱室信息失败,请先初始化舱室");
  1003. return;
  1004. }
  1005. if (vm.ManualTuneWell < 1 || vm.ManualTuneWell > 16)
  1006. {
  1007. ShowMessageDefeat("well 只能为 1-16");
  1008. return;
  1009. }
  1010. vm.LoadManualLayerSetting(vm.ManualTuneWell);
  1011. }
  1012. catch (Exception ex)
  1013. {
  1014. ExLog(ex, "LoadManualTune_Click");
  1015. }
  1016. }
  1017. /// <summary>
  1018. /// M2-07 实时预览:用该 well 标定 FocusZ + 当前手调值调 M2-02 ComputeLayerPositions 算各层 Z(关联 V-045)。
  1019. /// </summary>
  1020. private void PreviewManualTune_Click(object sender, RoutedEventArgs e)
  1021. {
  1022. try
  1023. {
  1024. if (vm.CurrentHouse == null)
  1025. {
  1026. ShowMessageDefeat("获取舱室信息失败,请先初始化舱室");
  1027. return;
  1028. }
  1029. vm.PreviewManualLayers();
  1030. }
  1031. catch (Exception ex)
  1032. {
  1033. ExLog(ex, "PreviewManualTune_Click");
  1034. }
  1035. }
  1036. /// <summary>
  1037. /// M2-07 存为该 well 默认:校验通过后写 house_well_setting well 级覆盖
  1038. /// (focus_layer_spacing_pulse/focus_layer_count + 复用 move_down_layer),留空继承设备级。
  1039. /// </summary>
  1040. private void SaveManualTune_Click(object sender, RoutedEventArgs e)
  1041. {
  1042. try
  1043. {
  1044. if (vm.CurrentHouse == null)
  1045. {
  1046. ShowMessageDefeat("获取舱室信息失败,请先初始化舱室");
  1047. return;
  1048. }
  1049. if (vm.SaveManualLayerTune())
  1050. {
  1051. ShowMessageSuccess("手调拍摄层已存为该 well 默认");
  1052. }
  1053. else
  1054. {
  1055. ShowMessageDefeat("手调拍摄层保存失败(见日志,可能为非法值或接口失败)");
  1056. }
  1057. }
  1058. catch (Exception ex)
  1059. {
  1060. ExLog(ex, "SaveManualTune_Click");
  1061. }
  1062. }
  1063. /// <summary>D2-02-T3.5 载入选定 well 当前 well 级对焦半幅覆盖值(空=继承设备级)。</summary>
  1064. private void LoadFocusRange_Click(object sender, RoutedEventArgs e)
  1065. {
  1066. try
  1067. {
  1068. if (vm.CurrentHouse == null)
  1069. {
  1070. ShowMessageDefeat("获取舱室信息失败,请先初始化舱室");
  1071. return;
  1072. }
  1073. if (vm.ManualTuneWell < 1 || vm.ManualTuneWell > 16)
  1074. {
  1075. ShowMessageDefeat("well 只能为 1-16");
  1076. return;
  1077. }
  1078. vm.LoadWellFocusRange(vm.ManualTuneWell);
  1079. }
  1080. catch (Exception ex)
  1081. {
  1082. ExLog(ex, "LoadFocusRange_Click");
  1083. }
  1084. }
  1085. /// <summary>D2-02-T3.5 保存该 well 对焦搜索半幅(写 house_well_setting well 级覆盖,留空继承设备级)。</summary>
  1086. private void SaveFocusRange_Click(object sender, RoutedEventArgs e)
  1087. {
  1088. try
  1089. {
  1090. if (vm.CurrentHouse == null)
  1091. {
  1092. ShowMessageDefeat("获取舱室信息失败,请先初始化舱室");
  1093. return;
  1094. }
  1095. if (vm.SaveWellFocusRange())
  1096. {
  1097. ShowMessageSuccess("对焦搜索半幅已保存为该 well 覆盖");
  1098. }
  1099. else
  1100. {
  1101. ShowMessageDefeat("对焦半幅保存失败(见日志,可能为非法值或接口失败)");
  1102. }
  1103. }
  1104. catch (Exception ex)
  1105. {
  1106. ExLog(ex, "SaveFocusRange_Click");
  1107. }
  1108. }
  1109. /// <summary>D2-02-T3.4 逐孔微调:对该格 well 经 control 协作单孔重标。</summary>
  1110. private async void RecalibrateWell_Click(object sender, RoutedEventArgs e)
  1111. {
  1112. try
  1113. {
  1114. if (!(sender is FrameworkElement fe) || fe.Tag == null) return;
  1115. if (!int.TryParse(fe.Tag.ToString(), out int well)) return;
  1116. await vm.RecalibrateWell(well);
  1117. }
  1118. catch (Exception ex)
  1119. {
  1120. ExLog(ex, "RecalibrateWell_Click");
  1121. AddMessageInfo($"单孔重标异常:{ex.Message}");
  1122. }
  1123. }
  1124. private void OpenVideo_Click(object sender, RoutedEventArgs e)
  1125. {
  1126. try
  1127. {
  1128. OpenVideo();
  1129. }
  1130. catch (Exception ex)
  1131. {
  1132. ExLog(ex, "OpenVideo_Click");
  1133. AddMessageInfo($"图像打开异常:{ex.Message}");
  1134. }
  1135. }
  1136. /// <summary>
  1137. /// 保存自动对焦起点
  1138. /// </summary>
  1139. /// <param name="sender"></param>
  1140. /// <param name="e"></param>
  1141. private void SaveAutoFocus_Click(object sender, RoutedEventArgs e)
  1142. {
  1143. try
  1144. {
  1145. if (vm.SetWellAutoFocus())
  1146. {
  1147. var bb = vm.ccdPhoto.FirstOrDefault(x => x.houseSn == vm.CurrentHouseId && x.wellSn == vm.CurrentWell);
  1148. if (bb != null) bb.autoFocusPosition = vm.CurrentVer;
  1149. this._autoFocus.Text = vm.CurrentVer.ToString();
  1150. AddMessageInfo($"自动对焦起点保存成功");
  1151. }
  1152. else
  1153. {
  1154. AddMessageInfo($"自动对焦起点保存失败");
  1155. }
  1156. }
  1157. catch (Exception ex)
  1158. {
  1159. ExLog(ex, "SaveAutoFocus_Click");
  1160. AddMessageInfo($"保存自动对焦起点异常:{ex.Message}");
  1161. }
  1162. }
  1163. private void _autoFocusNum_TextBox_TextChanged(object sender, TextChangedEventArgs e)
  1164. {
  1165. TextBox passwordBox = sender as TextBox;
  1166. if (passwordBox == null) return;
  1167. string ss = passwordBox.Text.Trim();
  1168. if (string.IsNullOrEmpty(ss))
  1169. {
  1170. passwordBox.Background = (VisualBrush)this.FindResource("HelpBrush2");
  1171. }
  1172. else
  1173. {
  1174. passwordBox.Background = new SolidColorBrush(Colors.White);
  1175. }
  1176. }
  1177. private void _autoFocus_TextBox_TextChanged(object sender, TextChangedEventArgs e)
  1178. {
  1179. TextBox passwordBox = sender as TextBox;
  1180. if (passwordBox == null) return;
  1181. string ss = passwordBox.Text.Trim();
  1182. if (string.IsNullOrEmpty(ss))
  1183. {
  1184. passwordBox.Background = (VisualBrush)this.FindResource("HelpBrush3");
  1185. }
  1186. else
  1187. {
  1188. passwordBox.Background = new SolidColorBrush(Colors.White);
  1189. }
  1190. }
  1191. private void Paiqi_Click(object sender, RoutedEventArgs e)
  1192. {
  1193. Task.Run(async () =>
  1194. {
  1195. await vm.HouseVent();
  1196. });
  1197. }
  1198. private void WriteOpenVentTime_Click(object sender, RoutedEventArgs e)
  1199. {
  1200. try
  1201. {
  1202. if (vm.CurrentHouse == null)
  1203. {
  1204. ShowMessageDefeat("未获取到对应的舱室信息");
  1205. return;
  1206. }
  1207. string timeString = this._openVentTime_TextBox.Text.Trim();
  1208. if (string.IsNullOrEmpty(timeString))
  1209. {
  1210. ShowMessageDefeat("请输入排气阀打开时间");
  1211. return;
  1212. }
  1213. if (int.TryParse(timeString, out int value) && value >= 0)
  1214. {
  1215. Task.Run(async () =>
  1216. {
  1217. await vm.WriteOpenVentTime(value);
  1218. //if (vm.CurrentHouse != null)
  1219. //{
  1220. // vm.CurrentHouse.inletValveOpeningTime = value;
  1221. // vm.SetHouseInfo();
  1222. //}
  1223. });
  1224. }
  1225. else
  1226. {
  1227. ShowMessageDefeat("排气阀打开时间只能输入大于等于0数字");
  1228. return;
  1229. }
  1230. }
  1231. catch (Exception ex)
  1232. {
  1233. ExLog(ex, "WriteOpenVentTime_Click");
  1234. }
  1235. }
  1236. private void ReadOpenVentTime_Click(object sender, RoutedEventArgs e)
  1237. {
  1238. Task.Run(async () =>
  1239. {
  1240. await vm.RedVentTime();
  1241. });
  1242. }
  1243. }
  1244. }