private void DeleteFtpFiles(FtpConfig ftpConfig)
        {
            Nlog.Info(ftpConfig.FileType, $"{ftpConfig.FileType}\t开始删除...");

            var ftp = Containers.Resolve <IFtp>(
                new NamedParameter("nLogName", ftpConfig.FileType),
                new NamedParameter("ftpServerIp", ftpConfig.Ip),
                new NamedParameter("ftpProt", ftpConfig.Port),
                new NamedParameter("privateKeyFile", Path.Combine(Environment.CurrentDirectory, @"SFTP_KEY\tmp.cap")),
                new NamedParameter("ftpRemotePath", ftpConfig.RemoteFolder),
                new NamedParameter("ftpUserId", ftpConfig.Uid),
                new NamedParameter("ftpPassword", ftpConfig.Pwd),
                new NamedParameter("isdirectory", ftpConfig.IsDirectory));

            ftp.InitializeFileListStyle();
            //Thread.Sleep(5000);

            var list1 = ftp.FindFiles().Where(x => ftpConfig.NameContains.Length <= 0 || x.Name.ToLower().Contains(ftpConfig.NameContains.ToLower()));

            Thread.Sleep(3000);
            var list2 = ftp.FindFiles();

            var list = list1.Join(list2, x => new { x.Name, x.Size }, y => new { y.Name, y.Size },
                                  (x, y) => new FileStruct {
                IsDirectory = x.IsDirectory, Name = x.Name, CreateTime = x.CreateTime, FullName = x.FullName, Size = x.Size, IsSuccess = false
            })
                       .Where(x => x.IsDirectory == ftpConfig.IsDirectory).ToList();

            foreach (var file in list)
            {
                ftp.Delete(file.FullName);
            }
        }
Esempio n. 2
0
 public bool Download(string sourcePath, string targetPath)
 {
     if (sourcePath.Length == 0)
     {
         Nlog.Info(NLogName, $"{Path.GetFileName(sourcePath) } is null"); return(false);
     }
     if (targetPath.Length == 0)
     {
         Nlog.Info(NLogName, $"{Path.GetFileName(targetPath) } is null"); return(false);
     }
     if (!File.Exists(sourcePath))
     {
         Nlog.Info(NLogName, $"未在磁盘上发现文件:{sourcePath}"); return(false);
     }
     try
     {
         File.Copy(sourcePath, targetPath, true);
     }
     catch (Exception ex)
     {
         Nlog.Info(NLogName, $"{Path.GetFileName(targetPath) } {ex.Message}");
         return(false);
     }
     Nlog.Info(NLogName, $"{Path.GetFileName(targetPath) }\tdown √");
     return(true);
 }
Esempio n. 3
0
 /// <summary>
 /// 上传文件
 /// </summary>
 /// <param name="loaclPath">本地文件路径</param>
 public bool Upload(string loaclPath)
 {
     try
     {
         FileInfo fileInf = new FileInfo(loaclPath);
         var      request = OpenRequest(new Uri(_ftpUri + fileInf.Name), WebRequestMethods.Ftp.UploadFile);
         request.ContentLength = fileInf.Length;
         int    buffLength = 2048;
         byte[] buff       = new byte[buffLength];
         using (var fs = fileInf.OpenRead())
             using (var strm = request.GetRequestStream())
             {
                 var contentLen = fs.Read(buff, 0, buffLength);
                 while (contentLen != 0)
                 {
                     strm.Write(buff, 0, contentLen);
                     contentLen = fs.Read(buff, 0, buffLength);
                 }
             }
         request.Abort();
         Nlog.Info(_nlogName, $"{fileInf.Name} \t upload √");
         return(true);
     }
     catch (Exception ex)
     {
         Nlog.Info(_nlogName, $"{Path.GetFileName(loaclPath)} \t upload failed {ex.Message}");
         return(false);
     }
 }
Esempio n. 4
0
        /// <summary>
        /// 删除目录(包括下面所有子目录和子文件)
        /// </summary>
        /// <param name="remoteDirectory">要删除的带路径目录名</param>
        /// <returns></returns>
        private bool DeleteDirectory(string remoteDirectory)
        {
            bool result = true;
            List <FileStruct> allList = FindAllFilesAndDirectories(remoteDirectory).FileList;

            allList.Add(new FileStruct {
                FullName = remoteDirectory, IsDirectory = true
            });
            foreach (var f in allList.Where(x => !x.IsDirectory))
            {
                DeleteFile(f.FullName);
            }
            foreach (var f in allList.Where(x => x.IsDirectory).OrderByDescending(x => x.FullName.Count(c => c == '/')))
            {
                try
                {
                    using (OpenResponse(new Uri($"{_ftpUri}/{f.FullName}"), WebRequestMethods.Ftp.RemoveDirectory, out FtpWebRequest request))
                    {
                        request.Abort();
                        Nlog.Info(_nlogName, $"{f.FullName} \t delete √");
                    }
                }
                catch (Exception ex)
                {
                    result = false;
                    Nlog.Info(_nlogName, $"{f.FullName} \t delete failed {ex.Message}");
                }
            }
            return(result);
        }
