HouseDebugPageView.xaml.cs 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296
  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. private void OpenVideo_Click(object sender, RoutedEventArgs e)
  1067. {
  1068. try
  1069. {
  1070. OpenVideo();
  1071. }
  1072. catch (Exception ex)
  1073. {
  1074. ExLog(ex, "OpenVideo_Click");
  1075. AddMessageInfo($"图像打开异常:{ex.Message}");
  1076. }
  1077. }
  1078. /// <summary>
  1079. /// 保存自动对焦起点
  1080. /// </summary>
  1081. /// <param name="sender"></param>
  1082. /// <param name="e"></param>
  1083. private void SaveAutoFocus_Click(object sender, RoutedEventArgs e)
  1084. {
  1085. try
  1086. {
  1087. if (vm.SetWellAutoFocus())
  1088. {
  1089. var bb = vm.ccdPhoto.FirstOrDefault(x => x.houseSn == vm.CurrentHouseId && x.wellSn == vm.CurrentWell);
  1090. if (bb != null) bb.autoFocusPosition = vm.CurrentVer;
  1091. this._autoFocus.Text = vm.CurrentVer.ToString();
  1092. AddMessageInfo($"自动对焦起点保存成功");
  1093. }
  1094. else
  1095. {
  1096. AddMessageInfo($"自动对焦起点保存失败");
  1097. }
  1098. }
  1099. catch (Exception ex)
  1100. {
  1101. ExLog(ex, "SaveAutoFocus_Click");
  1102. AddMessageInfo($"保存自动对焦起点异常:{ex.Message}");
  1103. }
  1104. }
  1105. private void _autoFocusNum_TextBox_TextChanged(object sender, TextChangedEventArgs e)
  1106. {
  1107. TextBox passwordBox = sender as TextBox;
  1108. if (passwordBox == null) return;
  1109. string ss = passwordBox.Text.Trim();
  1110. if (string.IsNullOrEmpty(ss))
  1111. {
  1112. passwordBox.Background = (VisualBrush)this.FindResource("HelpBrush2");
  1113. }
  1114. else
  1115. {
  1116. passwordBox.Background = new SolidColorBrush(Colors.White);
  1117. }
  1118. }
  1119. private void _autoFocus_TextBox_TextChanged(object sender, TextChangedEventArgs e)
  1120. {
  1121. TextBox passwordBox = sender as TextBox;
  1122. if (passwordBox == null) return;
  1123. string ss = passwordBox.Text.Trim();
  1124. if (string.IsNullOrEmpty(ss))
  1125. {
  1126. passwordBox.Background = (VisualBrush)this.FindResource("HelpBrush3");
  1127. }
  1128. else
  1129. {
  1130. passwordBox.Background = new SolidColorBrush(Colors.White);
  1131. }
  1132. }
  1133. private void Paiqi_Click(object sender, RoutedEventArgs e)
  1134. {
  1135. Task.Run(() =>
  1136. {
  1137. vm.HouseVent();
  1138. });
  1139. }
  1140. private void WriteOpenVentTime_Click(object sender, RoutedEventArgs e)
  1141. {
  1142. try
  1143. {
  1144. if (vm.CurrentHouse == null)
  1145. {
  1146. ShowMessageDefeat("未获取到对应的舱室信息");
  1147. return;
  1148. }
  1149. string timeString = this._openVentTime_TextBox.Text.Trim();
  1150. if (string.IsNullOrEmpty(timeString))
  1151. {
  1152. ShowMessageDefeat("请输入排气阀打开时间");
  1153. return;
  1154. }
  1155. if (int.TryParse(timeString, out int value) && value >= 0)
  1156. {
  1157. Task.Run(() =>
  1158. {
  1159. vm.WriteOpenVentTime(value);
  1160. //if (vm.CurrentHouse != null)
  1161. //{
  1162. // vm.CurrentHouse.inletValveOpeningTime = value;
  1163. // vm.SetHouseInfo();
  1164. //}
  1165. });
  1166. }
  1167. else
  1168. {
  1169. ShowMessageDefeat("排气阀打开时间只能输入大于等于0数字");
  1170. return;
  1171. }
  1172. }
  1173. catch (Exception ex)
  1174. {
  1175. ExLog(ex, "WriteOpenVentTime_Click");
  1176. }
  1177. }
  1178. private void ReadOpenVentTime_Click(object sender, RoutedEventArgs e)
  1179. {
  1180. Task.Run(() =>
  1181. {
  1182. vm.RedVentTime();
  1183. });
  1184. }
  1185. }
  1186. }