123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- using System;
- using System.IO;
- using Vinno.IUS.Common.Log;
- using Vinno.vCloud.Common.FIS.Remedicals;
- namespace Vinno.vCloud.Common.FIS.vStation
- {
- public class vStationVidData : IDisposable
- {
- private UploadStatus _status;
- private bool _disposed;
- /// <inheritdoc />
- /// <summary>
- /// Gets the id of this vStation Vid Data
- /// </summary>
- public string Id { get; }
- /// <inheritdoc />
- /// <summary>
- /// Gets the vid file path.
- /// </summary>
- public string VidFilePath { get; private set; }
- /// <summary>
- /// Gets the store file path.
- /// </summary>
- public string StoreFilePath { get; private set; }
- /// <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();
- }
- }
- }
- public vStationVidData(string id, string vidFilePath, UploadStatus status)
- {
- _status = status;
- Id = id;
- VidFilePath = vidFilePath;
- Save();
- }
- ~vStationVidData()
- {
- DoDispose();
- }
- private void OnStatusChanged()
- {
- StatusChanged?.Invoke(this, EventArgs.Empty);
- }
- public void Delete()
- {
- Status = UploadStatus.Deleted;
- }
- /// <summary>
- /// Force update the vStation VID data's status.
- /// </summary>
- /// <param name="status">The new status</param>
- public void ForceUpdateStatus(UploadStatus status)
- {
- Status = status;
- }
- public void Update()
- {
- Save();
- }
- private string GetStoreFilePath(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}.vd";
- return Path.Combine(folder, fileName);
- }
- private string GetWorkingFolder(string subFolder)
- {
- var folder = Path.Combine(vStation.FolderPath, "vStation", subFolder);
- if (!Directory.Exists(folder))
- {
- Directory.CreateDirectory(folder);
- }
- return folder;
- }
- private void Delete(UploadStatus status)
- {
- var storeFilePath = GetStoreFilePath(status);
- if (File.Exists(storeFilePath))
- {
- File.Delete(storeFilePath);
- }
- }
- private void Save()
- {
- StoreFilePath = GetStoreFilePath(Status);
- this.Serialize(StoreFilePath);
- }
- private void Move(UploadStatus source, UploadStatus dest)
- {
- var sourceStoreFilePath = GetStoreFilePath(source);
- var destStoreFilePath = GetStoreFilePath(dest);
- if (File.Exists(sourceStoreFilePath) && !File.Exists(destStoreFilePath))
- {
- File.Move(sourceStoreFilePath, destStoreFilePath);
- StoreFilePath = destStoreFilePath;
- }
- }
- 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);
- }
- }
- }
|