frmMain4.cs 45 KB

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