Esempio n. 5
0
 /// <summary>
 /// 下载文件
 /// </summary>
 /// <param name="remotePath">下载后的保存路径</param>
 /// <param name="localPath">要下载的文件名</param>
 private bool DownloadFile(string remotePath, string localPath)
 {
     //var path = Path.GetDirectoryName(loaclPath);
     //if (path?.Length > 0 && !Directory.Exists(path)) Directory.CreateDirectory(path);
     byte[] buffer = new byte[2048];
     try
     {
         using (var response = OpenResponse(new Uri($"{_ftpUri}/{remotePath}"), WebRequestMethods.Ftp.DownloadFile, out FtpWebRequest request))
         {
             using (Stream reader = response.GetResponseStream())
                 using (FileStream fileStream = new FileStream(localPath, FileMode.Create))
                     while (true)
                     {
                         int bytesRead = reader.Read(buffer, 0, buffer.Length);
                         if (bytesRead == 0)
                         {
                             break;
                         }
                         fileStream.Write(buffer, 0, bytesRead);
                     }
             request.Abort();
         }
         var result = File.Exists(localPath) && new FileInfo(localPath).Length > 0;
         Nlog.Info(_nlogName, $"{remotePath} \t down {(result ? "√" : "failed")}");
         return(result);
     }
     catch (Exception ex)
     {
         Nlog.Info(_nlogName, $"{remotePath} \t down failed {ex.Message}");
         return(false);
     }
 }
Esempio n. 6
0
        private (bool code, List <string> FileList) FindFilesAndDirectories(string uri, string webRequestMethods)
        {
            List <string> listFiles = new List <string>();

            try
            {
                using (var response = OpenResponse(new Uri(uri), webRequestMethods, out FtpWebRequest request))
                {
                    using (var stream = response.GetResponseStream())
                        using (var sr = new StreamReader(stream, Encoding.Default))
                        {
                            string line;
                            while ((line = sr.ReadLine()) != null)
                            {
                                listFiles.Add(line);
                            }
                        }
                    request.Abort();
                }
                return(true, listFiles);
            }
            catch (Exception ex)
            {
                Nlog.Info(_nlogName, $"获取文件信息失败,{uri}.{ex.Message}");
            }
            return(false, listFiles);
        }
Esempio n. 7
0
        public bool GetResult(int imimKey, string json, DownloadConfig config, bool status)
        {
            if (!status)
            {
                var e = new SPEH_CLIM_CLM_IMIM_STTS_UPDATE_OCR
                {
                    ConnectionString  = ConnectionStringConfig <SPEH_CLIM_CLM_IMIM_STTS_UPDATE_OCR> .GetConnectionString(config.ConnectionStringDictionary),
                    pIMIM_STTS_UPDATE = new List <IMIM_STTS_UPDATE>
                    {
                        new IMIM_STTS_UPDATE {
                            IMIM_KY = imimKey
                        }
                    }

                    .ToDataTable(),
                    pSTS = ((int)ResultJsonStatus.Failed).ToString()
                };
                _commonBl.Execute(e);
                return(e.ReturnValue == 1);
            }
            var item = json.FromJson <ImageDefineJson>();

            //item.Type = "0M";
            Console.WriteLine(item.Name + "\t\t" + item.Type);

            var list = new List <IMIM_INFO_INSERT> {
                new IMIM_INFO_INSERT(imimKey, item.Type, float.Parse(item.Conf) * (float)100)
            };
            var entity = new SPEH_CLIM_CLM_IMIM_PRE_SECOND_DEFINE_OCR
            {
                ConnectionString  = ConnectionStringConfig <SPEH_CLIM_CLM_IMIM_PRE_SECOND_DEFINE_OCR> .GetConnectionString(config.ConnectionStringDictionary),
                pIMIM_INFO_INSERT = list.ToDataTable()
            };

            try
            {
                _commonBl.Execute(entity);
                return(entity.ReturnValue == 1);
            }
            catch (Exception ex)
            {
                Nlog.Info($"{config.NLogName}.json", $"getresult catch:{ex.Message}");
                var e = new SPEH_CLIM_CLM_IMIM_STTS_UPDATE_OCR
                {
                    ConnectionString  = ConnectionStringConfig <SPEH_CLIM_CLM_IMIM_STTS_UPDATE_OCR> .GetConnectionString(config.ConnectionStringDictionary),
                    pIMIM_STTS_UPDATE = new List <IMIM_STTS_UPDATE>
                    {
                        new IMIM_STTS_UPDATE {
                            IMIM_KY = imimKey
                        }
                    }

                    .ToDataTable(),
                    pSTS = ((int)ResultJsonStatus.Failed).ToString()
                };
                _commonBl.Execute(e);
                return(false);
            }
        }
