| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- 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.Net;
- using System.Net.Sockets;
- using System.Text.RegularExpressions;
- using System.IO.Ports;
- using static System.Windows.Forms.VisualStyles.VisualStyleElement;
- using System.Windows.Media.Media3D;
- using System.Runtime.InteropServices;
- namespace FileWatcher
- {
- public partial class AutoMakeQTY : Form
- {
- DataHelper dh;
- DataTable dt;
- DataTable DB;
- /// <summary>
- /// 用户编号
- /// </summary>
- string iusercode;
- /// <summary>
- /// 岗位资源
- /// </summary>
- string isource;
- 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>();
- string master = "HUAG";
- ftpOperater ftp = new ftpOperater();
- public AutoMakeQTY()
- {
- InitializeComponent();
- StartPosition = FormStartPosition.CenterScreen;
- }
- string IPAddress = "";
- private void Form1_Load(object sender, EventArgs e)
- {
- CheckForIllegalCrossThreadCalls = false;
- FormBorderStyle = FormBorderStyle.FixedSingle;
- InitDB = new Thread(ConnectDB);
- //添加监控事件
- XmlWatcher.Changed += new FileSystemEventHandler(XmlWatcher_Created);
- //ATEFile.Changed += new FileSystemEventHandler(XmlWatcher_Created);
- SetLoadingWindow stw = new SetLoadingWindow(InitDB, "正在启动程序");
- stw.StartPosition = FormStartPosition.CenterScreen;
- stw.ShowDialog();
- List<string> CacheInf = new List<string>();
- IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName());
- for (int i = 0; i < IpEntry.AddressList.Length; i++)
- {
- if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
- IPAddress = IpEntry.AddressList[i].ToString();
- }
- //获取缓存信息
- try
- {
- Type.Text = BaseUtil.GetCacheData("Type").ToString();
- ComPort.Text = BaseUtil.GetCacheData("ComPort").ToString();
- BaudRate.Text = BaseUtil.GetCacheData("BaudRate").ToString();
- }
- 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();
- }
- StartWatch.PerformClick();
- }
- private void ConnectDB()
- {
- dh = new DataHelper();
- }
- SerialPort serialPort1 = new SerialPort();
- bool GetData = true;
- private void getSerialData()
- {
- if (serialPort1.IsOpen)
- {
- try
- {
- while (GetData)
- {
- try
- {
- int len = serialPort1.BytesToRead;
- Byte[] readBuffer = new Byte[len];
- serialPort1.Read(readBuffer, 0, len); //将数据读入缓存
- string weigh = Encoding.Default.GetString(readBuffer);
- if (weigh != "")
- {
- OperateResult.AppendText(weigh + "\n");
- }
- }
- catch (Exception)
- {
- GetData = false;
- }
- }
- }
- catch (IOException ex) { MessageBox.Show(ex.Message); }
- }
- else OperateResult.AppendText(">>端口已被占用,请关闭其他窗口\n");
- }
- Thread thread;
- private void StartWatch_Click(object sender, EventArgs e)
- {
- BaseUtil.SetCacheData("Type", Type.Text);
- BaseUtil.SetCacheData("ComPort", ComPort.Text);
- BaseUtil.SetCacheData("BaudRate", BaudRate.Text);
- Timer.Interval = 1000 * 60;
- Timer.Start();
- //设置按钮不可点击
- StartWatch.Enabled = false;
- ma_code.Enabled = false;
- StopWatch.Enabled = true;
- thread = new Thread(getSerialData);
- try
- {
- serialPort1.PortName = BaudRate.Text;
- serialPort1.BaudRate = int.Parse(BaudRate.Text);
- serialPort1.Open();
- GetData = true;
- thread.Start();
- }
- catch (Exception mes)
- {
- if (BaudRate.Text == "" || BaudRate.Text == "")
- OperateResult.AppendText(">>请先维护波特率和串口\n");
- else
- OperateResult.AppendText(">>" + mes.Message + "\n");
- }
- OperateResult.AppendText("开始执行监控\n");
- }
- private void XmlWatcher_Created(object sender, FileSystemEventArgs e)
- {
- OperateResult.AppendText("文件修改:" + e.FullPath + "\n");
- TxtHandleProcess(e.FullPath);
- }
- string nextLine;
- private void TxtHandleProcess(string FileName)
- {
- List<string> badcode = new List<string>();
- List<string> badlocation = new List<string>();
- List<string> badprod = new List<string>();
- }
- private void StopWatch_Click(object sender, EventArgs e)
- {
- XmlWatcher.EnableRaisingEvents = false;
- StartWatch.Enabled = true;
- ma_code.Enabled = true;
- StopWatch.Enabled = false;
- OperateResult.AppendText("停止执行监控\n");
- }
- private void Clean_Click(object sender, EventArgs e)
- {
- OperateResult.Clear();
- }
- 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("FileWatcher.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("FileWatcher.exe", false);
- rk2.Close();
- rk.Close();
- }
- }
- /// <summary>
- /// 请求上传图片到阿里云
- /// </summary>
- /// <param name="url">上传地址</param>
- /// <param name="filepath">本地文件路径</param>
- /// <param name="dic">上传的数据信息</param>
- /// <returns></returns>
- public string UploadFilesToRemoteUrl(string url1, string filepath, Dictionary<string, object> dic, out string fp_path, out string fp_id)
- {
- fp_id = "";
- fp_path = "";
- try
- {
- ServicePointManager.DefaultConnectionLimit = 50;
- string boundary = DateTime.Now.Ticks.ToString("x");
- byte[] boundarybytes = System.Text.Encoding.UTF8.GetBytes("--" + boundary + "\r\n");
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url1);
- request.Method = "POST";
- request.Timeout = 10 * 10000;
- request.ContentType = "multipart/form-data; boundary=" + boundary;
- Stream rs = request.GetRequestStream();
- var endBoundaryBytes = Encoding.UTF8.GetBytes("--" + boundary + "--\r\n");
- string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n" + "\r\n" + "{1}" + "\r\n";
- if (dic != null)
- {
- foreach (string key in dic.Keys)
- {
- rs.Write(boundarybytes, 0, boundarybytes.Length);
- string formitem = string.Format(formdataTemplate, key, dic[key]);
- byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
- rs.Write(formitembytes, 0, formitembytes.Length);
- }
- }
- string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n\r\n";
- {
- rs.Write(boundarybytes, 0, boundarybytes.Length);
- var header = string.Format(headerTemplate, "file", Path.GetFileName(filepath));
- var headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
- rs.Write(headerbytes, 0, headerbytes.Length);
- using (var fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read))
- {
- var buffer = new byte[1024];
- var bytesRead = 0;
- while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
- {
- rs.Write(buffer, 0, bytesRead);
- }
- }
- var cr = Encoding.UTF8.GetBytes("\r\n");
- rs.Write(cr, 0, cr.Length);
- }
- rs.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
- var response = request.GetResponse() as HttpWebResponse;
- StreamReader newReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
- string Content = newReader.ReadToEnd();
- Dictionary<string, object> dic1 = new Dictionary<string, object>();
- List<Dictionary<string, object>> dic2 = null;
- dic1 = BaseUtil.ToDictionary(Content);
- dic2 = dic1["data"] as List<Dictionary<string, object>>;
- if (dic2[0]["filepath"] != null)
- {
- fp_id = dic2[0]["filepath"].ToString();
- fp_path = dic2[0]["path"].ToString();
- }
- if (response.StatusCode == HttpStatusCode.OK)
- {
- Console.WriteLine(fp_id);
- return fp_id;
- }
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message + e.StackTrace);
- }
- return "";
- }
- }
- }
|