using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;
using Saraff.Twain;
using HXX.Scanner.Socket;
using HXX.Scanner.Common;
namespace HXX.Scanner.Biz.Socket
{
///
/// 业务处理 开始扫描
///
public class biz_startScan
{
public static del_http_notify2 on_http_notify2;
public static del_notify_startScan on_startScan;
///
/// 主业务
///
///
///
public async static void Work(IntPtr connId, cmd_startScan2 data)
{
response_startScan response = new response_startScan();
if (!config_environment.scan_state)
{
if (http_manager.check_is_queue_empty())
{
var result = await work_detail(connId, data);
if (result.Status == 1)
{
response.code = 200;
response.msg = "启动扫描成功";
}
else
{
response.code = result.Status;
response.msg = result.Message;
}
}
else
{
response.code = 510;
response.msg = "启动扫描失败,上传正在进行中";
}
}
else
{
response.code = 509;
response.msg = "扫描仪正在使用中";
}
response.data = data;
//SocketServerManager.Send_Family(connId, response.ToJson());
SocketServerManager.Send_Single(connId, response.ToJson());
}
///
/// 扫描及构造返回值
///
///
///
///
private async static Task work_detail(IntPtr connId, cmd_startScan2 data)
{
var result = new ResponseEntity();
try
{
//获取当前激活的扫描仪
var scanner = scanner_manager.get_current();
if (scanner != null)
{
//设置环境参数
config_environment.scan_type2 = ScanType.Formal;
config_environment.scan_type = biz_environment.get_scan_type(0);
config_environment.scan_ab = biz_environment.get_ab(data.paperSchema);
config_environment.paper_size = biz_environment.get_paper_size(3);
//config_environment.use_source_ui = data.isLocalConfig == "0";
config_environment.web_parameter = data;//保存参数本身
config_environment.current_invoker = connId;
//config_environment.use_source_ui = false;
config_environment.use_source_ui = data.useDriveUI == 1;
if (data.dpi > 0)
{
scanner.resolution = data.dpi;
}
scanner.setPaperSize(config_environment.paper_size);
//初始化文件信息,文件名及计数器
file_manager.Init(data);
on_startScan(data.uploadUrl);
//告诉网页端开始扫描
var response = new response_uploading() { batchNumber = config_environment.web_parameter.batchNumber, subjectCode = config_environment.web_parameter.subjectCode };
on_http_notify2(JsonConvert.SerializeObject(response));
//发送扫描指令
result = await engine_twain32.Engine.Scan(scanner);
}
else
{
result.Status = 509;
result.Message = "未找到指定的扫描仪";
}
}
catch (TwainException te)
{
config_environment.scan_state = false;
LogManager.WriteLog(te);
result.Message = biz_twainException.get_TwainException_msg(te);
if (te.ConditionCode.Equals(TwCC.OperationError))
{
result.Status = 501;
}
else
{
result.Status = 501;
}
}
catch (Exception ee)
{
LogManager.WriteLog(ee);
result.Status = 509;
result.Message = ee.Message;
}
finally
{
//config_environment.scan_state = false;
}
return result;
}
private async static Task work_detail_test(IntPtr connId, cmd_startScan2 data)
{
var result = new ResponseEntity();
try
{
//设置环境参数
//config_environment.scan_type = biz_environment.get_scan_type(data.param.isTest);
//config_environment.scan_ab = biz_environment.get_ab(data.param.scannerParams.paperSchema);
//config_environment.paper_size = biz_environment.get_paper_size(data.param.scannerParams.paperSize);
//config_environment.use_source_ui = data.param.isLocalConfig == "1";
//config_environment.web_parameter = data.param;//保存参数本身
//config_environment.current_invoker = connId;
config_environment.scan_type2 = ScanType.Formal;
config_environment.scan_type = biz_environment.get_scan_type(0);
config_environment.scan_ab = biz_environment.get_ab(data.paperSchema);
config_environment.paper_size = biz_environment.get_paper_size(4);
config_environment.use_source_ui = data.isLocalConfig == "1";
config_environment.web_parameter = data;//保存参数本身
config_environment.current_invoker = connId;
config_environment.scan_state = true;
file_manager.Init(data);
var response = new response_uploading() { batchNumber = config_environment.web_parameter.batchNumber, subjectCode = config_environment.web_parameter.subjectCode };
on_http_notify2(JsonConvert.SerializeObject(response));
//模拟扫描
string ff = @"D:\test2\1\{0}.jpg";
//Directory.Delete(@"D:\test2\2\", true);
for (int i = 1; i <= 5; i++)
{
var fileInfo = file_manager.get_file_info2();
var file = fileInfo.file_full_name.Replace(".bmp", ".jpg");
var source = string.Format(ff, i.ToString());
File.Copy(source, file);
fileInfo.file_full_name = file;
fileInfo.file_name= fileInfo.file_name.Replace(".bmp", ".jpg");
fileInfo.file_ext = "jpg";
file_manager.dic_file_list[file] = fileInfo;
file_manager.save(file);
}
result.Status = 1;
result.Message = "okk";
}
catch (Exception ee)
{
LogManager.WriteLog(ee);
result.Status = 509;
result.Message = ee.Message;
}
finally
{
config_environment.scan_state = false;
}
return result;
}
}
}