Esempio n. 8
0
        static void BeginWork(IDownload down, DownloadConfig config, IRpcProxy proxy)
        {
            try
            {
                //调用sp 获取imimKy batchNo imimPath
                var callDataBase = Containers.Resolve <ICallDataBase>(config.CallDataBaseServiceName);
                var data         = callDataBase.FindData(config);
                //var data = Enumerable.Range(1, 16).Select(x => new ImageInfo { ImimKy = x.ToJson(), BatchNo = "5", ImimPath = $@"C:\Users\sh179\Desktop\testx\{x}.jpg" }).ToList();

                var batchNos = data.Select(x => x.BatchNo).Distinct().ToList();
                Nlog.Info(down.NLogName, $"{config.SourceServerIp}{down.NLogName} 批次 down star,count:{batchNos.Count}");
                foreach (var item in batchNos)
                {
                    var directoryName = $"{config.SourceServerIp}{item}";
                    var path          = $@"{config.TargetServerPath}\{directoryName}";
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    var list = data.Where(x => x.BatchNo == item).DefaultIfEmpty().ToList();

                    Nlog.Info(down.NLogName, $"{config.SourceServerIp}{down.NLogName} image down star,count:{list.Count}\tBatchNo:{item}");
                    Parallel.ForEach(list, new ParallelOptions {
                        MaxDegreeOfParallelism = int.Parse(config.MaxDegreeOfParallelism)
                    }, f => f.IsSuccess = down.Download(f.ImimPath, $@"{path}\{config.SourceServerIp}{f.ImimKy}{(Path.GetExtension(f.ImimPath)?.ToLower() == "" ? ".jpg" : Path.GetExtension(f.ImimPath)?.ToLower())}"));
                    Nlog.Info(down.NLogName, $"{config.SourceServerIp}{down.NLogName} image down end,count:{list.Count(x => x.IsSuccess)}");

                    string result = "未接收到";
                    try
                    {
                        callDataBase.SendJson(list.Where(x => x.IsSuccess).ToList(), config);
                        result = proxy.SendMessage(config.CallDataBaseServiceName, $"{directoryName}");
                        //todo:批次全部发送成功,跟新数据库
                        if (result.ToLower() == "ok")
                        {
                            callDataBase.SendImage(list.Where(x => x.IsSuccess).ToList(), config);
                        }
                        Nlog.Info(down.NLogName, $"{config.SourceServerIp}{down.NLogName} Ocr.Send:{item} Ocr.Result:{result}");
                    }
                    catch (Exception ex)
                    {
                        Nlog.Info(down.NLogName, $"{config.SourceServerIp}{down.NLogName} Ocr.Send:{item} Ocr.Result:{result}.Catch:{ex.Message}");
                    }
                    Thread.Sleep(1000);
                }
                if (batchNos.Count > 0)
                {
                    Nlog.Info(down.NLogName, $"{config.SourceServerIp}{down.NLogName} 批次 down end!\r\n\r\n");
                }
            }
            catch (Exception ex) { Nlog.Info($"{config.NLogName}", $"beginwork catch:{config.SourceServerIp}.{ex.Message}"); }
            Thread.Sleep(config.SendMillisecondsTimeout);
        }
Esempio n. 9
0
        static void BeginGetOcrResult(DownloadConfig config)
        {
            //try
            //{
            //    //删除空文件夹
            //    foreach (var item in Directory.GetDirectories(config.TargetServerPath))
            //    {
            //        var dir = new DirectoryInfo(item);
            //        if (dir.GetFiles().Length == 0) Directory.Delete(item);
            //    }
            //}
            //catch (Exception ex) { Nlog.Info($"{config.NLogName}.json", $"Directory.Delete catch:{config.SourceServerIp}.{ex.Message}"); }

            try
            {
                Stopwatch sw = new Stopwatch();
                sw.Start();
                var jsons = Directory.GetFiles(config.TargetServerPath, "*.json.ok", SearchOption.AllDirectories);
                sw.Stop();

                Nlog.Info($"{config.NLogName}.json", $"{config.SourceServerIp}{config.NLogName} 获取到json.Count:{jsons.Length} 耗时:{sw.Elapsed}");
                var database = Containers.Resolve <ICallDataBase>(config.CallDataBaseServiceName);
                foreach (var item in jsons)
                {
                    var fileName       = Path.GetFileName(item) ?? "";
                    var sourceServerIp = fileName.Substring(0, config.SourceServerIp.Length);
                    if (sourceServerIp != config.SourceServerIp)
                    {
                        continue;
                    }
                    try
                    {
                        string json   = File.ReadAllText(item).Replace("\r\n", "");
                        var    result = database.GetResult(int.Parse(fileName.Replace(config.SourceServerIp, "").Replace(".json.ok", "")), json, config, json.Length > 10);
                        Nlog.Info($"{config.NLogName}.json", $"{fileName} json result:{(json.Length > 10 ? "√" : "╳")},input database:{(result ? "√" : "╳")}");

                        //File.Delete(item);
                        var backupDirectory = $@"{OcrJsonBackupDirectory}\{config.SourceServerIp}\{config.CallDataBaseServiceName}\{DateTime.Now:yyyy-MM-dd}";
                        if (!Directory.Exists(backupDirectory))
                        {
                            Directory.CreateDirectory(backupDirectory);
                        }
                        File.Move(item, $@"{backupDirectory}\{DateTime.Now:yyyy.MM.dd HH.mm.ss.fff}.{fileName}");
                    }
                    catch (Exception ex) { Nlog.Info($"{config.NLogName}.json", $"move json catch:{config.SourceServerIp}{item}.{ex.Message}"); }
                    Thread.Sleep(0);
                }
            }
            catch (Exception ex) { Nlog.Info($"{config.NLogName}.json", $"getresult catch:{ex.Message}"); }
            Thread.Sleep(config.SendMillisecondsTimeout);
        }
Esempio n. 10
0
 /// <summary>
 /// 创建目录
 /// </summary>
 /// <param name="remoteDirectoryName">目录名</param>
 public bool CreateDirectory(string remoteDirectoryName)
 {
     try
     {
         using (var response = OpenResponse(new Uri(_ftpUri + remoteDirectoryName), WebRequestMethods.Ftp.MakeDirectory, out FtpWebRequest request))
         {
             request.Abort();
             return(response.StatusCode == FtpStatusCode.FileActionOK);
         }
     }
     catch (Exception ex)
     {
         Nlog.Info(_nlogName, $"makeDirectory failed,{ex.Message}");
     }
     return(false);
 }
