| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Threading;
- using System.IO;
- using Microsoft.Win32;
- using System.Net;
- using System.Net.Http;
- using System.Text.RegularExpressions;
- using System.Runtime.InteropServices;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using Saraff.Tiff;
- using Saraff.Tiff.Core;
- using Saraff.Twain;
- using HXX.Scanner.Client.Base;
- using HXX.Scanner.Client.Properties;
- using HXX.Scanner.Biz;
- using HXX.Scanner.Common;
- using HXX.Scanner.Biz.Socket;
- using HXX.Scanner.Client.biz;
- namespace HXX.Scanner.Client
- {
- /// <summary>
- /// 主窗体
- /// </summary>
- public partial class frmMain3 : Form
- {
- public frmMain3()
- {
- //Font = new Font(Font.Name, 8.25f * 96f / CreateGraphics().DpiX, Font.Style, Font.Unit, Font.GdiCharSet, Font.GdiVerticalFont);
- CheckForIllegalCrossThreadCalls = false;
- InitializeComponent();
- SetStyle(
- ControlStyles.UserPaint |
- ControlStyles.AllPaintingInWmPaint |
- ControlStyles.OptimizedDoubleBuffer |
- ControlStyles.ResizeRedraw |
- ControlStyles.DoubleBuffer, true);
- this.DoubleBuffered = true;
- this.SetStyle(ControlStyles.Opaque, true);
- this.UpdateStyles();
- }
- //扫描中心
- engine_twain32 engine;
- #region 页面转态变化提示
- /// <summary>
- /// 初始化计时器,页面各状态值
- /// </summary>
- public void initTimer()
- {
- //System.Windows.Forms.Timer formTimer = new System.Windows.Forms.Timer();
- //formTimer.Interval = 3000;//设置中断时间 单位ms
- //formTimer.Enabled = true;
- //formTimer.Tick += FormTimer_Tick;
- this.checkBox1.Checked = RegisterHelper.GetRun();//开机启动
- Task.Run(async () =>
- {
- while (true)
- {
- this.Invoke(new Action(() =>
- {
- if (config_environment.scan_state)
- {
- this.lblTestA.Enabled = false;
- this.lblTestAB.Enabled = false;
- }
- else
- {
- this.lblTestA.Enabled = true;
- this.lblTestAB.Enabled = true;
- }
- }));
- await FormTimer_Tick(null, null);
- Thread.Sleep(10000);
- }
- });
- }
- /// <summary>
- /// 检查中状态
- /// </summary>
- private bool onCheck_status = true;
- /// <summary>
- /// 是否检查网络
- /// </summary>
- private bool to_check_net = true;
- /// <summary>
- /// 扫描仪状态
- /// </summary>
- private int scanner_status = 0;
- /// <summary>
- /// 磁盘状态
- /// </summary>
- private int disk_status = 1;
- /// <summary>
- /// 网络状态
- /// </summary>
- private int net_status = 1;
- /// <summary>
- /// 云端状态
- /// </summary>
- private int cloud_status = 1;
- /// <summary>
- /// 页面各状态检测
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- /// <returns></returns>
- private async Task FormTimer_Tick(object sender, EventArgs e)
- {
- ///1:检查扫描仪状态
- var scannerResult = await engine.check_status_ui();
- ///3:检查磁盘空间
- var diskResult = biz_disk.check_disk_status();
- if (to_check_net)
- {
- await checkAnswerCardServer();
- await check_net_status();
- //pingTestAsync();
- if (net_status == 1 && cloud_status == 1)
- {
- to_check_net = false;
- }
- }
- this.Invoke(new Action(() =>
- {
- if (scannerResult.Status == 501 || scannerResult.Status == 509)
- {
- scanner_status = 2;//未连接
- }
- else
- {
- scanner_status = 1;
- }
- if (diskResult.Status == 200)
- {
- disk_status = 1;
- }
- else if (diskResult.Status == 520)
- {
- disk_status = 2;//请先设置数据存放位置
- }
- else if (diskResult.Status == 510)
- {
- disk_status = 3;//磁盘剩余空间小于5G
- }
- else if (diskResult.Status == 509)
- {
- disk_status = 4;//磁盘剩余空间小于500M
- }
- show_status();
- }));
- }
- /// <summary>
- /// 页面状态信息改变
- /// </summary>
- /// <param name="Status">0:正常 1:scanner异常,2:disk异常</param>
- private void show_status()
- {
- if (this.onCheck_status)
- {
- this.picStatus.Image = Properties.Resources._1;
- }
- else
- {
- if (this.scanner_status == 1 && this.disk_status == 1 && this.cloud_status == 1 && this.net_status == 1)
- {
- //this.lab_banner.Text = "扫描环境检测完成,扫描设备已就位,可以开始进行扫描工作";
- //this.pan_banner.BackColor = Color.FromArgb(236, 249, 228);
- //this.lab_banner.ForeColor = Color.FromArgb(0, 192, 0);
- //this.lab_banner.BackColor = Color.FromArgb(236, 249, 228);
- //this.pic_banner.Image = Properties.Resources.校验_check_one_2x;
- this.picStatus.Image = Properties.Resources.正常;
- this.lbl_msg1.Text = "扫描环境检测完成";
- this.lbl_msg2.Text = "可以开始扫描工作";
- }
- else
- {
- if (this.scanner_status > 1)
- {
- //this.lab_banner.Text = "扫描仪连接异常,请检测扫描仪设备连接及驱动!";
- //this.pan_banner.BackColor = Color.FromArgb(254, 233, 233);
- //this.lab_banner.ForeColor = Color.FromArgb(245, 87, 87);
- //this.lab_banner.BackColor = Color.FromArgb(254, 233, 233);
- //this.pic_banner.Image = Properties.Resources.关闭_close_one_2x;
- this.picStatus.Image = Properties.Resources.异常2;
- this.lbl_msg1.Text = "扫描仪连接异常";
- this.lbl_msg2.Text = "请检测扫描仪设备连接及驱动";
- //新增,在有扫描仪连接的情况下,尝试启动
- //if (this.cmbDeviceList.SelectedIndex >= 0)
- //{
- // this.cmbDeviceList_SelectedIndexChanged(null, null);
- //}
- }
- else if (this.net_status > 1)
- {
- //this.lab_banner.Text = "网络故障,无法上传文件,请检查网络。";
- //this.pan_banner.BackColor = Color.FromArgb(254, 233, 233);
- //this.lab_banner.ForeColor = Color.FromArgb(245, 87, 87);
- //this.lab_banner.BackColor = Color.FromArgb(254, 233, 233);
- //this.pic_banner.Image = Properties.Resources.关闭_close_one_2x;
- this.picStatus.Image = Properties.Resources.异常2;
- this.lbl_msg1.Text = "网络故障";
- this.lbl_msg2.Text = "无法上传文件,请检查网络";
- }
- else if (this.cloud_status > 1)
- {
- //this.lab_banner.Text = "云服务器连接失败,请检查网络设置或联系技术人员。";
- //this.pan_banner.BackColor = Color.FromArgb(254, 233, 233);
- //this.lab_banner.ForeColor = Color.FromArgb(245, 87, 87);
- //this.lab_banner.BackColor = Color.FromArgb(254, 233, 233);
- //this.pic_banner.Image = Properties.Resources.关闭_close_one_2x;
- this.picStatus.Image = Properties.Resources.异常2;
- this.lbl_msg1.Text = "网络故障";
- this.lbl_msg2.Text = "无法上传文件,请检查网络";
- }
- else if (this.disk_status == 2)
- {
- //this.lab_banner.Text = "请先设置数据存放位置";
- //this.pan_banner.BackColor = Color.FromArgb(254, 233, 233);
- //this.lab_banner.ForeColor = Color.FromArgb(245, 87, 87);
- //this.lab_banner.BackColor = Color.FromArgb(254, 233, 233);
- //this.pic_banner.Image = Properties.Resources.关闭_close_one_2x;
- this.picStatus.Image = Properties.Resources.异常;
- this.lbl_msg1.Text = "请先设置数据存放位置";
- this.lbl_msg2.Text = "";
- }
- else if (this.disk_status == 4)
- {
- //this.lab_banner.Text = "数据存放目录空间不足,无法保存数据";
- //this.pan_banner.BackColor = Color.FromArgb(254, 233, 233);
- //this.lab_banner.ForeColor = Color.FromArgb(245, 87, 87);
- //this.lab_banner.BackColor = Color.FromArgb(254, 233, 233);
- //this.pic_banner.Image = Properties.Resources.注意_attention_2x;
- this.picStatus.Image = Properties.Resources.异常;
- this.lbl_msg1.Text = "数据存放目录空间不足";
- this.lbl_msg2.Text = "无法保存数据";
- }
- else if (this.disk_status == 3)
- {
- //this.lab_banner.Text = "磁盘剩余空间较少,建议清理或更换目录";
- //this.pan_banner.BackColor = Color.FromArgb(253, 244, 232);
- //this.lab_banner.ForeColor = Color.FromArgb(242, 191, 70);
- //this.lab_banner.BackColor = Color.FromArgb(253, 244, 232);
- //this.pic_banner.Image = Properties.Resources.注意_attention_2x;
- this.picStatus.Image = Properties.Resources.异常;
- this.lbl_msg1.Text = "磁盘剩余空间较少";
- this.lbl_msg2.Text = "建议清理或更换目录";
- }
- }
- if (this.scanner_status == 1)
- {
- this.pic_scannerStatus.Image = Properties.Resources.对勾_1;
- this.pl_scanner.BackgroundImage = Properties.Resources.Frame_7;
- }
- else
- {
- this.pic_scannerStatus.Image = Properties.Resources.错_2x;
- this.pl_scanner.BackgroundImage = Properties.Resources.Frame_14;
- }
- if (this.net_status == 1)
- {
- this.picNetStatus.Image = Properties.Resources.对勾_1;
- this.pl_net.BackgroundImage = Properties.Resources.Frame_7;
- }
- else
- {
- this.picNetStatus.Image = Properties.Resources.错_2x;
- this.pl_net.BackgroundImage = Properties.Resources.Frame_14;
- }
- if (this.cloud_status == 1)
- {
- this.picCloudStatus.Image = Properties.Resources.对勾_1;
- this.pl_cloud.BackgroundImage = Properties.Resources.Frame_7;
- }
- else
- {
- this.picCloudStatus.Image = Properties.Resources.错_2x;
- this.pl_cloud.BackgroundImage = Properties.Resources.Frame_14;
- }
- if (this.disk_status == 1)
- {
- this.pic_diskStatus.Image = Properties.Resources.对勾_1;
- this.pl_disk.BackgroundImage = Properties.Resources.Frame_7;
- }
- else
- {
- this.pic_diskStatus.Image = Properties.Resources.感叹_2x;
- this.pl_disk.BackgroundImage = Properties.Resources.Frame_12;
- }
- }
- }
- #endregion
- /// <summary>
- /// 主界面初始化
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void frmMain_Load(object sender, EventArgs e)
- {
- //this.Text = "慧教研扫描客户端";
- show_status();
- update_scan_side_check();
- engine = new engine_twain32(this, Settings.Default, this.change_scanner);
- showDataDir();
- this.lblInstallPath.Text = Application.StartupPath;
- biz_environment.init_unique_client_id();
- //biz_scannedBatch.Work();
- http_manager.on_net_broken = this.on_net_broken;
- http_manager.on_http_notify = this.on_http_notify;
- http_manager.on_http_notify2 = this.on_http_notify2;
- file_manager.on_http_notify2 = this.on_http_notify2;
- biz_startScan.on_http_notify2 = this.on_http_notify2;
- biz_startScan.on_startScan = this.on_startScan;
- biz_reUpload.on_startScan = this.on_startScan;
- this.lbl_msg1.Text = string.Empty;
- this.lbl_msg2.Text = string.Empty;
- this.toolTip1.IsBalloon = true;
- this.toolTip1.SetToolTip(this.lblInstallPath, this.lblInstallPath.Text);
- this.toolTip1.SetToolTip(this.lblDataPath, this.lblDataPath.Text);
- this.toolTip1.SetToolTip(this.but_min, "最小化");
- this.toolTip1.SetToolTip(this.but_close, "最小化到托盘");
- if (!biz_socketServer.Start(Settings.Default.webSocket_url))
- {
- MsgManager.Error("服务开启失败,请检查");
- }
- engine.load_tw_scanner_list();
- init_timer_auto_load_scanner();
- //lblRefreshDevice_LinkClicked(null, null);
- initTimer();
- //pingTestAsync();
- check_net_status();
- checkAnswerCardServer();
- update_scan_history();
- http_manager.start_http_upload();
- init_environment();
- ThreadPool_manager.init();
- new self_http_manager().Start();
- check_version();
- }
- //protected override CreateParams CreateParams
- //{
- // get
- // {
- // CreateParams cp = base.CreateParams;
- // // 移除 WS_CAPTION 样式(包括标题栏)
- // cp.Style &= ~0x00C00000; // ~WS_CAPTION
- // return cp;
- // }
- //}
- /// <summary>
- /// 更新扫描历史信息
- /// </summary>
- private void update_scan_history()
- {
- Task.Run(() =>
- {
- while (true)
- {
- var history = biz_scanHistory.get_history();
- this.Invoke(new Action(() =>
- {
- this.lblStartTime.Text = history.last_start_time.ToString("HH:mm");
- //this.lblScanElapse.Text = history.last_scan_elapse.ToString() + "秒";
- this.lblScanElapse.Text = history.last_scan_elapse;
- this.lblTotalSum.Text = history.total_sum.ToString() + "张";
- this.lblSpeed.Text = history.last_speed.ToString() + "张/秒"; ;
- }));
- Thread.Sleep(1000);
- }
- });
- }
- /// <summary>
- /// 强杀驱动弹框
- /// </summary>
- private void update_scan_side_check()
- {
- Task.Run(() =>
- {
- while (true)
- {
- biz_engine_side.check();
- Thread.Sleep(50);
- }
- });
- }
- /// <summary>
- /// 根据插拔usb,随时检测扫描仪变化,更新扫描仪下拉框
- /// </summary>
- private void init_timer_auto_load_scanner()
- {
- Task.Run(() =>
- {
- while (true)
- {
- scanner_manager.refresh_scanner_list();
- //if (!scanner_manager.refresh_scanner_list())
- {
- //if (scanner_manager.Scanner_List.Count > 0 && scanner_manager.Scanner_List.Count(x => x.openSourceReady) == 0)
- {
- this.Invoke(new Action(() =>
- {
- this.lblRefreshDevice_LinkClicked(null, null);
- }));
- }
- }
- Thread.Sleep(2000);
- }
- });
- }
- /// <summary>
- /// 点击【刷新设备】按钮事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void lblRefreshDevice_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
- {
- if (!config_environment.loading)
- {
- config_environment.loading = true;
- loadScannerByTwain();
- config_environment.loading = false;
- }
- }
- /// <summary>
- /// 点击【刷新设备】按钮事件,加载gif,执行刷新
- /// </summary>
- private async void loadScannerByTwain()
- {
- //updateUIOLoadingState(true);
- updateUIOLoadingState2(true);
- //await engine.load_tw_scanner_list();
- InitScannerCombobox();
- //updateUIOLoadingState(false);
- updateUIOLoadingState2(false);
- }
- /// <summary>
- /// 加载gif
- /// </summary>
- /// <param name="loading"></param>
- //private void updateUIOLoadingState(Boolean loading)
- //{
- // if (loading)
- // {
- // this.loadSB.Visible = true;
- // }
- // else
- // {
- // this.loadSB.Visible = false;
- // if (this.cmbDeviceList.Items.Count == 0)
- // {
- // //MsgManager.Show("没有加载到扫描仪");
- // }
- // }
- //}
- /// <summary>
- /// 加载gif
- /// </summary>
- /// <param name="loading"></param>
- private void updateUIOLoadingState2(bool status)
- {
- this.onCheck_status = status;
- }
- /// <summary>
- /// 初始化扫描仪下拉框
- /// </summary>
- private void InitScannerCombobox()
- {
- this.cmbDeviceList.SelectedIndexChanged -= new System.EventHandler(this.cmbDeviceList_SelectedIndexChanged);
- this.cmbDeviceList.DataSource = null;
- this.cmbDeviceList.DataSource = scanner_manager.Scanner_List;
- if (this.cmbDeviceList.Items.Count > 0)
- {
- var scanner = scanner_manager.Scanner_List.FirstOrDefault(x => x.deviceName == Settings.Default.lastScanner);
- if (scanner != null)
- {
- this.cmbDeviceList.SelectedItem = scanner;
- }
- else
- {
- this.cmbDeviceList.SelectedIndex = 0;
- }
- }
- this.cmbDeviceList.SelectedIndexChanged += new System.EventHandler(this.cmbDeviceList_SelectedIndexChanged);
- this.cmbDeviceList_SelectedIndexChanged(null, null);
- }
- /// <summary>
- /// 单面测试扫描,点击事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void lblTestA_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
- {
- try
- {
- //config_environment.scan_type = ScanType.Formal;
- //config_environment.web_parameter = new subCmd_startScan();
- //config_environment.web_parameter.uploadUrl = "http://localhost:3768/api/upload/pppp";
- //var entity = new Database.tb_file_info();
- //entity.fi_schema = 1;
- //entity.fi_full_name = @"d:\1.txt";
- //entity.fi_accountId = "lii";
- //config_environment.web_parameter.token = "aabbcc22 " + DateTime.Now;
- //var e_q = new upf() { entity = entity, result = new ResponseEntity<response_http_uploadImg>() { Status = 1 }, seqNumber = 0 };
- //new http_manager().Upload(e_q);
- //return;
- //biz_engine_side.active_3rd_form(this.Handle);
- //return;
- config_environment.scan_type = ScanType.Test_Panel_1_Side;
- config_environment.use_source_ui = false;
- config_environment.scan_ab = false;
- config_environment.paper_size = PaperSize.A4;
- test_scan();
- }
- catch (Exception ee)
- {
- LogManager.WriteLog(ee);
- MsgManager.Error("扫描出错。");
- }
- }
- /// <summary>
- /// 双面测试扫描,点击事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void lblTestAB_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
- {
- //scanner_manager.set_current(-1);
- //return;
- try
- {
- config_environment.scan_type = ScanType.Test_Panel_2_Sides;
- config_environment.use_source_ui = false;
- config_environment.scan_ab = true;
- config_environment.paper_size = PaperSize.A4;
- test_scan();
- }
- catch (Exception ee)
- {
- LogManager.WriteLog(ee);
- MsgManager.Error("扫描出错。");
- }
- }
- /// <summary>
- /// 测试扫描业务
- /// </summary>
- private async void test_scan()
- {
- var scanner = this.cmbDeviceList.SelectedItem as Device_Scanner;
- if (scanner != null)
- {
- scanner = scanner_manager.Scanner_List.FirstOrDefault(x => x.dsIndex == scanner.dsIndex);
- }
- if (scanner != null)
- {
- var result = await engine.Scan(scanner);
- if (result.Status != 1)
- {
- MsgManager.Warn(result.Message);
- }
- }
- else
- {
- this.Invoke(new Action(() =>
- {
- MsgManager.Warn("请选择扫描设备");
- }));
- }
- }
- /// <summary>
- /// 修改目录,点击事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void lblChangeDir_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
- {
- changeDataDir();
- this.toolTip1.SetToolTip(this.lblDataPath, this.lblDataPath.Text);
- }
- private void changeDataDir()
- {
- FolderBrowserDialog dialog = new FolderBrowserDialog();
- dialog.Description = "请选择扫描数据存放位置";
- if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- if (string.IsNullOrEmpty(dialog.SelectedPath))
- {
- MsgManager.Warn("存放位置不能为空");
- }
- else
- {
- Settings.Default.dataDir = dialog.SelectedPath;
- Settings.Default.Save();
- showDataDir();
- }
- }
- }
- /// <summary>
- /// 显示当前剩余空间和路径展示
- /// </summary>
- private void showDataDir()
- {
- lblDataPath.Text = Settings.Default.dataDir;
- lblDiskLeft.Text = biz_disk.getDataDirLeftSpace(Settings.Default.dataDir);
- }
- /// <summary>
- /// 窗体关闭按钮点击
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void but_close_Click(object sender, EventArgs e)
- {
- this.WindowState = FormWindowState.Minimized;
- this.ShowInTaskbar = false;
- }
- /// <summary>
- /// 窗体最小化按钮点击
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void but_min_Click(object sender, EventArgs e)
- {
- this.WindowState = FormWindowState.Minimized;
- }
- Point mouseOff;
- bool leftFlag = false;
- /// <summary>
- /// 窗体移动相关
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void panel2_MouseMove(object sender, MouseEventArgs e)
- {
- if (leftFlag)
- {
- Point mouseSet = Control.MousePosition;
- mouseSet.Offset(mouseOff.X, mouseOff.Y);
- this.Location = mouseSet;
- }
- }
- /// <summary>
- /// 窗体移动相关
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void panel2_MouseUp(object sender, MouseEventArgs e)
- {
- if (leftFlag)
- {
- leftFlag = false;
- }
- }
- /// <summary>
- /// 窗体移动相关
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void panel2_MouseDown(object sender, MouseEventArgs e)
- {
- if (e.Button == MouseButtons.Left)
- {
- mouseOff = new Point(-e.X, -e.Y);
- leftFlag = true;
- }
- }
- /// <summary>
- /// 窗体关闭事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void frmMain2_FormClosing(object sender, FormClosingEventArgs e)
- {
- notifyIcon1.Dispose();
- Environment.Exit(0);
- }
- /// <summary>
- /// 托盘右键 退出按钮
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void exit_Click(object sender, EventArgs e)
- {
- string msg = string.Empty;
- if (config_environment.scan_state == true)
- {
- msg = "正在扫描中,您确定要退出吗?";
- }
- else if (!http_manager.check_is_queue_empty())
- {
- msg = "正在上传中,您确定要退出吗?";
- }
- else
- {
- msg = "您确定要退出吗?";
- }
- if (MsgManager.Ask(msg) == DialogResult.Yes)
- {
- Environment.Exit(0);
- }
- }
- /// <summary>
- /// 托盘右键 打开按钮
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void open_Click(object sender, EventArgs e)
- {
- this.WindowState = FormWindowState.Normal;
- this.ShowInTaskbar = true;
- }
- /// <summary>
- /// 扫描仪驱动下载 按钮点击
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
- {
- System.Diagnostics.Process.Start(Properties.Settings.Default.apiUrlBase + Properties.Settings.Default.DriverDownloadUrl);
- }
- /// <summary>
- /// 扫描仪列表,切换选择事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private async void cmbDeviceList_SelectedIndexChanged(object sender, EventArgs e)
- {
- var scanner = this.cmbDeviceList.SelectedItem as Device_Scanner;
- if (scanner != null)
- {
- scanner = scanner_manager.Scanner_List.FirstOrDefault(x => x.dsIndex == scanner.dsIndex);
- }
- //updateUIOLoadingState(true);
- updateUIOLoadingState2(true);
- show_status();
- if (scanner != null)
- {
- biz_getScannerList.chosen_index = scanner.dsIndex;
- }
- var result = await engine.set_current(scanner);
- if (result.Status == 1)
- {
- Settings.Default.lastScanner = scanner.deviceName;
- Settings.Default.Save();
- }
- //updateUIOLoadingState(false);
- updateUIOLoadingState2(false);
- await FormTimer_Tick(null, null);
- //auto_connect();
- await connect_next();
- }
- /// <summary>
- /// 切换扫描仪业务
- /// </summary>
- /// <param name="scanner"></param>
- /// <returns></returns>
- private async Task<ResponseEntity> change_scanner(Device_Scanner scanner)
- {
- ResponseEntity response = new ResponseEntity() { Status = 1, Message = "ok" };
- this.Invoke(new Action(() =>
- {
- this.cmbDeviceList.SelectedIndexChanged -= new System.EventHandler(this.cmbDeviceList_SelectedIndexChanged);
- this.cmbDeviceList.SelectedValue = scanner.dsIndex;
- biz_getScannerList.chosen_index = scanner.dsIndex;
- this.cmbDeviceList.SelectedIndexChanged += new System.EventHandler(this.cmbDeviceList_SelectedIndexChanged);
- //updateUIOLoadingState(true);
- updateUIOLoadingState2(true);
- }));
- response = await engine.set_current(scanner);
- this.Invoke(new Action(async () =>
- {
- if (response.Status == 1)
- {
- Settings.Default.lastScanner = scanner.deviceName;
- Settings.Default.Save();
- }
- //updateUIOLoadingState(false);
- updateUIOLoadingState2(false);
- await FormTimer_Tick(null, null);
- }));
- return response;
- }
- /// <summary>
- /// 测试网速 按钮点击
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private async void lblTestNet_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
- {
- //pingTestAsync();
- this.lblNetStatus.Text = "检查中...";
- this.lblNetStatus.ForeColor = Color.Gray;
- this.pl_net.BackgroundImage = Properties.Resources.Frame_15;
- this.picStatus.Image = Properties.Resources._1;
- //await checkAnswerCardServer();
- await check_net_status();
- show_status();
- }
- /// <summary>
- /// 测试网速业务
- /// </summary>
- //private void pingTestAsync()
- //{
- // this.Invoke(new Action(() =>
- // {
- // lblNetStatus.Text = "测试中";
- // lblNetStatus.ForeColor = Color.Gray;
- // }));
- // ThreadStart ts = new ThreadStart(ping);
- // Thread thread = new Thread(ts);
- // thread.Priority = ThreadPriority.Highest;
- // //thread.IsBackground = true; //关闭窗体继续执行
- // thread.Start();
- //}
- /// <summary>
- /// 测试网速业务
- /// </summary>
- //private void ping()
- //{
- // var url = Properties.Settings.Default.pingUrl;
- // float state = biz_ping.Test(Properties.Settings.Default.pingUrl);
- // string msg = "测试中";
- // Color color = Color.Gray;
- // Image img = Properties.Resources.yes;
- // this.net_status = 1;
- // if (state < 1)
- // {
- // msg = "较差";
- // color = Color.Red;
- // img = Properties.Resources.错_2x;
- // this.net_status = 2;
- // }
- // else if (state < 2.5)
- // {
- // msg = "一般";
- // color = Color.Orange;
- // }
- // else if (state < 5)
- // {
- // msg = "良好";
- // color = Color.Black;
- // }
- // else if (state < 15)
- // {
- // msg = "畅通";
- // color = Color.DarkGreen;
- // }
- // else
- // {
- // msg = "极佳";
- // color = Color.Green;
- // }
- // this.Invoke(new Action(() =>
- // {
- // picNetStatus.Image = img;
- // lblNetStatus.Text = msg;
- // lblNetStatus.ForeColor = color;
- // show_status();
- // }));
- //}
- /// <summary>
- /// 测试云端 按钮点击
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private async void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
- {
- this.lblCloudStatus.ForeColor = Color.Gray;
- this.lblCloudStatus.Text = "检查中...";
- this.pl_cloud.BackgroundImage = Properties.Resources.Frame_15;
- this.picStatus.Image = Properties.Resources._1;
- await checkAnswerCardServer();
- show_status();
- }
- /// <summary>
- /// 测试网络通畅 业务
- /// </summary>
- /// <returns></returns>
- private async Task check_net_status()
- {
- var isOk = await get_net_status();
- await Task.Run(() => { Thread.Sleep(1000); });
- this.Invoke(new Action(() =>
- {
- if (isOk)
- {
- picNetStatus.Image = Properties.Resources.对勾_1;
- lblNetStatus.Text = "正常";
- lblNetStatus.ForeColor = Color.Green;
- this.net_status = 1;
- }
- else
- {
- picNetStatus.Image = Properties.Resources.错_2x;
- lblNetStatus.Text = "无法连接,请联系客服";
- lblNetStatus.ForeColor = Color.Orange;
- this.net_status = 2;
- }
- }));
- }
- /// <summary>
- /// 测试云端 业务
- /// </summary>
- /// <returns></returns>
- private async Task checkAnswerCardServer()
- {
- var isOk = await getAnswerCardServer();
- await Task.Run(() => { Thread.Sleep(1000); });
- this.Invoke(new Action(() =>
- {
- if (isOk)
- {
- picCloudStatus.Image = Properties.Resources.对勾_1;
- lblCloudStatus.Text = "正常";
- lblCloudStatus.ForeColor = Color.Green;
- //picNetStatus.Image = Properties.Resources.对勾_1;
- //lblNetStatus.Text = "正常";
- //lblNetStatus.ForeColor = Color.Green;
- this.cloud_status = 1;
- //this.net_status = 1;
- }
- else
- {
- picCloudStatus.Image = Properties.Resources.错_2x;
- lblCloudStatus.Text = "无法连接,请联系客服";
- lblCloudStatus.ForeColor = Color.Orange;
- //picNetStatus.Image = Properties.Resources.错_2x;
- //lblNetStatus.Text = "无法连接,请联系客服";
- //lblNetStatus.ForeColor = Color.Orange;
- this.cloud_status = 2;
- //this.net_status = 2;
- }
- }));
- }
- /// <summary>
- /// 测试云端 业务
- /// </summary>
- /// <returns></returns>
- private async Task<bool> getAnswerCardServer()
- {
- Boolean isOk = false;
- try
- {
- ServicePointManager.SecurityProtocol =
- SecurityProtocolType.Ssl3 |
- SecurityProtocolType.Tls |
- SecurityProtocolType.Tls11 |
- SecurityProtocolType.Tls12;
- HttpClient httpClient = new HttpClient();
- //String url = Properties.Settings.Default.apiUrlBase + Properties.Settings.Default.answerCardUrl;
- //String url = Properties.Settings.Default.answerCardUrl;
- //url = "https://dev3.k12100.net/teaching/api/v1/scanned_student/available";
- //Dictionary<String, String> data = new Dictionary<string, string>();
- //data.Add("test", "1");
- //FormUrlEncodedContent content = new FormUrlEncodedContent(data);
- //HttpResponseMessage response = await httpClient.PostAsync(url, content);
- string url = ConfigManager.GetConfig("answerCardUrl");
- HttpResponseMessage response = await httpClient.GetAsync(url);
- response.EnsureSuccessStatusCode();
- String result = await response.Content.ReadAsStringAsync();
- JObject jsonData = (JObject)JsonConvert.DeserializeObject(result);
- int code = Int32.Parse(jsonData["code"].ToString());
- if (code == 200 || code == 500 || code == 401)
- {
- string dataStr = jsonData["data"].ToString();
- //云端状态ok
- isOk = true;
- }
- else
- {
- Console.WriteLine(DateTime.Now.ToString() + jsonData["msg"].ToString());
- }
- }
- catch (Exception e)
- {
- Console.WriteLine(DateTime.Now.ToString() + e);
- }
- return isOk;
- }
- private async Task<bool> get_net_status()
- {
- Boolean isOk = false;
- try
- {
- var url = "https://www.baidu.com";
- HttpClient client = new HttpClient();
- client.Timeout = TimeSpan.FromSeconds(2);
- var response = await client.GetAsync(url);
- response.EnsureSuccessStatusCode();
- isOk = true;
- }
- catch (Exception ee)
- {
- isOk = false;
- LogManager.WriteLog(ee);
- }
- return isOk;
- }
- /// <summary>
- /// 开机自启动
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void checkBox1_CheckedChanged(object sender, EventArgs e)
- {
- RegisterHelper.AutoStart(checkBox1.Checked);
- }
- /// <summary>
- /// 当http上传图片失败业务
- /// </summary>
- private void on_net_broken()
- {
- config_environment.scan_state = false;
- to_check_net = true;
- }
- /// <summary>
- /// http上传结果通知
- /// </summary>
- private void on_http_notify(upf result)
- {
- try
- {
- result.entity = null;
- var txt = JsonConvert.SerializeObject(result.result.Data);
- biz_socketServer.Send(txt);
- }
- catch (Exception ee)
- {
- }
- }
- /// <summary>
- /// http上传结果通知2
- /// </summary>
- private void on_http_notify2(string result)
- {
- try
- {
- biz_socketServer.Send(result);
- }
- catch (Exception ee)
- {
- }
- }
- /// <summary>
- /// 扫描开始通知
- /// </summary>
- private void on_startScan(string url)
- {
- try
- {
- Uri uri = new Uri(url);
- Properties.Settings.Default.answerCardUrl = url;
- Properties.Settings.Default.pingUrl = uri.Host;
- Properties.Settings.Default.Save();
- }
- catch (Exception ee)
- {
- }
- }
- private bool first_change = true;
- /// <summary>
- /// 自动连接列表中下一台机器,在加入对usb的判断后,本方法已无意义
- /// </summary>
- /// <returns></returns>
- private async Task connect_next()
- {
- await Task.Run(() =>
- {
- Thread.Sleep(1000);
- });
- if (!first_change)
- {
- return;
- }
- try
- {
- if (scanner_manager.Scanner_List.Count > 0)
- {
- var count = scanner_manager.Scanner_List.Count(x => x.openSourceReady == true);
- if (count == 0)
- {
- this.Invoke(new Action(() =>
- {
- var index = this.cmbDeviceList.SelectedIndex;
- if (index + 1 >= scanner_manager.Scanner_List.Count)
- {
- first_change = false;
- return;
- index = 0;
- }
- else
- {
- index++;
- }
- this.cmbDeviceList.SelectedIndex = index;
- }));
- }
- }
- }
- catch (Exception ee)
- {
- }
- }
- bool first_active = true;
- /// <summary>
- /// 若第一次运行时发现没有设置目录,则弹出目录设置
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void frmMain2_Activated(object sender, EventArgs e)
- {
- if (first_active)
- {
- first_active = false;
- if (string.IsNullOrEmpty(Settings.Default.dataDir))
- {
- lblChangeDir_LinkClicked(null, null);
- }
- this.TopMost = true;
- this.TopMost = false;
- }
- }
- /// <summary>
- /// 版本检查
- /// </summary>
- private async void check_version()
- {
- //检查上次升级情况
- var result = biz_version.read_updateInfo();
- var version = ConfigManager.GetConfig("appVersion");
- this.lblVersion.Text = "慧教研客户端 V" + version;
- //版本检测
- var entity = await biz_version.check();
- //有升级进入升级流程,否则进入普通流程
- if (entity != null && entity.code == 200 && entity.data.statusCode != 0 && entity.data.url.Length > 0)
- {
- //为1弹选择升级框,否则弹强制升级框
- if (entity.data.statusCode == 1)
- {
- biz_mask.show_ask(this, entity);
- }
- else
- {
- biz_mask.show_force(this, entity);
- }
- }
- else
- {
- //删除升级信息
- var path = System.Windows.Forms.Application.StartupPath + @"\updateTemp";
- if (Directory.Exists(path))
- {
- Directory.Delete(path, true);
- }
- if (result)
- {
- MsgManager.Show("已成功升级到 v" + version);
- }
- }
- }
- /// <summary>
- /// 初始化升级所需参数
- /// </summary>
- private void init_environment()
- {
- //config_environment.environment = Custom_Environment.Test;
- var environment = ConfigManager.GetConfig("environment");
- if (environment == "1")
- {
- config_environment.url_base = ConfigManager.GetConfig("url_upgrade_formal");
- }
- else
- {
- config_environment.url_base = ConfigManager.GetConfig("url_upgrade_test");
- }
- }
- /// <summary>
- /// 托盘 鼠标双击
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
- {
- this.WindowState = FormWindowState.Normal;
- this.ShowInTaskbar = true;
- this.TopMost = true;
- this.TopMost = false;
- }
- private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
- {
- notifyIcon1_MouseDoubleClick(null, null);
- }
- }
- }
|