vStation.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Net.Http;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using Vinno.IUS.Common.Log;
  12. using Vinno.IUS.Common.Utilities;
  13. using Vinno.vCloud.Common.FIS.Remedicals;
  14. namespace Vinno.vCloud.Common.FIS.vStation
  15. {
  16. internal class Security
  17. {
  18. public static string Salt(string input)
  19. {
  20. return $"V1NN0{input}vStation";
  21. }
  22. public static string Hash(string input)
  23. {
  24. return HashEngine.GetHashString(input, Encoding.Unicode).ToLower();
  25. }
  26. }
  27. public class vStation : IvStation
  28. {
  29. private readonly ConcurrentDictionary<string, vStationVidData> _uploadingvStationVidDatas = new ConcurrentDictionary<string, vStationVidData>();
  30. private HttpClient _httpClient;
  31. private string _token;
  32. private bool _disposed;
  33. private CancellationTokenSource _cancellationTokenSource;
  34. private string _ipAddress;
  35. private string _port;
  36. /// <summary>
  37. ///senconds
  38. /// </summary>
  39. private int _timeOut;
  40. internal static string FolderPath;
  41. public string IpAddress
  42. {
  43. get => _ipAddress;
  44. set
  45. {
  46. if (_ipAddress != value)
  47. {
  48. _ipAddress = value;
  49. }
  50. }
  51. }
  52. public string Port
  53. {
  54. get => _port;
  55. set
  56. {
  57. if (_port != value)
  58. {
  59. _port = value;
  60. }
  61. }
  62. }
  63. public int TimeOut
  64. {
  65. get => _timeOut;
  66. set
  67. {
  68. if (_timeOut != value)
  69. {
  70. _timeOut = value;
  71. }
  72. }
  73. }
  74. /// <summary>
  75. /// Raised when a vStation Vid Data changed.
  76. /// </summary>
  77. public event EventHandler<vStationVidDataChangedEventArgs> vStationVidDataChanged;
  78. public vStation(string ipAddress, string port, int timeout, string folderPath = null)
  79. {
  80. _ipAddress = ipAddress;
  81. _port = port;
  82. _timeOut = timeout;
  83. if (string.IsNullOrEmpty(folderPath))
  84. {
  85. FolderPath = AppDomain.CurrentDomain.BaseDirectory;
  86. }
  87. else
  88. {
  89. FolderPath = folderPath;
  90. }
  91. _cancellationTokenSource = new CancellationTokenSource();
  92. }
  93. /// <inheritdoc />
  94. /// <summary>
  95. /// Gets all vStation Vid datas.
  96. /// </summary>
  97. /// <returns>The uploading and failed scan datas.</returns>
  98. public IEnumerable<vStationVidData> GetvStationVidDatas()
  99. {
  100. return _uploadingvStationVidDatas.Values;
  101. }
  102. public vStationVidData CreatevStationVidData(string filePath, UploadStatus status)
  103. {
  104. var id = Guid.NewGuid().ToString("N").ToUpper();
  105. var vStationVidData = new vStationVidData(id, filePath, status);
  106. return vStationVidData;
  107. }
  108. public void LoadvStationVidData()
  109. {
  110. var vStationFolder = Path.Combine(FolderPath, "vStation");
  111. if (Directory.Exists(vStationFolder))
  112. {
  113. foreach (var storeFolder in Directory.GetDirectories(vStationFolder))
  114. {
  115. var storeFiles = Directory.GetFiles(storeFolder, "*.vd");
  116. foreach (var storeFile in storeFiles)
  117. {
  118. if (string.IsNullOrEmpty(Path.GetFileNameWithoutExtension(storeFile)))
  119. {
  120. continue;
  121. }
  122. var vidData = vStationVidDataSerializer.Deserialize(storeFile);
  123. if (vidData != null)
  124. {
  125. _uploadingvStationVidDatas.AddOrUpdate(vidData.Id, a =>
  126. {
  127. OnvStationVidDataChanged(new vStationVidDataChangedEventArgs(vidData, ScanDataChangeType.Added));
  128. return vidData;
  129. }
  130. , (s, exist) =>
  131. {
  132. exist.StatusChanged -= OnvStationVidDataStatusChanged;
  133. exist.Dispose();
  134. OnvStationVidDataChanged(new vStationVidDataChangedEventArgs(vidData, ScanDataChangeType.Update));
  135. return vidData;
  136. });
  137. vidData.StatusChanged += OnvStationVidDataStatusChanged;
  138. }
  139. }
  140. }
  141. }
  142. }
  143. private void OnvStationVidDataChanged(vStationVidDataChangedEventArgs e)
  144. {
  145. vStationVidDataChanged?.Invoke(this, e);
  146. }
  147. private void OnvStationVidDataStatusChanged(object sender, EventArgs e)
  148. {
  149. var vidData = (vStationVidData)sender;
  150. OnvStationVidDataChanged(new vStationVidDataChangedEventArgs(vidData, ScanDataChangeType.Update));
  151. if (vidData.Status == UploadStatus.Uploaded)
  152. {
  153. if (_uploadingvStationVidDatas.TryRemove(vidData.Id, out var removedData))
  154. {
  155. removedData.StatusChanged -= OnvStationVidDataStatusChanged;
  156. OnvStationVidDataChanged(new vStationVidDataChangedEventArgs(removedData, ScanDataChangeType.Removed));
  157. removedData.Dispose();
  158. }
  159. }
  160. if (vidData.Status == UploadStatus.Deleted)
  161. {
  162. if (_uploadingvStationVidDatas.TryRemove(vidData.Id, out var removedData))
  163. {
  164. removedData.StatusChanged -= OnvStationVidDataStatusChanged;
  165. }
  166. }
  167. }
  168. private bool Connect()
  169. {
  170. try
  171. {
  172. if (_httpClient != null)
  173. {
  174. _httpClient.Dispose();
  175. _httpClient = null;
  176. }
  177. var validateString = $"Connect{DateTime.Now.ToString("yyyyMMdd")}";
  178. _httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(_timeOut) };
  179. var content = new StringContent(Security.Hash(Security.Salt(validateString)));
  180. content.Headers.Add("Action", "Connect");
  181. var response = Task.Run(() => _httpClient.PostAsync(new Uri($"http://{_ipAddress}:{_port}/"), content, _cancellationTokenSource.Token).GetAwaiter().GetResult()).GetAwaiter().GetResult();
  182. if (response.StatusCode == HttpStatusCode.OK)
  183. {
  184. var data = Task.Run(() => response.Content.ReadAsStringAsync().GetAwaiter().GetResult()).GetAwaiter().GetResult();
  185. var spliters = data.Split(';');
  186. if (spliters.Count() == 2 && spliters[0] == "ConnectSuccess")
  187. {
  188. _token = spliters[1];
  189. return true;
  190. }
  191. }
  192. }
  193. catch (Exception ex)
  194. {
  195. _httpClient?.Dispose();
  196. _httpClient = null;
  197. Logger.WriteLineError($"Connect to vStation server error: {ex}");
  198. }
  199. return false;
  200. }
  201. private bool Disconnect()
  202. {
  203. try
  204. {
  205. _httpClient?.Dispose();
  206. _httpClient = null;
  207. return true;
  208. }
  209. catch (Exception ex)
  210. {
  211. Logger.WriteLineError($"DisConnect to vStation server error: {ex}");
  212. }
  213. return false;
  214. }
  215. /// <summary>
  216. /// Test whether the network is ok or not.
  217. /// </summary>
  218. /// <returns></returns>
  219. public bool TestConnection()
  220. {
  221. try
  222. {
  223. var validateString = $"TestConnection{DateTime.Now.ToString("yyyyMMdd")}";
  224. if (_httpClient == null)
  225. {
  226. _httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(_timeOut) };
  227. }
  228. var content = new StringContent(Security.Hash(Security.Salt(validateString)));
  229. content.Headers.Add("Action", "TestConnection");
  230. var response = Task.Run(() => _httpClient.PostAsync(new Uri($"http://{_ipAddress}:{_port}/"), content, _cancellationTokenSource.Token).GetAwaiter().GetResult()).GetAwaiter().GetResult();
  231. if (response.StatusCode == HttpStatusCode.OK)
  232. {
  233. var data = Task.Run(() => response.Content.ReadAsStringAsync().GetAwaiter().GetResult()).GetAwaiter().GetResult();
  234. if (!string.IsNullOrEmpty(data) && data == Security.Hash(Security.Salt("TestConnectionSuccess")))
  235. {
  236. return true;
  237. }
  238. }
  239. }
  240. catch (Exception ex)
  241. {
  242. _httpClient?.Dispose();
  243. _httpClient = null;
  244. Logger.WriteLineError($"Connect to vStation error: {ex}");
  245. }
  246. return false;
  247. }
  248. public void UploadFiles(IEnumerable<vStationVidData> vidDatas)
  249. {
  250. try
  251. {
  252. foreach (var vidData in vidDatas)
  253. {
  254. _uploadingvStationVidDatas.AddOrUpdate(vidData.Id, a => vidData, (s, exist) =>
  255. {
  256. exist.StatusChanged -= OnvStationVidDataStatusChanged;
  257. exist.Dispose();
  258. return vidData;
  259. });
  260. vidData.StatusChanged += OnvStationVidDataStatusChanged;
  261. OnvStationVidDataChanged(new vStationVidDataChangedEventArgs(vidData, ScanDataChangeType.Added));
  262. }
  263. if (!Connect())
  264. {
  265. foreach (var vidData in vidDatas)
  266. {
  267. vidData.Status = UploadStatus.Fail;
  268. try
  269. {
  270. vidData.Update();
  271. }
  272. catch (Exception e)
  273. {
  274. Logger.WriteLineError($"update vStationVidData error, Id:{vidData.Id}, {e}");
  275. }
  276. }
  277. return;
  278. }
  279. foreach (var vidData in vidDatas)
  280. {
  281. UploadFile(vidData);
  282. }
  283. }
  284. catch (Exception ex)
  285. {
  286. Logger.WriteLineError($"upload vStationVidDatas error,{ex}");
  287. }
  288. finally
  289. {
  290. Disconnect();
  291. }
  292. }
  293. private bool UploadFile(vStationVidData vidData)
  294. {
  295. try
  296. {
  297. if (_httpClient == null)
  298. {
  299. Logger.WriteLineWarn("Upload file to vStation error: the httpclient not started.");
  300. return false;
  301. }
  302. vidData.Status = UploadStatus.Waiting;
  303. var content = new ByteArrayContent(File.ReadAllBytes(vidData.VidFilePath));
  304. content.Headers.Add("Token", _token);
  305. content.Headers.Add("Action", "UploadFile");
  306. vidData.Status = UploadStatus.Uploading;
  307. var response = Task.Run(() => _httpClient.PostAsync(new Uri($"http://{_ipAddress}:{_port}/"), content, _cancellationTokenSource.Token).GetAwaiter().GetResult()).GetAwaiter().GetResult();
  308. var result = CheckResult(response);
  309. if (!result)
  310. {
  311. vidData.Status = UploadStatus.Fail;
  312. try
  313. {
  314. vidData.Update();
  315. }
  316. catch (Exception e)
  317. {
  318. Logger.WriteLineError($"update vStationVidData error, Id:{vidData.Id}, {e}");
  319. }
  320. }
  321. else
  322. {
  323. vidData.Status = UploadStatus.Uploaded;
  324. }
  325. return result;
  326. }
  327. catch (Exception ex)
  328. {
  329. vidData.Status = UploadStatus.Fail;
  330. try
  331. {
  332. vidData.Update();
  333. }
  334. catch (Exception e)
  335. {
  336. Logger.WriteLineError($"update vStationVidData error, Id:{vidData.Id}, {e}");
  337. }
  338. Logger.WriteLineError($"Upload file to vStation error: {ex}");
  339. }
  340. return false;
  341. }
  342. private bool CheckResult(HttpResponseMessage response)
  343. {
  344. if (response.StatusCode == HttpStatusCode.OK)
  345. {
  346. var data = Task.Run(() => response.Content.ReadAsStringAsync().GetAwaiter().GetResult()).GetAwaiter().GetResult();
  347. if (data == Security.Hash(Security.Salt("200 - OK.")))
  348. {
  349. return true;
  350. }
  351. }
  352. return false;
  353. }
  354. private void DoDispose()
  355. {
  356. if (!_disposed)
  357. {
  358. _cancellationTokenSource?.Cancel();
  359. foreach (var vidData in _uploadingvStationVidDatas.Values.ToArray())
  360. {
  361. vidData.StatusChanged -= OnvStationVidDataStatusChanged;
  362. vidData.Dispose();
  363. if (_uploadingvStationVidDatas.TryRemove(vidData.Id, out var removedData))
  364. {
  365. OnvStationVidDataChanged(new vStationVidDataChangedEventArgs(removedData, ScanDataChangeType.Removed));
  366. }
  367. }
  368. _disposed = true;
  369. }
  370. }
  371. public void Dispose()
  372. {
  373. DoDispose();
  374. GC.SuppressFinalize(this);
  375. }
  376. }
  377. }