Esempio n. 11
0
 /// <summary>
 /// 删除文件
 /// </summary>
 /// <param name="remotePath">要删除的文件</param>
 private bool DeleteFile(string remotePath)
 {
     try
     {
         using (var response = OpenResponse(new Uri($"{_ftpUri}/{remotePath}"), WebRequestMethods.Ftp.DeleteFile, out FtpWebRequest request))
         {
             request.Abort();
             return(response.StatusCode == FtpStatusCode.FileActionOK);
         }
     }
     catch (Exception ex)
     {
         Nlog.Info(_nlogName, $"{remotePath} delete failed {ex.Message}");
     }
     return(false);
 }
Esempio n. 12
0
 /// <summary>
 /// 更改目录或文件名
 /// </summary>
 /// <param name="uri"></param>
 /// <param name="newName">修改后新名称</param>
 private bool ReName(string uri, string newName)
 {
     try
     {
         var request = OpenRequest(new Uri(uri), WebRequestMethods.Ftp.Rename);
         request.RenameTo = newName;
         using (var response = (FtpWebResponse)request.GetResponse())
         {
             request.Abort();
             return(response.StatusCode == FtpStatusCode.FileActionOK);
         }
     }
     catch (Exception ex)
     {
         Nlog.Info(_nlogName, $"rename failed,{ex.Message}");
     }
     return(false);
 }
 public void BeginWork()
 {
     while (true)
     {
         SPIN_FLFL_FILE_LOG_INFO_INSERT entity = null;
         var result = _msmq.ReceiveTranMessageQueue(x =>
         {
             entity = x.FromJson <SPIN_FLFL_FILE_LOG_INFO_INSERT>();
             _commonBl.Execute(entity);
             return(entity.ReturnValue == 0);
         });
         Nlog.Info(_nLogName, $"{entity?.pFILE_NAME}\t{result.msg}");
         if (result.code == 1)
         {
             break;
         }
     }
     Thread.Sleep(10000);
 }
Esempio n. 14
0
        /// <summary>
        /// 获取当前目录下指定目录下的文件和一级子目录信息
        /// </summary>
        /// <param name="directory">如未指定目录,则启用默认目录</param>
        /// <returns></returns>
        private (bool code, List <FileStruct> FileList) FindFilesByCurrentDirectories(string directory = "")
        {
            //必须保留最后的[/]符号,不然给出的列表会是带有directory/fileName这样的结果
            string uri  = directory.Length == 0 ? _ftpUri : $"{_ftpUri}/{directory}/";
            var    res1 = FindFilesAndDirectories(uri, WebRequestMethods.Ftp.ListDirectory);
            var    res2 = FindFilesAndDirectories(uri, WebRequestMethods.Ftp.ListDirectoryDetails);


            var fileList = new List <FileStruct>();

            foreach (var name in res1.FileList)
            {
                string line = res2.FileList.FirstOrDefault(x => x.Contains(name)) ?? "";
                if (line.Length > 0)
                {
                    if (_fileListStyle == FileListStyle.Unknown || line == "")
                    {
                        continue;
                    }
                    FileStruct f = new FileStruct
                    {
                        Name     = name,
                        FullName = directory.Length > 0 ? $"{directory}/{name}" : name
                    };
                    switch (_fileListStyle)
                    {
                    case FileListStyle.UnixStyle: ParseFileStructFromUnixStyleRecord(line, ref f); break;

                    case FileListStyle.WindowsStyle: ParseFileStructFromWindowsStyleRecord(line, ref f); break;
                    }
                    fileList.Add(f);
                }
            }
            if (res2.FileList.Count > 0)
            {
                res2.FileList = res2.FileList.Select(x => $"{directory}/{x}").ToList();
                Nlog.Info(_nlogName, $"\r\n{string.Join("\r\n", res2.FileList)}");
            }
            return(res1.code && res2.code, fileList);
        }
Esempio n. 15
0
        /// <summary>
        /// 获取当前目录的文件和一级子目录信息
        /// </summary>
        /// <returns></returns>
        private (bool code, List <FileStruct> FileList) ListFilesAndDirectories(string ftpRemotePath)
        {
            ftpRemotePath = ftpRemotePath.Length > 0 ? $"/{_ftpRemotePath}/{ftpRemotePath}" : $"/{_ftpRemotePath}";
            try
            {
                List <SftpFile> sftpFileList;
                using (var sftp = new SftpClient(_connInfo))
                {
                    sftp.Connect();
                    sftpFileList = sftp.ListDirectory(ftpRemotePath)?.Where(x => x.Name != "." && x.Name != "..").ToList();
                    sftp.Disconnect();
                }

                if (sftpFileList?.Count > 0)
                {
                    Nlog.Info(_nlogName, $"\r\n{string.Join("\r\n", sftpFileList.Select(x => $"{x.LastWriteTime} {x.Length.ToString().PadRight(8, ' ')}\t{x.FullName.Replace($"/{_ftpRemotePath}/", "")}"))}");
                }

                //if (sftpFileList == null || sftpFileList.Any(x => x.Length == 0))
                //{
                //    Nlog.Info(_nlogName, $@"获取文件信息失败,{ftpRemotePath.Replace($"/{_ftpRemotePath}/", "")}/{sftpFileList?.FirstOrDefault(x => x.Length == 0)?.Name} size=0||size=null");
                //    return (false, new List<FileStruct>());
                //}

                return(true, sftpFileList?.Select(x => new FileStruct
                {
                    IsDirectory = x.IsDirectory,
                    CreateTime = x.LastWriteTime,
                    Name = x.Name,
                    FullName = x.FullName,
                    Size = x.Length.ToString()
                }).ToList());
            }
            catch (Exception ex)
            {
                Nlog.Info(_nlogName, $"获取文件信息失败,{ftpRemotePath.Replace($"/{_ftpRemotePath}/", "")}.{ex.Message}");
            }
            return(false, new List <FileStruct>());
        }
