123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444 |
- using System;
- using System.IO;
- using System.Threading;
- using Vinno.IUS.Common.Log;
- using Vinno.IUS.Common.Network.Leaf;
- using Vinno.IUS.Common.Network.Transfer;
- using Vinno.vCloud.Common.Storage;
- using Vinno.vCloud.Common.Storage.Upload;
- using Vinno.vCloud.FIS.CrossPlatform.Common.Helper;
- using Vinno.vCloud.Protocol.Infrastructures;
- using Vinno.vCloud.Protocol.Messages.Report;
- using Vinno.vCloud.Protocol.Messages.Terminal.Remedical.TerminalDatas;
- namespace Vinno.vCloud.Common.FIS.Remedicals
- {
- internal class vCloudScanData : IvCloudScanData, IDisposable
- {
- private UploadStatus _status;
- private bool _disposed;
- public readonly ClientLeaf Leaf;
- /// <inheritdoc />
- /// <summary>
- /// Gets the id of this scan data.
- /// </summary>
- public string Id { get; }
- /// <inheritdoc />
- /// <summary>
- /// Gets the examId of this scan data.
- /// </summary>
- public string ExamId { get; }
- /// <inheritdoc />
- /// <summary>
- /// Gets or sets the ExamRecordId for this scan data.
- /// </summary>
- public string ExamRecordId { get; set; }
- /// <inheritdoc />
- /// <summary>
- /// Gets the workorder Id.
- /// </summary>
- public string WorkOrderId { get; }
- /// <summary>
- /// Raised when the status changed.
- /// </summary>
- public event EventHandler StatusChanged;
- /// <inheritdoc />
- /// <summary>
- /// Gets the status of this Scan data.
- /// </summary>
- public UploadStatus Status
- {
- get => _status;
- internal set
- {
- if (_status != value)
- {
- var old = _status;
- _status = value;
- if (_status == UploadStatus.Uploaded || _status == UploadStatus.Deleted)
- {
- Delete(old);
- }
- else
- {
- Move(old, _status);
- }
- OnStatusChanged();
- }
- }
- }
- /// <inheritdoc />
- /// <summary>
- /// Gets the vid file path.
- /// </summary>
- public string VidFilePath { get; private set; }
- /// <summary>
- /// Gets the sd file path.
- /// </summary>
- public string ScanDataFilePath { get; private set; }
- /// <inheritdoc />
- /// <summary>
- /// Gets the description of this scan data.
- /// </summary>
- public string Description { get; }
- /// <inheritdoc />
- /// <summary>
- /// Gets the data type of this scan data.
- /// </summary>
- public VidType DataType { get; }
- /// <inheritdoc />
- /// <summary>
- /// Gets the patient info for this scan data.
- /// </summary>
- public PatientScanInfo PatientInfo { get; }
- public vCloudScanData(ClientLeaf leaf, IvCloudExamRecord record, string id, string vidFilePath,
- VidType dataType, string description, PatientScanInfo patientInfo, UploadStatus status)
- {
- _status = status;
- Leaf = leaf;
- ExamRecordId = record.Id;
- ExamId = record.ExamId;
- WorkOrderId = record.WorkOrderId;
- Id = id;
- DataType = dataType;
- if (description == "FromSonopost")
- {
- Description = "From agent";
- }
- else
- {
- Description = description;
- }
- PatientInfo = patientInfo;
- //Generate files.
- VidFilePath = GetVidFilePath(_status);
- if (string.Compare(VidFilePath, vidFilePath, StringComparison.OrdinalIgnoreCase) != 0)
- {
- File.Move(vidFilePath, VidFilePath);
- }
- //Save self.
- Save();
- }
- ~vCloudScanData()
- {
- DoDispose();
- }
- private void OnStatusChanged()
- {
- StatusChanged?.Invoke(this, EventArgs.Empty);
- }
- private ReportPatientInfoMessage1 CreateReportPatientInfoMessage()
- {
- ReportPatientInfoMessage1 patientInfoMessage = null;
- if (PatientInfo != null)
- {
- ReportPatientURInfoMessage patientUrInfo;
- if (PatientInfo.PatientUrInfo != null)
- {
- patientUrInfo = MessagePool.GetMessage<ReportPatientURInfoMessage>();
- patientUrInfo.PPSACoefficient = PatientInfo.PatientUrInfo.PPSACoefficient;
- patientUrInfo.PSA = PatientInfo.PatientUrInfo.PSA;
- }
- else
- {
- patientUrInfo = null;
- }
- ReportPatientGYNInfoMessage patientGynInfo;
- if (PatientInfo.PatientGynInfo != null)
- {
- patientGynInfo = MessagePool.GetMessage<ReportPatientGYNInfoMessage>();
- patientGynInfo.AB = PatientInfo.PatientGynInfo.AB;
- patientGynInfo.Ectopic = PatientInfo.PatientGynInfo.Ectopic;
- patientGynInfo.Gravida = PatientInfo.PatientGynInfo.Gravida;
- patientGynInfo.LMP = PatientInfo.PatientGynInfo.LMP;
- patientGynInfo.Para = PatientInfo.PatientGynInfo.Para;
- }
- else
- {
- patientGynInfo = null;
- }
- ReportPatientOBInfoMessage patientObInfo;
- if (PatientInfo.PatientObInfo != null)
- {
- patientObInfo = MessagePool.GetMessage<ReportPatientOBInfoMessage>();
- patientObInfo.AB = PatientInfo.PatientObInfo.AB;
- patientObInfo.Ectopic = PatientInfo.PatientObInfo.Ectopic;
- patientObInfo.Gravida = PatientInfo.PatientObInfo.Gravida;
- patientObInfo.LMP = PatientInfo.PatientObInfo.LMP;
- patientObInfo.Para = PatientInfo.PatientObInfo.Para;
- patientObInfo.FetusNumber = PatientInfo.PatientObInfo.FetusNumber;
- patientObInfo.BBT = PatientInfo.PatientObInfo.BBT;
- patientObInfo.EstimateDueDate = PatientInfo.PatientObInfo.EstimateDueDate;
- patientObInfo.GestationalAge = PatientInfo.PatientObInfo.GestationalAge;
- patientObInfo.GestationalAgeOrigin = PatientInfo.PatientObInfo.GestationalAgeOrigin;
- }
- else
- {
- patientObInfo = null;
- }
- PatientAnimalDetailMessage patientAnimalDetailMessage = null;
- if (PatientInfo.AnimalDetail != null)
- {
- patientAnimalDetailMessage = MessagePool.GetMessage<PatientAnimalDetailMessage>();
- patientAnimalDetailMessage.Species = PatientInfo.AnimalDetail.Species;
- patientAnimalDetailMessage.Owner = PatientInfo.AnimalDetail.Owner;
- patientAnimalDetailMessage.Breed = PatientInfo.AnimalDetail.Breed;
- }
- var patientId = PatientInfo.PatientId;
- var firstName = PatientInfo.FirstName;
- var lastNamae = PatientInfo.LastName;
- patientInfoMessage = MessagePool.GetMessage<ReportPatientInfoMessage1>();
- patientInfoMessage.Id = string.Empty;
- patientInfoMessage.PatientId = patientId;
- patientInfoMessage.FirstName = firstName;
- patientInfoMessage.LastName = lastNamae;
- patientInfoMessage.Birthday = PatientInfo.Birthday;
- patientInfoMessage.IsMale = PatientInfo.IsMale;
- patientInfoMessage.Height = PatientInfo.Height;
- patientInfoMessage.Weight = PatientInfo.Weight;
- patientInfoMessage.PatientGynInfo = patientGynInfo;
- patientInfoMessage.PatientObInfo = patientObInfo;
- patientInfoMessage.PatientUrInfo = patientUrInfo;
- patientInfoMessage.AnimalDetail = patientAnimalDetailMessage;
- patientInfoMessage.Age = PatientInfo.Age;
- }
- return patientInfoMessage;
- }
- /// <summary>
- /// Delete this scanData and its files.
- /// </summary>
- public void Delete()
- {
- Status = UploadStatus.Deleted;
- }
- /// <summary>
- /// Force update the scan data's status.
- /// </summary>
- /// <param name="status">The new status</param>
- public void ForceUpdateStaus(UploadStatus status)
- {
- Status = status;
- }
- /// <summary>
- /// Force update and save the scan data.
- /// </summary>
- public void Update()
- {
- Save();
- }
- /// <summary>
- /// Get saved scan data file path
- /// </summary>
- /// <param name="status"></param>
- /// <returns></returns>
- private string GetScanDataFilePath(UploadStatus status)
- {
- if (status == UploadStatus.Deleted)
- {
- throw new InvalidOperationException("File already deleted.");
- }
- var subFolder = string.Empty;
- if (status == UploadStatus.Idle || status == UploadStatus.Waiting || status == UploadStatus.Uploading)
- {
- subFolder = "Upload";
- }
- if (status == UploadStatus.Fail)
- {
- subFolder = "Failed";
- }
- var folder = GetWorkingFolder(subFolder);
- var fileName = $"{Id}.sd";
- return Path.Combine(folder, fileName);
- }
- private string GetWorkingFolder(string subFolder)
- {
- var folder = Path.Combine(vCloudTerminal.WorkingFolder, "Remedical", subFolder);
- DirectoryHelper.CreateDirectory(folder);
- return folder;
- }
- /// <summary>
- /// Get the vid file path.
- /// </summary>
- /// <param name="status"></param>
- /// <returns></returns>
- private string GetVidFilePath(UploadStatus status)
- {
- if (status == UploadStatus.Deleted)
- {
- throw new InvalidOperationException("File already deleted.");
- }
- var subFolder = string.Empty;
- if (status == UploadStatus.Idle || status == UploadStatus.Waiting || status == UploadStatus.Uploading)
- {
- subFolder = "Upload";
- }
- if (status == UploadStatus.Fail)
- {
- subFolder = "Failed";
- }
- var folder = GetWorkingFolder(subFolder);
- var fileName = $"{Id}.vid";
- return Path.Combine(folder, fileName);
- }
- /// <summary>
- /// Delete the files
- /// </summary>
- /// <param name="status"></param>
- private void Delete(UploadStatus status)
- {
- var scanDataFilePath = GetScanDataFilePath(status);
- var vidFilePath = GetVidFilePath(status);
- FileHelper.DeleteFile(scanDataFilePath);
- FileHelper.DeleteFile(vidFilePath);
- }
- /// <summary>
- /// Save scan data and vid file to the disk by current status.
- /// </summary>
- private void Save()
- {
- ScanDataFilePath = GetScanDataFilePath(Status);
- this.Serialize(ScanDataFilePath);
- }
- /// <summary>
- /// Move scan data from one to another
- /// </summary>
- /// <param name="source"></param>
- /// <param name="dest"></param>
- private void Move(UploadStatus source, UploadStatus dest)
- {
- var sourceScanDataFilePath = GetScanDataFilePath(source);
- var sourceVidFilePath = GetVidFilePath(source);
- var destScanDataFilePath = GetScanDataFilePath(dest);
- var destVidFilePath = GetVidFilePath(dest);
- if (File.Exists(sourceScanDataFilePath) && !File.Exists(destScanDataFilePath))
- {
- File.Move(sourceScanDataFilePath, destScanDataFilePath);
- ScanDataFilePath = destScanDataFilePath;
- }
- if (File.Exists(sourceVidFilePath) && !File.Exists(destVidFilePath))
- {
- File.Move(sourceVidFilePath, destVidFilePath);
- VidFilePath = destVidFilePath;
- }
- }
- /// <summary>
- /// Upload the data to the vCloud server.
- /// </summary>
- public void Upload(StorageInfo storageInfo, CancellationTokenSource cancellationTokenSource)
- {
- if (cancellationTokenSource.IsCancellationRequested)
- {
- throw new OperationCanceledException("Cancelled");
- }
- var fileToken = UploadHelper.UploadFile(storageInfo, VidFilePath, string.Empty, null, cancellationTokenSource);
- if (cancellationTokenSource.IsCancellationRequested)
- {
- throw new OperationCanceledException("Cancelled");
- }
- if (string.IsNullOrWhiteSpace(fileToken))
- {
- throw new InvalidOperationException("Get token failed when Uploading file");
- }
- var patientInfoMessage = CreateReportPatientInfoMessage();
- var fileType = VidType.VinnoVidSingle;
- if (DataType == VidType.VinnoVidMovie)
- {
- fileType = VidType.VinnoVidMovie;
- }
- if (DataType == VidType.ThirdVidSingle)
- {
- fileType = VidType.ThirdVidSingle;
- }
- if (DataType == VidType.ThirdVidMovie)
- {
- fileType = VidType.ThirdVidMovie;
- }
- if (cancellationTokenSource.IsCancellationRequested)
- {
- throw new OperationCanceledException("Cancelled");
- }
- using (var request = MessagePool.GetMessage<AddRecordDataRequest1>())
- {
- request.TerminalRecordId = ExamRecordId;
- request.DataType = fileType;
- request.Application = Description;
- request.ImageFileToken = fileToken;
- request.PatientInfo = patientInfoMessage;
- var result = Leaf.Send(request);
- var success = AddRecordDataSuccess.Convert(result);
- if (success == null)
- {
- throw new InvalidOperationException("AddRecordDataRequest fail.");
- }
- }
- Status = UploadStatus.Uploaded;
- }
- private void DoDispose()
- {
- try
- {
- if (!_disposed)
- {
- //if not uploaded just save it to disk.
- if (Status != UploadStatus.Uploaded && Status != UploadStatus.Deleted)
- {
- Save();
- }
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"vCloudScanData: dispose upload data error:{ex}");
- }
- finally
- {
- _disposed = true;
- }
- }
- public void Dispose()
- {
- DoDispose();
- GC.SuppressFinalize(this);
- }
- }
- }
|