AutoAnalysisDeviceKS.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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.Net;
  11. using System.Net.Sockets;
  12. using System.Text.RegularExpressions;
  13. using Oracle.ManagedDataAccess.Client;
  14. using NPOI.SS.UserModel;
  15. using NPOI.XSSF.UserModel;
  16. namespace FileWatcher
  17. {
  18. public partial class AutoAnalysisDeviceKS : Form
  19. {
  20. DataHelper dh;
  21. /// <summary>
  22. /// 用户编号
  23. /// </summary>
  24. string iusercode;
  25. /// <summary>
  26. /// 岗位资源
  27. /// </summary>
  28. string isource;
  29. Tip tipform;
  30. Thread InitDB;
  31. StringBuilder sql = new StringBuilder();
  32. /// <summary>
  33. /// 缓存的文件
  34. /// </summary>
  35. public static string CachePath = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/UAS_MES/XmlAnalysor/Cache.xml";
  36. /// <summary>
  37. /// 缓存的文件夹
  38. /// </summary>
  39. public static string CachePathFolder = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/UAS_MES/XmlAnalysor/";
  40. /// <summary>
  41. /// 需要解析的文件名
  42. /// </summary>
  43. List<string> _FileName = new List<string>();
  44. ftpOperater ftp = new ftpOperater();
  45. public AutoAnalysisDeviceKS()
  46. {
  47. tipform = new Tip();
  48. InitializeComponent();
  49. StartPosition = FormStartPosition.CenterScreen;
  50. }
  51. public AutoAnalysisDeviceKS(string iUserName, string iSource, string iMaster)
  52. {
  53. tipform = new Tip();
  54. InitializeComponent();
  55. iusercode = iUserName;
  56. isource = iSource.ToUpper();
  57. StartPosition = FormStartPosition.CenterScreen;
  58. }
  59. string IPAddress = "";
  60. private void Form1_Load(object sender, EventArgs e)
  61. {
  62. tipform.Show();
  63. CheckForIllegalCrossThreadCalls = false;
  64. FormBorderStyle = FormBorderStyle.FixedSingle;
  65. InitDB = new Thread(ConnectDB);
  66. //添加监控事件
  67. XmlWatcher.Changed += new FileSystemEventHandler(XmlWatcher_Created);
  68. //ATEFile.Changed += new FileSystemEventHandler(XmlWatcher_Created);
  69. SetLoadingWindow stw = new SetLoadingWindow(InitDB, "正在启动程序");
  70. stw.StartPosition = FormStartPosition.CenterScreen;
  71. stw.ShowDialog();
  72. List<string> CacheInf = new List<string>();
  73. IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName());
  74. for (int i = 0; i < IpEntry.AddressList.Length; i++)
  75. {
  76. if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
  77. IPAddress = IpEntry.AddressList[i].ToString();
  78. }
  79. //获取缓存信息
  80. try
  81. {
  82. FolderPath.Text = BaseUtil.GetCacheData("FolderPath").ToString();
  83. Device.Text = BaseUtil.GetCacheData("Device").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) { MessageBox.Show(ex.Message); }
  89. //获取岗位资源相关信息
  90. StartWatch.PerformClick();
  91. }
  92. private void ConnectDB()
  93. {
  94. DataHelper.DBConnectionString = "Connection Timeout=0;Pooling=false;Password=select!#%*(;User ID=N_MES;Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.3.7)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
  95. dh = new DataHelper();
  96. }
  97. private void StartWatch_Click(object sender, EventArgs e)
  98. {
  99. if (FolderPath.Text == "")
  100. {
  101. OperateResult.AppendText("请选择监控文件夹\n");
  102. return;
  103. }
  104. else
  105. {
  106. if (!Directory.Exists(FolderPath.Text))
  107. {
  108. OperateResult.AppendText("监控文件夹不存在\n");
  109. return;
  110. }
  111. }
  112. XmlWatcher.Path = FolderPath.Text;
  113. XmlWatcher.EnableRaisingEvents = true;
  114. XmlWatcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite;
  115. //设置缓存数据
  116. BaseUtil.SetCacheData("FolderPath", FolderPath.Text);
  117. BaseUtil.SetCacheData("Device", Device.Text);
  118. Timer.Interval = 1000 * 60;
  119. Timer.Start();
  120. //设置按钮不可点击
  121. StartWatch.Enabled = false;
  122. ChooseFolder.Enabled = false;
  123. StopWatch.Enabled = true;
  124. OperateResult.AppendText("开始执行监控\n");
  125. }
  126. private void XmlWatcher_Created(object sender, FileSystemEventArgs e)
  127. {
  128. OperateResult.AppendText("文件修改:" + e.FullPath + "\n");
  129. TxtHandleProcess(e.FullPath);
  130. }
  131. string nextLine;
  132. private void TxtHandleProcess(string FileName)
  133. {
  134. }
  135. private void StopWatch_Click(object sender, EventArgs e)
  136. {
  137. XmlWatcher.EnableRaisingEvents = false;
  138. StartWatch.Enabled = true;
  139. ChooseFolder.Enabled = true;
  140. StopWatch.Enabled = false;
  141. OperateResult.AppendText("停止执行监控\n");
  142. }
  143. private void Clean_Click(object sender, EventArgs e)
  144. {
  145. OperateResult.Clear();
  146. }
  147. private void ChooseFolder_Click(object sender, EventArgs e)
  148. {
  149. FolderBrowserDialog folder = new FolderBrowserDialog();
  150. folder.Description = "选择监控文件夹";
  151. DialogResult result = folder.ShowDialog();
  152. if (result == DialogResult.OK)
  153. {
  154. FolderPath.Text = folder.SelectedPath;
  155. }
  156. }
  157. private void ChooseBackUpFolder_Click(object sender, EventArgs e)
  158. {
  159. FolderBrowserDialog folder = new FolderBrowserDialog();
  160. folder.Description = "选择备份文件夹";
  161. DialogResult result = folder.ShowDialog();
  162. if (result == DialogResult.OK)
  163. {
  164. }
  165. }
  166. private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  167. {
  168. string ExitConfirm = MessageBox.Show(this, "确认退出?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
  169. if (ExitConfirm != "Yes")
  170. {
  171. WindowState = FormWindowState.Minimized;
  172. e.Cancel = true;
  173. }
  174. }
  175. private void AutoStart_CheckedChanged(object sender, EventArgs e)
  176. {
  177. SetAutoRun();
  178. }
  179. private void SetAutoRun()
  180. {
  181. if (AutoStart.Checked) //设置开机自启动
  182. {
  183. string path = Application.ExecutablePath;
  184. RegistryKey rk = Registry.LocalMachine;
  185. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  186. rk2.SetValue("FileWatcher.exe", path);
  187. rk2.Close();
  188. rk.Close();
  189. }
  190. else //取消开机自启动
  191. {
  192. string path = Application.ExecutablePath;
  193. RegistryKey rk = Registry.LocalMachine;
  194. RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
  195. rk2.DeleteValue("FileWatcher.exe", false);
  196. rk2.Close();
  197. rk.Close();
  198. }
  199. }
  200. private void Timer_Tick(object sender, EventArgs e)
  201. {
  202. string filePath = FolderPath.Text+ @"\LotLog.csv";
  203. string connectionString = "Connection Timeout=0;Pooling=false;Password=select!#%*(;User ID=N_MES;Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.3.7)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
  204. // 读取Excel文件
  205. var data = ReadExcelFile(filePath);
  206. // 插入Oracle数据库
  207. InsertDataIntoOracle(data, connectionString);
  208. }
  209. static List<Dictionary<string, string>> ReadExcelFile(string filePath)
  210. {
  211. var data = new List<Dictionary<string, string>>();
  212. using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
  213. {
  214. XSSFWorkbook workbook = new XSSFWorkbook(file);
  215. ISheet sheet = workbook.GetSheetAt(0); // 获取第一个工作表
  216. IRow headerRow = sheet.GetRow(0); // 获取表头行
  217. int cellCount = headerRow.LastCellNum;
  218. for (int i = 1; i <= sheet.LastRowNum; i++) // 从第二行开始读取数据
  219. {
  220. IRow row = sheet.GetRow(i);
  221. if (row == null) continue;
  222. var rowData = new Dictionary<string, string>();
  223. for (int j = 0; j < cellCount; j++)
  224. {
  225. ICell cell = row.GetCell(j);
  226. string cellValue = cell?.ToString() ?? string.Empty;
  227. string header = headerRow.GetCell(j).ToString();
  228. rowData[header] = cellValue;
  229. }
  230. data.Add(rowData);
  231. }
  232. }
  233. return data;
  234. }
  235. static void InsertDataIntoOracle(List<Dictionary<string, string>> data, string connectionString)
  236. {
  237. using (OracleConnection connection = new OracleConnection(connectionString))
  238. {
  239. connection.Open();
  240. foreach (var row in data)
  241. {
  242. string query = @"
  243. INSERT INTO your_table_name (psl_program, psl_begintime, psl_endtime, psl_stop, psl_repair, psl_boards, psl_waitupper, psl_waitlower, psl_move, psl_points, psl_consumes, psl_plat)
  244. VALUES (:psl_program, :psl_begintime, :psl_endtime, :psl_stop, :psl_repair, :psl_boards, :psl_waitupper, :psl_waitlower, :psl_move, :psl_points, :psl_consumes, :psl_plat)";
  245. using (OracleCommand command = new OracleCommand(query, connection))
  246. {
  247. command.Parameters.Add("psl_program", OracleDbType.Varchar2).Value = row["基板名"];
  248. command.Parameters.Add("psl_begintime", OracleDbType.Date).Value = DateTime.Parse(row["生产开始时间"]);
  249. command.Parameters.Add("psl_endtime", OracleDbType.Date).Value = DateTime.Parse(row["生产完成时间"]);
  250. command.Parameters.Add("psl_stop", OracleDbType.Int32).Value = int.Parse(row["出错停止时间(秒)"]);
  251. command.Parameters.Add("psl_repair", OracleDbType.Int32).Value = int.Parse(row["出错修复时间(秒)"]);
  252. command.Parameters.Add("psl_boards", OracleDbType.Int32).Value = int.Parse(row["完成贴装的拼板数"]);
  253. command.Parameters.Add("psl_waitupper", OracleDbType.Int32).Value = int.Parse(row["因上游待机的时间(秒)"]);
  254. command.Parameters.Add("psl_waitlower", OracleDbType.Int32).Value = int.Parse(row["因下游待机的时间(秒)"]);
  255. command.Parameters.Add("psl_move", OracleDbType.Int32).Value = int.Parse(row["可动率(%)"]);
  256. command.Parameters.Add("psl_points", OracleDbType.Int32).Value = int.Parse(row["贴装点数"]);
  257. command.Parameters.Add("psl_consumes", OracleDbType.Int32).Value = int.Parse(row["元件消耗数"]);
  258. command.Parameters.Add("psl_plat", OracleDbType.Varchar2).Value = row["输送台"];
  259. command.ExecuteNonQuery();
  260. }
  261. }
  262. }
  263. }
  264. }
  265. }