Esempio n. 16
0
        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="remoteName">要删除的文件名</param>
        private bool DeleteFile(string remoteName)
        {
            bool result;

            try
            {
                using (var sftp = new SftpClient(_connInfo))
                {
                    sftp.Connect();
                    sftp.DeleteFile($"/{remoteName}");
                    sftp.Disconnect();
                    result = true;
                }
                Nlog.Info(_nlogName, $"{remoteName.Replace($"/{_ftpRemotePath}/", "")} \t delete √");
            }
            catch (Exception ex)
            {
                result = false;
                Nlog.Info(_nlogName, $"{remoteName.Replace($"/{_ftpRemotePath}/", "")} \t delete failed {ex.Message}");
            }
            return(result);
        }
Esempio n. 17
0
        /// <summary>
        /// 删除目录
        /// </summary>
        /// <param name="directory">要删除的目录名</param>
        private bool DeleteDirectory(string directory)
        {
            bool result;

            try
            {
                using (SshClient ssh = new SshClient(_connInfo))
                {
                    ssh.Connect();
                    using (var scmd = ssh.RunCommand($"rm -rf {directory}"))
                        result = scmd.ExitStatus == 0;
                    ssh.Disconnect();
                }

                Nlog.Info(_nlogName, $"{directory.Replace($"/{_ftpRemotePath}/", "")} \t delete √");
            }
            catch (Exception ex)
            {
                result = false;
                Nlog.Info(_nlogName, $"{directory.Replace($"/{_ftpRemotePath}/", "")} \t delete failed {ex.Message}");
            }
            return(result);
        }
Esempio n. 18
0
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="remoteName">下载后的保存路径</param>
        /// <param name="localPath">要下载的文件名</param>
        private bool DownloadFile(string remoteName, string localPath)
        {
            try
            {
                using (var sftp = new SftpClient(_connInfo))
                {
                    sftp.Connect();
                    var byt = sftp.ReadAllBytes(remoteName);
                    sftp.Disconnect();
                    File.WriteAllBytes(localPath, byt);
                }
                //var result = File.Exists(localPath) && new FileInfo(localPath).Length > 0;

                var result = File.Exists(localPath);
                Nlog.Info(_nlogName, $"{remoteName.Replace($"/{_ftpRemotePath}/", "")} \t down {(result ? "√" : "failed")}");
                return(result);
            }
            catch (Exception ex)
            {
                Nlog.Info(_nlogName, $"{remoteName.Replace($"/{_ftpRemotePath}/", "")} \t down failed {ex.Message}");
                return(false);
            }
        }
Esempio n. 19
0
        public bool Download(string sourcePath, string targetPath)
        {
            if (sourcePath.Length == 0)
            {
                Nlog.Info(NLogName, $"{Path.GetFileName(sourcePath) } is null"); return(false);
            }
            if (targetPath.Length == 0)
            {
                Nlog.Info(NLogName, $"{Path.GetFileName(targetPath) } is null"); return(false);
            }

            try
            {
                var result = api.GetAsync(sourcePath, targetPath);
                //var result = _api.Download(sourcePath, targetPath);
                Nlog.Info(NLogName, $"{Path.GetFileName(targetPath) }\tCloud down {(result ? "√" : "failed")}");
                return(result);
            }
            catch (Exception ex)
            {
                Nlog.Info(NLogName, $"{Path.GetFileName(targetPath) } {ex.Message}");
                return(false);
            }
        }
