protected override FileUploadConfigElement GetFileUploadConfigElement() { string srcRootPath = PropertyService.Get(PROPERTY_FIELDS.EL_PICTURE_SOURCE_ROOT_PATH).Trim(); string srcPathFormat = PropertyService.Get(PROPERTY_FIELDS.EL_PICTURE_SOURCE_PATH_FORMAT).Trim(); string destRootPath = PropertyService.Get(PROPERTY_FIELDS.EL_PICTURE_DEST_ROOT_PATH).Trim(); string destPathFormat = PropertyService.Get(PROPERTY_FIELDS.EL_PICTURE_DEST_PATH_FORMAT).Trim(); string fileExistension = PropertyService.Get(PROPERTY_FIELDS.EL_PICTURE_EXISTENSION).Trim(); string userName = PropertyService.Get(PROPERTY_FIELDS.EL_PICTURE_USER_NAME).Trim(); string userPassword = PropertyService.Get(PROPERTY_FIELDS.EL_PICTURE_USER_PASSWORD).Trim(); string isDeleteLocalFile = PropertyService.Get(PROPERTY_FIELDS.EL_PICTURE_IS_DELETE_LOCAL_FILE); string isSemimanufactures = PropertyService.Get(PROPERTY_FIELDS.EL_PICTURE_IS_SEMIMANUFACTURES); FileUploadConfigElement cfg = new FileUploadConfigElement(srcRootPath, destRootPath, fileExistension); cfg.UserName = userName; cfg.UserPassword = userPassword; if (!string.IsNullOrEmpty(srcPathFormat)) { cfg.SourcePathFormat = Path.Combine(srcRootPath, srcPathFormat); } if (!string.IsNullOrEmpty(destPathFormat)) { cfg.DestPathFormat = Path.Combine(destRootPath, destPathFormat); } if (!string.IsNullOrEmpty(isDeleteLocalFile)) { cfg.IsDeleteLocalFile = Convert.ToBoolean(isDeleteLocalFile); } if (!string.IsNullOrEmpty(isSemimanufactures)) { cfg.IsSemimanufactures = Convert.ToBoolean(isSemimanufactures); } return(cfg); }
/// <summary> /// 启动线程。 /// </summary> public void Start(FileUploadConfigElement cfg) { LoggingService.Info(string.Format("FileUpload--Start {0}", DateTime.Now)); this.Loop = true; this.Config = cfg; this.Thread = new Thread(this._threadStart); this.Thread.Start(this); }
/// <summary> /// 执行命令。 /// </summary> public override void Run() { FileUploadConfigElement cfg = GetFileUploadConfigElement(); if (!string.IsNullOrEmpty(cfg.SourceRootPath)) { if (ThreadWrapper.Loop) { ThreadWrapper.Stop(); } ThreadWrapper.Start(cfg); } else { if (_wrapper != null) { _wrapper.Stop(); _wrapper.Dispose(); _wrapper = null; } } }
/// <summary> /// 获取源文件路径。 /// </summary> /// <param name="cfg">文件上传配置元素。</param> /// <returns>源文件路径。</returns> private string GetSourcePath(FileUploadConfigElement cfg, DateTime d) { string path = string.Format(cfg.SourcePathFormat, d); return(path); }
/// <summary> /// 文件上传 /// </summary> private void UploadFile(FileUploadThreadWrapper wrapper) { while (wrapper.Loop) { FileUploadConfigElement cfg = wrapper.Config; FileUpload fileUpload = new FileUpload(cfg.UserName, cfg.UserPassword); int count = 0; try { DateTime dtStartTime = DateTime.Now; DateTime nowDateTime = DateTime.Now; string msg = string.Empty; string sourceFilePath = string.Empty; string destFilePath = string.Empty; //获取当前时间对应的 小时、分钟、秒 int hour = nowDateTime.Hour; int minute = nowDateTime.Minute; int second = nowDateTime.Second; //根据抓去时间初始化源文件夹目录和目标地址;如果满足条件的捕抓 前一天的图片,如果不满足获取当天图片上传 if (hour % 5 == 0 && minute % 15 == 0 && (30 < second) && cfg.IsSemimanufactures) { sourceFilePath = GetSourcePath(cfg, nowDateTime.AddDays(-1)); destFilePath = GetDestPath(cfg, nowDateTime.AddDays(-1)); } else { sourceFilePath = GetSourcePath(cfg, nowDateTime); destFilePath = GetDestPath(cfg, nowDateTime); } //源文件夹存在。 if (Directory.Exists(sourceFilePath)) { DateTime lastTime = new DateTime(nowDateTime.Year, nowDateTime.Month, nowDateTime.Day, 23, 50, 00); DateTime startTime = new DateTime(nowDateTime.Year, nowDateTime.Month, nowDateTime.Day, 00, 10, 00); string[] existensions = cfg.FileExistension.Split(new char[] { ',', ';' }); List <string> fileNames = new List <string>(); foreach (string existension in existensions) { fileNames.AddRange(Directory.GetFiles(sourceFilePath, existension)); } if (fileNames.Count > 0) { fileUpload.MakeDir(cfg.DestRootPath, destFilePath); } foreach (string fileName in fileNames) { FileInfo fileInfo = new FileInfo(fileName); TimeSpan ts = nowDateTime - fileInfo.LastWriteTime; if (fileInfo.Name.IndexOf("_") >= 0) { LoggingService.Warn(string.Format("组件序号[{0}]错误!", fileInfo.Name)); File.Delete(fileName); } else { if (ts.TotalSeconds >= 30 && File.Exists(fileName)) { if (nowDateTime >= lastTime || nowDateTime <= startTime) { string sn = Path.GetFileNameWithoutExtension(fileInfo.Name); //获取IV测试日期 IVTestDataEntity entity = new IVTestDataEntity(); DataSet dsIVTestData = entity.GetIVTestDateInfo(sn, "1"); if (!string.IsNullOrEmpty(entity.ErrorMsg)) { LoggingService.Error(entity.ErrorMsg); } else if (dsIVTestData.Tables[0].Rows.Count > 0) { DateTime dTestDate = Convert.ToDateTime(dsIVTestData.Tables[0].Rows[0]["T_DATE"]); //生成目标文件夹。 destFilePath = GetDestPath(cfg, dTestDate); fileUpload.MakeDir(cfg.DestRootPath, destFilePath); } } string reFileName = fileUpload.ExistFile(fileName, destFilePath); if (!string.IsNullOrEmpty(reFileName)) { fileUpload.ReFileName(fileName, destFilePath, reFileName); } if (fileUpload.UploadFile(fileName, destFilePath) == true) { count++; //获取对应文件的数据文件 string dataFileName = fileName.Replace(".GIF", ".daq").Replace(".JPEG", ".daq"); //直接删除本地文件。 if (cfg.IsDeleteLocalFile) { File.Delete(fileName); //判断属否存在图片对应的数据文件若存进行删除 if (File.Exists(dataFileName)) { File.Delete(dataFileName); } } else { //移动本地文件到当前目录下的LocalFiles文件夹下。 string savePath = Path.Combine(sourceFilePath, "LocalFiles"); if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } string destFileName = Path.Combine(savePath, fileInfo.Name); if (File.Exists(destFileName)) { File.Delete(destFileName); } File.Move(fileName, destFileName); if (File.Exists(dataFileName)) { FileInfo dataFileInfo = new FileInfo(dataFileName); string destDataFileName = Path.Combine(savePath, dataFileInfo.Name); if (File.Exists(destDataFileName)) { File.Delete(destDataFileName); } File.Move(dataFileName, destDataFileName); } } } else { //获取对应文件的数据文件 string dataFileName = fileName.Replace(".GIF", "black.daq").Replace(".JPEG", "black.daq"); //移动本地文件到当前目录下的LocalFiles文件夹下。 string savePath = Path.Combine(sourceFilePath, "LocalFiles"); if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } string destFileName = Path.Combine(savePath, fileInfo.Name); if (File.Exists(destFileName)) { File.Delete(destFileName); } File.Move(fileName, destFileName); if (File.Exists(dataFileName)) { FileInfo dataFileInfo = new FileInfo(dataFileName); string destDataFileName = Path.Combine(savePath, dataFileInfo.Name); if (File.Exists(destDataFileName)) { File.Delete(destDataFileName); } File.Move(dataFileName, destDataFileName); } MessageBox.Show(string.Format("组件序号[{0}]对应图片为黑片,请重拍!", fileInfo.Name.Replace(".GIF", "").Replace(".JPEG", "")), "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } } } DateTime dtEndTime = DateTime.Now; if (count > 0) { msg = string.Format("FileUpload--开始时间:{0};结束时间:{1};耗用时间:{2}秒;上传文件数量:{3}。{4}", dtStartTime, dtEndTime, (dtEndTime - dtStartTime).TotalSeconds, count, destFilePath); LoggingService.Info(msg); } } else { msg = string.Format("FileUpload--开始时间:{0};源文件夹({1})不存在。", dtStartTime, sourceFilePath); LoggingService.Info(msg); } } catch (Exception ex) { LoggingService.Error(string.Format("FileUpload--Error:{0}", ex.Message)); } if (wrapper.Loop) { Thread.Sleep(cfg.Millisecond); } } wrapper.AutoResetEvent.Set(); }