Form1.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.IO;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Windows.Forms;
  9. using System.Xml;
  10. using UAS_AutoPass.ToolClass;
  11. namespace UAS_XmlAnalysor
  12. {
  13. public partial class Form1 : Form
  14. {
  15. DataHelper dh;
  16. DataTable dt;
  17. Tip tipform;
  18. /// <summary>
  19. /// 用户编号
  20. /// </summary>
  21. string iusercode;
  22. /// <summary>
  23. /// 岗位资源
  24. /// </summary>
  25. string isource;
  26. /// <summary>
  27. /// 线别
  28. /// </summary>
  29. string ilinecode;
  30. /// <summary>
  31. /// 不良代码组
  32. /// </summary>
  33. string ibadgroup;
  34. /// <summary>
  35. /// 当前工序
  36. /// </summary>
  37. string istepcode;
  38. StringBuilder sql = new StringBuilder();
  39. DataTable DB;
  40. Thread InitDB;
  41. /// <summary>
  42. /// 缓存的文件
  43. /// </summary>
  44. public static string CachePath = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/UAS_MES/XmlAnalysor/Cache.xml";
  45. /// <summary>
  46. /// 缓存的文件夹
  47. /// </summary>
  48. public static string CachePathFolder = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/UAS_MES/XmlAnalysor/";
  49. private string oErrMessage;
  50. public Form1()
  51. {
  52. InitializeComponent();
  53. StartPosition = FormStartPosition.CenterScreen;
  54. tipform = new Tip();
  55. }
  56. public Form1(string iUserName, string iSource)
  57. {
  58. tipform = new Tip();
  59. InitializeComponent();
  60. iusercode = iUserName;
  61. isource = iSource.ToUpper();
  62. StartPosition = FormStartPosition.CenterScreen;
  63. }
  64. private void Form1_Load(object sender, EventArgs e)
  65. {
  66. tipform.Show();
  67. CheckForIllegalCrossThreadCalls = false;
  68. FormBorderStyle = FormBorderStyle.FixedSingle;
  69. InitDB = new Thread(ConnectDB);
  70. //添加监控事件
  71. XmlWatcher.Created += new FileSystemEventHandler(XmlWatcher_Created);
  72. SetLoadingWindow stw = new SetLoadingWindow(InitDB, "正在启动程序");
  73. stw.StartPosition = FormStartPosition.CenterScreen;
  74. stw.ShowDialog();
  75. List<string> CacheInf = new List<string>();
  76. DB = (DataTable)dh.ExecuteSql("select ms_pwd,ma_user,ma_address,ma_inneraddress from master", "select");
  77. Master.DataSource = DB;
  78. Master.DisplayMember = "ma_user";
  79. Master.ValueMember = "ma_user";
  80. //获取缓存信息
  81. try
  82. {
  83. FolderPath.Text = BaseUtil.GetCacheData("FolderPath").ToString();
  84. BackUpFolderPath.Text = BaseUtil.GetCacheData("BackUpFolderPath").ToString();
  85. Master.Text = BaseUtil.GetCacheData("Master").ToString();
  86. AutoStart.Checked = (bool)BaseUtil.GetCacheData("AutoStart");
  87. }
  88. catch (Exception ex) { Console.WriteLine(ex.Message); }
  89. for (int i = 0; i < DB.Rows.Count; i++)
  90. {
  91. if (Master.Text == DB.Rows[i]["ma_user"].ToString())
  92. {
  93. DataHelper.DBConnectionString = "Connection Timeout=0;Pooling=false;Password=select!#%*(;User ID=" + DB.Rows[i]["ma_user"].ToString() + ";Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=" + DB.Rows[i]["ma_inneraddress"].ToString() + ")(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
  94. dh = new DataHelper();
  95. }
  96. }
  97. //获取岗位资源相关信息
  98. dt = (DataTable)dh.ExecuteSql("select sc_linecode,sc_stepcode,st_badgroupcode from source left join step on sc_stepcode=st_code where sc_code='" + isource + "'", "select");
  99. if (dt.Rows.Count > 0)
  100. {
  101. ilinecode = dt.Rows[0]["sc_linecode"].ToString();
  102. istepcode = dt.Rows[0]["sc_stepcode"].ToString();
  103. ibadgroup = dt.Rows[0]["st_badgroupcode"].ToString();
  104. }
  105. }
  106. private void ConnectDB()
  107. {
  108. dh = new DataHelper();
  109. }
  110. private void StartWatch_Click(object sender, EventArgs e)
  111. {
  112. if (FolderPath.Text == "" || BackUpFolderPath.Text == "")
  113. {
  114. OperateResult.AppendText("请选择监控文件夹和备份文件夹\n");
  115. return;
  116. }
  117. else
  118. {
  119. if (!Directory.Exists(FolderPath.Text))
  120. {
  121. OperateResult.AppendText("监控文件夹不存在\n");
  122. return;
  123. }
  124. if (!Directory.Exists(BackUpFolderPath.Text))
  125. {
  126. OperateResult.AppendText("备份文件夹不存在\n");
  127. return;
  128. }
  129. }
  130. if (FolderPath.Text == BackUpFolderPath.Text)
  131. {
  132. OperateResult.AppendText("监控文件夹和备份文件夹不能相同\n");
  133. return;
  134. }
  135. for (int i = 0; i < DB.Rows.Count; i++)
  136. {
  137. if (Master.Text == DB.Rows[i]["ma_user"].ToString())
  138. {
  139. DataHelper.DBConnectionString = "Connection Timeout=0;Pooling=false;Password=select!#%*(;User ID=" + DB.Rows[i]["ma_user"].ToString() + ";Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=" + DB.Rows[i]["ma_inneraddress"].ToString() + ")(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
  140. dh = new DataHelper();
  141. }
  142. }
  143. if (!dh.CheckExist("source", "sc_code='" + isource + "' and sc_statuscode='AUDITED'"))
  144. {
  145. OperateResult.AppendText("岗位资源错误或者未审核\n");
  146. return;
  147. }
  148. XmlWatcher.Path = FolderPath.Text;
  149. XmlWatcher.Filter = "*.xml";
  150. XmlWatcher.EnableRaisingEvents = true;
  151. BaseUtil.SetCacheData("FolderPath", FolderPath.Text);
  152. BaseUtil.SetCacheData("BackUpFolderPath", BackUpFolderPath.Text);
  153. BaseUtil.SetCacheData("Source", isource);
  154. BaseUtil.SetCacheData("Master", Master.Text);
  155. BaseUtil.SetCacheData("AutoStart", AutoStart.Checked);
  156. StartWatch.Enabled = false;
  157. ChooseFolder.Enabled = false;
  158. ChooseBackUpFolder.Enabled = false;
  159. StopWatch.Enabled = true;
  160. OperateResult.AppendText("开始执行监控\n");
  161. }
  162. public void CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string name, string value)
  163. {
  164. XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, null);
  165. node.InnerText = value;
  166. parentNode.AppendChild(node);
  167. }
  168. public void SetAutoRun()
  169. {
  170. if (AutoStart.Checked) //设置开机自启动
  171. {
  172. string path = Application.ExecutablePath;
  173. RegistryKey rk = Registry.LocalMachine;
  174. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  175. rk2.SetValue("UAS_XML解析器.exe", path);
  176. rk2.Close();
  177. rk.Close();
  178. }
  179. else //取消开机自启动
  180. {
  181. string path = Application.ExecutablePath;
  182. RegistryKey rk = Registry.LocalMachine;
  183. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  184. rk2.DeleteValue("UAS_XML解析器.exe", false);
  185. rk2.Close();
  186. rk.Close();
  187. }
  188. }
  189. private void XmlWatcher_Created(object sender, FileSystemEventArgs e)
  190. {
  191. while (true)
  192. {
  193. try
  194. {
  195. using (Stream stream = File.Open(e.FullPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
  196. {
  197. if (stream != null)
  198. break;
  199. }
  200. }
  201. catch (Exception ex)
  202. {
  203. Console.WriteLine(ex.Message);
  204. }
  205. }
  206. string test_result = "";
  207. string oMSID = "";
  208. string oErrMessage = "";
  209. string testDate = "";
  210. string testTime = "";
  211. XmlReader myReader = XmlReader.Create(FolderPath.Text + @"\" + e.Name);
  212. //获取文件名的序列号,如SA123456.xml
  213. string sncode = e.Name.Split('.')[0];
  214. string makecode = "";
  215. //获取序列号ID最大的工单号
  216. string iMakeCode = dh.getFieldDataByCondition("makeserial", "ms_makecode", "ms_sncode='" + sncode + "' order by ms_id desc").ToString();
  217. OperateResult.AppendText("读取文件" + e.Name + "\n");
  218. //获取采集的项目名称
  219. List<string> name = new List<string>();
  220. //获取采集项目的结果
  221. List<string> result = new List<string>();
  222. int name_or_result = 0;
  223. while (myReader.Read())
  224. {
  225. if (myReader.NodeType == XmlNodeType.Element && myReader.Name == "test" && myReader.IsStartElement())
  226. {
  227. test_result = myReader.GetAttribute("test_result");
  228. testDate = myReader.GetAttribute("test_date");
  229. testTime = myReader.GetAttribute("test_time");
  230. }
  231. if (myReader.NodeType == XmlNodeType.Text)
  232. {
  233. if (name_or_result % 2 == 0)
  234. {
  235. name.Add(myReader.Value);
  236. name_or_result++;
  237. }
  238. else
  239. {
  240. result.Add(myReader.Value);
  241. name_or_result++;
  242. }
  243. }
  244. }
  245. string test_date = testDate + " " + testTime;
  246. string status = "";
  247. string errmessage = "";
  248. bool NoteAlready = false;
  249. LogicHandler.GetMakeInfo(sncode, out makecode, out status, out errmessage);
  250. if (ma_code.Text != makecode && makecode != "" && ma_code.Text != "" && status != "2")
  251. {
  252. string ChangeMakeCode = MessageBox.Show(this.ParentForm, "序列号" + sncode + "所属工单不同,是否切换?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  253. //如果选择不切换赋值当前界面工单
  254. if (ChangeMakeCode != "Yes")
  255. {
  256. makecode = ma_code.Text;
  257. }
  258. else
  259. {
  260. NoteAlready = true;
  261. }
  262. }
  263. if (LogicHandler.CheckStepSNAndMacode(iMakeCode, isource, sncode, iusercode, out iMakeCode, out oMSID, out oErrMessage))
  264. {
  265. if (!NoteAlready)
  266. {
  267. if (makecode != ma_code.Text && ma_code.Text != "")
  268. {
  269. string ChangeMakeCode = MessageBox.Show(this.ParentForm, "序列号所属工单不同,是否切换?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  270. //如果选择不切换赋值当前界面工单
  271. if (ChangeMakeCode == "Yes")
  272. {
  273. ma_code.Text = makecode;
  274. }
  275. else
  276. {
  277. OperateResult.AppendText(">>请重新采集序列号\n");
  278. tipform.startthread("请重新采集序列号", test_result);
  279. return;
  280. }
  281. }
  282. }
  283. //获取序列号ID最大的工单
  284. dt = (DataTable)dh.ExecuteSql("select ms_makecode,ms_id from makeserial where ms_sncode='" + sncode + "' order by ms_id desc", "select");
  285. if (dt.Rows.Count > 0)
  286. {
  287. ma_code.Text = dt.Rows[0]["ms_makecode"].ToString();
  288. makecode = dt.Rows[0]["ms_makecode"].ToString();
  289. switch (test_result)
  290. {
  291. case "OK":
  292. if (!LogicHandler.SetStepResult(makecode, isource, sncode, "自动过站采集", test_result, iusercode, out oErrMessage))
  293. {
  294. OperateResult.AppendText(oErrMessage + "\n");
  295. tipform.startthread(oErrMessage, test_result);
  296. return;
  297. }
  298. tipform.startthread("序列号 " + sncode + "通过检测", test_result);
  299. break;
  300. case "NG":
  301. //RecordBadInfo(sncode, makecode, e.Name, "", badcode.ToArray(), badlocation.ToArray());
  302. ////所有的序列号均采集为良品
  303. //if (!LogicHandler.SetTestNGDetail(sncode, makecode, iusercode, isource, test_result, out oErrMessage))
  304. //{
  305. // OperateResult.AppendText(oErrMessage + "\n");
  306. // tipform.startthread(oErrMessage, test_result);
  307. // return;
  308. //}
  309. tipform.startthread("序列号 " + sncode + "未通过检测", test_result);
  310. break;
  311. default:
  312. break;
  313. }
  314. LogicHandler.AutoPassLog(sncode, isource, makecode, test_date, istepcode, ilinecode, e.Name, test_result == "NG" ? "1" : "0", "0");
  315. }
  316. }
  317. else
  318. {
  319. OperateResult.AppendText(oErrMessage + "\n");
  320. tipform.startthread(oErrMessage, "NG");
  321. return;
  322. }
  323. string sql = "insert into STEPTESTDETAIL(std_id,std_makecode,std_sn,std_subclass1,std_testresult,std_indate,";
  324. sql += "std_rescode,std_testdate,std_testtime,std_date) values(STEPTESTDETAIL_seq.nextval, '" + iMakeCode + "', ";
  325. sql += "'" + sncode + "',:std_subclass1,:std_testresult, sysdate,'" + isource + "',to_char(to_date('" + testDate + "','YYYY/MM/DD'), 'YYYYMMDD'),";
  326. sql += "to_char(to_date('" + testTime + "','hh24:mi:ss'), 'hh24miss'),to_date('" + test_date + "','YYYY/MM/DD hh24:mi:ss'))";
  327. dh.BatchInsert(sql, new string[] { "std_subclass1", "std_testresult" }, name.ToArray(), result.ToArray());
  328. myReader.Close();
  329. FileInfo file = new FileInfo(FolderPath.Text + @"\" + e.Name);
  330. if (file.Exists)
  331. {
  332. try
  333. {
  334. for (int i = 1; i <= 20; i++)
  335. {
  336. if (!File.Exists(BackUpFolderPath.Text + @"\" + e.Name))
  337. {
  338. file.MoveTo(BackUpFolderPath.Text + @"\" + e.Name);
  339. OperateResult.AppendText("成功解析文件" + e.Name + "\n");
  340. //tipform.startthread(e.Name);
  341. break;
  342. }
  343. else if (!File.Exists(BackUpFolderPath.Text + @"\" + e.Name.Split('.')[0] + "(" + i + ")" + "." + e.Name.Split('.')[1]))
  344. {
  345. file.MoveTo(BackUpFolderPath.Text + @"\" + e.Name.Split('.')[0] + "(" + i + ")" + "." + e.Name.Split('.')[1]);
  346. OperateResult.AppendText("成功解析文件" + e.Name + "\n");
  347. //tipform.startthread(e.Name);
  348. break;
  349. }
  350. }
  351. }
  352. catch (Exception ex)
  353. {
  354. OperateResult.AppendText(e.Name + ex.Message + "\n");
  355. }
  356. }
  357. }
  358. private void StopWatch_Click(object sender, EventArgs e)
  359. {
  360. XmlWatcher.EnableRaisingEvents = false;
  361. StartWatch.Enabled = true;
  362. ChooseFolder.Enabled = true;
  363. ChooseBackUpFolder.Enabled = true;
  364. StopWatch.Enabled = false;
  365. OperateResult.AppendText("停止执行监控\n");
  366. }
  367. private void Clean_Click(object sender, EventArgs e)
  368. {
  369. OperateResult.Clear();
  370. }
  371. private void ChooseFolder_Click(object sender, EventArgs e)
  372. {
  373. FolderBrowserDialog folder = new FolderBrowserDialog();
  374. folder.Description = "选择监控文件夹";
  375. DialogResult result = folder.ShowDialog();
  376. if (result == DialogResult.OK)
  377. {
  378. FolderPath.Text = folder.SelectedPath;
  379. }
  380. }
  381. private void ReadNodeFromXML(string FileName)
  382. {
  383. }
  384. private void ChooseBackUpFolder_Click(object sender, EventArgs e)
  385. {
  386. FolderBrowserDialog folder = new FolderBrowserDialog();
  387. folder.Description = "选择备份文件夹";
  388. DialogResult result = folder.ShowDialog();
  389. if (result == DialogResult.OK)
  390. {
  391. BackUpFolderPath.Text = folder.SelectedPath;
  392. }
  393. }
  394. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  395. {
  396. string ExitConfirm = MessageBox.Show(this, "确认退出?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  397. if (ExitConfirm != "Yes")
  398. {
  399. WindowState = FormWindowState.Minimized;
  400. e.Cancel = true;
  401. }
  402. }
  403. private void AutoStart_CheckedChanged(object sender, EventArgs e)
  404. {
  405. SetAutoRun();
  406. }
  407. }
  408. }