123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453 |
-
- using System;
- using System.Net.Http;
- using System.IO;
- using System.Linq;
- using System.Threading.Tasks;
- using Vinno.IUS.Common.Log;
- using System.Collections.Generic;
- using Vinno.vCloud.Common.Vid2;
- using Vinno.IUS.Common.Network.Transfer;
- using Vinno.IUS.Common.Network.Leaf;
- using Vinno.IUS.Common.Network.Tcp;
- using Vinno.vCloud.Protocol.Messages;
- using Vinno.vCloud.Protocol.Infrastructures;
- using Vinno.vCloud.Protocol.Messages.Client.Account;
- using Vinno.vCloud.Protocol.Messages.Client.Remedical.TerminialReords;
- using Vinno.vCloud.Protocol.Messages.Client.Remedical.TerminalDatas;
- using Vinno.vCloud.Protocol.Messages.Client.AfterSales;
- using Vinno.vCloud.Protocol.Messages.Client.AssignTermianl;
- using System.Collections.Concurrent;
- using System.Threading;
- namespace DownloadImagesTool
- {
- class DownloadArgs
- {
- public string DownloadPath { get; set; }
- public string DownloadUrl { get; set; }
- }
- class ConvertArgs
- {
- public string FilePath { get; set; }
- }
- internal class DownloadImageWorker
- {
- private ConcurrentQueue<DownloadArgs> _downloadQueue = new ConcurrentQueue<DownloadArgs>();
- private SemaphoreSlim _downloadSlim = new SemaphoreSlim(5);
- private ConcurrentQueue<ConvertArgs> _convertQueue = new ConcurrentQueue<ConvertArgs>();
- private SemaphoreSlim _convertSlim = new SemaphoreSlim(5);
- private readonly SpinThread _downloadThread;
- private readonly SpinThread _convertThread;
- private ClientLeaf _leaf;
- private HttpClient _httpClient = new HttpClient();
- private string _url = "127.0.0.1:9096";
- private string _tempAccoundId;
- private string _tempAccoundName;
- private Action<string> _setQueue;
- public DownloadImageWorker()
- {
- _downloadThread = new SpinThread("DownloadQueueThread1", TimeSpan.FromMilliseconds(10), RunDownloadVid);
- _downloadThread.Start();
- _convertThread = new SpinThread("ConvertQueueThread1", TimeSpan.FromMilliseconds(10), RunConvertVid);
- _convertThread.Start();
- }
- private async void RunDownloadVid(object item)
- {
- if (_downloadSlim.Wait(10))
- {
- await Task.Run(async () =>
- {
- try
- {
- if (_downloadQueue.TryDequeue(out DownloadArgs parms))
- {
- await DownVid(parms.DownloadUrl.Replace("1!U$", "").Replace("http://cdn-bj.fis.plus", "https://flyinsono-bj-1300984704.cos.ap-beijing.myqcloud.com"), parms.DownloadPath);
- _convertQueue.Enqueue(new ConvertArgs { FilePath = parms.DownloadPath });
- _setQueue($"Downing Queue:{_downloadQueue.Count}");
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"RunDownloadVid error {ex}");
- }
- finally
- {
- _downloadSlim.Release();
- }
- });
- }
- }
- private async void RunConvertVid(object item)
- {
- if (_convertSlim.Wait(10))
- {
- await Task.Run(() =>
- {
- try
- {
- if (_convertQueue.TryDequeue(out ConvertArgs parms))
- {
- ConvertImage(parms.FilePath);
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"RunConvertVid error {ex}");
- }
- finally
- {
- _convertSlim.Release();
- }
- });
- }
- }
- /// <summary>
- /// 根据用户Id下载对应的图像
- /// </summary>
- /// <param name="UserId">用户Id</param>
- public async Task StartDownload(string url, Action<string> writeLog, Action<string> setQueue, params string[] userNames)
- {
- _url = url;
- _setQueue = setQueue;
- CreateLeaf();
- var vidPath = Path.Combine(Environment.CurrentDirectory, "Vid");
- if (!Directory.Exists(vidPath))
- {
- Directory.CreateDirectory(vidPath);
- }
- writeLog($"设置了Vid路径:{vidPath}");
- var logFile = Path.Combine(Environment.CurrentDirectory, "DownloadFileLog.txt");
- if (!File.Exists(logFile))
- {
- File.Create(logFile);
- }
- writeLog($"设置了Token记录文件路径:{logFile}");
- writeLog("正在查找用户...");
- //查找用户
- List<string> userIds = new List<string>();
- foreach (var name in userNames)
- {
- using (var request = MessagePool.GetMessage<SearchAccurateUserRequest>())
- {
- request.Keyword = name;
- var resultMessage = _leaf.Send(request);
- var result = SearchAccurateUserResult.Convert(resultMessage);
- if (result == null)
- {
- writeLog("查找用户失败,请检查账号");
- Logger.WriteLineInfo($"SearchAccurateUserRequest fail");
- return;
- }
- var userInfo = result.Users.FirstOrDefault(f => f.Name.ToLower() == name.ToLower());
- if (userInfo != null)
- {
- userIds.Add(userInfo.Id);
- _tempAccoundId = userInfo.Id;
- _tempAccoundName = userInfo.Name;
- }
- }
- }
- writeLog("正在查找用户超声机...");
- //查找用户
- List<string> terminalIds = new List<string>();
- List<string> organizationNames = new List<string>();
- foreach (var userId in userIds)
- {
- using (var request = MessagePool.GetMessage<FindTerminalsBySalesUserRequest2>())
- {
- request.UserId = userId;
- var resultMessage = _leaf.Send(request);
- var result = FindTerminalsBySalesUserResult2.Convert(resultMessage);
- if (result == null)
- {
- writeLog("查找用户失败,请检查账号");
- Logger.WriteLineInfo($"SearchAccurateUserRequest fail");
- return;
- }
- var tempTerminalIds = result.EntityMessages?.Select(f => f.Id);
- organizationNames = result.EntityMessages?.Select(f => f.OrganizationDescription).ToList();
- if (tempTerminalIds != null)
- {
- terminalIds.AddRange(tempTerminalIds);
- }
- }
- }
- writeLog("正在查找用户组织...");
- //查找用户
- List<string> organizationIds = new List<string>();
- foreach (var name in organizationNames)
- {
- using (var request = MessagePool.GetMessage<FindOrganizationByKeyWordRequest>())
- {
- request.KeyWord = name;
- var resultMessage = _leaf.Send(request);
- var result = FindOrganizationInfosResult.Convert(resultMessage);
- if (result == null)
- {
- writeLog("查找用户失败,请检查账号");
- Logger.WriteLineInfo($"SearchAccurateUserRequest fail");
- return;
- }
- var tempOrganizationIds = result.OrganizationInfos.Select(f => f.OrganizationId);
- if (tempOrganizationIds != null)
- {
- organizationIds.AddRange(tempOrganizationIds);
- }
- }
- }
- writeLog($"userIds:{userIds.Count}\n获取所有检查记录...");
- //获取所有检查记录
- List<string> terminlalRecords = new List<string>();
- foreach (var userId in userIds)
- {
- using (var request = MessagePool.GetMessage<GetRecordsRequest>())
- {
- request.Filter = new RecordFilterMessage();
- request.Filter.StartDateTime = DateTime.MinValue;
- request.Filter.EndDateTime = DateTime.Now;
- request.Filter.FilterType = RecordFilterType.All;
- request.Filter.OrganizationIds = organizationIds;
- request.Filter.TerminalIds = terminalIds;
- request.PageIndex = 0;
- request.PageSize = 9000000;
- //SetAccountDataToMessage(request, _tempAccoundId, _tempAccoundName);
- request.UserId = userId;
- var resultMessage = _leaf.Send(request, 100000);
- var result = GetRecordsSuccess.Convert(resultMessage);
- if (result == null)
- {
- writeLog("获取所有检查数据失败,请检查账号");
- Logger.WriteLineInfo($"GetRecordsRequest fail");
- return;
- }
- var records = result.TerminalRecords.Select(f => f.Id).ToList();
- if (records != null && records.Count > 0)
- {
- terminlalRecords.AddRange(records);
- }
- }
- }
- writeLog($"terminlalRecords:{terminlalRecords.Count}\n获取所有检查数据...");
- //获取所有检查数据
- List<string> imageUrls = new List<string>();
- var directories = Directory.GetDirectories(vidPath);
- //foreach(var dir in directories)
- foreach (var recordId in terminlalRecords)
- {
- try
- {
- var savePath = Path.Combine(vidPath, recordId);
- var fPan = Path.Combine("F:\\DataGet\\Vid", recordId);
- if (Directory.Exists(fPan))
- {
- savePath = fPan;
- }
- if (!Directory.Exists(savePath) || Directory.GetFiles(savePath).Length == 0)
- {
- using (var request = MessagePool.GetMessage<GetRecordDatasRequest>())
- {
- request.TerminalRecordId = recordId;
- var resultMessage = _leaf.Send(request, 1000000);
- var result = GetRecordDatasSuccess.Convert(resultMessage);
- if (result == null)
- {
- continue;
- }
- var recordDatas = result.Datas.Select(f =>
- {
- var fileData = f.Files.FirstOrDefault(f => f.ImageQuality == 0);
- return (recordId, fileData?.FileName);
- }).ToList();
- if (recordDatas != null && recordDatas.Count > 0)
- {
- foreach (var imageInfo in recordDatas)
- {
- if (string.IsNullOrWhiteSpace(imageInfo.FileName))
- {
- continue;
- }
- var filePath = Path.Combine(savePath, Path.GetFileName(imageInfo.FileName));
- if (!File.Exists(filePath))
- {
- _downloadQueue.Enqueue(new DownloadArgs { DownloadUrl = imageInfo.FileName.Replace("1!U$", ""), DownloadPath = filePath });
- Logger.WriteLineInfo($"Download file token {imageInfo.FileName}");
- imageUrls.Add($"Token:{imageInfo.FileName}");
- }
- }
- }
- }
- }
- else
- {
- var files = Directory.GetFiles(savePath, "*.dat");
- foreach (var file in files)
- {
- if (File.Exists(file.Replace(".dat", ".jpeg")) || File.Exists(file.Replace(".dat", ".mp4")))
- {
- File.Delete(file);
- continue;
- }
- _convertQueue.Enqueue(new ConvertArgs { FilePath = file });
- }
- }
- }
- catch
- {
- File.AppendAllLines(logFile, imageUrls);
- imageUrls.Clear();
- continue;
- }
- }
- File.AppendAllLines(logFile, imageUrls);
- await Task.CompletedTask;
- }
- private async Task DownVid(string url, string filePath, int times = 0)
- {
- try
- {
- if (File.Exists(filePath)){
- return;
- }
- var parentDir = Directory.GetParent(filePath).FullName;
- if (!Directory.Exists(parentDir))
- {
- Directory.CreateDirectory(parentDir);
- }
- using (var webStream = await _httpClient.GetStreamAsync(url))
- {
- using (var fileStream = File.Create(filePath))
- {
- var readLength = 0;
- byte[] bytes = new byte[10240];
- do
- {
- readLength = webStream.Read(bytes, 0, bytes.Length);
- if (readLength != 0)
- {
- if (readLength == bytes.Length)
- {
- fileStream.Write(bytes);
- }
- else
- {
- fileStream.Write(bytes.Take(readLength).ToArray());
- }
- }
- }
- while (readLength != 0);
- fileStream.Flush();
- }
- }
- }
- catch (Exception ex)
- {
- if (times < 3)
- {
- times++;
- await DownVid(url, filePath, times);
- }
- }
- }
- private void ConvertImage(string vidFile)
- {
- try
- {
- var vinnoImageData = new VinnoImageData(vidFile, OperationMode.Open);
- if (vinnoImageData.ImageCount > 1)
- {
- var destFile = vidFile.Replace(".dat", ".mp4");
- if (!File.Exists(destFile))
- {
- Mpeg4Converter.ConvertVidToMpeg4(vinnoImageData, destFile);
- }
- return;
- }
- else
- {
- var destFile = vidFile.Replace(".dat", ".jpeg");
- File.WriteAllBytes(destFile, vinnoImageData.GetImage(0)?.ImageData);
- return;
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"Convert fail! {ex}");
- return;
- }
- }
- /// <summary>
- /// Create a leaf to connect to the server.
- /// </summary>
- private void CreateLeaf()
- {
- if (_leaf != null)
- {
- _leaf.Close();
- }
- _leaf = new ClientLeaf(new LeafIdContext(), LeafMode.Dual, new TcpCreator(_url), "Terminal upgrade:");
- if (!_leaf.Online)
- {
- _leaf.Close();
- }
- else
- {
- _leaf.RegisterSetAccountDataMessageFunc(SetAccountDataToMessage);
- }
- }
- private void CloseLeaf()
- {
- _leaf?.Close();
- }
- private Message SetAccountDataToMessage(Message message)
- {
- if (message is ClientRequestMessage clientRequestMessage)
- {
- clientRequestMessage.AccountData = GetAccountDataMessage() as ClientAccountMessage;
- return clientRequestMessage;
- }
- return message;
- }
- private Message SetAccountDataToMessage(Message message, string accoundId, string accoundName)
- {
- if (message is ClientRequestMessage clientRequestMessage)
- {
- clientRequestMessage.AccountData = new ClientAccountMessage
- {
- AccountId = accoundId,
- AccountName = accoundName
- };
- return clientRequestMessage;
- }
- return message;
- }
- private Message GetAccountDataMessage()
- {
- var accountData = MessagePool.GetMessage<ClientAccountMessage>();
- accountData.AccountId = "TerminalUpgradeId";
- accountData.AccountName = "TerminalUpgrade";
- accountData.Source = LoginSource.UltrasoundMachine;
- return accountData;
- }
- }
- }
|