frmMain3.cs 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Threading;
  11. using System.IO;
  12. using Microsoft.Win32;
  13. using System.Net;
  14. using System.Net.Http;
  15. using System.Text.RegularExpressions;
  16. using System.Runtime.InteropServices;
  17. using Newtonsoft.Json;
  18. using Newtonsoft.Json.Linq;
  19. using Saraff.Tiff;
  20. using Saraff.Tiff.Core;
  21. using Saraff.Twain;
  22. using HXX.Scanner.Client.Base;
  23. using HXX.Scanner.Client.Properties;
  24. using HXX.Scanner.Biz;
  25. using HXX.Scanner.Common;
  26. using HXX.Scanner.Biz.Socket;
  27. using HXX.Scanner.Client.biz;
  28. namespace HXX.Scanner.Client
  29. {
  30. /// <summary>
  31. /// 主窗体
  32. /// </summary>
  33. public partial class frmMain3 : Form
  34. {
  35. public frmMain3()
  36. {
  37. //Font = new Font(Font.Name, 8.25f * 96f / CreateGraphics().DpiX, Font.Style, Font.Unit, Font.GdiCharSet, Font.GdiVerticalFont);
  38. CheckForIllegalCrossThreadCalls = false;
  39. InitializeComponent();
  40. SetStyle(
  41. ControlStyles.UserPaint |
  42. ControlStyles.AllPaintingInWmPaint |
  43. ControlStyles.OptimizedDoubleBuffer |
  44. ControlStyles.ResizeRedraw |
  45. ControlStyles.DoubleBuffer, true);
  46. this.DoubleBuffered = true;
  47. this.SetStyle(ControlStyles.Opaque, true);
  48. this.UpdateStyles();
  49. }
  50. //扫描中心
  51. engine_twain32 engine;
  52. #region 页面转态变化提示
  53. /// <summary>
  54. /// 初始化计时器,页面各状态值
  55. /// </summary>
  56. public void initTimer()
  57. {
  58. //System.Windows.Forms.Timer formTimer = new System.Windows.Forms.Timer();
  59. //formTimer.Interval = 3000;//设置中断时间 单位ms
  60. //formTimer.Enabled = true;
  61. //formTimer.Tick += FormTimer_Tick;
  62. this.checkBox1.Checked = RegisterHelper.GetRun();//开机启动
  63. Task.Run(async () =>
  64. {
  65. while (true)
  66. {
  67. this.Invoke(new Action(() =>
  68. {
  69. if (config_environment.scan_state)
  70. {
  71. this.lblTestA.Enabled = false;
  72. this.lblTestAB.Enabled = false;
  73. }
  74. else
  75. {
  76. this.lblTestA.Enabled = true;
  77. this.lblTestAB.Enabled = true;
  78. }
  79. }));
  80. await FormTimer_Tick(null, null);
  81. Thread.Sleep(10000);
  82. }
  83. });
  84. }
  85. /// <summary>
  86. /// 检查中状态
  87. /// </summary>
  88. private bool onCheck_status = true;
  89. /// <summary>
  90. /// 是否检查网络
  91. /// </summary>
  92. private bool to_check_net = true;
  93. /// <summary>
  94. /// 扫描仪状态
  95. /// </summary>
  96. private int scanner_status = 0;
  97. /// <summary>
  98. /// 磁盘状态
  99. /// </summary>
  100. private int disk_status = 1;
  101. /// <summary>
  102. /// 网络状态
  103. /// </summary>
  104. private int net_status = 1;
  105. /// <summary>
  106. /// 云端状态
  107. /// </summary>
  108. private int cloud_status = 1;
  109. /// <summary>
  110. /// 页面各状态检测
  111. /// </summary>
  112. /// <param name="sender"></param>
  113. /// <param name="e"></param>
  114. /// <returns></returns>
  115. private async Task FormTimer_Tick(object sender, EventArgs e)
  116. {
  117. ///1:检查扫描仪状态
  118. var scannerResult = await engine.check_status_ui();
  119. ///3:检查磁盘空间
  120. var diskResult = biz_disk.check_disk_status();
  121. if (to_check_net)
  122. {
  123. await checkAnswerCardServer();
  124. await check_net_status();
  125. //pingTestAsync();
  126. if (net_status == 1 && cloud_status == 1)
  127. {
  128. to_check_net = false;
  129. }
  130. }
  131. this.Invoke(new Action(() =>
  132. {
  133. if (scannerResult.Status == 501 || scannerResult.Status == 509)
  134. {
  135. scanner_status = 2;//未连接
  136. }
  137. else
  138. {
  139. scanner_status = 1;
  140. }
  141. if (diskResult.Status == 200)
  142. {
  143. disk_status = 1;
  144. }
  145. else if (diskResult.Status == 520)
  146. {
  147. disk_status = 2;//请先设置数据存放位置
  148. }
  149. else if (diskResult.Status == 510)
  150. {
  151. disk_status = 3;//磁盘剩余空间小于5G
  152. }
  153. else if (diskResult.Status == 509)
  154. {
  155. disk_status = 4;//磁盘剩余空间小于500M
  156. }
  157. show_status();
  158. }));
  159. }
  160. /// <summary>
  161. /// 页面状态信息改变
  162. /// </summary>
  163. /// <param name="Status">0:正常 1:scanner异常,2:disk异常</param>
  164. private void show_status()
  165. {
  166. if (this.onCheck_status)
  167. {
  168. this.picStatus.Image = Properties.Resources._1;
  169. }
  170. else
  171. {
  172. if (this.scanner_status == 1 && this.disk_status == 1 && this.cloud_status == 1 && this.net_status == 1)
  173. {
  174. //this.lab_banner.Text = "扫描环境检测完成,扫描设备已就位,可以开始进行扫描工作";
  175. //this.pan_banner.BackColor = Color.FromArgb(236, 249, 228);
  176. //this.lab_banner.ForeColor = Color.FromArgb(0, 192, 0);
  177. //this.lab_banner.BackColor = Color.FromArgb(236, 249, 228);
  178. //this.pic_banner.Image = Properties.Resources.校验_check_one_2x;
  179. this.picStatus.Image = Properties.Resources.正常;
  180. this.lbl_msg1.Text = "扫描环境检测完成";
  181. this.lbl_msg2.Text = "可以开始扫描工作";
  182. }
  183. else
  184. {
  185. if (this.scanner_status > 1)
  186. {
  187. //this.lab_banner.Text = "扫描仪连接异常,请检测扫描仪设备连接及驱动!";
  188. //this.pan_banner.BackColor = Color.FromArgb(254, 233, 233);
  189. //this.lab_banner.ForeColor = Color.FromArgb(245, 87, 87);
  190. //this.lab_banner.BackColor = Color.FromArgb(254, 233, 233);
  191. //this.pic_banner.Image = Properties.Resources.关闭_close_one_2x;
  192. this.picStatus.Image = Properties.Resources.异常2;
  193. this.lbl_msg1.Text = "扫描仪连接异常";
  194. this.lbl_msg2.Text = "请检测扫描仪设备连接及驱动";
  195. //新增,在有扫描仪连接的情况下,尝试启动
  196. //if (this.cmbDeviceList.SelectedIndex >= 0)
  197. //{
  198. // this.cmbDeviceList_SelectedIndexChanged(null, null);
  199. //}
  200. }
  201. else if (this.net_status > 1)
  202. {
  203. //this.lab_banner.Text = "网络故障,无法上传文件,请检查网络。";
  204. //this.pan_banner.BackColor = Color.FromArgb(254, 233, 233);
  205. //this.lab_banner.ForeColor = Color.FromArgb(245, 87, 87);
  206. //this.lab_banner.BackColor = Color.FromArgb(254, 233, 233);
  207. //this.pic_banner.Image = Properties.Resources.关闭_close_one_2x;
  208. this.picStatus.Image = Properties.Resources.异常2;
  209. this.lbl_msg1.Text = "网络故障";
  210. this.lbl_msg2.Text = "无法上传文件,请检查网络";
  211. }
  212. else if (this.cloud_status > 1)
  213. {
  214. //this.lab_banner.Text = "云服务器连接失败,请检查网络设置或联系技术人员。";
  215. //this.pan_banner.BackColor = Color.FromArgb(254, 233, 233);
  216. //this.lab_banner.ForeColor = Color.FromArgb(245, 87, 87);
  217. //this.lab_banner.BackColor = Color.FromArgb(254, 233, 233);
  218. //this.pic_banner.Image = Properties.Resources.关闭_close_one_2x;
  219. this.picStatus.Image = Properties.Resources.异常2;
  220. this.lbl_msg1.Text = "网络故障";
  221. this.lbl_msg2.Text = "无法上传文件,请检查网络";
  222. }
  223. else if (this.disk_status == 2)
  224. {
  225. //this.lab_banner.Text = "请先设置数据存放位置";
  226. //this.pan_banner.BackColor = Color.FromArgb(254, 233, 233);
  227. //this.lab_banner.ForeColor = Color.FromArgb(245, 87, 87);
  228. //this.lab_banner.BackColor = Color.FromArgb(254, 233, 233);
  229. //this.pic_banner.Image = Properties.Resources.关闭_close_one_2x;
  230. this.picStatus.Image = Properties.Resources.异常;
  231. this.lbl_msg1.Text = "请先设置数据存放位置";
  232. this.lbl_msg2.Text = "";
  233. }
  234. else if (this.disk_status == 4)
  235. {
  236. //this.lab_banner.Text = "数据存放目录空间不足,无法保存数据";
  237. //this.pan_banner.BackColor = Color.FromArgb(254, 233, 233);
  238. //this.lab_banner.ForeColor = Color.FromArgb(245, 87, 87);
  239. //this.lab_banner.BackColor = Color.FromArgb(254, 233, 233);
  240. //this.pic_banner.Image = Properties.Resources.注意_attention_2x;
  241. this.picStatus.Image = Properties.Resources.异常;
  242. this.lbl_msg1.Text = "数据存放目录空间不足";
  243. this.lbl_msg2.Text = "无法保存数据";
  244. }
  245. else if (this.disk_status == 3)
  246. {
  247. //this.lab_banner.Text = "磁盘剩余空间较少,建议清理或更换目录";
  248. //this.pan_banner.BackColor = Color.FromArgb(253, 244, 232);
  249. //this.lab_banner.ForeColor = Color.FromArgb(242, 191, 70);
  250. //this.lab_banner.BackColor = Color.FromArgb(253, 244, 232);
  251. //this.pic_banner.Image = Properties.Resources.注意_attention_2x;
  252. this.picStatus.Image = Properties.Resources.异常;
  253. this.lbl_msg1.Text = "磁盘剩余空间较少";
  254. this.lbl_msg2.Text = "建议清理或更换目录";
  255. }
  256. }
  257. if (this.scanner_status == 1)
  258. {
  259. this.pic_scannerStatus.Image = Properties.Resources.对勾_1;
  260. this.pl_scanner.BackgroundImage = Properties.Resources.Frame_7;
  261. }
  262. else
  263. {
  264. this.pic_scannerStatus.Image = Properties.Resources.错_2x;
  265. this.pl_scanner.BackgroundImage = Properties.Resources.Frame_14;
  266. }
  267. if (this.net_status == 1)
  268. {
  269. this.picNetStatus.Image = Properties.Resources.对勾_1;
  270. this.pl_net.BackgroundImage = Properties.Resources.Frame_7;
  271. }
  272. else
  273. {
  274. this.picNetStatus.Image = Properties.Resources.错_2x;
  275. this.pl_net.BackgroundImage = Properties.Resources.Frame_14;
  276. }
  277. if (this.cloud_status == 1)
  278. {
  279. this.picCloudStatus.Image = Properties.Resources.对勾_1;
  280. this.pl_cloud.BackgroundImage = Properties.Resources.Frame_7;
  281. }
  282. else
  283. {
  284. this.picCloudStatus.Image = Properties.Resources.错_2x;
  285. this.pl_cloud.BackgroundImage = Properties.Resources.Frame_14;
  286. }
  287. if (this.disk_status == 1)
  288. {
  289. this.pic_diskStatus.Image = Properties.Resources.对勾_1;
  290. this.pl_disk.BackgroundImage = Properties.Resources.Frame_7;
  291. }
  292. else
  293. {
  294. this.pic_diskStatus.Image = Properties.Resources.感叹_2x;
  295. this.pl_disk.BackgroundImage = Properties.Resources.Frame_12;
  296. }
  297. }
  298. }
  299. #endregion
  300. /// <summary>
  301. /// 主界面初始化
  302. /// </summary>
  303. /// <param name="sender"></param>
  304. /// <param name="e"></param>
  305. private void frmMain_Load(object sender, EventArgs e)
  306. {
  307. //this.Text = "慧教研扫描客户端";
  308. show_status();
  309. update_scan_side_check();
  310. engine = new engine_twain32(this, Settings.Default, this.change_scanner);
  311. showDataDir();
  312. this.lblInstallPath.Text = Application.StartupPath;
  313. biz_environment.init_unique_client_id();
  314. //biz_scannedBatch.Work();
  315. http_manager.on_net_broken = this.on_net_broken;
  316. http_manager.on_http_notify = this.on_http_notify;
  317. http_manager.on_http_notify2 = this.on_http_notify2;
  318. file_manager.on_http_notify2 = this.on_http_notify2;
  319. biz_startScan.on_http_notify2 = this.on_http_notify2;
  320. biz_startScan.on_startScan = this.on_startScan;
  321. biz_reUpload.on_startScan = this.on_startScan;
  322. this.lbl_msg1.Text = string.Empty;
  323. this.lbl_msg2.Text = string.Empty;
  324. this.toolTip1.IsBalloon = true;
  325. this.toolTip1.SetToolTip(this.lblInstallPath, this.lblInstallPath.Text);
  326. this.toolTip1.SetToolTip(this.lblDataPath, this.lblDataPath.Text);
  327. this.toolTip1.SetToolTip(this.but_min, "最小化");
  328. this.toolTip1.SetToolTip(this.but_close, "最小化到托盘");
  329. if (!biz_socketServer.Start(Settings.Default.webSocket_url))
  330. {
  331. MsgManager.Error("服务开启失败,请检查");
  332. }
  333. engine.load_tw_scanner_list();
  334. init_timer_auto_load_scanner();
  335. //lblRefreshDevice_LinkClicked(null, null);
  336. initTimer();
  337. //pingTestAsync();
  338. check_net_status();
  339. checkAnswerCardServer();
  340. update_scan_history();
  341. http_manager.start_http_upload();
  342. init_environment();
  343. new self_http_manager().Start();
  344. check_version();
  345. }
  346. //protected override CreateParams CreateParams
  347. //{
  348. // get
  349. // {
  350. // CreateParams cp = base.CreateParams;
  351. // // 移除 WS_CAPTION 样式(包括标题栏)
  352. // cp.Style &= ~0x00C00000; // ~WS_CAPTION
  353. // return cp;
  354. // }
  355. //}
  356. /// <summary>
  357. /// 更新扫描历史信息
  358. /// </summary>
  359. private void update_scan_history()
  360. {
  361. Task.Run(() =>
  362. {
  363. while (true)
  364. {
  365. var history = biz_scanHistory.get_history();
  366. this.Invoke(new Action(() =>
  367. {
  368. this.lblStartTime.Text = history.last_start_time.ToString("HH:mm");
  369. //this.lblScanElapse.Text = history.last_scan_elapse.ToString() + "秒";
  370. this.lblScanElapse.Text = history.last_scan_elapse;
  371. this.lblTotalSum.Text = history.total_sum.ToString() + "张";
  372. this.lblSpeed.Text = history.last_speed.ToString() + "张/秒"; ;
  373. }));
  374. Thread.Sleep(1000);
  375. }
  376. });
  377. }
  378. /// <summary>
  379. /// 强杀驱动弹框
  380. /// </summary>
  381. private void update_scan_side_check()
  382. {
  383. Task.Run(() =>
  384. {
  385. while (true)
  386. {
  387. biz_engine_side.check();
  388. Thread.Sleep(50);
  389. }
  390. });
  391. }
  392. /// <summary>
  393. /// 根据插拔usb,随时检测扫描仪变化,更新扫描仪下拉框
  394. /// </summary>
  395. private void init_timer_auto_load_scanner()
  396. {
  397. Task.Run(() =>
  398. {
  399. while (true)
  400. {
  401. scanner_manager.refresh_scanner_list();
  402. //if (!scanner_manager.refresh_scanner_list())
  403. {
  404. //if (scanner_manager.Scanner_List.Count > 0 && scanner_manager.Scanner_List.Count(x => x.openSourceReady) == 0)
  405. {
  406. this.Invoke(new Action(() =>
  407. {
  408. this.lblRefreshDevice_LinkClicked(null, null);
  409. }));
  410. }
  411. }
  412. Thread.Sleep(2000);
  413. }
  414. });
  415. }
  416. /// <summary>
  417. /// 点击【刷新设备】按钮事件
  418. /// </summary>
  419. /// <param name="sender"></param>
  420. /// <param name="e"></param>
  421. private void lblRefreshDevice_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  422. {
  423. if (!config_environment.loading)
  424. {
  425. config_environment.loading = true;
  426. loadScannerByTwain();
  427. config_environment.loading = false;
  428. }
  429. }
  430. /// <summary>
  431. /// 点击【刷新设备】按钮事件,加载gif,执行刷新
  432. /// </summary>
  433. private async void loadScannerByTwain()
  434. {
  435. //updateUIOLoadingState(true);
  436. updateUIOLoadingState2(true);
  437. //await engine.load_tw_scanner_list();
  438. InitScannerCombobox();
  439. //updateUIOLoadingState(false);
  440. updateUIOLoadingState2(false);
  441. }
  442. /// <summary>
  443. /// 加载gif
  444. /// </summary>
  445. /// <param name="loading"></param>
  446. //private void updateUIOLoadingState(Boolean loading)
  447. //{
  448. // if (loading)
  449. // {
  450. // this.loadSB.Visible = true;
  451. // }
  452. // else
  453. // {
  454. // this.loadSB.Visible = false;
  455. // if (this.cmbDeviceList.Items.Count == 0)
  456. // {
  457. // //MsgManager.Show("没有加载到扫描仪");
  458. // }
  459. // }
  460. //}
  461. /// <summary>
  462. /// 加载gif
  463. /// </summary>
  464. /// <param name="loading"></param>
  465. private void updateUIOLoadingState2(bool status)
  466. {
  467. this.onCheck_status = status;
  468. }
  469. /// <summary>
  470. /// 初始化扫描仪下拉框
  471. /// </summary>
  472. private void InitScannerCombobox()
  473. {
  474. this.cmbDeviceList.SelectedIndexChanged -= new System.EventHandler(this.cmbDeviceList_SelectedIndexChanged);
  475. this.cmbDeviceList.DataSource = null;
  476. this.cmbDeviceList.DataSource = scanner_manager.Scanner_List;
  477. if (this.cmbDeviceList.Items.Count > 0)
  478. {
  479. var scanner = scanner_manager.Scanner_List.FirstOrDefault(x => x.deviceName == Settings.Default.lastScanner);
  480. if (scanner != null)
  481. {
  482. this.cmbDeviceList.SelectedItem = scanner;
  483. }
  484. else
  485. {
  486. this.cmbDeviceList.SelectedIndex = 0;
  487. }
  488. }
  489. this.cmbDeviceList.SelectedIndexChanged += new System.EventHandler(this.cmbDeviceList_SelectedIndexChanged);
  490. this.cmbDeviceList_SelectedIndexChanged(null, null);
  491. }
  492. /// <summary>
  493. /// 单面测试扫描,点击事件
  494. /// </summary>
  495. /// <param name="sender"></param>
  496. /// <param name="e"></param>
  497. private void lblTestA_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  498. {
  499. try
  500. {
  501. //config_environment.scan_type = ScanType.Formal;
  502. //config_environment.web_parameter = new subCmd_startScan();
  503. //config_environment.web_parameter.uploadUrl = "http://localhost:3768/api/upload/pppp";
  504. //var entity = new Database.tb_file_info();
  505. //entity.fi_schema = 1;
  506. //entity.fi_full_name = @"d:\1.txt";
  507. //entity.fi_accountId = "lii";
  508. //config_environment.web_parameter.token = "aabbcc22 " + DateTime.Now;
  509. //var e_q = new upf() { entity = entity, result = new ResponseEntity<response_http_uploadImg>() { Status = 1 }, seqNumber = 0 };
  510. //new http_manager().Upload(e_q);
  511. //return;
  512. //biz_engine_side.active_3rd_form(this.Handle);
  513. //return;
  514. config_environment.scan_type = ScanType.Test_Panel_1_Side;
  515. config_environment.use_source_ui = false;
  516. config_environment.scan_ab = false;
  517. config_environment.paper_size = PaperSize.A4;
  518. test_scan();
  519. }
  520. catch (Exception ee)
  521. {
  522. LogManager.WriteLog(ee);
  523. MsgManager.Error("扫描出错。");
  524. }
  525. }
  526. /// <summary>
  527. /// 双面测试扫描,点击事件
  528. /// </summary>
  529. /// <param name="sender"></param>
  530. /// <param name="e"></param>
  531. private void lblTestAB_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  532. {
  533. //scanner_manager.set_current(-1);
  534. //return;
  535. try
  536. {
  537. config_environment.scan_type = ScanType.Test_Panel_2_Sides;
  538. config_environment.use_source_ui = false;
  539. config_environment.scan_ab = true;
  540. config_environment.paper_size = PaperSize.A4;
  541. test_scan();
  542. }
  543. catch (Exception ee)
  544. {
  545. LogManager.WriteLog(ee);
  546. MsgManager.Error("扫描出错。");
  547. }
  548. }
  549. /// <summary>
  550. /// 测试扫描业务
  551. /// </summary>
  552. private async void test_scan()
  553. {
  554. var scanner = this.cmbDeviceList.SelectedItem as Device_Scanner;
  555. if (scanner != null)
  556. {
  557. scanner = scanner_manager.Scanner_List.FirstOrDefault(x => x.dsIndex == scanner.dsIndex);
  558. }
  559. if (scanner != null)
  560. {
  561. var result = await engine.Scan(scanner);
  562. if (result.Status != 1)
  563. {
  564. MsgManager.Warn(result.Message);
  565. }
  566. }
  567. else
  568. {
  569. this.Invoke(new Action(() =>
  570. {
  571. MsgManager.Warn("请选择扫描设备");
  572. }));
  573. }
  574. }
  575. /// <summary>
  576. /// 修改目录,点击事件
  577. /// </summary>
  578. /// <param name="sender"></param>
  579. /// <param name="e"></param>
  580. private void lblChangeDir_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  581. {
  582. changeDataDir();
  583. this.toolTip1.SetToolTip(this.lblDataPath, this.lblDataPath.Text);
  584. }
  585. private void changeDataDir()
  586. {
  587. FolderBrowserDialog dialog = new FolderBrowserDialog();
  588. dialog.Description = "请选择扫描数据存放位置";
  589. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  590. {
  591. if (string.IsNullOrEmpty(dialog.SelectedPath))
  592. {
  593. MsgManager.Warn("存放位置不能为空");
  594. }
  595. else
  596. {
  597. Settings.Default.dataDir = dialog.SelectedPath;
  598. Settings.Default.Save();
  599. showDataDir();
  600. }
  601. }
  602. }
  603. /// <summary>
  604. /// 显示当前剩余空间和路径展示
  605. /// </summary>
  606. private void showDataDir()
  607. {
  608. lblDataPath.Text = Settings.Default.dataDir;
  609. lblDiskLeft.Text = biz_disk.getDataDirLeftSpace(Settings.Default.dataDir);
  610. }
  611. /// <summary>
  612. /// 窗体关闭按钮点击
  613. /// </summary>
  614. /// <param name="sender"></param>
  615. /// <param name="e"></param>
  616. private void but_close_Click(object sender, EventArgs e)
  617. {
  618. this.WindowState = FormWindowState.Minimized;
  619. this.ShowInTaskbar = false;
  620. }
  621. /// <summary>
  622. /// 窗体最小化按钮点击
  623. /// </summary>
  624. /// <param name="sender"></param>
  625. /// <param name="e"></param>
  626. private void but_min_Click(object sender, EventArgs e)
  627. {
  628. this.WindowState = FormWindowState.Minimized;
  629. }
  630. Point mouseOff;
  631. bool leftFlag = false;
  632. /// <summary>
  633. /// 窗体移动相关
  634. /// </summary>
  635. /// <param name="sender"></param>
  636. /// <param name="e"></param>
  637. private void panel2_MouseMove(object sender, MouseEventArgs e)
  638. {
  639. if (leftFlag)
  640. {
  641. Point mouseSet = Control.MousePosition;
  642. mouseSet.Offset(mouseOff.X, mouseOff.Y);
  643. this.Location = mouseSet;
  644. }
  645. }
  646. /// <summary>
  647. /// 窗体移动相关
  648. /// </summary>
  649. /// <param name="sender"></param>
  650. /// <param name="e"></param>
  651. private void panel2_MouseUp(object sender, MouseEventArgs e)
  652. {
  653. if (leftFlag)
  654. {
  655. leftFlag = false;
  656. }
  657. }
  658. /// <summary>
  659. /// 窗体移动相关
  660. /// </summary>
  661. /// <param name="sender"></param>
  662. /// <param name="e"></param>
  663. private void panel2_MouseDown(object sender, MouseEventArgs e)
  664. {
  665. if (e.Button == MouseButtons.Left)
  666. {
  667. mouseOff = new Point(-e.X, -e.Y);
  668. leftFlag = true;
  669. }
  670. }
  671. /// <summary>
  672. /// 窗体关闭事件
  673. /// </summary>
  674. /// <param name="sender"></param>
  675. /// <param name="e"></param>
  676. private void frmMain2_FormClosing(object sender, FormClosingEventArgs e)
  677. {
  678. notifyIcon1.Dispose();
  679. Environment.Exit(0);
  680. }
  681. /// <summary>
  682. /// 托盘右键 退出按钮
  683. /// </summary>
  684. /// <param name="sender"></param>
  685. /// <param name="e"></param>
  686. private void exit_Click(object sender, EventArgs e)
  687. {
  688. string msg = string.Empty;
  689. if (config_environment.scan_state == true)
  690. {
  691. msg = "正在扫描中,您确定要退出吗?";
  692. }
  693. else if (!http_manager.check_is_queue_empty())
  694. {
  695. msg = "正在上传中,您确定要退出吗?";
  696. }
  697. else
  698. {
  699. msg = "您确定要退出吗?";
  700. }
  701. if (MsgManager.Ask(msg) == DialogResult.Yes)
  702. {
  703. Environment.Exit(0);
  704. }
  705. }
  706. /// <summary>
  707. /// 托盘右键 打开按钮
  708. /// </summary>
  709. /// <param name="sender"></param>
  710. /// <param name="e"></param>
  711. private void open_Click(object sender, EventArgs e)
  712. {
  713. this.WindowState = FormWindowState.Normal;
  714. this.ShowInTaskbar = true;
  715. }
  716. /// <summary>
  717. /// 扫描仪驱动下载 按钮点击
  718. /// </summary>
  719. /// <param name="sender"></param>
  720. /// <param name="e"></param>
  721. private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  722. {
  723. System.Diagnostics.Process.Start(Properties.Settings.Default.apiUrlBase + Properties.Settings.Default.DriverDownloadUrl);
  724. }
  725. /// <summary>
  726. /// 扫描仪列表,切换选择事件
  727. /// </summary>
  728. /// <param name="sender"></param>
  729. /// <param name="e"></param>
  730. private async void cmbDeviceList_SelectedIndexChanged(object sender, EventArgs e)
  731. {
  732. var scanner = this.cmbDeviceList.SelectedItem as Device_Scanner;
  733. if (scanner != null)
  734. {
  735. scanner = scanner_manager.Scanner_List.FirstOrDefault(x => x.dsIndex == scanner.dsIndex);
  736. }
  737. //updateUIOLoadingState(true);
  738. updateUIOLoadingState2(true);
  739. show_status();
  740. if (scanner != null)
  741. {
  742. biz_getScannerList.chosen_index = scanner.dsIndex;
  743. }
  744. var result = await engine.set_current(scanner);
  745. if (result.Status == 1)
  746. {
  747. Settings.Default.lastScanner = scanner.deviceName;
  748. Settings.Default.Save();
  749. }
  750. //updateUIOLoadingState(false);
  751. updateUIOLoadingState2(false);
  752. await FormTimer_Tick(null, null);
  753. //auto_connect();
  754. await connect_next();
  755. }
  756. /// <summary>
  757. /// 切换扫描仪业务
  758. /// </summary>
  759. /// <param name="scanner"></param>
  760. /// <returns></returns>
  761. private async Task<ResponseEntity> change_scanner(Device_Scanner scanner)
  762. {
  763. ResponseEntity response = new ResponseEntity() { Status = 1, Message = "ok" };
  764. this.Invoke(new Action(() =>
  765. {
  766. this.cmbDeviceList.SelectedIndexChanged -= new System.EventHandler(this.cmbDeviceList_SelectedIndexChanged);
  767. this.cmbDeviceList.SelectedValue = scanner.dsIndex;
  768. biz_getScannerList.chosen_index = scanner.dsIndex;
  769. this.cmbDeviceList.SelectedIndexChanged += new System.EventHandler(this.cmbDeviceList_SelectedIndexChanged);
  770. //updateUIOLoadingState(true);
  771. updateUIOLoadingState2(true);
  772. }));
  773. response = await engine.set_current(scanner);
  774. this.Invoke(new Action(async () =>
  775. {
  776. if (response.Status == 1)
  777. {
  778. Settings.Default.lastScanner = scanner.deviceName;
  779. Settings.Default.Save();
  780. }
  781. //updateUIOLoadingState(false);
  782. updateUIOLoadingState2(false);
  783. await FormTimer_Tick(null, null);
  784. }));
  785. return response;
  786. }
  787. /// <summary>
  788. /// 测试网速 按钮点击
  789. /// </summary>
  790. /// <param name="sender"></param>
  791. /// <param name="e"></param>
  792. private async void lblTestNet_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  793. {
  794. //pingTestAsync();
  795. this.lblNetStatus.Text = "检查中...";
  796. this.lblNetStatus.ForeColor = Color.Gray;
  797. this.pl_net.BackgroundImage = Properties.Resources.Frame_15;
  798. this.picStatus.Image = Properties.Resources._1;
  799. //await checkAnswerCardServer();
  800. await check_net_status();
  801. show_status();
  802. }
  803. /// <summary>
  804. /// 测试网速业务
  805. /// </summary>
  806. //private void pingTestAsync()
  807. //{
  808. // this.Invoke(new Action(() =>
  809. // {
  810. // lblNetStatus.Text = "测试中";
  811. // lblNetStatus.ForeColor = Color.Gray;
  812. // }));
  813. // ThreadStart ts = new ThreadStart(ping);
  814. // Thread thread = new Thread(ts);
  815. // thread.Priority = ThreadPriority.Highest;
  816. // //thread.IsBackground = true; //关闭窗体继续执行
  817. // thread.Start();
  818. //}
  819. /// <summary>
  820. /// 测试网速业务
  821. /// </summary>
  822. //private void ping()
  823. //{
  824. // var url = Properties.Settings.Default.pingUrl;
  825. // float state = biz_ping.Test(Properties.Settings.Default.pingUrl);
  826. // string msg = "测试中";
  827. // Color color = Color.Gray;
  828. // Image img = Properties.Resources.yes;
  829. // this.net_status = 1;
  830. // if (state < 1)
  831. // {
  832. // msg = "较差";
  833. // color = Color.Red;
  834. // img = Properties.Resources.错_2x;
  835. // this.net_status = 2;
  836. // }
  837. // else if (state < 2.5)
  838. // {
  839. // msg = "一般";
  840. // color = Color.Orange;
  841. // }
  842. // else if (state < 5)
  843. // {
  844. // msg = "良好";
  845. // color = Color.Black;
  846. // }
  847. // else if (state < 15)
  848. // {
  849. // msg = "畅通";
  850. // color = Color.DarkGreen;
  851. // }
  852. // else
  853. // {
  854. // msg = "极佳";
  855. // color = Color.Green;
  856. // }
  857. // this.Invoke(new Action(() =>
  858. // {
  859. // picNetStatus.Image = img;
  860. // lblNetStatus.Text = msg;
  861. // lblNetStatus.ForeColor = color;
  862. // show_status();
  863. // }));
  864. //}
  865. /// <summary>
  866. /// 测试云端 按钮点击
  867. /// </summary>
  868. /// <param name="sender"></param>
  869. /// <param name="e"></param>
  870. private async void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  871. {
  872. this.lblCloudStatus.ForeColor = Color.Gray;
  873. this.lblCloudStatus.Text = "检查中...";
  874. this.pl_cloud.BackgroundImage = Properties.Resources.Frame_15;
  875. this.picStatus.Image = Properties.Resources._1;
  876. await checkAnswerCardServer();
  877. show_status();
  878. }
  879. /// <summary>
  880. /// 测试网络通畅 业务
  881. /// </summary>
  882. /// <returns></returns>
  883. private async Task check_net_status()
  884. {
  885. var isOk = await get_net_status();
  886. await Task.Run(() => { Thread.Sleep(1000); });
  887. this.Invoke(new Action(() =>
  888. {
  889. if (isOk)
  890. {
  891. picNetStatus.Image = Properties.Resources.对勾_1;
  892. lblNetStatus.Text = "正常";
  893. lblNetStatus.ForeColor = Color.Green;
  894. this.net_status = 1;
  895. }
  896. else
  897. {
  898. picNetStatus.Image = Properties.Resources.错_2x;
  899. lblNetStatus.Text = "无法连接,请联系客服";
  900. lblNetStatus.ForeColor = Color.Orange;
  901. this.net_status = 2;
  902. }
  903. }));
  904. }
  905. /// <summary>
  906. /// 测试云端 业务
  907. /// </summary>
  908. /// <returns></returns>
  909. private async Task checkAnswerCardServer()
  910. {
  911. var isOk = await getAnswerCardServer();
  912. await Task.Run(() => { Thread.Sleep(1000); });
  913. this.Invoke(new Action(() =>
  914. {
  915. if (isOk)
  916. {
  917. picCloudStatus.Image = Properties.Resources.对勾_1;
  918. lblCloudStatus.Text = "正常";
  919. lblCloudStatus.ForeColor = Color.Green;
  920. //picNetStatus.Image = Properties.Resources.对勾_1;
  921. //lblNetStatus.Text = "正常";
  922. //lblNetStatus.ForeColor = Color.Green;
  923. this.cloud_status = 1;
  924. //this.net_status = 1;
  925. }
  926. else
  927. {
  928. picCloudStatus.Image = Properties.Resources.错_2x;
  929. lblCloudStatus.Text = "无法连接,请联系客服";
  930. lblCloudStatus.ForeColor = Color.Orange;
  931. //picNetStatus.Image = Properties.Resources.错_2x;
  932. //lblNetStatus.Text = "无法连接,请联系客服";
  933. //lblNetStatus.ForeColor = Color.Orange;
  934. this.cloud_status = 2;
  935. //this.net_status = 2;
  936. }
  937. }));
  938. }
  939. /// <summary>
  940. /// 测试云端 业务
  941. /// </summary>
  942. /// <returns></returns>
  943. private async Task<bool> getAnswerCardServer()
  944. {
  945. Boolean isOk = false;
  946. try
  947. {
  948. ServicePointManager.SecurityProtocol =
  949. SecurityProtocolType.Ssl3 |
  950. SecurityProtocolType.Tls |
  951. SecurityProtocolType.Tls11 |
  952. SecurityProtocolType.Tls12;
  953. HttpClient httpClient = new HttpClient();
  954. //String url = Properties.Settings.Default.apiUrlBase + Properties.Settings.Default.answerCardUrl;
  955. //String url = Properties.Settings.Default.answerCardUrl;
  956. //url = "https://dev3.k12100.net/teaching/api/v1/scanned_student/available";
  957. //Dictionary<String, String> data = new Dictionary<string, string>();
  958. //data.Add("test", "1");
  959. //FormUrlEncodedContent content = new FormUrlEncodedContent(data);
  960. //HttpResponseMessage response = await httpClient.PostAsync(url, content);
  961. string url = ConfigManager.GetConfig("answerCardUrl");
  962. HttpResponseMessage response = await httpClient.GetAsync(url);
  963. response.EnsureSuccessStatusCode();
  964. String result = await response.Content.ReadAsStringAsync();
  965. JObject jsonData = (JObject)JsonConvert.DeserializeObject(result);
  966. int code = Int32.Parse(jsonData["code"].ToString());
  967. if (code == 200 || code == 500 || code == 401)
  968. {
  969. string dataStr = jsonData["data"].ToString();
  970. //云端状态ok
  971. isOk = true;
  972. }
  973. else
  974. {
  975. Console.WriteLine(DateTime.Now.ToString() + jsonData["msg"].ToString());
  976. }
  977. }
  978. catch (Exception e)
  979. {
  980. Console.WriteLine(DateTime.Now.ToString() + e);
  981. }
  982. return isOk;
  983. }
  984. private async Task<bool> get_net_status()
  985. {
  986. Boolean isOk = false;
  987. try
  988. {
  989. var url = "https://www.baidu.com";
  990. HttpClient client = new HttpClient();
  991. client.Timeout = TimeSpan.FromSeconds(2);
  992. var response = await client.GetAsync(url);
  993. response.EnsureSuccessStatusCode();
  994. isOk = true;
  995. }
  996. catch (Exception ee)
  997. {
  998. isOk = false;
  999. LogManager.WriteLog(ee);
  1000. }
  1001. return isOk;
  1002. }
  1003. /// <summary>
  1004. /// 开机自启动
  1005. /// </summary>
  1006. /// <param name="sender"></param>
  1007. /// <param name="e"></param>
  1008. private void checkBox1_CheckedChanged(object sender, EventArgs e)
  1009. {
  1010. RegisterHelper.AutoStart(checkBox1.Checked);
  1011. }
  1012. /// <summary>
  1013. /// 当http上传图片失败业务
  1014. /// </summary>
  1015. private void on_net_broken()
  1016. {
  1017. config_environment.scan_state = false;
  1018. to_check_net = true;
  1019. }
  1020. /// <summary>
  1021. /// http上传结果通知
  1022. /// </summary>
  1023. private void on_http_notify(upf result)
  1024. {
  1025. try
  1026. {
  1027. result.entity = null;
  1028. var txt = JsonConvert.SerializeObject(result.result.Data);
  1029. biz_socketServer.Send(txt);
  1030. }
  1031. catch (Exception ee)
  1032. {
  1033. }
  1034. }
  1035. /// <summary>
  1036. /// http上传结果通知2
  1037. /// </summary>
  1038. private void on_http_notify2(string result)
  1039. {
  1040. try
  1041. {
  1042. biz_socketServer.Send(result);
  1043. }
  1044. catch (Exception ee)
  1045. {
  1046. }
  1047. }
  1048. /// <summary>
  1049. /// 扫描开始通知
  1050. /// </summary>
  1051. private void on_startScan(string url)
  1052. {
  1053. try
  1054. {
  1055. Uri uri = new Uri(url);
  1056. Properties.Settings.Default.answerCardUrl = url;
  1057. Properties.Settings.Default.pingUrl = uri.Host;
  1058. Properties.Settings.Default.Save();
  1059. }
  1060. catch (Exception ee)
  1061. {
  1062. }
  1063. }
  1064. private bool first_change = true;
  1065. /// <summary>
  1066. /// 自动连接列表中下一台机器,在加入对usb的判断后,本方法已无意义
  1067. /// </summary>
  1068. /// <returns></returns>
  1069. private async Task connect_next()
  1070. {
  1071. await Task.Run(() =>
  1072. {
  1073. Thread.Sleep(1000);
  1074. });
  1075. if (!first_change)
  1076. {
  1077. return;
  1078. }
  1079. try
  1080. {
  1081. if (scanner_manager.Scanner_List.Count > 0)
  1082. {
  1083. var count = scanner_manager.Scanner_List.Count(x => x.openSourceReady == true);
  1084. if (count == 0)
  1085. {
  1086. this.Invoke(new Action(() =>
  1087. {
  1088. var index = this.cmbDeviceList.SelectedIndex;
  1089. if (index + 1 >= scanner_manager.Scanner_List.Count)
  1090. {
  1091. first_change = false;
  1092. return;
  1093. index = 0;
  1094. }
  1095. else
  1096. {
  1097. index++;
  1098. }
  1099. this.cmbDeviceList.SelectedIndex = index;
  1100. }));
  1101. }
  1102. }
  1103. }
  1104. catch (Exception ee)
  1105. {
  1106. }
  1107. }
  1108. bool first_active = true;
  1109. /// <summary>
  1110. /// 若第一次运行时发现没有设置目录,则弹出目录设置
  1111. /// </summary>
  1112. /// <param name="sender"></param>
  1113. /// <param name="e"></param>
  1114. private void frmMain2_Activated(object sender, EventArgs e)
  1115. {
  1116. if (first_active)
  1117. {
  1118. first_active = false;
  1119. if (string.IsNullOrEmpty(Settings.Default.dataDir))
  1120. {
  1121. lblChangeDir_LinkClicked(null, null);
  1122. }
  1123. this.TopMost = true;
  1124. this.TopMost = false;
  1125. }
  1126. }
  1127. /// <summary>
  1128. /// 版本检查
  1129. /// </summary>
  1130. private async void check_version()
  1131. {
  1132. //检查上次升级情况
  1133. var result = biz_version.read_updateInfo();
  1134. var version = ConfigManager.GetConfig("appVersion");
  1135. this.lblVersion.Text = "慧教研客户端 V" + version;
  1136. //版本检测
  1137. var entity = await biz_version.check();
  1138. //有升级进入升级流程,否则进入普通流程
  1139. if (entity != null && entity.code == 200 && entity.data.statusCode != 0 && entity.data.url.Length > 0)
  1140. {
  1141. //为1弹选择升级框,否则弹强制升级框
  1142. if (entity.data.statusCode == 1)
  1143. {
  1144. biz_mask.show_ask(this, entity);
  1145. }
  1146. else
  1147. {
  1148. biz_mask.show_force(this, entity);
  1149. }
  1150. }
  1151. else
  1152. {
  1153. //删除升级信息
  1154. var path = System.Windows.Forms.Application.StartupPath + @"\updateTemp";
  1155. if (Directory.Exists(path))
  1156. {
  1157. Directory.Delete(path, true);
  1158. }
  1159. if (result)
  1160. {
  1161. MsgManager.Show("已成功升级到 v" + version);
  1162. }
  1163. }
  1164. }
  1165. /// <summary>
  1166. /// 初始化升级所需参数
  1167. /// </summary>
  1168. private void init_environment()
  1169. {
  1170. //config_environment.environment = Custom_Environment.Test;
  1171. var environment = ConfigManager.GetConfig("environment");
  1172. if (environment == "1")
  1173. {
  1174. config_environment.url_base = ConfigManager.GetConfig("url_upgrade_formal");
  1175. }
  1176. else
  1177. {
  1178. config_environment.url_base = ConfigManager.GetConfig("url_upgrade_test");
  1179. }
  1180. }
  1181. /// <summary>
  1182. /// 托盘 鼠标双击
  1183. /// </summary>
  1184. /// <param name="sender"></param>
  1185. /// <param name="e"></param>
  1186. private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
  1187. {
  1188. this.WindowState = FormWindowState.Normal;
  1189. this.ShowInTaskbar = true;
  1190. this.TopMost = true;
  1191. this.TopMost = false;
  1192. }
  1193. private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
  1194. {
  1195. notifyIcon1_MouseDoubleClick(null, null);
  1196. }
  1197. }
  1198. }