Log.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.Collections.Generic;
  3. using Vinno.IUS.Common.Log;
  4. using Vinno.IUS.Common.Network.Leaf;
  5. using Vinno.IUS.Common.Network.Transfer;
  6. using Vinno.vCloud.Protocol.Infrastructures;
  7. using Vinno.vCloud.Protocol.Messages.Terminal.Remedical.TerminialLog;
  8. namespace Vinno.vCloud.Common.FIS.Log
  9. {
  10. internal class Log : ILog
  11. {
  12. private readonly string _terminalId;
  13. private readonly ClientLeaf _leaf;
  14. private bool _disposed;
  15. /// <summary>
  16. /// Raised when the Upload Log Rule Changed;
  17. /// </summary>
  18. public event EventHandler<UploadLogRule> UploadLogRuleChanged;
  19. public Log(string terminalId, ClientLeaf leaf)
  20. {
  21. _terminalId = terminalId;
  22. _leaf = leaf;
  23. _leaf.MessageArrived += OnMessageArrived;
  24. }
  25. /// <summary>
  26. /// Get Upload Log Rule
  27. /// </summary>
  28. /// <returns>Upload Log Rule</returns>
  29. public UploadLogRule GetUploadLogRule()
  30. {
  31. using (var request = MessagePool.GetMessage<GetUploadLogRuleRequest>())
  32. {
  33. request.TerminalId = _terminalId;
  34. var result = _leaf.Send(request);
  35. var success = GetUploadLogRuleResult.Convert(result);
  36. if (success != null)
  37. {
  38. var logCategories = new List<DeviceLogCategory>();
  39. foreach (var logCategory in success.LogCategorys)
  40. {
  41. if (Enum.TryParse<DeviceLogCategory>(logCategory.Name, out var deviceLogCategory))
  42. {
  43. logCategories.Add(deviceLogCategory);
  44. }
  45. }
  46. return new UploadLogRule(success.UploadLogStatus, success.RuleType, logCategories);
  47. }
  48. }
  49. return null;
  50. }
  51. /// <summary>
  52. /// Upload Device Logs To Server
  53. /// </summary>
  54. /// <param name="deviceLogPlatform"></param>
  55. /// <param name="deviceLogs">Device logs</param>
  56. /// <returns>True:Upload Success/False:Upload Failed</returns>
  57. public bool UploadDeviceLogs(LogChannel deviceLogPlatform, IEnumerable<TerminalDeviceLogMessage> deviceLogs)
  58. {
  59. using (var request = MessagePool.GetMessage<UploadDeviceLogsRequest>())
  60. {
  61. request.TerminalId = _terminalId;
  62. request.DeviceLogPlatform = deviceLogPlatform;
  63. request.DeviceLogs = deviceLogs;
  64. var result = _leaf.Send(request);
  65. var success = UploadDeviceLogsResult.Convert(result);
  66. if (success != null && success.IsSuccess)
  67. {
  68. return true;
  69. }
  70. }
  71. return false;
  72. }
  73. private void OnMessageArrived(object sender, Message e)
  74. {
  75. var uploadLogNotification = UploadLogNotification.Convert(e);
  76. if (uploadLogNotification != null)
  77. {
  78. HandleUploadLogNotification(uploadLogNotification);
  79. }
  80. }
  81. private void HandleUploadLogNotification(UploadLogNotification uploadLogNotification)
  82. {
  83. Logger.WriteLineInfo("UploadLogNotification Arrived ");
  84. UploadLogRuleChanged?.Invoke(null, new UploadLogRule(uploadLogNotification.UploadLogStatus, uploadLogNotification.RuleType, uploadLogNotification.LogCategorys));
  85. }
  86. private void DoDispose()
  87. {
  88. if (!_disposed)
  89. {
  90. _leaf.MessageArrived -= OnMessageArrived;
  91. _disposed = true;
  92. }
  93. }
  94. public void Dispose()
  95. {
  96. DoDispose();
  97. GC.SuppressFinalize(this);
  98. }
  99. }
  100. }