| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- using Microsoft.Win32;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.IO;
- using System.Threading;
- using System.Windows.Forms;
- using System.Xml;
- using System.Text;
- using System.Media;
- using System.Linq;
- namespace FileWatcher
- {
- public partial class AutoAnalysisXmlByStep : Form
- {
- public static DataHelper dh;
- DataTable dt;
- DataTable DB;
- /// <summary>
- /// 用户编号
- /// </summary>
- string iusercode;
- /// <summary>
- /// 岗位资源
- /// </summary>
- string isource;
- Tip tipform;
- Thread InitDB;
- /// <summary>
- /// 线别
- /// </summary>
- string ilinecode;
- /// <summary>
- /// 不良代码组
- /// </summary>
- string ibadgroup;
- /// <summary>
- /// 当前工序
- /// </summary>
- string istepcode;
- StringBuilder sql = new StringBuilder();
- /// <summary>
- /// 缓存的文件
- /// </summary>
- public static string CachePath = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/UAS_MES/XmlAnalysor/Cache.xml";
- /// <summary>
- /// 缓存的文件夹
- /// </summary>
- public static string CachePathFolder = Environment.GetEnvironmentVariable("windir").Substring(0, 1) + @":/UAS_MES/XmlAnalysor/";
- /// <summary>
- /// 需要解析的文件名
- /// </summary>
- List<string> _FileName = new List<string>();
- ftpOperater ftp = new ftpOperater();
- string imaster;
- public AutoAnalysisXmlByStep()
- {
- tipform = new Tip();
- InitializeComponent();
- StartPosition = FormStartPosition.CenterScreen;
- }
- public AutoAnalysisXmlByStep(string iUserName, string iSource, string iMaster)
- {
- tipform = new Tip();
- InitializeComponent();
- iusercode = iUserName;
- isource = iSource.ToUpper();
- imaster = iMaster;
- StartPosition = FormStartPosition.CenterScreen;
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- tipform.Show();
- CheckForIllegalCrossThreadCalls = false;
- FormBorderStyle = FormBorderStyle.FixedSingle;
- InitDB = new Thread(ConnectDB);
- //添加监控事件
- XmlWatcher.Created += new FileSystemEventHandler(XmlWatcher_Created);
- SetLoadingWindow stw = new SetLoadingWindow(InitDB, "正在启动程序");
- stw.StartPosition = FormStartPosition.CenterScreen;
- stw.ShowDialog();
- List<string> CacheInf = new List<string>();
- //获取缓存信息
- try
- {
- //FolderPath.Text = BaseUtil.GetCacheData("FolderPath").ToString();
- //BackUpFolderPath.Text = BaseUtil.GetCacheData("BackUpFolderPath").ToString();
- //Master.Text = BaseUtil.GetCacheData("Master").ToString();
- //AutoStart.Checked = (bool)BaseUtil.GetCacheData("AutoStart");
- }
- catch (Exception ex) { MessageBox.Show(ex.Message); }
- //获取岗位资源相关信息
- DataTable 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");
- if (dt.Rows.Count > 0)
- {
- ilinecode = dt.Rows[0]["sc_linecode"].ToString();
- istepcode = dt.Rows[0]["sc_stepcode"].ToString();
- ibadgroup = dt.Rows[0]["st_badgroupcode"].ToString();
- }
- dt = (DataTable)dh.ExecuteSql("select st_name,st_code from step", "select");
- Device.DataSource = dt;
- Device.DisplayMember = "st_name";
- Device.ValueMember = "st_code";
- }
- private void ConnectDB()
- {
- dh = new DataHelper();
- }
- private void StartWatch_Click(object sender, EventArgs e)
- {
- if (FolderPath.Text == "")
- {
- OperateResult.AppendText("请选择监控文件夹\n");
- return;
- }
- else
- {
- if (!Directory.Exists(FolderPath.Text))
- {
- OperateResult.AppendText("监控文件夹不存在\n");
- return;
- }
- }
- //存在工单必须是已下放状态
- if ((!dh.CheckExist("make", "ma_code='" + ma_code.Text + "'") || ma_code.Text == ""))
- {
- OperateResult.AppendText("工单不存在或者未下放\n");
- return;
- }
- XmlWatcher.Path = FolderPath.Text;
- XmlWatcher.Filter = "*.txt";
- XmlWatcher.EnableRaisingEvents = true;
- //设置缓存数据
- //BaseUtil.SetCacheData("FolderPath", FolderPath.Text);
- //BaseUtil.SetCacheData("BackUpFolderPath", BackUpFolderPath.Text);
- //BaseUtil.SetCacheData("Master", Master.Text);
- //BaseUtil.SetCacheData("AutoStart", AutoStart.Checked);
- Timer.Interval = 1000 * 10;
- Timer.Start();
- //设置按钮不可点击
- StartWatch.Enabled = false;
- ChooseFolder.Enabled = false;
- Master.Enabled = false;
- ma_code.Enabled = false;
- StopWatch.Enabled = true;
- OperateResult.AppendText("开始执行监控\n");
- }
- public void CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string name, string value)
- {
- XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, null);
- node.InnerText = value;
- parentNode.AppendChild(node);
- }
- private void XmlWatcher_Created(object sender, FileSystemEventArgs e)
- {
- }
- private void TxtHandleProcess(string FileName)
- {
- List<string> badcode = new List<string>();
- List<string> badlocation = new List<string>();
- List<string> badprod = new List<string>();
- try
- {
- string filename = FileName;
- string folderpath = FileName.Substring(0, FileName.LastIndexOf(@"\"));
- string SN = FileName.Substring(FileName.LastIndexOf(@"\") + 1);
- {
- string makecode = ma_code.Text;
- DataTable 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");
- if (dt.Rows.Count > 0)
- {
- ilinecode = dt.Rows[0]["sc_linecode"].ToString();
- istepcode = dt.Rows[0]["sc_stepcode"].ToString();
- ibadgroup = dt.Rows[0]["st_badgroupcode"].ToString();
- }
- ma_code.Text = makecode;
- try
- {
- string ftppath = "/" + imaster + "/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
- ftp.UpLoadFile(folderpath, SN, ftppath, "");
- 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 + "','" + ma_code + "',sysdate,'http://81.71.42.91:8099/ftp/" + imaster + "/" + ftppath + SN + ".txt" + "','" + isource + "','" + Device.SelectedValue + "' from dual", "insert").ToString());
- if (num > 0)
- {
- OperateResult.AppendText("序列号:" + SN + "上传成功\n");
- File.Delete(FileName);
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- }
- }
- catch (Exception e)
- {
- OperateResult.AppendText("解析失败:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\n" + FileName + "\n" + e.Message + "\n");
- }
- }
- private void StopWatch_Click(object sender, EventArgs e)
- {
- XmlWatcher.EnableRaisingEvents = false;
- StartWatch.Enabled = true;
- ma_code.Enabled = true;
- Master.Enabled = true;
- ChooseFolder.Enabled = true;
- StopWatch.Enabled = false;
- OperateResult.AppendText("停止执行监控\n");
- }
- private void Clean_Click(object sender, EventArgs e)
- {
- OperateResult.Clear();
- }
- private void ChooseFolder_Click(object sender, EventArgs e)
- {
- FolderBrowserDialog folder = new FolderBrowserDialog();
- folder.Description = "选择监控文件夹";
- DialogResult result = folder.ShowDialog();
- if (result == DialogResult.OK)
- {
- FolderPath.Text = folder.SelectedPath;
- }
- }
- private void ChooseBackUpFolder_Click(object sender, EventArgs e)
- {
- FolderBrowserDialog folder = new FolderBrowserDialog();
- folder.Description = "选择备份文件夹";
- DialogResult result = folder.ShowDialog();
- if (result == DialogResult.OK)
- {
- }
- }
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- string ExitConfirm = MessageBox.Show(this, "确认退出?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
- if (ExitConfirm != "Yes")
- {
- WindowState = FormWindowState.Minimized;
- e.Cancel = true;
- }
- }
- private void AutoStart_CheckedChanged(object sender, EventArgs e)
- {
- SetAutoRun();
- }
- private void SetAutoRun()
- {
- if (AutoStart.Checked) //设置开机自启动
- {
- string path = Application.ExecutablePath;
- RegistryKey rk = Registry.LocalMachine;
- RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
- rk2.SetValue("UAS自动过站解析器.exe", path);
- rk2.Close();
- rk.Close();
- }
- else //取消开机自启动
- {
- string path = Application.ExecutablePath;
- RegistryKey rk = Registry.LocalMachine;
- RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
- rk2.DeleteValue("UAS自动过站解析器.exe", false);
- rk2.Close();
- rk.Close();
- }
- }
- private void BatchAnalysus_Click(object sender, EventArgs e)
- {
- var AllXmls = Directory.GetFiles(FolderPath.Text, "*.txt");
- foreach (var Xml in AllXmls)
- {
- TxtHandleProcess(Xml);
- }
- }
- private void Timer_Tick(object sender, EventArgs e)
- {
- var AllXmls = Directory.GetFiles(FolderPath.Text, "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".txt") || s.EndsWith(".ini"));
- foreach (var Xml in AllXmls)
- {
- TxtHandleProcess(Xml);
- }
- }
- }
- }
|