| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364 |
- 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();
- 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);
- }
- }
- }
|