Esempio n. 20
0
        public bool GetResult(int imimKey, string json, DownloadConfig config, bool status)
        {
            if (!status)
            {
                var e = new SPEH_IMAGE_SECOND_SEND_STS_UPDATE_OCR
                {
                    ConnectionString = ConnectionStringConfig <SPEH_IMAGE_SECOND_SEND_STS_UPDATE_OCR> .GetConnectionString(config.ConnectionStringDictionary),
                    pIMIM_KY_LIST    = new List <Entites.IvInput.TableType.KY_LIST>
                    {
                        new Entites.IvInput.TableType.KY_LIST(imimKey)
                    }

                    .ToList().ToDataTable(),
                    pSTS = ((int)ResultJsonStatus.Failed).ToString()
                };
                _commonBl.Execute(e);
                return(e.ReturnValue == 1);
            }
            //Console.WriteLine(imimKey + ":" + json);
            //var item = json.FromJson<List<ImageIvInputJson>>();
            var data    = json.FromJson <ImageIvInputJson>();
            var item    = data.data;
            var prjList = item.Where(x => x.name != "项目2").Select(x => new USDF_CLSV_PRJ_INFO_OCR
            {
                CLSV_PRJ_TYPE           = x.name == "项目1" ? "CLIV_PRJ" : "CLIV",
                CLSV_PRJ_NAME           = x.name == "项目1" ? x.value1 : x.name,
                CLSV_PRJ_VALUES         = x.name == "项目1" ? x.value2 : x.value1,
                IMCO_PRJ_COORDINATE_1_X = !string.IsNullOrEmpty(x.coord1) ? float.Parse(x.coord1.Split(',')[0]) : 0F,
                IMCO_PRJ_COORDINATE_1_Y = !string.IsNullOrEmpty(x.coord1) ? float.Parse(x.coord1.Split(',')[1]) : 0F,
                IMCO_PRJ_COORDINATE_2_X = !string.IsNullOrEmpty(x.coord2) ? float.Parse(x.coord2.Split(',')[0]) : 0F,
                IMCO_PRJ_COORDINATE_2_Y = !string.IsNullOrEmpty(x.coord2) ? float.Parse(x.coord2.Split(',')[1]) : 0F,
                CONF_1 = x.conf1,
                CONF_2 = x.conf2
            }).ToList();

            var spspList = item.Where(x => x.name == "项目2").Select(x => new USDF_CLSV_SPSP_INFO_OCR
            {
                CLSV_SPSP_NAME           = x.value1,
                CLSV_CHG                 = x.value2,
                IMCO_SPSP_COORDINATE_1_X = !string.IsNullOrEmpty(x.coord1) ? float.Parse(x.coord1.Split(',')[0]) : 0F,
                IMCO_SPSP_COORDINATE_1_Y = !string.IsNullOrEmpty(x.coord1) ? float.Parse(x.coord1.Split(',')[1]) : 0F,
                IMCO_SPSP_COORDINATE_2_X = !string.IsNullOrEmpty(x.coord2) ? float.Parse(x.coord2.Split(',')[0]) : 0F,
                IMCO_SPSP_COORDINATE_2_Y = !string.IsNullOrEmpty(x.coord2) ? float.Parse(x.coord2.Split(',')[1]) : 0F,
                CONF_1 = x.conf1,
                CONF_2 = x.conf2
            }).ToList();

            var entity = new SPIN_CLIV_INSERT_OCR
            {
                ConnectionString = ConnectionStringConfig <SPIN_CLIV_INSERT_OCR> .GetConnectionString(config.ConnectionStringDictionary),
                pFLFL_KY         = $"{config.SourceServerIp }{ imimKey}",
                pIMIM_KY         = imimKey.ToString(),
                pCLIV_ID         = item.FirstOrDefault(x => x.name == "NO.")?.value1,
                pCLSV_PRJ_INFO   = prjList.ToDataTable(),
                pCLSV_SPSP_INFO  = spspList.ToDataTable()
            };

            try
            {
                _commonBl.Execute(entity);
                return(entity.ReturnValue == 1);
            }
            catch (Exception ex)
            {
                Nlog.Info($"{config.NLogName}.json", $"getresult catch:{ex.Message}");
                var e = new SPEH_IMAGE_SECOND_SEND_STS_UPDATE_OCR
                {
                    ConnectionString = ConnectionStringConfig <SPEH_IMAGE_SECOND_SEND_STS_UPDATE_OCR> .GetConnectionString(config.ConnectionStringDictionary),
                    pIMIM_KY_LIST    = new List <Entites.IvInput.TableType.KY_LIST>
                    {
                        new Entites.IvInput.TableType.KY_LIST(imimKey)
                    }

                    .ToList().ToDataTable(),
                    pSTS = ((int)ResultJsonStatus.Failed).ToString()
                };
                _commonBl.Execute(e);
                return(false);
            }
        }
