HouseDebugPageView.xaml.cs 42 KB

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