Example #1
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);
        }
Example #2
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);
        }