Beispiel #1
0
        /// <summary>
        /// 文件上传。
        /// </summary>
        private void UploadFile(object obj)
        {
            FileUploadThreadWrapper wrapper = obj as FileUploadThreadWrapper;

            if (wrapper == null)
            {
                return;
            }
            UploadFile(wrapper);
        }
Beispiel #2
0
        /// <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;
                }
            }
        }
Beispiel #3
0
        /// <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();
        }