using System; using System.Collections.Generic; using System.Linq; using System.Text; using QMAPP.FJC.Entity.FileCopy; namespace QMAPP.WinForm.FileSynchronizers { public class HaitianInjectionFileSynchronizer:FileSynchronizer { //获取服务代理 QMAPP.ServicesAgent.ServiceAgent _agent = ClientContext.GetServiceAgent(); bool skiphistory = false; bool firsttick = true; string _File_Copy_Path = "C:\\"; System.Threading.Timer timer; public HaitianInjectionFileSynchronizer(QMAPP.FJC.Entity.Basic.MachineInfo machien) : base(machien) { _File_Copy_Path = System.Configuration.ConfigurationManager.AppSettings["File_Copy_Path"]; if (!string.IsNullOrWhiteSpace(System.Configuration.ConfigurationManager.AppSettings["File_Copy_Path_" + this.Machine.MACHINECODDE])) { _File_Copy_Path = System.Configuration.ConfigurationManager.AppSettings["File_Copy_Path_" + this.Machine.MACHINECODDE]; } timer = new System.Threading.Timer(new System.Threading.TimerCallback(Tick)); } private void Tick(object obj) { FileCopyRecord fcr = null; try { //QMAPP.FJC.BLL.FileCopy.FileCopyRecordBLL fcrbll = new QMAPP.FJC.BLL.FileCopy.FileCopyRecordBLL(); //FileCopyRecord fcr = fcrbll.GetLastRecord(this.Machine.MACHINECODDE); fcr=_agent.InvokeServiceFunction("FileCopyRecordBLL_GetLastRecord",this.Machine.MACHINECODDE); if (fcr == null) { fcr = new FileCopyRecord { EQUIPMENT_INFO_KEY = this.Machine.PID, FILE_HOSTNAME = this.Machine.IPADDRESS, RECORD_EQUIPMENT_CODE = this.Machine.MACHINECODDE, RECORD_EQUIPMENT_DIRECTORY = this.Machine.EQUIPMENT_DIRECTORY, RECORD_EQUIPMENT_TYPE_CODE = this.Machine.EQUIPMENT_TYPE_CODE, RECORD_KEY = Guid.NewGuid().ToString(), RECORD_READFLAG = "0", FILE_LASTWRITETIME = DateTime.Parse("1975-1-1"), IsNewInfo = true }; } using (IdentityScope idscope = new IdentityScope(this.Machine.USERNAME, this.Machine.IPADDRESS, this.Machine.PASSWORD)) { IEnumerable files = Common.FileHelper.GetFileInfos(Machine.EQUIPMENT_DIRECTORY); var newfiles = (from file in files where file.LastWriteTime > fcr.FILE_LASTWRITETIME.AddSeconds(1) select file).ToList(); foreach (var file in newfiles) { string filecopy = string.Format("{0}\\{1}", _File_Copy_Path.TrimEnd('\\'), file.Name); file.CopyTo(filecopy,true); using (System.IO.StreamReader sreader = new System.IO.StreamReader(filecopy, System.Text.Encoding.UTF8)) { Console.WriteLine(string.Format("{3:HH:mm:ss}:{0}({1}) {2} ReadFile {4}", this.Machine.MACHINENAME, this.Machine.MACHINECODDE, this.GetType().Name, DateTime.Now, file.Name)); long read_pos = 0; if (file.Name.Equals((fcr.RECORD_FILENAME + ""), StringComparison.CurrentCultureIgnoreCase)) { read_pos = fcr.RECORD_FILE_LENGTH; } else { fcr.RECORD_KEYWORDS = ""; } sreader.BaseStream.Position = read_pos; while (!sreader.EndOfStream) { var line = sreader.ReadLine(); if (line.StartsWith("_.sv_Scrap [ ];")) { fcr.RECORD_KEYWORDS = line; } else { if (firsttick && skiphistory)////如果非跳过历史数据选项为true 并且 第一次进行文件扫描 { } else { if (!string.IsNullOrWhiteSpace(fcr.RECORD_KEYWORDS)) { Console.WriteLine(line); InjParamEventArgs args = new InjParamEventArgs(DateTime.Now, file.Name, fcr.RECORD_KEYWORDS, line,';'); OnNewRecordFound(args); } } } } fcr.RECORD_FILENAME = file.Name; fcr.FILE_LASTWRITETIME = file.LastWriteTime; fcr.RECORD_FILE_LENGTH = sreader.BaseStream.Length; fcr.RECORD_COPY_DATETIME = DateTime.Now; } System.IO.File.Delete(filecopy); } } if (fcr.IsNewInfo) { _agent.InvokeServiceFunction("FileCopyRecordBLL_Insert", fcr); } else { _agent.InvokeServiceFunction("FileCopyRecordBLL_Update", fcr); } firsttick = false; } catch(Exception ex) { Console.WriteLine(string.Format("{3:HH:mm:ss}:{0}({1}) {2} Exception ------------------------- \r\n {4} \r\n---------------------End", this.Machine.MACHINENAME, this.Machine.MACHINECODDE, this.GetType().Name, DateTime.Now, ex.Message)); WriteLog.WriteError(ex.Message); OnExceptionCatched(new InjParamEventArgs(DateTime.Now,ex)); } finally { if (timer != null) { timer.Change(60000, System.Threading.Timeout.Infinite); } } } public override void Start(bool skiphistory) { this.skiphistory = skiphistory; firsttick = true; if (timer != null) { timer.Change(60000, System.Threading.Timeout.Infinite); } base.Start(skiphistory); } public override void Stop() { if (timer != null) { timer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite); } base.Stop(); } public override void Dispose() { if (timer != null) { timer.Dispose(); timer = null; } base.Dispose(); } } }