123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407 |
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using Vinno.IUS.Common.Log;
- using Vinno.IUS.Common.Utilities;
- using Vinno.vCloud.Common.FIS.Remedicals;
- namespace Vinno.vCloud.Common.FIS.vStation
- {
- internal class Security
- {
- public static string Salt(string input)
- {
- return $"V1NN0{input}vStation";
- }
- public static string Hash(string input)
- {
- return HashEngine.GetHashString(input, Encoding.Unicode).ToLower();
- }
- }
- public class vStation : IvStation
- {
- private readonly ConcurrentDictionary<string, vStationVidData> _uploadingvStationVidDatas = new ConcurrentDictionary<string, vStationVidData>();
- private HttpClient _httpClient;
- private string _token;
- private bool _disposed;
- private CancellationTokenSource _cancellationTokenSource;
- private string _ipAddress;
- private string _port;
- /// <summary>
- ///senconds
- /// </summary>
- private int _timeOut;
- internal static string FolderPath;
- public string IpAddress
- {
- get => _ipAddress;
- set
- {
- if (_ipAddress != value)
- {
- _ipAddress = value;
- }
- }
- }
- public string Port
- {
- get => _port;
- set
- {
- if (_port != value)
- {
- _port = value;
- }
- }
- }
- public int TimeOut
- {
- get => _timeOut;
- set
- {
- if (_timeOut != value)
- {
- _timeOut = value;
- }
- }
- }
- /// <summary>
- /// Raised when a vStation Vid Data changed.
- /// </summary>
- public event EventHandler<vStationVidDataChangedEventArgs> vStationVidDataChanged;
- public vStation(string ipAddress, string port, int timeout, string folderPath = null)
- {
- _ipAddress = ipAddress;
- _port = port;
- _timeOut = timeout;
- if (string.IsNullOrEmpty(folderPath))
- {
- FolderPath = AppDomain.CurrentDomain.BaseDirectory;
- }
- else
- {
- FolderPath = folderPath;
- }
- _cancellationTokenSource = new CancellationTokenSource();
- }
- /// <inheritdoc />
- /// <summary>
- /// Gets all vStation Vid datas.
- /// </summary>
- /// <returns>The uploading and failed scan datas.</returns>
- public IEnumerable<vStationVidData> GetvStationVidDatas()
- {
- return _uploadingvStationVidDatas.Values;
- }
- public vStationVidData CreatevStationVidData(string filePath, UploadStatus status)
- {
- var id = Guid.NewGuid().ToString("N").ToUpper();
- var vStationVidData = new vStationVidData(id, filePath, status);
- return vStationVidData;
- }
- public void LoadvStationVidData()
- {
- var vStationFolder = Path.Combine(FolderPath, "vStation");
- if (Directory.Exists(vStationFolder))
- {
- foreach (var storeFolder in Directory.GetDirectories(vStationFolder))
- {
- var storeFiles = Directory.GetFiles(storeFolder, "*.vd");
- foreach (var storeFile in storeFiles)
- {
- if (string.IsNullOrEmpty(Path.GetFileNameWithoutExtension(storeFile)))
- {
- continue;
- }
- var vidData = vStationVidDataSerializer.Deserialize(storeFile);
- if (vidData != null)
- {
- _uploadingvStationVidDatas.AddOrUpdate(vidData.Id, a =>
- {
- OnvStationVidDataChanged(new vStationVidDataChangedEventArgs(vidData, ScanDataChangeType.Added));
- return vidData;
- }
- , (s, exist) =>
- {
- exist.StatusChanged -= OnvStationVidDataStatusChanged;
- exist.Dispose();
- OnvStationVidDataChanged(new vStationVidDataChangedEventArgs(vidData, ScanDataChangeType.Update));
- return vidData;
- });
- vidData.StatusChanged += OnvStationVidDataStatusChanged;
- }
- }
- }
- }
- }
- private void OnvStationVidDataChanged(vStationVidDataChangedEventArgs e)
- {
- vStationVidDataChanged?.Invoke(this, e);
- }
- private void OnvStationVidDataStatusChanged(object sender, EventArgs e)
- {
- var vidData = (vStationVidData)sender;
- OnvStationVidDataChanged(new vStationVidDataChangedEventArgs(vidData, ScanDataChangeType.Update));
- if (vidData.Status == UploadStatus.Uploaded)
- {
- if (_uploadingvStationVidDatas.TryRemove(vidData.Id, out var removedData))
- {
- removedData.StatusChanged -= OnvStationVidDataStatusChanged;
- OnvStationVidDataChanged(new vStationVidDataChangedEventArgs(removedData, ScanDataChangeType.Removed));
- removedData.Dispose();
- }
- }
- if (vidData.Status == UploadStatus.Deleted)
- {
- if (_uploadingvStationVidDatas.TryRemove(vidData.Id, out var removedData))
- {
- removedData.StatusChanged -= OnvStationVidDataStatusChanged;
- }
- }
- }
- private bool Connect()
- {
- try
- {
- if (_httpClient != null)
- {
- _httpClient.Dispose();
- _httpClient = null;
- }
- var validateString = $"Connect{DateTime.Now.ToString("yyyyMMdd")}";
- _httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(_timeOut) };
- var content = new StringContent(Security.Hash(Security.Salt(validateString)));
- content.Headers.Add("Action", "Connect");
- var response = Task.Run(() => _httpClient.PostAsync(new Uri($"http://{_ipAddress}:{_port}/"), content, _cancellationTokenSource.Token).GetAwaiter().GetResult()).GetAwaiter().GetResult();
- if (response.StatusCode == HttpStatusCode.OK)
- {
- var data = Task.Run(() => response.Content.ReadAsStringAsync().GetAwaiter().GetResult()).GetAwaiter().GetResult();
- var spliters = data.Split(';');
- if (spliters.Count() == 2 && spliters[0] == "ConnectSuccess")
- {
- _token = spliters[1];
- return true;
- }
- }
- }
- catch (Exception ex)
- {
- _httpClient?.Dispose();
- _httpClient = null;
- Logger.WriteLineError($"Connect to vStation server error: {ex}");
- }
- return false;
- }
- private bool Disconnect()
- {
- try
- {
- _httpClient?.Dispose();
- _httpClient = null;
- return true;
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"DisConnect to vStation server error: {ex}");
- }
- return false;
- }
- /// <summary>
- /// Test whether the network is ok or not.
- /// </summary>
- /// <returns></returns>
- public bool TestConnection()
- {
- try
- {
- var validateString = $"TestConnection{DateTime.Now.ToString("yyyyMMdd")}";
- if (_httpClient == null)
- {
- _httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(_timeOut) };
- }
- var content = new StringContent(Security.Hash(Security.Salt(validateString)));
- content.Headers.Add("Action", "TestConnection");
- var response = Task.Run(() => _httpClient.PostAsync(new Uri($"http://{_ipAddress}:{_port}/"), content, _cancellationTokenSource.Token).GetAwaiter().GetResult()).GetAwaiter().GetResult();
- if (response.StatusCode == HttpStatusCode.OK)
- {
- var data = Task.Run(() => response.Content.ReadAsStringAsync().GetAwaiter().GetResult()).GetAwaiter().GetResult();
- if (!string.IsNullOrEmpty(data) && data == Security.Hash(Security.Salt("TestConnectionSuccess")))
- {
- return true;
- }
- }
- }
- catch (Exception ex)
- {
- _httpClient?.Dispose();
- _httpClient = null;
- Logger.WriteLineError($"Connect to vStation error: {ex}");
- }
- return false;
- }
- public void UploadFiles(IEnumerable<vStationVidData> vidDatas)
- {
- try
- {
- foreach (var vidData in vidDatas)
- {
- _uploadingvStationVidDatas.AddOrUpdate(vidData.Id, a => vidData, (s, exist) =>
- {
- exist.StatusChanged -= OnvStationVidDataStatusChanged;
- exist.Dispose();
- return vidData;
- });
- vidData.StatusChanged += OnvStationVidDataStatusChanged;
- OnvStationVidDataChanged(new vStationVidDataChangedEventArgs(vidData, ScanDataChangeType.Added));
- }
- if (!Connect())
- {
- foreach (var vidData in vidDatas)
- {
- vidData.Status = UploadStatus.Fail;
- try
- {
- vidData.Update();
- }
- catch (Exception e)
- {
- Logger.WriteLineError($"update vStationVidData error, Id:{vidData.Id}, {e}");
- }
- }
- return;
- }
- foreach (var vidData in vidDatas)
- {
- UploadFile(vidData);
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"upload vStationVidDatas error,{ex}");
- }
- finally
- {
- Disconnect();
- }
- }
- private bool UploadFile(vStationVidData vidData)
- {
- try
- {
- if (_httpClient == null)
- {
- Logger.WriteLineWarn("Upload file to vStation error: the httpclient not started.");
- return false;
- }
- vidData.Status = UploadStatus.Waiting;
- var content = new ByteArrayContent(File.ReadAllBytes(vidData.VidFilePath));
- content.Headers.Add("Token", _token);
- content.Headers.Add("Action", "UploadFile");
- vidData.Status = UploadStatus.Uploading;
- var response = Task.Run(() => _httpClient.PostAsync(new Uri($"http://{_ipAddress}:{_port}/"), content, _cancellationTokenSource.Token).GetAwaiter().GetResult()).GetAwaiter().GetResult();
- var result = CheckResult(response);
- if (!result)
- {
- vidData.Status = UploadStatus.Fail;
- try
- {
- vidData.Update();
- }
- catch (Exception e)
- {
- Logger.WriteLineError($"update vStationVidData error, Id:{vidData.Id}, {e}");
- }
- }
- else
- {
- vidData.Status = UploadStatus.Uploaded;
- }
- return result;
- }
- catch (Exception ex)
- {
- vidData.Status = UploadStatus.Fail;
- try
- {
- vidData.Update();
- }
- catch (Exception e)
- {
- Logger.WriteLineError($"update vStationVidData error, Id:{vidData.Id}, {e}");
- }
- Logger.WriteLineError($"Upload file to vStation error: {ex}");
- }
- return false;
- }
- private bool CheckResult(HttpResponseMessage response)
- {
- if (response.StatusCode == HttpStatusCode.OK)
- {
- var data = Task.Run(() => response.Content.ReadAsStringAsync().GetAwaiter().GetResult()).GetAwaiter().GetResult();
- if (data == Security.Hash(Security.Salt("200 - OK.")))
- {
- return true;
- }
- }
- return false;
- }
- private void DoDispose()
- {
- if (!_disposed)
- {
- _cancellationTokenSource?.Cancel();
- foreach (var vidData in _uploadingvStationVidDatas.Values.ToArray())
- {
- vidData.StatusChanged -= OnvStationVidDataStatusChanged;
- vidData.Dispose();
- if (_uploadingvStationVidDatas.TryRemove(vidData.Id, out var removedData))
- {
- OnvStationVidDataChanged(new vStationVidDataChangedEventArgs(removedData, ScanDataChangeType.Removed));
- }
- }
- _disposed = true;
- }
- }
- public void Dispose()
- {
- DoDispose();
- GC.SuppressFinalize(this);
- }
- }
- }
|