12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610 |
- using System;
- using System.Windows.Forms;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.IO;
- using System.Configuration;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using System.Net;
- using System.Net.Sockets;
- //using GenerateReport;
- namespace AiControlRequest
- {
- /// <summary>
- /// Ai控制过程结果
- /// </summary>
- public class AiControl
- {
- #region 检测项目枚举参数
- /// <summary>
- /// 检测分类参数
- /// </summary>
- public enum catagoryParam //钢种
- {
- /// <summary>
- /// 灰铸铁
- /// </summary>
- GrayCastIron = 14, //灰铸铁
- /// <summary>
- /// 球墨铸铁
- /// </summary>
- NodularCastIron = 15, //球墨铸铁
- /// <summary>
- /// 轴承钢
- /// </summary>
- BearingSteel = 16, //轴承钢
- /// <summary>
- /// 亚共析带状
- /// </summary>
- HypoeutectoidStrip = 17, //亚共析带状
- /// <summary>
- /// 组织识别
- /// </summary>
- OrganizationIdentification = 0, //组织识别
- /// <summary>
- /// 魏氏
- /// </summary>
- Westergren = 18, //魏氏
- /// <summary>
- /// 夹杂物
- /// </summary>
- Inclusion = 18, //夹杂物
- /// <summary>
- /// 晶粒度
- /// </summary>
- GrainSize = 18, //晶粒度
- }
- /// <summary>
- /// 检测项目参数
- /// </summary>
- public enum itemParam //项目
- {
- /// <summary>
- /// 石墨形状
- /// </summary>
- GraphiteShape = 1,
- /// <summary>
- /// 石墨长度
- /// </summary>
- GraphiteLength = 2,
- /// <summary>
- /// 珠光体数量
- /// </summary>
- PearliteNumber = 3,
- /// <summary>
- /// 磷共晶数量
- /// </summary>
- SteaditeNumber = 5,
- /// <summary>
- /// 共晶团分类检测
- /// </summary>
- EutecticClusterItemize = 10,
- /// <summary>
- /// 球化率
- /// </summary>
- Nodularity = 1,
- /// <summary>
- /// 球墨大小
- /// </summary>
- SpheroidalGraphiteSize = 2,
- /// <summary>
- /// 铁素体数量
- /// </summary>
- FerriteNumber = 4,
- /// <summary>
- /// 球化退火
- /// </summary>
- Spheroidizing = 1,
- /// <summary>
- /// 碳化物网状
- /// </summary>
- CarbideNetwork = 2,
- /// <summary>
- /// 碳化物带状
- /// </summary>
- CarbideBanding = 4,
- /// <summary>
- /// 亚共析钢带状-A
- /// </summary>
- HypoeutectoidStripA = 11,
- /// <summary>
- /// 亚共析钢带状-B
- /// </summary>
- HypoeutectoidStripB = 12,
- /// <summary>
- /// 亚共析钢带状-C
- /// </summary>
- HypoeutectoidStripC = 13,
- /// <summary>
- /// 亚共析钢带状-D
- /// </summary>
- HypoeutectoidStripD = 14,
- /// <summary>
- /// 亚共析钢带状-E
- /// </summary>
- HypoeutectoidStripE = 15,
- /// <summary>
- /// 魏氏-A
- /// </summary>
- WestergrenA = 3,
- /// <summary>
- /// 魏氏-B
- /// </summary>
- WestergrenB = 3,
- /// <summary>
- /// 夹杂物
- /// </summary>
- Inclusion = 1,
- /// <summary>
- /// 晶粒度-定级
- /// </summary>
- GrainSizeGrading = 2,
- /// <summary>
- /// 晶粒度-分类检测
- /// </summary>
- GrainSizeItemize = 10,
- /// <summary>
- /// 组织识别
- /// </summary>
- OrganizationIdentification = 0
- }
- /// <summary>
- /// 评级参数
- /// </summary>
- public enum jsonParam //评级json参数
- {
- /// <summary>
- /// 无参数
- /// </summary>
- Null = 0,
- /// <summary>
- /// 魏氏-A
- /// </summary>
- WestergrenA = 1,
- /// <summary>
- /// 魏氏-B
- /// </summary>
- WestergrenB = 2,
- /// <summary>
- /// 夹杂物ABC
- /// </summary>
- InclusionABC = 3,
- /// <summary>
- /// 夹杂物DDS
- /// </summary>
- InclusionDDS = 4,
- /// <summary>
- /// 夹杂物ALL
- /// </summary>
- InclusionALL = 5,
- /// <summary>
- /// 轴承钢-碳化物带状垂直
- /// </summary>
- CarbideBandingVertical = 6,
- /// <summary>
- /// 组织识别-碳素钢珠光体定量
- /// </summary>
- CarbonSteelPearliteNumber = 7,
- /// <summary>
- /// 组织识别-α铁素体含量
- /// </summary>
- AlphafFerriteContent = 8,
- /// <summary>
- /// 组织识别-δ铁素体含量
- /// </summary>
- DeltafFerriteContent = 9,
- /// <summary>
- /// 组织识别-A+F双相含量
- /// </summary>
- AFBiphasicContent = 10
- }
- /// <summary>
- /// 评级参数对应的字符串
- /// </summary>
- private List<String> jsonList = new List<string>()
- {
- @"",
- @"{'YBG-W': {'w_cls':'wa'}}",
- @"{'YBG-W': {'w_cls':'wb'}}",
- @"{'YGB-JZW': {'output_cls_names': ['a','b','c']}}",
- @"{'YGB-JZW': {'output_cls_names': ['d','ds']}}",
- @"{'YGB-JZW': {'output_cls_names': ['a','b','c','d','ds']}}",
- @"{'ZCG_GTG-DZ': {'direction':'v'}}",
- @"{'draw_color_zz':['p']}",
- @"{'draw_color_zz':['alphaf']}",
- @"{'draw_color_zz':['deltaf']}",
- @"{'draw_color_zz':['a'],'zzsb_names':['a','f']}"
- };
- #endregion
- #region 参数定义
- //读取程序所在目录
- private string path = System.AppDomain.CurrentDomain.BaseDirectory;
- //配置文件参数
- private ExeConfigurationFileMap map = new ExeConfigurationFileMap();
- private Configuration cfg;
- //定义一个文件夹路径
- private String _gradePath;
- /// <summary>
- /// 必须在评级结束时才可以更改目录
- /// </summary>
- public String GradePaht
- {
- get { return _gradePath; }
- set
- {
- if (_isStart == false)
- {
- _gradePath = value;
- }
- }
- }
- //定义一个列表,存放一些列表的集合
- private List<List<String>> _nameList = new List<List<string>>();
- //定义一个批次返回处理完成的文件列表
- private List<String> _batchList = new List<string>();
- //定义一个存储文件全路径的集合
- private List<String> _tempList = new List<string>();
- //定义一个锁,判断请求是否有返回
- private Boolean _postLock = false;
- //操作之间的互斥锁
- private Boolean _mutexLock = false;
- //定义一个发送请求的最小数
- private int _countList = 100;
- //定义执行该操作的开始标志
- private Boolean _isStart;
- private Boolean _isEnd;
- private Boolean _isFirst;
- //定义本机评级开关
- private Boolean _localLock = false;
- //定义是远程还是本机
- private Boolean _isLocalHost;
- //请求的URL
- private String _url = "";
- private const String Post = "Post";
- private const String Content_Type = "Content-Type:application/json";
- //本地评级参数
- PythonExecutor pythonExecutor = null;
- private SynchronizationContext synchronizationContext = null;
- Label lblret = new Label();
- private int test_index = 0;//当前完成评级的图片数量
- private int test_count = 0;//当前评级的总张数
- //评级检测参数-分类、项目、参数
- private String _catagory;
- private String _item;
- private String _param;
- //报告参数
- //WordReport wordReport = null;
- //评级结果文件名
- String resultName = "";
- //是否单机测试
- Boolean _isDebug = false;
- //放大倍数
- private int _zoom_key = 1;
- //分辨率
- private double _um_per_pix = 0;
- //批次序号
- int batch_index = 0;
- #endregion
- #region 事件
- //委托事件-批次结束
- public delegate void CompleteBatch(List<String> batchfilenames);
- public event CompleteBatch CompleteBatchEvent;
- //委托事件-全部结束
- public delegate void CompleteAll(String resultfilename);
- public event CompleteAll CompleteAllEvent;
- #endregion
- #region 初始化参数
- /// <summary>
- /// 构造函数
- /// </summary>
- /// <param name="gradepath">评级图片的路径</param>
- public AiControl(String gradepath)
- {
- LogHelper.info("初始化20210520");
- //识别图片目录
- _gradePath = gradepath;
- LogHelper.info("获取路径");
- if (!System.IO.Directory.Exists(_gradePath))
- {
- LogHelper.info("目录不存在");
- throw (new Exception("目录文件不存在!"));
- }
- LogHelper.info("初始化评级参数");
- //初始化评级参数
- Init();
- }
- /// <summary>
- /// 初始化参数
- /// </summary>
- private void Init()
- {
- _postLock = false; //远程锁
- _mutexLock = false; //互斥锁
- _isStart = false; //未开始评级
- _isEnd = true; //评级没有结束
- _isFirst = false;
- //线程间同步上下文
- lblret.Text = "";
- //this.synchronizationContext = SynchronizationContext.Current;
- //pythonExecutor = new PythonExecutor(SynchronizationContext.Current);
- //pythonExecutor.ReceivedControlData += ReceivedControlData;
- LogHelper.info("读取配置文件");
- ReadAISetting();
- }
- /// <summary>
- /// 读配置文件参数
- /// </summary>
- private void ReadAISetting()
- {
- try
- {
- //读取配置文件参数
- map.ExeConfigFilename = Path.Combine(path, "AISetting.config");
- cfg = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
- //服务器URL
- _url = cfg.AppSettings.Settings["ServerURL"].Value;
- //远程或本地
- _isLocalHost = Convert.ToBoolean(cfg.AppSettings.Settings["IsLocalHost"].Value);
- //评级文件最小数
- _countList = Convert.ToInt32(cfg.AppSettings.Settings["CountList"].Value);
- //读结果文件名
- resultName = cfg.AppSettings.Settings["ResultName"].Value;
- //调试状态
- _isDebug = Convert.ToBoolean(cfg.AppSettings.Settings["Debug"].Value);
- }
- catch
- {
- throw (new Exception("读取配置文件出错!"));
- }
- }
- #endregion
- #region 检测远程端口是否打开
- public Boolean Test_Port()
- {
- int s1 = this._url.IndexOf("//") + 2;
- int s2 = this._url.IndexOf(":");
- s2 = this._url.IndexOf(":", s2 + 1);
- int s3 = this._url.IndexOf("/", s2 + 1);
- string ipd = this._url.Substring(s1, s2 - s1);
- string port = this._url.Substring(s2 + 1, s3 - s2 - 1);
- IPAddress ip = IPAddress.Parse(ipd);
- IPEndPoint point = new IPEndPoint(ip, int.Parse(port));
- try
- {
- TcpClient tcp = new TcpClient();
- tcp.Connect(point);
- tcp.Close();
- tcp = null;
- }
- catch
- {
- return false;
- }
- return true;
- }
- #endregion
- #region 参数设置
- /// <summary>
- /// 服务器URL设置(包括IP:端口和服务地址)
- /// </summary>
- /// <param name="url">服务器URL设置(包括IP:端口和服务地址)</param>
- public void SetServerURL(String url)
- {
- if (_isStart == false)
- {
- this._url = url;
- cfg.AppSettings.Settings["ServerURL"].Value = url;
- cfg.Save();
- LogHelper.info(url);
- }
- }
- /// <summary>
- /// 评级文件的最小总数
- /// </summary>
- /// <param name="countList">评级文件的最小总数</param>
- public void SetCountList(int countList)
- {
- if (_isStart == false)
- {
- if (countList < 0)
- {
- return;
- }
- this._countList = countList;
- cfg.AppSettings.Settings["CountList"].Value = countList.ToString();
- cfg.Save();
- LogHelper.info("文件最小总数设置=" + countList.ToString());
- }
- }
- /// <summary>
- /// 远程或本地评级
- /// </summary>
- /// <param name="isLocalhost">True:本地;False:远程</param>
- private void SetIsLocalHost(Boolean isLocalhost)
- {
- this._isLocalHost = isLocalhost;
- cfg.AppSettings.Settings["IsLocalHost"].Value = this._isLocalHost.ToString();
- cfg.Save();
- }
- #endregion
- #region 评级开始和结束处理函数
- /// <summary>
- /// 开始评级且设置评级参数
- /// </summary>
- /// <param name="catagory">分类</param>
- /// <param name="item">项目</param>
- /// <param name="param">评级参数</param>
- /// <returns>评级开始设置是否成功</returns>
- public Boolean StartAndSetGradeParam(catagoryParam catagory, itemParam item, jsonParam param, int zoom_key, double um_per_pix)
- {
- if (_isEnd == false || this._postLock == true)
- {
- return false;
- }
- try
- {
- this._zoom_key = zoom_key;
- this._um_per_pix = um_per_pix;
- this._catagory = ((int)catagory).ToString();
- String ctgy = catagoryParam.GetName(typeof(catagoryParam), catagory);
- //根据分类查找模板文件
- //try
- //{
- // switch (ctgy)
- // {
- // case "GrayCastIron":
- // wordReport = new WordReport(path + "\\Template\\灰铸铁.docx");
- // break;
- // case "NodularCastIron":
- // wordReport = new WordReport(path + "\\Template\\球墨铸铁.docx");
- // break;
- // case "BearingSteel":
- // wordReport = new WordReport(path + "\\Template\\轴承钢.docx");
- // break;
- // case "HypoeutectoidStrip":
- // wordReport = new WordReport(path + "\\Template\\亚共析带状.docx");
- // break;
- // case "Westergren":
- // wordReport = new WordReport(path + "\\Template\\魏氏.docx");
- // break;
- // case "Inclusion":
- // wordReport = new WordReport(path + "\\Template\\夹杂物.docx");
- // break;
- // case "GrainSize":
- // wordReport = new WordReport(path + "\\Template\\晶粒度.docx");
- // break;
- // default:
- // wordReport = new WordReport(path + "\\Template\\灰铸铁.docx");
- // break;
- // }
- //}
- //catch(Exception e)
- //{
- // throw (new Exception(e.ToString()));
- //}
- LogHelper.info("zoom_key=" + zoom_key.ToString());
- LogHelper.info("um_per_pix=" + um_per_pix.ToString());
- LogHelper.info("Server URL=" + this._url);
- LogHelper.info("CountList =" + this._countList.ToString());
- LogHelper.info("开始评级");
- batch_index = 0;
- this._item = ((int)item).ToString();
- //wordReport = new WordReport(path + "\\Template\\夹杂物.docx");
- this._param = jsonList[(int)param];
- _isStart = true;
- _isEnd = false;
- _isFirst = true;
- return true;
- }
- catch
- {
- return false;
- }
- }
- public Boolean StartAndSetGradeParam(int zoom_key, double um_per_pix)
- {
- if (_isEnd == false || this._postLock == true)
- {
- return false;
- }
- try
- {
- LogHelper.info("zoom_key=" + zoom_key.ToString());
- LogHelper.info("um_per_pix=" + um_per_pix.ToString());
- LogHelper.info("开始评级");
- batch_index = 0;
- this._zoom_key = zoom_key;
- this._um_per_pix = um_per_pix;
- this._catagory = "18";
- this._item = "1";
- this._param = jsonList[5];
- _isStart = true;
- _isEnd = false;
- _isFirst = true;
- return true;
- }
- catch
- {
- return false;
- }
- }
- /// <summary>
- /// 结束本次评级
- /// </summary>
- public void EndGrade()
- {
- if (!this._isStart)
- {
- return;
- }
- this._isEnd = true;
- LogHelper.info("执行结束本次评级");
- //判断是否还有文件,如果还有则进行评级
- if (_nameList.Count == 1 && _nameList[0].Count > 0 && _postLock == false)
- {
- //互斥锁检测
- while (_mutexLock) { }
- //上锁
- _mutexLock = true;
- //发送请求
- Thread th = new Thread(SendPost);
- th.Start();
- }
- else if (_postLock == false)
- {
- //如果结束时所有文件都评完级,则发一个空的图片数据和结束参数
- JObject json = new JObject();
- JObject json1 = new JObject();
- JObject json2 = new JObject();
- json1.Add("end", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
- json1.Add("catagory", this._catagory);
- json1.Add("items", this._item);
- json1.Add("path", this._gradePath.Replace("\\", "/"));
- if (this._catagory == "0")
- {
- json1.Add("func", "zzsb");
- }
- else
- {
- json1.Add("func", "bzpj");
- }
- json1.Add("images", "");
- json.Add("json1", json1);
- if (this._param != "")
- {
- json2 = JObject.Parse(this._param);
- }
- json2.Add("zoom_key", this._zoom_key);
- json2.Add("um_per_pix", this._um_per_pix);
- json.Add("json2", json2);
- String param = json.ToString();
- LogHelper.info(param);
- if (!this._isDebug)
- {
- //发送请求
- String res = RequestString(param);
- }
- //完成后且记录条数为0则发送完成事件
- try
- {
- this._isStart = false;
- CompleteAllEvent(AnalysisAIFile());
- LogHelper.info("评级全部结束");
- }
- catch (Exception e)
- {
- CompleteAllEvent("");
- LogHelper.info("ERR:" + e.ToString());
- }
- }
- }
- #endregion
- #region 返回图像检测结果
- /// <summary>
- /// 获取图像检测结果
- /// </summary>
- /// <param name="filename">检测图像的全路径和文件名</param>
- /// <returns>检测结果</returns>
- public String GetImageTestResult(String filename)
- {
- string ret = "";
- if (this._isDebug)
- {
- return "{\"fun\":\"test\",\"return\":\"test_result\"}";
- }
- if (File.Exists(filename))
- {
- String aifilename = Path.Combine(Path.GetDirectoryName(filename), "AI", Path.GetFileNameWithoutExtension(filename) + ".ai");
- if (File.Exists(aifilename))
- {
- using (System.IO.StreamReader _file = System.IO.File.OpenText(aifilename))
- {
- using (JsonTextReader reader = new JsonTextReader(_file))
- {
- try
- {
- JObject o = (JObject)JToken.ReadFrom(reader);
- JObject content = JObject.Parse(o["content"].ToString());
- ret = content["title"].ToString();
- }
- catch (Exception e)
- {
- throw (new Exception("AI文件错误!"));
- }
- reader.Close();
- _file.Close();
- _file.Dispose();
- }
- }
- }
- }
- LogHelper.info("返回" + filename + "的结果:" + ret);
- return ret;
- }
- #endregion
- #region 返回检测过程图像
- /// <summary>
- /// 返回检测过程图像
- /// </summary>
- /// <param name="filename">检测图像的全路径和文件名</param>
- /// <returns>过程图像的路径和文件名</returns>
- public String GetImageProcess(String filename)
- {
- string ret = "";
- if (this._isDebug)
- {
- return "{\"fun\":\"test\",\"return\":\"fullfilename\"}";
- }
- if (File.Exists(filename))
- {
- String aifilename = Path.Combine(Path.GetDirectoryName(filename), "AI", Path.GetFileName(filename));
- if (File.Exists(aifilename))
- {
- ret = aifilename;
- }
- }
- LogHelper.info("返回" + filename + "的过程图像:" + ret);
- return ret;
- }
- #endregion
- #region 插入单个文件
- /// <summary>
- /// 插入单个文件路径
- /// </summary>
- /// <param name="filename">文件路径字符串</param>
- /// <returns>0:满足条件发送请求;1:记录不够;2:请求正在执行;3:本地请求正在执行;255:没有开始工作</returns>
- public int PostFileName(String filename)
- {
- //如果没有开始过程则不与理会
- if (!this._isStart)
- {
- return 255;
- }
- //如果文件路径为空
- if (filename == "")
- {
- return 254;
- }
- //向最后的文件列表添加数据
- if (_nameList.Count == 0 || _nameList[_nameList.Count - 1].Count == this._countList)
- {
- List<String> fileList = new List<string>();
- _nameList.Add(fileList);
- }
- _nameList[_nameList.Count - 1].Add(filename);
- LogHelper.info("插入单个文件" + filename);
- //请求锁
- if (_postLock)
- {
- return 2;
- }
- //文件数量不满足
- else if (_nameList[0].Count < _countList)
- {
- return 1;
- }
- //如果请求锁打开且文件列表第一个集合满足记录条数则发送请求
- else
- {
- //请求锁应该在这里上
- _postLock = true;
- //互斥锁检测
- while (_mutexLock) { }
- //上锁
- _mutexLock = true;
- //发送请求
- Thread th = new Thread(SendPost);
- th.Start();
- }
- return 0;
- }
- #endregion
- #region 发送Post线程
- /// <summary>
- /// 线程-发送请求
- /// </summary>
- private void SendPost()
- {
- //发送请求锁
- _postLock = true;
- //解锁
- _mutexLock = false;
- //清空数据
- _batchList.Clear();
- //取发送请求参数
- string images = "";
- for (int i = 0; i < _nameList[0].Count; i++)
- {
- images += _nameList[0][i] + ";";
- _batchList.Add(_nameList[0][i]);
- }
- images = images.Substring(0, images.Length - 1);
- JObject json = new JObject();
- JObject json1 = new JObject();
- JObject json2 = new JObject();
- if (_isFirst)
- {
- json1.Add("start", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
- _isFirst = false;
- }
- if (_isEnd == true && _nameList.Count == 1)
- {
- json1.Add("end", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
- }
- json1.Add("catagory", this._catagory);
- json1.Add("items", this._item);
- json1.Add("path", this._gradePath.Replace("\\", "/"));
- if (this._catagory == "0")
- {
- json1.Add("func", "zzsb");
- }
- else
- {
- json1.Add("func", "bzpj");
- }
- json1.Add("images", images);
- json.Add("json1", json1);
- if (this._param != "")
- {
- json2 = JObject.Parse(this._param);
- }
- json2.Add("zoom_key", this._zoom_key);
- json2.Add("um_per_pix", this._um_per_pix);
- json.Add("json2", json2);
- String param = json.ToString();
- //发送完请求后删除文件
- _nameList.RemoveAt(0);
- LogHelper.info(param);
- if (!this._isDebug)
- {
- //发送请求
- String res = RequestString(param);
- }
- //批次完成事件
- try
- {
- CompleteBatchEvent(_batchList);
- batch_index++;
- LogHelper.info("batchListCount=" + _batchList.Count.ToString());
- LogHelper.info("批次结束,序号-" + batch_index.ToString());
- }
- catch (Exception e)
- {
- LogHelper.info("ERR:" + e.ToString());
- }
- //再判断是否满足条件
- if (_nameList.Count > 0 && _nameList[0].Count == _countList)
- {
- //上锁
- _mutexLock = true;
- //请求锁
- _postLock = true;
- //发送请求
- Thread th = new Thread(SendPost);
- th.Start();
- }
- else if (_nameList.Count == 1 && _nameList[0].Count > 0 && _isEnd)
- {
- //上锁
- _mutexLock = true;
- //请求锁
- _postLock = true;
- //发送请求
- Thread th = new Thread(SendPost);
- th.Start();
- }
- else if (_isEnd == true && _nameList.Count == 0)
- {
- //完成后且记录条数为0则发送完成事件
- try
- {
- this._isStart = false;
- CompleteAllEvent(AnalysisAIFile());
- LogHelper.info("全部评级结束");
- }
- catch (Exception e)
- {
- CompleteAllEvent("");
- LogHelper.info("ERR:" + e.ToString());
- }
- }
- //请求返回后解锁
- _postLock = false;
- }
- #endregion
- #region Post返回结果处理
- /// <summary>
- /// 向WebServer请求数据及返回结果
- /// </summary>
- /// <param name="postString">请求数据的JSON</param>
- /// <returns>返回结果的JSON</returns>
- private String RequestString(String postString)
- {
- try
- {
- String res = HttpRequestHelper.DoRequest(this._url, Post, Content_Type, postString);
- return res;
- }
- catch
- {
- return "Error";
- }
- }
- #endregion
- #region 生成报告
- /// <summary>
- /// 生成报告
- /// </summary>
- /// <param name="wordPath">生成报告的文件</param>
- /// <param name="project_json">从界面传的json参数</param>
- /// <returns></returns>
- private Boolean GetWordReport(String wordPath, String project_json)
- {
- return true;
- //JObject jo = new JObject();
- //if (_isDebug)
- //{
- // jo.Add("Project", "这是一个测试报告");
- // return wordReport.GenerateWordReport(wordPath, jo.ToString());
- //}
- //else
- //{
- // try
- // {
- // jo = JObject.Parse(project_json);
- // jo.Add("REPORT_NO", DateTime.Now.ToString("yyyyMMddHHmmssfff"));
- // jo.Add("REPORT_DATE", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
- // //查找result文件
- // String aiFile = @"D:/aitest".Replace('/', '\\') + "\\AI\\result.ai";
- // if (File.Exists(aiFile))
- // {
- // using (System.IO.StreamReader _file = System.IO.File.OpenText(aiFile))
- // {
- // using (JsonTextReader reader = new JsonTextReader(_file))
- // {
- // JObject o = (JObject)JToken.ReadFrom(reader);
- // jo.Add("REPORT_RESULT", o["result"].ToString());
- // string[] results = o["result"].ToString().Split(';');
- // jo.Add("REPORT_IMAGE", results.Length.ToString());
- // JArray jarr = new JArray();
- // for (int i = 0; i < results.Length; i++)
- // {
- // jo.Add("REPORT_RESULT" + i.ToString(), results[i]);
- // jarr = JArray.Parse(o[results[i]].ToString());
- // for (int j = 0; j < jarr.Count; j++)
- // {
- // jarr[j] = o["path"].ToString().Replace('/', '\\') + "\\" + jarr[j];
- // }
- // jo.Add("REPORT_PIC" + i.ToString(), jarr);
- // }
- // }
- // }
- // }
- // return wordReport.GenerateWordReport(wordPath, jo.ToString());
- // }
- // catch
- // {
- // throw (new Exception("参数不是标准的JSON数据!"));
- // }
- //}
- }
- #endregion
- #region 结束评级后处理文件
- /// <summary>
- /// 结束评级后处理文件
- /// </summary>
- /// <param name="aiFile">AI结果文件</param>
- /// <returns>结果文件内容</returns>
- private String AnalysisAIFile()
- {
- String ret = "";
- try
- {
- String aiFile = Path.Combine(this._gradePath, "AI") + "\\" + resultName;
- if (File.Exists(aiFile))
- {
- using (System.IO.StreamReader _file = System.IO.File.OpenText(aiFile))
- {
- using (JsonTextReader reader = new JsonTextReader(_file))
- {
- try
- {
- Dictionary<String, List<String>> gradeA = new Dictionary<string, List<string>>();
- Dictionary<String, List<String>> gradeAe = new Dictionary<string, List<string>>();
- Dictionary<String, List<String>> gradeAs = new Dictionary<string, List<string>>();
- Dictionary<String, List<String>> gradeB = new Dictionary<string, List<string>>();
- Dictionary<String, List<String>> gradeBe = new Dictionary<string, List<string>>();
- Dictionary<String, List<String>> gradeBs = new Dictionary<string, List<string>>();
- Dictionary<String, List<String>> gradeC = new Dictionary<string, List<string>>();
- Dictionary<String, List<String>> gradeCe = new Dictionary<string, List<string>>();
- Dictionary<String, List<String>> gradeCs = new Dictionary<string, List<string>>();
- Dictionary<String, List<String>> gradeD = new Dictionary<string, List<string>>();
- Dictionary<String, List<String>> gradeDe = new Dictionary<string, List<string>>();
- Dictionary<String, List<String>> gradeDs = new Dictionary<string, List<string>>();
- JObject o = (JObject)JToken.ReadFrom(reader);
- JObject arrList = JObject.Parse(o["file_results"].ToString());
- foreach (var item in arrList)
- {
- string temp = item.Value.ToString();
- string[] v = temp.Split('+');
- for (int j = 0; j < v.Length; j++)
- {
- //包含A的
- if (v[j].IndexOf("A") > -1)
- {
- if (v[j].IndexOf("e") < 0 && v[j].IndexOf("s") < 0)
- {
- if (gradeA.ContainsKey(v[j]))
- {
- gradeA[v[j]].Add(item.Key);
- }
- else
- {
- gradeA.Add(v[j], new List<string>());
- gradeA[v[j]].Add(item.Key);
- }
- }
- else if (v[j].IndexOf("e") > -1 && v[j].IndexOf("s") < 0)
- {
- if (gradeAe.ContainsKey(v[j]))
- {
- gradeAe[v[j]].Add(item.Key);
- }
- else
- {
- gradeAe.Add(v[j], new List<string>());
- gradeAe[v[j]].Add(item.Key);
- }
- }
- else if (v[j].IndexOf("s") > -1)
- {
- if (gradeAs.ContainsKey(v[j]))
- {
- gradeAs[v[j]].Add(item.Key);
- }
- else
- {
- gradeAs.Add(v[j], new List<string>());
- gradeAs[v[j]].Add(item.Key);
- }
- }
- }
- //包含B的
- if (v[j].IndexOf("B") > -1)
- {
- if (v[j].IndexOf("e") < 0 && v[j].IndexOf("s") < 0)
- {
- if (gradeB.ContainsKey(v[j]))
- {
- gradeB[v[j]].Add(item.Key);
- }
- else
- {
- gradeB.Add(v[j], new List<string>());
- gradeB[v[j]].Add(item.Key);
- }
- }
- else if (v[j].IndexOf("e") > -1 && v[j].IndexOf("s") < 0)
- {
- if (gradeBe.ContainsKey(v[j]))
- {
- gradeBe[v[j]].Add(item.Key);
- }
- else
- {
- gradeBe.Add(v[j], new List<string>());
- gradeBe[v[j]].Add(item.Key);
- }
- }
- else if (v[j].IndexOf("s") > -1)
- {
- if (gradeBs.ContainsKey(v[j]))
- {
- gradeBs[v[j]].Add(item.Key);
- }
- else
- {
- gradeBs.Add(v[j], new List<string>());
- gradeBs[v[j]].Add(item.Key);
- }
- }
- }
- //包含C的
- if (v[j].IndexOf("C") > -1)
- {
- if (v[j].IndexOf("e") < 0 && v[j].IndexOf("s") < 0)
- {
- if (gradeC.ContainsKey(v[j]))
- {
- gradeC[v[j]].Add(item.Key);
- }
- else
- {
- gradeC.Add(v[j], new List<string>());
- gradeC[v[j]].Add(item.Key);
- }
- }
- else if (v[j].IndexOf("e") > -1 && v[j].IndexOf("s") < 0)
- {
- if (gradeCe.ContainsKey(v[j]))
- {
- gradeCe[v[j]].Add(item.Key);
- }
- else
- {
- gradeCe.Add(v[j], new List<string>());
- gradeCe[v[j]].Add(item.Key);
- }
- }
- else if (v[j].IndexOf("s") > -1)
- {
- if (gradeCs.ContainsKey(v[j]))
- {
- gradeCs[v[j]].Add(item.Key);
- }
- else
- {
- gradeCs.Add(v[j], new List<string>());
- gradeCs[v[j]].Add(item.Key);
- }
- }
- }
- //包含D的
- if (v[j].IndexOf("D") > -1)
- {
- if (v[j].IndexOf("e") < 0 && v[j].IndexOf("S") < 0)
- {
- if (gradeD.ContainsKey(v[j]))
- {
- gradeD[v[j]].Add(item.Key);
- }
- else
- {
- gradeD.Add(v[j], new List<string>());
- gradeD[v[j]].Add(item.Key);
- }
- }
- else if (v[j].IndexOf("e") > -1 && v[j].IndexOf("S") < 0)
- {
- if (gradeDe.ContainsKey(v[j]))
- {
- gradeDe[v[j]].Add(item.Key);
- }
- else
- {
- gradeDe.Add(v[j], new List<string>());
- gradeDe[v[j]].Add(item.Key);
- }
- }
- else if (v[j].IndexOf("S") > -1)
- {
- if (gradeDs.ContainsKey(v[j]))
- {
- gradeDs[v[j]].Add(item.Key);
- }
- else
- {
- gradeDs.Add(v[j], new List<string>());
- gradeDs[v[j]].Add(item.Key);
- }
- }
- }
- }
- }
- //结果JSON
- JObject res = new JObject();
- res.Add("path", o["path"]);
- //结果显示
- String result = "";
- //排序,S的都输出,所以无需排序(DS除外)
- if (gradeA.Count > 0)
- {
- string maxStr = "";
- foreach (string key in gradeA.Keys)
- {
- if (maxStr == "")
- {
- maxStr = key;
- }
- else
- {
- if (string.Compare(maxStr, key, true) < 0)
- {
- maxStr = key;
- }
- }
- }
-
- JArray jarr = new JArray();
- for (int i = 0; i < gradeA[maxStr].Count; i++)
- {
- jarr.Add(gradeA[maxStr][i]);
- }
- if (res.ContainsKey(maxStr))
- {
- }
- else
- {
- result += maxStr + ";";
- res.Add(maxStr, jarr);
- }
- }
- if (gradeAe.Count > 0)
- {
- string maxStr = "";
- foreach (string key in gradeAe.Keys)
- {
- if (maxStr == "")
- {
- maxStr = key;
- }
- else
- {
- if (string.Compare(maxStr, key, true) < 0)
- {
- maxStr = key;
- }
- }
- }
-
- JArray jarr = new JArray();
- for (int i = 0; i < gradeAe[maxStr].Count; i++)
- {
- jarr.Add(gradeAe[maxStr][i]);
- }
- if (res.ContainsKey(maxStr))
- {
- }
- else
- {
- result += maxStr + ";";
- res.Add(maxStr, jarr);
- }
- }
- if (gradeB.Count > 0)
- {
- string maxStr = "";
- foreach (string key in gradeB.Keys)
- {
- if (maxStr == "")
- {
- maxStr = key;
- }
- else
- {
- if (string.Compare(maxStr, key, true) < 0)
- {
- maxStr = key;
- }
- }
- }
-
- JArray jarr = new JArray();
- for (int i = 0; i < gradeB[maxStr].Count; i++)
- {
- jarr.Add(gradeB[maxStr][i]);
- }
- if (res.ContainsKey(maxStr))
- {
- }
- else
- {
- result += maxStr + ";";
- res.Add(maxStr, jarr);
- }
- }
- if (gradeBe.Count > 0)
- {
- string maxStr = "";
- foreach (string key in gradeBe.Keys)
- {
- if (maxStr == "")
- {
- maxStr = key;
- }
- else
- {
- if (string.Compare(maxStr, key, true) < 0)
- {
- maxStr = key;
- }
- }
- }
-
- JArray jarr = new JArray();
- for (int i = 0; i < gradeBe[maxStr].Count; i++)
- {
- jarr.Add(gradeBe[maxStr][i]);
- }
- if (res.ContainsKey(maxStr))
- {
- }
- else
- {
- result += maxStr + ";";
- res.Add(maxStr, jarr);
- }
- }
- if (gradeC.Count > 0)
- {
- string maxStr = "";
- foreach (string key in gradeC.Keys)
- {
- if (maxStr == "")
- {
- maxStr = key;
- }
- else
- {
- if (string.Compare(maxStr, key, true) < 0)
- {
- maxStr = key;
- }
- }
- }
-
- JArray jarr = new JArray();
- for (int i = 0; i < gradeC[maxStr].Count; i++)
- {
- jarr.Add(gradeC[maxStr][i]);
- }
- if (res.ContainsKey(maxStr))
- {
- }
- else
- {
- result += maxStr + ";";
- res.Add(maxStr, jarr);
- }
- }
- if (gradeCe.Count > 0)
- {
- string maxStr = "";
- foreach (string key in gradeCe.Keys)
- {
- if (maxStr == "")
- {
- maxStr = key;
- }
- else
- {
- if (string.Compare(maxStr, key, true) < 0)
- {
- maxStr = key;
- }
- }
- }
-
- JArray jarr = new JArray();
- for (int i = 0; i < gradeCe[maxStr].Count; i++)
- {
- jarr.Add(gradeCe[maxStr][i]);
- }
- if (res.ContainsKey(maxStr))
- {
- }
- else
- {
- result += maxStr + ";";
- res.Add(maxStr, jarr);
- }
- }
- if (gradeD.Count > 0)
- {
- string maxStr = "";
- foreach (string key in gradeD.Keys)
- {
- if (maxStr == "")
- {
- maxStr = key;
- }
- else
- {
- if (string.Compare(maxStr, key, true) < 0)
- {
- maxStr = key;
- }
- }
- }
-
- JArray jarr = new JArray();
- for (int i = 0; i < gradeD[maxStr].Count; i++)
- {
- jarr.Add(gradeD[maxStr][i]);
- }
- if (res.ContainsKey(maxStr))
- {
- }
- else
- {
- result += maxStr + ";";
- res.Add(maxStr, jarr);
- }
- }
- if (gradeDe.Count > 0)
- {
- string maxStr = "";
- foreach (string key in gradeDe.Keys)
- {
- if (maxStr == "")
- {
- maxStr = key;
- }
- else
- {
- if (string.Compare(maxStr, key, true) < 0)
- {
- maxStr = key;
- }
- }
- }
-
- JArray jarr = new JArray();
- for (int i = 0; i < gradeDe[maxStr].Count; i++)
- {
- jarr.Add(gradeDe[maxStr][i]);
- }
- if (res.ContainsKey(maxStr))
- {
- }
- else
- {
- result += maxStr + ";";
- res.Add(maxStr, jarr);
- }
- }
- if (gradeDs.Count > 0)
- {
- string maxStr = "";
- foreach (string key in gradeDs.Keys)
- {
- if (maxStr == "")
- {
- maxStr = key;
- }
- else
- {
- if (string.Compare(maxStr, key, true) < 0)
- {
- maxStr = key;
- }
- }
- }
-
- JArray jarr = new JArray();
- for (int i = 0; i < gradeDs[maxStr].Count; i++)
- {
- jarr.Add(gradeDs[maxStr][i]);
- }
- if (res.ContainsKey(maxStr))
- {
- }
- else
- {
- result += maxStr + ";";
- res.Add(maxStr, jarr);
- }
- }
- if (gradeAs.Count > 0)
- {
- foreach (string key in gradeAs.Keys)
- {
- result += key + ";";
- JArray jarr = new JArray();
- for (int i = 0; i < gradeAs[key].Count; i++)
- {
- jarr.Add(gradeAs[key][i]);
- }
- if (res.ContainsKey(key))
- {
- continue;
- }
- else
- {
- res.Add(key, jarr);
- }
- }
- }
- if (gradeBs.Count > 0)
- {
- foreach (string key in gradeBs.Keys)
- {
- result += key + ";";
- JArray jarr = new JArray();
- for (int i = 0; i < gradeBs[key].Count; i++)
- {
- jarr.Add(gradeBs[key][i]);
- }
- if (res.ContainsKey(key))
- {
- continue;
- }
- else
- {
- res.Add(key, jarr);
- }
- }
- }
- if (gradeCs.Count > 0)
- {
- foreach (string key in gradeCs.Keys)
- {
- result += key + ";";
- JArray jarr = new JArray();
- for (int i = 0; i < gradeCs[key].Count; i++)
- {
- jarr.Add(gradeCs[key][i]);
- }
- if (res.ContainsKey(key))
- {
- continue;
- }
- else
- {
- res.Add(key, jarr);
- }
- }
- }
- if (result.Length == 0)
- {
- ret = "not result!";
- return ret;
- }
- res.Add("result", result.Substring(0, result.Length - 1));
- ret = res.ToString();
- using (System.IO.StreamWriter file = new System.IO.StreamWriter(Path.Combine(this._gradePath, "AI") + "\\result.ai", false, Encoding.Default))
- {
- file.WriteLine(ret);// 直接追加文件末尾,换行
- file.Close();
- }
- }
- catch (Exception e)
- {
- throw (new Exception("AI文件错误!"));
- }
- reader.Close();
- _file.Close();
- _file.Dispose();
- }
- }
- }
- }
- catch { }
- return ret;
- }
- #endregion
- }
- }
|