AutoAnalysisXmlByStep.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.IO;
  6. using System.Threading;
  7. using System.Windows.Forms;
  8. using System.Xml;
  9. using System.Text;
  10. using System.Media;
  11. using System.Linq;
  12. namespace FileWatcher
  13. {
  14. public partial class AutoAnalysisXmlByStep : Form
  15. {
  16. public static DataHelper dh;
  17. DataTable dt;
  18. DataTable DB;
  19. /// <summary>
  20. /// 用户编号
  21. /// </summary>
  22. string iusercode;
  23. /// <summary>
  24. /// 岗位资源
  25. /// </summary>
  26. string isource;
  27. Tip tipform;
  28. Thread InitDB;
  29. /// <summary>
  30. /// 线别
  31. /// </summary>
  32. string ilinecode;
  33. /// <summary>
  34. /// 不良代码组
  35. /// </summary>
  36. string ibadgroup;
  37. /// <summary>
  38. /// 当前工序
  39. /// </summary>
  40. string istepcode;
  41. StringBuilder sql = new StringBuilder();
  42. /// <summary>
  43. /// 缓存的文件
  44. /// </summary>
  45. public static string CachePath = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/UAS_MES/XmlAnalysor/Cache.xml";
  46. /// <summary>
  47. /// 缓存的文件夹
  48. /// </summary>
  49. public static string CachePathFolder = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/UAS_MES/XmlAnalysor/";
  50. /// <summary>
  51. /// 需要解析的文件名
  52. /// </summary>
  53. List<string> _FileName = new List<string>();
  54. ftpOperater ftp;
  55. string imaster;
  56. public AutoAnalysisXmlByStep()
  57. {
  58. tipform = new Tip();
  59. InitializeComponent();
  60. StartPosition = FormStartPosition.CenterScreen;
  61. }
  62. public AutoAnalysisXmlByStep(string iUserName, string iSource, string iMaster)
  63. {
  64. tipform = new Tip();
  65. InitializeComponent();
  66. iusercode = iUserName;
  67. isource = iSource.ToUpper();
  68. imaster = iMaster;
  69. StartPosition = FormStartPosition.CenterScreen;
  70. }
  71. string fieltype = "";
  72. string ftpulr = "";
  73. private void Form1_Load(object sender, EventArgs e)
  74. {
  75. tipform.Show();
  76. CheckForIllegalCrossThreadCalls = false;
  77. FormBorderStyle = FormBorderStyle.FixedSingle;
  78. InitDB = new Thread(ConnectDB);
  79. //添加监控事件
  80. XmlWatcher.Created += new FileSystemEventHandler(XmlWatcher_Created);
  81. SetLoadingWindow stw = new SetLoadingWindow(InitDB, "正在启动程序");
  82. stw.StartPosition = FormStartPosition.CenterScreen;
  83. stw.ShowDialog();
  84. List<string> CacheInf = new List<string>();
  85. ftpulr = dh.getFieldDataByCondition("configs", "data", "code='UploadFTP' and caller='MESSetting'").ToString();
  86. fieltype = dh.getFieldDataByCondition("configs", "data", "code='UploadFile' and caller='MESSetting'").ToString();
  87. string[] FTPInf = ftpulr.Split('#');
  88. ftp = new ftpOperater(FTPInf[0], FTPInf[1], FTPInf[2]);
  89. //获取缓存信息
  90. try
  91. {
  92. //FolderPath.Text = BaseUtil.GetCacheData("FolderPath").ToString();
  93. //BackUpFolderPath.Text = BaseUtil.GetCacheData("BackUpFolderPath").ToString();
  94. //Master.Text = BaseUtil.GetCacheData("Master").ToString();
  95. //AutoStart.Checked = (bool)BaseUtil.GetCacheData("AutoStart");
  96. }
  97. catch (Exception ex) { MessageBox.Show(ex.Message); }
  98. }
  99. private void ConnectDB()
  100. {
  101. dh = new DataHelper();
  102. }
  103. private void StartWatch_Click(object sender, EventArgs e)
  104. {
  105. if (FolderPath.Text == "")
  106. {
  107. OperateResult.AppendText("请选择监控文件夹\n");
  108. return;
  109. }
  110. else
  111. {
  112. if (!Directory.Exists(FolderPath.Text))
  113. {
  114. OperateResult.AppendText("监控文件夹不存在\n");
  115. return;
  116. }
  117. }
  118. XmlWatcher.Path = FolderPath.Text;
  119. XmlWatcher.Filter = "*.*";
  120. XmlWatcher.EnableRaisingEvents = true;
  121. //设置缓存数据
  122. //BaseUtil.SetCacheData("FolderPath", FolderPath.Text);
  123. //BaseUtil.SetCacheData("BackUpFolderPath", BackUpFolderPath.Text);
  124. //BaseUtil.SetCacheData("Master", Master.Text);
  125. //BaseUtil.SetCacheData("AutoStart", AutoStart.Checked);
  126. Timer.Interval = 1000 * 10;
  127. Timer.Start();
  128. //设置按钮不可点击
  129. StartWatch.Enabled = false;
  130. ChooseFolder.Enabled = false;
  131. Master.Enabled = false;
  132. StopWatch.Enabled = true;
  133. OperateResult.AppendText("开始执行监控\n");
  134. }
  135. public void CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string name, string value)
  136. {
  137. XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, null);
  138. node.InnerText = value;
  139. parentNode.AppendChild(node);
  140. }
  141. private void XmlWatcher_Created(object sender, FileSystemEventArgs e)
  142. {
  143. string[] str = fieltype.Split('#');
  144. for (int i = 0; i < str.Length; i++)
  145. {
  146. if (e.FullPath.EndsWith(str[i]))
  147. {
  148. TxtHandleProcess(e.FullPath);
  149. }
  150. }
  151. }
  152. private void TxtHandleProcess(string FileName)
  153. {
  154. List<string> badcode = new List<string>();
  155. List<string> badlocation = new List<string>();
  156. List<string> badprod = new List<string>();
  157. try
  158. {
  159. string filename = FileName;
  160. string folderpath = FileName.Substring(0, FileName.LastIndexOf(@"\"));
  161. string SN = FileName.Substring(FileName.LastIndexOf(@"\") + 1);
  162. {
  163. try
  164. {
  165. string ftppath = "/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
  166. ftp.UpLoadFile(folderpath, SN, ftppath, "");
  167. int num = int.Parse(dh.ExecuteSql("insert into STEPTESTDETAIL (std_id,std_sn,std_makecode,std_indate,std_class,STD_RESCODE,std_stepcode)select STEPTESTDETAIL_seq.nextval,'" + SN + "','',sysdate,'" + ftpulr.Split('#')[0] + imaster + "/" + ftppath + SN + ".txt" + "','" + isource + "','' from dual", "insert").ToString());
  168. if (num > 0)
  169. {
  170. OperateResult.AppendText("序列号:" + SN + "上传成功\n");
  171. //File.Delete(FileName);
  172. }
  173. }
  174. catch (Exception ex)
  175. {
  176. Console.WriteLine(ex.Message);
  177. }
  178. }
  179. }
  180. catch (Exception e)
  181. {
  182. OperateResult.AppendText("解析失败:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\n" + FileName + "\n" + e.Message + "\n");
  183. }
  184. }
  185. private void StopWatch_Click(object sender, EventArgs e)
  186. {
  187. XmlWatcher.EnableRaisingEvents = false;
  188. StartWatch.Enabled = true;
  189. Master.Enabled = true;
  190. ChooseFolder.Enabled = true;
  191. StopWatch.Enabled = false;
  192. OperateResult.AppendText("停止执行监控\n");
  193. }
  194. private void Clean_Click(object sender, EventArgs e)
  195. {
  196. OperateResult.Clear();
  197. }
  198. private void ChooseFolder_Click(object sender, EventArgs e)
  199. {
  200. FolderBrowserDialog folder = new FolderBrowserDialog();
  201. folder.Description = "选择监控文件夹";
  202. DialogResult result = folder.ShowDialog();
  203. if (result == DialogResult.OK)
  204. {
  205. FolderPath.Text = folder.SelectedPath;
  206. }
  207. }
  208. private void ChooseBackUpFolder_Click(object sender, EventArgs e)
  209. {
  210. FolderBrowserDialog folder = new FolderBrowserDialog();
  211. folder.Description = "选择备份文件夹";
  212. DialogResult result = folder.ShowDialog();
  213. if (result == DialogResult.OK)
  214. {
  215. }
  216. }
  217. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  218. {
  219. string ExitConfirm = MessageBox.Show(this, "确认退出?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  220. if (ExitConfirm != "Yes")
  221. {
  222. WindowState = FormWindowState.Minimized;
  223. e.Cancel = true;
  224. }
  225. }
  226. private void AutoStart_CheckedChanged(object sender, EventArgs e)
  227. {
  228. SetAutoRun();
  229. }
  230. private void SetAutoRun()
  231. {
  232. if (AutoStart.Checked) //设置开机自启动
  233. {
  234. string path = Application.ExecutablePath;
  235. RegistryKey rk = Registry.LocalMachine;
  236. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  237. rk2.SetValue("UAS自动过站解析器.exe", path);
  238. rk2.Close();
  239. rk.Close();
  240. }
  241. else //取消开机自启动
  242. {
  243. string path = Application.ExecutablePath;
  244. RegistryKey rk = Registry.LocalMachine;
  245. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  246. rk2.DeleteValue("UAS自动过站解析器.exe", false);
  247. rk2.Close();
  248. rk.Close();
  249. }
  250. }
  251. private void BatchAnalysus_Click(object sender, EventArgs e)
  252. {
  253. //var AllXmls = Directory.GetFiles(FolderPath.Text, "*.txt");
  254. //foreach (var Xml in AllXmls)
  255. //{
  256. // TxtHandleProcess(Xml);
  257. //}
  258. }
  259. private void Timer_Tick(object sender, EventArgs e)
  260. {
  261. //var AllXmls = Directory.GetFiles(FolderPath.Text, "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".txt") || s.EndsWith(".ini"));
  262. //foreach (var Xml in AllXmls)
  263. //{
  264. // TxtHandleProcess(Xml);
  265. //}
  266. }
  267. }
  268. }