Esempio n. 21
0
        public bool GetResult(int imimKey, string json, DownloadConfig config, bool status)
        {
            if (!status)
            {
                var e = new SPEH_IMDI_IMAGE_DETAIL_INFO_SENT_STS_UPDATE_OCR
                {
                    ConnectionString = ConnectionStringConfig <SPEH_CLIM_CLM_IMIM_STTS_UPDATE_OCR> .GetConnectionString(config.ConnectionStringDictionary),
                    pIMIM_KY_LIST    = new List <Entites.HpList.TableType.KY_LIST>
                    {
                        new Entites.HpList.TableType.KY_LIST(imimKey)
                    }

                    .ToDataTable(),
                    pSTS = ((int)ResultJsonStatus.Failed).ToString(),
                };
                _commonBl.Execute(e);
                return(e.ReturnValue == 1);
            }
            var item = json.FromJson <HospitalListJson>();
            //Console.WriteLine(imimKey + ":" + json);

            var pclslList = item.data.Select(x => new USDF_CLSL_INFO_OCR
            {
                CLSL_TYPE            = x.name.Contains(@"门诊") ? "1" : x.name.Contains(@"住院") ? "2" : "0",
                CLSL_NAME            = x.value1,
                CLSL_VALUES          = x.value2,
                CLSL_VALUES_3        = x.value3,
                CLSL_VALUES_4        = x.value4,
                CLSL_VALUES_5        = x.value5,
                IMCO_COORDINATE_INFO = x.coord.ToJson(),
                //IMCO_PRJ_COORDINATE_1_Y = !string.IsNullOrEmpty(x.coord1) ? float.Parse(x.coord1.Split(',')[1]) : 0F,
                //IMCO_PRJ_COORDINATE_2_X = !string.IsNullOrEmpty(x.coord2) ? float.Parse(x.coord2.Split(',')[0]) : 0F,
                //IMCO_PRJ_COORDINATE_2_Y = !string.IsNullOrEmpty(x.coord2) ? float.Parse(x.coord2.Split(',')[1]) : 0F,
                CONF_1 = x.conf1,
                CONF_2 = x.conf2,
                CONF_3 = x.conf3,
                CONF_4 = x.conf4,
                CONF_5 = x.conf5,
            }).ToList();

            var entity = new SPIN_CLSL_INSERT_OCR
            {
                ConnectionString = ConnectionStringConfig <SPIN_CLSL_INSERT_OCR> .GetConnectionString(config.ConnectionStringDictionary),
                pFLFL_KY         = $"{config.SourceServerIp }{ imimKey}",
                pIMIM_KY         = imimKey.ToString(),
                pCLSL_INFO       = pclslList.ToDataTable(),
            };

            try
            {
                _commonBl.Execute(entity);
                return(entity.ReturnValue == 1);
            }
            catch (Exception ex)
            {
                Nlog.Info($"{config.NLogName}.json", $"getresult catch:{ex.Message}");
                var e = new SPEH_IMDI_IMAGE_DETAIL_INFO_SENT_STS_UPDATE_OCR
                {
                    ConnectionString = ConnectionStringConfig <SPEH_CLIM_CLM_IMIM_STTS_UPDATE_OCR> .GetConnectionString(config.ConnectionStringDictionary),
                    pIMIM_KY_LIST    = new List <Entites.HpList.TableType.KY_LIST>
                    {
                        new Entites.HpList.TableType.KY_LIST(imimKey)
                    }

                    .ToDataTable(),
                    pSTS = ((int)ResultJsonStatus.Failed).ToString(),
                };
                _commonBl.Execute(e);
                return(false);
            }
        }
        public void BeginWork(params FtpConfig[] ftpConfigs)
        {
            var ftpConfig = ftpConfigs[0];

            Nlog.Info(ftpConfig.FileType, $"{ftpConfig.FileType}\t开始运行...");

            var ftp = Containers.Resolve <IFtp>(
                new NamedParameter("nLogName", ftpConfig.FileType),
                new NamedParameter("ftpServerIp", ftpConfig.Ip),
                new NamedParameter("ftpProt", ftpConfig.Port),
                new NamedParameter("privateKeyFile", Path.Combine(Environment.CurrentDirectory, @"SFTP_KEY\tmp.cap")),
                new NamedParameter("ftpRemotePath", ftpConfig.RemoteFolder),
                new NamedParameter("ftpUserId", ftpConfig.Uid),
                new NamedParameter("ftpPassword", ftpConfig.Pwd),
                new NamedParameter("isdirectory", ftpConfig.IsDirectory));

            ftp.InitializeFileListStyle();
            //Thread.Sleep(5000);

            var list1 = ftp.FindFiles().Where(x => ftpConfig.NameContains.Length <= 0 || x.Name.ToLower().Contains(ftpConfig.NameContains.ToLower()));

            Thread.Sleep(3000);
            var list2 = ftp.FindFiles();

            var list = list1.Join(list2, x => new { x.Name, x.Size }, y => new { y.Name, y.Size },
                                  (x, y) => new FileStruct {
                IsDirectory = x.IsDirectory, Name = x.Name, CreateTime = x.CreateTime, FullName = x.FullName, Size = x.Size, IsSuccess = false
            })
                       .Where(x => x.IsDirectory == ftpConfig.IsDirectory).ToList();

            string time        = DateTime.Now.ToString("yyyy-MM-dd");
            var    messageList = new List <string>();

            Parallel.ForEach(list, new ParallelOptions()
            {
                MaxDegreeOfParallelism = ftpConfig.MaxDegreeOfParallelism
            }, f =>
            {
                f.LocalFullName = $@"{ftpConfig.LocalDirectory}\{ftpConfig.FileType}\{(ftpConfig.LocalBackupFolder.Length > 0 ? ftpConfig.LocalBackupFolder : ftpConfig.RemoteFolder)}\{time}\{Math.Abs(Guid.NewGuid().GetHashCode()).ToString().PadRight(10, '0')}";
                if (ftp.Download(f.Name, f.LocalFullName))
                {
                    f.IsSuccess = true;
                    var message = new SPIN_FLFL_FILE_LOG_INFO_INSERT
                    {
                        pFLFL_TYPE    = ftpConfig.FileType,
                        pFILE_NAME    = f.Name,
                        pFLFL_URL     = $@"{f.LocalFullName}\{f.Name}",
                        pFLFL_STS     = "0",
                        pFLFL_USUS_ID = ftpConfig.RemoteFolder
                    }.ToJson();
                    messageList.Add(message);
                }
            });

            lock (_obj)
            {
                _listFileStruct.AddRange(list);
                _listMessage.AddRange(messageList);
            }

            if (list.Count <= 0 || list.Count(x => !x.IsSuccess) > 0)
            {
                Nlog.Info(ftpConfig.FileType, $"整包下载总数:{list.Count},其中失败{list.Count(x => !x.IsSuccess)}");
            }

            Nlog.Info(ftpConfig.FileType, $"{ftpConfig.FileType}\t运行完毕...\r\n\r\n");
        }
        public void BeginWork(params FtpConfig[] ftpConfigs)
        {
            var ftpConfig = ftpConfigs[0];

            Nlog.Info(ftpConfig.FileType, $"{ftpConfig.FileType}\t开始运行...");

            var ftp = Containers.Resolve <IFtp>(
                new NamedParameter("nLogName", ftpConfig.FileType),
                new NamedParameter("ftpServerIp", ftpConfig.Ip),
                new NamedParameter("ftpProt", ftpConfig.Port),
                new NamedParameter("privateKeyFile", Path.Combine(Environment.CurrentDirectory, @"SFTP_KEY\tmp.cap")),
                new NamedParameter("ftpRemotePath", ftpConfig.RemoteFolder),
                new NamedParameter("ftpUserId", ftpConfig.Uid),
                new NamedParameter("ftpPassword", ftpConfig.Pwd),
                new NamedParameter("isdirectory", ftpConfig.IsDirectory));

            ftp.InitializeFileListStyle();

            var list1 = ftp.FindFiles().Where(x => ftpConfig.NameContains.Length <= 0 || x.Name.ToLower().Contains(ftpConfig.NameContains.ToLower()));

            Thread.Sleep(3000);
            var list2 = ftp.FindFiles();

            var list = list1.Join(list2, x => new { x.Name, x.Size }, y => new { y.Name, y.Size },
                                  (x, y) => new FileStruct {
                IsDirectory = x.IsDirectory, Name = x.Name, CreateTime = x.CreateTime, FullName = x.FullName, Size = x.Size, IsSuccess = false
            })
                       .Where(x => x.IsDirectory == ftpConfig.IsDirectory && int.TryParse(x.Size, out int size) && size > 0).ToList();

            string time = DateTime.Now.ToString("yyyy-MM-dd");

            Parallel.ForEach(list, new ParallelOptions()
            {
                MaxDegreeOfParallelism = ftpConfig.MaxDegreeOfParallelism
            }, f =>
            {
                f.LocalFullName = $@"{ftpConfig.LocalDirectory}\{ftpConfig.FileType}\{(ftpConfig.LocalBackupFolder.Length > 0 ? ftpConfig.LocalBackupFolder : ftpConfig.RemoteFolder)}\{time}\{Math.Abs(Guid.NewGuid().GetHashCode()).ToString().PadRight(10, '0')}";
                if (ftp.Download(f.Name, f.LocalFullName))
                {
                    f.IsSuccess = true;
                    var message = new SPIN_FLFL_FILE_LOG_INFO_INSERT
                    {
                        pFLFL_TYPE    = ftpConfig.FileType,
                        pFILE_NAME    = f.Name,
                        pFLFL_URL     = $@"{f.LocalFullName}\{f.Name}",
                        pFLFL_STS     = "0",
                        pFLFL_USUS_ID = ftpConfig.RemoteFolder
                    }.ToJson();

                    if (_msmq.SendTranMessageQueue(message, (result, msg) => Nlog.Info(ftpConfig.FileType, $"{f.Name} \t msmq {(result ? "√" : "failed")} {msg}")))
                    {
                        //备份文件
                        if (ftpConfig.RemoteBackupFolder.Length > 0)
                        {
                            ftp.ReNameToBackupDirectory(ftpConfig.RemoteBackupFolder, f.Name);
                        }
                        else
                        {
                            ftp.Delete(f.FullName);
                        }
                    }
                }
            });

            Nlog.Info(ftpConfig.FileType, $"{ftpConfig.FileType}\t运行完毕...\r\n\r\n");
            Thread.Sleep(ftpConfig.ThreadMillisecondsTimeout);
        }
        public void InitializeComponent(string path)
        {
            _listFileStruct.Clear();
            _listMessage.Clear();
            _msmq.InitializeMessageQueue(path);
            _inputDataBase.InitializeComponent(path);
            StringBuilder stb = new StringBuilder();

            foreach (var config in _ftpConfigs)
            {
                stb.Clear();
                stb.AppendLine($"开始检查 {config.FileType} 配置.....");
                Console.WriteLine();

                if (config.RemoteBackupFolder.Length > 0)
                {
                    stb.AppendLine($"启用文件备份,下载文件将会备份至Ftp/{config.RemoteBackupFolder}目录");
                }
                if (config.NameContains.Length > 0)
                {
                    stb.AppendLine($"启用文件名验证,仅会下载包含{config.NameContains}的文件");
                }
                stb.AppendLine($"{config.FileType} 配置 √");
                Console.WriteLine(stb);
            }


            //Console.WriteLine("running after 3 s...");
            //Thread.Sleep(1000);
            //Console.WriteLine("running after 2 s...");
            //Thread.Sleep(1000);
            //Console.WriteLine("running after 1 s...");
            //Thread.Sleep(1000);
            //Console.WriteLine("Go");


            List <Task> taskList = new List <Task>();

            taskList.AddRange(_ftpConfigs.Select(f => Task.Factory.StartNew(x =>
            {
                f.MaxDegreeOfParallelism = f.MaxDegreeOfParallelism == 0 || f.MaxDegreeOfParallelism > Environment.ProcessorCount ? Environment.ProcessorCount : f.MaxDegreeOfParallelism;
                BeginWork(f);
            }, 1)));

            Task.WaitAll(taskList.ToArray());

            //仅全部下载成功情况下,才能整批导入

            if (_listFileStruct.Count > 0 && _listFileStruct.Count(x => !x.IsSuccess) == 0)
            {
                if (_msmq.SendTranMessageQueue(_listMessage, (result, msg) => Nlog.Info("MessageQueue", $"{_listFileStruct.Count}个 msmq {(result ? "√" : "failed")} {msg}")))
                {
                    _ftpConfigs.ForEach(DeleteFtpFiles);
                }
            }
            else
            {
                Nlog.Info($"整包下载总数:{_listFileStruct.Count},其中失败{_listFileStruct.Count(x => !x.IsSuccess)}");
            }
            _inputDataBase.BeginWork();
        }