frmMain3.cs 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365
  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. ThreadPool_manager.init();
  344. new self_http_manager().Start();
  345. check_version();
  346. }
  347. //protected override CreateParams CreateParams
  348. //{
  349. // get
  350. // {
  351. // CreateParams cp = base.CreateParams;
  352. // // 移除 WS_CAPTION 样式(包括标题栏)
  353. // cp.Style &= ~0x00C00000; // ~WS_CAPTION
  354. // return cp;
  355. // }
  356. //}
  357. /// <summary>
  358. /// 更新扫描历史信息
  359. /// </summary>
  360. private void update_scan_history()
  361. {
  362. Task.Run(() =>
  363. {
  364. while (true)
  365. {
  366. var history = biz_scanHistory.get_history();
  367. this.Invoke(new Action(() =>
  368. {
  369. this.lblStartTime.Text = history.last_start_time.ToString("HH:mm");
  370. //this.lblScanElapse.Text = history.last_scan_elapse.ToString() + "秒";
  371. this.lblScanElapse.Text = history.last_scan_elapse;
  372. this.lblTotalSum.Text = history.total_sum.ToString() + "张";
  373. this.lblSpeed.Text = history.last_speed.ToString() + "张/秒"; ;
  374. }));
  375. Thread.Sleep(1000);
  376. }
  377. });
  378. }
  379. /// <summary>
  380. /// 强杀驱动弹框
  381. /// </summary>
  382. private void update_scan_side_check()
  383. {
  384. Task.Run(() =>
  385. {
  386. while (true)
  387. {
  388. biz_engine_side.check();
  389. Thread.Sleep(50);
  390. }
  391. });
  392. }
  393. /// <summary>
  394. /// 根据插拔usb,随时检测扫描仪变化,更新扫描仪下拉框
  395. /// </summary>
  396. private void init_timer_auto_load_scanner()
  397. {
  398. Task.Run(() =>
  399. {
  400. while (true)
  401. {
  402. scanner_manager.refresh_scanner_list();
  403. //if (!scanner_manager.refresh_scanner_list())
  404. {
  405. //if (scanner_manager.Scanner_List.Count > 0 && scanner_manager.Scanner_List.Count(x => x.openSourceReady) == 0)
  406. {
  407. this.Invoke(new Action(() =>
  408. {
  409. this.lblRefreshDevice_LinkClicked(null, null);
  410. }));
  411. }
  412. }
  413. Thread.Sleep(2000);
  414. }
  415. });
  416. }
  417. /// <summary>
  418. /// 点击【刷新设备】按钮事件
  419. /// </summary>
  420. /// <param name="sender"></param>
  421. /// <param name="e"></param>
  422. private void lblRefreshDevice_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  423. {
  424. if (!config_environment.loading)
  425. {
  426. config_environment.loading = true;
  427. loadScannerByTwain();
  428. config_environment.loading = false;
  429. }
  430. }
  431. /// <summary>
  432. /// 点击【刷新设备】按钮事件,加载gif,执行刷新
  433. /// </summary>
  434. private async void loadScannerByTwain()
  435. {
  436. //updateUIOLoadingState(true);
  437. updateUIOLoadingState2(true);
  438. //await engine.load_tw_scanner_list();
  439. InitScannerCombobox();
  440. //updateUIOLoadingState(false);
  441. updateUIOLoadingState2(false);
  442. }
  443. /// <summary>
  444. /// 加载gif
  445. /// </summary>
  446. /// <param name="loading"></param>
  447. //private void updateUIOLoadingState(Boolean loading)
  448. //{
  449. // if (loading)
  450. // {
  451. // this.loadSB.Visible = true;
  452. // }
  453. // else
  454. // {
  455. // this.loadSB.Visible = false;
  456. // if (this.cmbDeviceList.Items.Count == 0)
  457. // {
  458. // //MsgManager.Show("没有加载到扫描仪");
  459. // }
  460. // }
  461. //}
  462. /// <summary>
  463. /// 加载gif
  464. /// </summary>
  465. /// <param name="loading"></param>
  466. private void updateUIOLoadingState2(bool status)
  467. {
  468. this.onCheck_status = status;
  469. }
  470. /// <summary>
  471. /// 初始化扫描仪下拉框
  472. /// </summary>
  473. private void InitScannerCombobox()
  474. {
  475. this.cmbDeviceList.SelectedIndexChanged -= new System.EventHandler(this.cmbDeviceList_SelectedIndexChanged);
  476. this.cmbDeviceList.DataSource = null;
  477. this.cmbDeviceList.DataSource = scanner_manager.Scanner_List;
  478. if (this.cmbDeviceList.Items.Count > 0)
  479. {
  480. var scanner = scanner_manager.Scanner_List.FirstOrDefault(x => x.deviceName == Settings.Default.lastScanner);
  481. if (scanner != null)
  482. {
  483. this.cmbDeviceList.SelectedItem = scanner;
  484. }
  485. else
  486. {
  487. this.cmbDeviceList.SelectedIndex = 0;
  488. }
  489. }
  490. this.cmbDeviceList.SelectedIndexChanged += new System.EventHandler(this.cmbDeviceList_SelectedIndexChanged);
  491. this.cmbDeviceList_SelectedIndexChanged(null, null);
  492. }
  493. /// <summary>
  494. /// 单面测试扫描,点击事件
  495. /// </summary>
  496. /// <param name="sender"></param>
  497. /// <param name="e"></param>
  498. private void lblTestA_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  499. {
  500. try
  501. {
  502. //config_environment.scan_type = ScanType.Formal;
  503. //config_environment.web_parameter = new subCmd_startScan();
  504. //config_environment.web_parameter.uploadUrl = "http://localhost:3768/api/upload/pppp";
  505. //var entity = new Database.tb_file_info();
  506. //entity.fi_schema = 1;
  507. //entity.fi_full_name = @"d:\1.txt";
  508. //entity.fi_accountId = "lii";
  509. //config_environment.web_parameter.token = "aabbcc22 " + DateTime.Now;
  510. //var e_q = new upf() { entity = entity, result = new ResponseEntity<response_http_uploadImg>() { Status = 1 }, seqNumber = 0 };
  511. //new http_manager().Upload(e_q);
  512. //return;
  513. //biz_engine_side.active_3rd_form(this.Handle);
  514. //return;
  515. config_environment.scan_type = ScanType.Test_Panel_1_Side;
  516. config_environment.use_source_ui = false;
  517. config_environment.scan_ab = false;
  518. config_environment.paper_size = PaperSize.A4;
  519. test_scan();
  520. }
  521. catch (Exception ee)
  522. {
  523. LogManager.WriteLog(ee);
  524. MsgManager.Error("扫描出错。");
  525. }
  526. }
  527. /// <summary>
  528. /// 双面测试扫描,点击事件
  529. /// </summary>
  530. /// <param name="sender"></param>
  531. /// <param name="e"></param>
  532. private void lblTestAB_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  533. {
  534. //scanner_manager.set_current(-1);
  535. //return;
  536. try
  537. {
  538. config_environment.scan_type = ScanType.Test_Panel_2_Sides;
  539. config_environment.use_source_ui = false;
  540. config_environment.scan_ab = true;
  541. config_environment.paper_size = PaperSize.A4;
  542. test_scan();
  543. }
  544. catch (Exception ee)
  545. {
  546. LogManager.WriteLog(ee);
  547. MsgManager.Error("扫描出错。");
  548. }
  549. }
  550. /// <summary>
  551. /// 测试扫描业务
  552. /// </summary>
  553. private async void test_scan()
  554. {
  555. var scanner = this.cmbDeviceList.SelectedItem as Device_Scanner;
  556. if (scanner != null)
  557. {
  558. scanner = scanner_manager.Scanner_List.FirstOrDefault(x => x.dsIndex == scanner.dsIndex);
  559. }
  560. if (scanner != null)
  561. {
  562. var result = await engine.Scan(scanner);
  563. if (result.Status != 1)
  564. {
  565. MsgManager.Warn(result.Message);
  566. }
  567. }
  568. else
  569. {
  570. this.Invoke(new Action(() =>
  571. {
  572. MsgManager.Warn("请选择扫描设备");
  573. }));
  574. }
  575. }
  576. /// <summary>
  577. /// 修改目录,点击事件
  578. /// </summary>
  579. /// <param name="sender"></param>
  580. /// <param name="e"></param>
  581. private void lblChangeDir_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  582. {
  583. changeDataDir();
  584. this.toolTip1.SetToolTip(this.lblDataPath, this.lblDataPath.Text);
  585. }
  586. private void changeDataDir()
  587. {
  588. FolderBrowserDialog dialog = new FolderBrowserDialog();
  589. dialog.Description = "请选择扫描数据存放位置";
  590. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  591. {
  592. if (string.IsNullOrEmpty(dialog.SelectedPath))
  593. {
  594. MsgManager.Warn("存放位置不能为空");
  595. }
  596. else
  597. {
  598. Settings.Default.dataDir = dialog.SelectedPath;
  599. Settings.Default.Save();
  600. showDataDir();
  601. }
  602. }
  603. }
  604. /// <summary>
  605. /// 显示当前剩余空间和路径展示
  606. /// </summary>
  607. private void showDataDir()
  608. {
  609. lblDataPath.Text = Settings.Default.dataDir;
  610. lblDiskLeft.Text = biz_disk.getDataDirLeftSpace(Settings.Default.dataDir);
  611. }
  612. /// <summary>
  613. /// 窗体关闭按钮点击
  614. /// </summary>
  615. /// <param name="sender"></param>
  616. /// <param name="e"></param>
  617. private void but_close_Click(object sender, EventArgs e)
  618. {
  619. this.WindowState = FormWindowState.Minimized;
  620. this.ShowInTaskbar = false;
  621. }
  622. /// <summary>
  623. /// 窗体最小化按钮点击
  624. /// </summary>
  625. /// <param name="sender"></param>
  626. /// <param name="e"></param>
  627. private void but_min_Click(object sender, EventArgs e)
  628. {
  629. this.WindowState = FormWindowState.Minimized;
  630. }
  631. Point mouseOff;
  632. bool leftFlag = false;
  633. /// <summary>
  634. /// 窗体移动相关
  635. /// </summary>
  636. /// <param name="sender"></param>
  637. /// <param name="e"></param>
  638. private void panel2_MouseMove(object sender, MouseEventArgs e)
  639. {
  640. if (leftFlag)
  641. {
  642. Point mouseSet = Control.MousePosition;
  643. mouseSet.Offset(mouseOff.X, mouseOff.Y);
  644. this.Location = mouseSet;
  645. }
  646. }
  647. /// <summary>
  648. /// 窗体移动相关
  649. /// </summary>
  650. /// <param name="sender"></param>
  651. /// <param name="e"></param>
  652. private void panel2_MouseUp(object sender, MouseEventArgs e)
  653. {
  654. if (leftFlag)
  655. {
  656. leftFlag = false;
  657. }
  658. }
  659. /// <summary>
  660. /// 窗体移动相关
  661. /// </summary>
  662. /// <param name="sender"></param>
  663. /// <param name="e"></param>
  664. private void panel2_MouseDown(object sender, MouseEventArgs e)
  665. {
  666. if (e.Button == MouseButtons.Left)
  667. {
  668. mouseOff = new Point(-e.X, -e.Y);
  669. leftFlag = true;
  670. }
  671. }
  672. /// <summary>
  673. /// 窗体关闭事件
  674. /// </summary>
  675. /// <param name="sender"></param>
  676. /// <param name="e"></param>
  677. private void frmMain2_FormClosing(object sender, FormClosingEventArgs e)
  678. {
  679. notifyIcon1.Dispose();
  680. Environment.Exit(0);
  681. }
  682. /// <summary>
  683. /// 托盘右键 退出按钮
  684. /// </summary>
  685. /// <param name="sender"></param>
  686. /// <param name="e"></param>
  687. private void exit_Click(object sender, EventArgs e)
  688. {
  689. string msg = string.Empty;
  690. if (config_environment.scan_state == true)
  691. {
  692. msg = "正在扫描中,您确定要退出吗?";
  693. }
  694. else if (!http_manager.check_is_queue_empty())
  695. {
  696. msg = "正在上传中,您确定要退出吗?";
  697. }
  698. else
  699. {
  700. msg = "您确定要退出吗?";
  701. }
  702. if (MsgManager.Ask(msg) == DialogResult.Yes)
  703. {
  704. Environment.Exit(0);
  705. }
  706. }
  707. /// <summary>
  708. /// 托盘右键 打开按钮
  709. /// </summary>
  710. /// <param name="sender"></param>
  711. /// <param name="e"></param>
  712. private void open_Click(object sender, EventArgs e)
  713. {
  714. this.WindowState = FormWindowState.Normal;
  715. this.ShowInTaskbar = true;
  716. }
  717. /// <summary>
  718. /// 扫描仪驱动下载 按钮点击
  719. /// </summary>
  720. /// <param name="sender"></param>
  721. /// <param name="e"></param>
  722. private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  723. {
  724. System.Diagnostics.Process.Start(Properties.Settings.Default.apiUrlBase + Properties.Settings.Default.DriverDownloadUrl);
  725. }
  726. /// <summary>
  727. /// 扫描仪列表,切换选择事件
  728. /// </summary>
  729. /// <param name="sender"></param>
  730. /// <param name="e"></param>
  731. private async void cmbDeviceList_SelectedIndexChanged(object sender, EventArgs e)
  732. {
  733. var scanner = this.cmbDeviceList.SelectedItem as Device_Scanner;
  734. if (scanner != null)
  735. {
  736. scanner = scanner_manager.Scanner_List.FirstOrDefault(x => x.dsIndex == scanner.dsIndex);
  737. }
  738. //updateUIOLoadingState(true);
  739. updateUIOLoadingState2(true);
  740. show_status();
  741. if (scanner != null)
  742. {
  743. biz_getScannerList.chosen_index = scanner.dsIndex;
  744. }
  745. var result = await engine.set_current(scanner);
  746. if (result.Status == 1)
  747. {
  748. Settings.Default.lastScanner = scanner.deviceName;
  749. Settings.Default.Save();
  750. }
  751. //updateUIOLoadingState(false);
  752. updateUIOLoadingState2(false);
  753. await FormTimer_Tick(null, null);
  754. //auto_connect();
  755. await connect_next();
  756. }
  757. /// <summary>
  758. /// 切换扫描仪业务
  759. /// </summary>
  760. /// <param name="scanner"></param>
  761. /// <returns></returns>
  762. private async Task<ResponseEntity> change_scanner(Device_Scanner scanner)
  763. {
  764. ResponseEntity response = new ResponseEntity() { Status = 1, Message = "ok" };
  765. this.Invoke(new Action(() =>
  766. {
  767. this.cmbDeviceList.SelectedIndexChanged -= new System.EventHandler(this.cmbDeviceList_SelectedIndexChanged);
  768. this.cmbDeviceList.SelectedValue = scanner.dsIndex;
  769. biz_getScannerList.chosen_index = scanner.dsIndex;
  770. this.cmbDeviceList.SelectedIndexChanged += new System.EventHandler(this.cmbDeviceList_SelectedIndexChanged);
  771. //updateUIOLoadingState(true);
  772. updateUIOLoadingState2(true);
  773. }));
  774. response = await engine.set_current(scanner);
  775. this.Invoke(new Action(async () =>
  776. {
  777. if (response.Status == 1)
  778. {
  779. Settings.Default.lastScanner = scanner.deviceName;
  780. Settings.Default.Save();
  781. }
  782. //updateUIOLoadingState(false);
  783. updateUIOLoadingState2(false);
  784. await FormTimer_Tick(null, null);
  785. }));
  786. return response;
  787. }
  788. /// <summary>
  789. /// 测试网速 按钮点击
  790. /// </summary>
  791. /// <param name="sender"></param>
  792. /// <param name="e"></param>
  793. private async void lblTestNet_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  794. {
  795. //pingTestAsync();
  796. this.lblNetStatus.Text = "检查中...";
  797. this.lblNetStatus.ForeColor = Color.Gray;
  798. this.pl_net.BackgroundImage = Properties.Resources.Frame_15;
  799. this.picStatus.Image = Properties.Resources._1;
  800. //await checkAnswerCardServer();
  801. await check_net_status();
  802. show_status();
  803. }
  804. /// <summary>
  805. /// 测试网速业务
  806. /// </summary>
  807. //private void pingTestAsync()
  808. //{
  809. // this.Invoke(new Action(() =>
  810. // {
  811. // lblNetStatus.Text = "测试中";
  812. // lblNetStatus.ForeColor = Color.Gray;
  813. // }));
  814. // ThreadStart ts = new ThreadStart(ping);
  815. // Thread thread = new Thread(ts);
  816. // thread.Priority = ThreadPriority.Highest;
  817. // //thread.IsBackground = true; //关闭窗体继续执行
  818. // thread.Start();
  819. //}
  820. /// <summary>
  821. /// 测试网速业务
  822. /// </summary>
  823. //private void ping()
  824. //{
  825. // var url = Properties.Settings.Default.pingUrl;
  826. // float state = biz_ping.Test(Properties.Settings.Default.pingUrl);
  827. // string msg = "测试中";
  828. // Color color = Color.Gray;
  829. // Image img = Properties.Resources.yes;
  830. // this.net_status = 1;
  831. // if (state < 1)
  832. // {
  833. // msg = "较差";
  834. // color = Color.Red;
  835. // img = Properties.Resources.错_2x;
  836. // this.net_status = 2;
  837. // }
  838. // else if (state < 2.5)
  839. // {
  840. // msg = "一般";
  841. // color = Color.Orange;
  842. // }
  843. // else if (state < 5)
  844. // {
  845. // msg = "良好";
  846. // color = Color.Black;
  847. // }
  848. // else if (state < 15)
  849. // {
  850. // msg = "畅通";
  851. // color = Color.DarkGreen;
  852. // }
  853. // else
  854. // {
  855. // msg = "极佳";
  856. // color = Color.Green;
  857. // }
  858. // this.Invoke(new Action(() =>
  859. // {
  860. // picNetStatus.Image = img;
  861. // lblNetStatus.Text = msg;
  862. // lblNetStatus.ForeColor = color;
  863. // show_status();
  864. // }));
  865. //}
  866. /// <summary>
  867. /// 测试云端 按钮点击
  868. /// </summary>
  869. /// <param name="sender"></param>
  870. /// <param name="e"></param>
  871. private async void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  872. {
  873. this.lblCloudStatus.ForeColor = Color.Gray;
  874. this.lblCloudStatus.Text = "检查中...";
  875. this.pl_cloud.BackgroundImage = Properties.Resources.Frame_15;
  876. this.picStatus.Image = Properties.Resources._1;
  877. await checkAnswerCardServer();
  878. show_status();
  879. }
  880. /// <summary>
  881. /// 测试网络通畅 业务
  882. /// </summary>
  883. /// <returns></returns>
  884. private async Task check_net_status()
  885. {
  886. var isOk = await get_net_status();
  887. await Task.Run(() => { Thread.Sleep(1000); });
  888. this.Invoke(new Action(() =>
  889. {
  890. if (isOk)
  891. {
  892. picNetStatus.Image = Properties.Resources.对勾_1;
  893. lblNetStatus.Text = "正常";
  894. lblNetStatus.ForeColor = Color.Green;
  895. this.net_status = 1;
  896. }
  897. else
  898. {
  899. picNetStatus.Image = Properties.Resources.错_2x;
  900. lblNetStatus.Text = "无法连接,请联系客服";
  901. lblNetStatus.ForeColor = Color.Orange;
  902. this.net_status = 2;
  903. }
  904. }));
  905. }
  906. /// <summary>
  907. /// 测试云端 业务
  908. /// </summary>
  909. /// <returns></returns>
  910. private async Task checkAnswerCardServer()
  911. {
  912. var isOk = await getAnswerCardServer();
  913. await Task.Run(() => { Thread.Sleep(1000); });
  914. this.Invoke(new Action(() =>
  915. {
  916. if (isOk)
  917. {
  918. picCloudStatus.Image = Properties.Resources.对勾_1;
  919. lblCloudStatus.Text = "正常";
  920. lblCloudStatus.ForeColor = Color.Green;
  921. //picNetStatus.Image = Properties.Resources.对勾_1;
  922. //lblNetStatus.Text = "正常";
  923. //lblNetStatus.ForeColor = Color.Green;
  924. this.cloud_status = 1;
  925. //this.net_status = 1;
  926. }
  927. else
  928. {
  929. picCloudStatus.Image = Properties.Resources.错_2x;
  930. lblCloudStatus.Text = "无法连接,请联系客服";
  931. lblCloudStatus.ForeColor = Color.Orange;
  932. //picNetStatus.Image = Properties.Resources.错_2x;
  933. //lblNetStatus.Text = "无法连接,请联系客服";
  934. //lblNetStatus.ForeColor = Color.Orange;
  935. this.cloud_status = 2;
  936. //this.net_status = 2;
  937. }
  938. }));
  939. }
  940. /// <summary>
  941. /// 测试云端 业务
  942. /// </summary>
  943. /// <returns></returns>
  944. private async Task<bool> getAnswerCardServer()
  945. {
  946. Boolean isOk = false;
  947. try
  948. {
  949. ServicePointManager.SecurityProtocol =
  950. SecurityProtocolType.Ssl3 |
  951. SecurityProtocolType.Tls |
  952. SecurityProtocolType.Tls11 |
  953. SecurityProtocolType.Tls12;
  954. HttpClient httpClient = new HttpClient();
  955. //String url = Properties.Settings.Default.apiUrlBase + Properties.Settings.Default.answerCardUrl;
  956. //String url = Properties.Settings.Default.answerCardUrl;
  957. //url = "https://dev3.k12100.net/teaching/api/v1/scanned_student/available";
  958. //Dictionary<String, String> data = new Dictionary<string, string>();
  959. //data.Add("test", "1");
  960. //FormUrlEncodedContent content = new FormUrlEncodedContent(data);
  961. //HttpResponseMessage response = await httpClient.PostAsync(url, content);
  962. string url = ConfigManager.GetConfig("answerCardUrl");
  963. HttpResponseMessage response = await httpClient.GetAsync(url);
  964. response.EnsureSuccessStatusCode();
  965. String result = await response.Content.ReadAsStringAsync();
  966. JObject jsonData = (JObject)JsonConvert.DeserializeObject(result);
  967. int code = Int32.Parse(jsonData["code"].ToString());
  968. if (code == 200 || code == 500 || code == 401)
  969. {
  970. string dataStr = jsonData["data"].ToString();
  971. //云端状态ok
  972. isOk = true;
  973. }
  974. else
  975. {
  976. Console.WriteLine(DateTime.Now.ToString() + jsonData["msg"].ToString());
  977. }
  978. }
  979. catch (Exception e)
  980. {
  981. Console.WriteLine(DateTime.Now.ToString() + e);
  982. }
  983. return isOk;
  984. }
  985. private async Task<bool> get_net_status()
  986. {
  987. Boolean isOk = false;
  988. try
  989. {
  990. var url = "https://www.baidu.com";
  991. HttpClient client = new HttpClient();
  992. client.Timeout = TimeSpan.FromSeconds(2);
  993. var response = await client.GetAsync(url);
  994. response.EnsureSuccessStatusCode();
  995. isOk = true;
  996. }
  997. catch (Exception ee)
  998. {
  999. isOk = false;
  1000. LogManager.WriteLog(ee);
  1001. }
  1002. return isOk;
  1003. }
  1004. /// <summary>
  1005. /// 开机自启动
  1006. /// </summary>
  1007. /// <param name="sender"></param>
  1008. /// <param name="e"></param>
  1009. private void checkBox1_CheckedChanged(object sender, EventArgs e)
  1010. {
  1011. RegisterHelper.AutoStart(checkBox1.Checked);
  1012. }
  1013. /// <summary>
  1014. /// 当http上传图片失败业务
  1015. /// </summary>
  1016. private void on_net_broken()
  1017. {
  1018. config_environment.scan_state = false;
  1019. to_check_net = true;
  1020. }
  1021. /// <summary>
  1022. /// http上传结果通知
  1023. /// </summary>
  1024. private void on_http_notify(upf result)
  1025. {
  1026. try
  1027. {
  1028. result.entity = null;
  1029. var txt = JsonConvert.SerializeObject(result.result.Data);
  1030. biz_socketServer.Send(txt);
  1031. }
  1032. catch (Exception ee)
  1033. {
  1034. }
  1035. }
  1036. /// <summary>
  1037. /// http上传结果通知2
  1038. /// </summary>
  1039. private void on_http_notify2(string result)
  1040. {
  1041. try
  1042. {
  1043. biz_socketServer.Send(result);
  1044. }
  1045. catch (Exception ee)
  1046. {
  1047. }
  1048. }
  1049. /// <summary>
  1050. /// 扫描开始通知
  1051. /// </summary>
  1052. private void on_startScan(string url)
  1053. {
  1054. try
  1055. {
  1056. Uri uri = new Uri(url);
  1057. Properties.Settings.Default.answerCardUrl = url;
  1058. Properties.Settings.Default.pingUrl = uri.Host;
  1059. Properties.Settings.Default.Save();
  1060. }
  1061. catch (Exception ee)
  1062. {
  1063. }
  1064. }
  1065. private bool first_change = true;
  1066. /// <summary>
  1067. /// 自动连接列表中下一台机器,在加入对usb的判断后,本方法已无意义
  1068. /// </summary>
  1069. /// <returns></returns>
  1070. private async Task connect_next()
  1071. {
  1072. await Task.Run(() =>
  1073. {
  1074. Thread.Sleep(1000);
  1075. });
  1076. if (!first_change)
  1077. {
  1078. return;
  1079. }
  1080. try
  1081. {
  1082. if (scanner_manager.Scanner_List.Count > 0)
  1083. {
  1084. var count = scanner_manager.Scanner_List.Count(x => x.openSourceReady == true);
  1085. if (count == 0)
  1086. {
  1087. this.Invoke(new Action(() =>
  1088. {
  1089. var index = this.cmbDeviceList.SelectedIndex;
  1090. if (index + 1 >= scanner_manager.Scanner_List.Count)
  1091. {
  1092. first_change = false;
  1093. return;
  1094. index = 0;
  1095. }
  1096. else
  1097. {
  1098. index++;
  1099. }
  1100. this.cmbDeviceList.SelectedIndex = index;
  1101. }));
  1102. }
  1103. }
  1104. }
  1105. catch (Exception ee)
  1106. {
  1107. }
  1108. }
  1109. bool first_active = true;
  1110. /// <summary>
  1111. /// 若第一次运行时发现没有设置目录,则弹出目录设置
  1112. /// </summary>
  1113. /// <param name="sender"></param>
  1114. /// <param name="e"></param>
  1115. private void frmMain2_Activated(object sender, EventArgs e)
  1116. {
  1117. if (first_active)
  1118. {
  1119. first_active = false;
  1120. if (string.IsNullOrEmpty(Settings.Default.dataDir))
  1121. {
  1122. lblChangeDir_LinkClicked(null, null);
  1123. }
  1124. this.TopMost = true;
  1125. this.TopMost = false;
  1126. }
  1127. }
  1128. /// <summary>
  1129. /// 版本检查
  1130. /// </summary>
  1131. private async void check_version()
  1132. {
  1133. //检查上次升级情况
  1134. var result = biz_version.read_updateInfo();
  1135. var version = ConfigManager.GetConfig("appVersion");
  1136. this.lblVersion.Text = "慧教研客户端 V" + version;
  1137. //版本检测
  1138. var entity = await biz_version.check();
  1139. //有升级进入升级流程,否则进入普通流程
  1140. if (entity != null && entity.code == 200 && entity.data.statusCode != 0 && entity.data.url.Length > 0)
  1141. {
  1142. //为1弹选择升级框,否则弹强制升级框
  1143. if (entity.data.statusCode == 1)
  1144. {
  1145. biz_mask.show_ask(this, entity);
  1146. }
  1147. else
  1148. {
  1149. biz_mask.show_force(this, entity);
  1150. }
  1151. }
  1152. else
  1153. {
  1154. //删除升级信息
  1155. var path = System.Windows.Forms.Application.StartupPath + @"\updateTemp";
  1156. if (Directory.Exists(path))
  1157. {
  1158. Directory.Delete(path, true);
  1159. }
  1160. if (result)
  1161. {
  1162. MsgManager.Show("已成功升级到 v" + version);
  1163. }
  1164. }
  1165. }
  1166. /// <summary>
  1167. /// 初始化升级所需参数
  1168. /// </summary>
  1169. private void init_environment()
  1170. {
  1171. //config_environment.environment = Custom_Environment.Test;
  1172. var environment = ConfigManager.GetConfig("environment");
  1173. if (environment == "1")
  1174. {
  1175. config_environment.url_base = ConfigManager.GetConfig("url_upgrade_formal");
  1176. }
  1177. else
  1178. {
  1179. config_environment.url_base = ConfigManager.GetConfig("url_upgrade_test");
  1180. }
  1181. }
  1182. /// <summary>
  1183. /// 托盘 鼠标双击
  1184. /// </summary>
  1185. /// <param name="sender"></param>
  1186. /// <param name="e"></param>
  1187. private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
  1188. {
  1189. this.WindowState = FormWindowState.Normal;
  1190. this.ShowInTaskbar = true;
  1191. this.TopMost = true;
  1192. this.TopMost = false;
  1193. }
  1194. private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
  1195. {
  1196. notifyIcon1_MouseDoubleClick(null, null);
  1197. }
  1198. }
  1199. }