123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- using System;
- using System.Collections.Generic;
- using Vinno.IUS.Common.Log;
- using Vinno.IUS.Common.Network.Leaf;
- using Vinno.IUS.Common.Network.Transfer;
- using Vinno.vCloud.Protocol.Infrastructures;
- using Vinno.vCloud.Protocol.Messages.Terminal.Remedical.TerminialLog;
- namespace Vinno.vCloud.Common.FIS.Log
- {
- internal class Log : ILog
- {
- private readonly string _terminalId;
- private readonly ClientLeaf _leaf;
- private bool _disposed;
- /// <summary>
- /// Raised when the Upload Log Rule Changed;
- /// </summary>
- public event EventHandler<UploadLogRule> UploadLogRuleChanged;
- public Log(string terminalId, ClientLeaf leaf)
- {
- _terminalId = terminalId;
- _leaf = leaf;
- _leaf.MessageArrived += OnMessageArrived;
- }
- /// <summary>
- /// Get Upload Log Rule
- /// </summary>
- /// <returns>Upload Log Rule</returns>
- public UploadLogRule GetUploadLogRule()
- {
- using (var request = MessagePool.GetMessage<GetUploadLogRuleRequest>())
- {
- request.TerminalId = _terminalId;
- var result = _leaf.Send(request);
- var success = GetUploadLogRuleResult.Convert(result);
- if (success != null)
- {
- var logCategories = new List<DeviceLogCategory>();
- foreach (var logCategory in success.LogCategorys)
- {
- if (Enum.TryParse<DeviceLogCategory>(logCategory.Name, out var deviceLogCategory))
- {
- logCategories.Add(deviceLogCategory);
- }
- }
- return new UploadLogRule(success.UploadLogStatus, success.RuleType, logCategories);
- }
- }
- return null;
- }
- /// <summary>
- /// Upload Device Logs To Server
- /// </summary>
- /// <param name="deviceLogPlatform"></param>
- /// <param name="deviceLogs">Device logs</param>
- /// <returns>True:Upload Success/False:Upload Failed</returns>
- public bool UploadDeviceLogs(LogChannel deviceLogPlatform, IEnumerable<TerminalDeviceLogMessage> deviceLogs)
- {
- using (var request = MessagePool.GetMessage<UploadDeviceLogsRequest>())
- {
- request.TerminalId = _terminalId;
- request.DeviceLogPlatform = deviceLogPlatform;
- request.DeviceLogs = deviceLogs;
- var result = _leaf.Send(request);
- var success = UploadDeviceLogsResult.Convert(result);
- if (success != null && success.IsSuccess)
- {
- return true;
- }
- }
- return false;
- }
- private void OnMessageArrived(object sender, Message e)
- {
- var uploadLogNotification = UploadLogNotification.Convert(e);
- if (uploadLogNotification != null)
- {
- HandleUploadLogNotification(uploadLogNotification);
- }
- }
- private void HandleUploadLogNotification(UploadLogNotification uploadLogNotification)
- {
- Logger.WriteLineInfo("UploadLogNotification Arrived ");
- UploadLogRuleChanged?.Invoke(null, new UploadLogRule(uploadLogNotification.UploadLogStatus, uploadLogNotification.RuleType, uploadLogNotification.LogCategorys));
- }
- private void DoDispose()
- {
- if (!_disposed)
- {
- _leaf.MessageArrived -= OnMessageArrived;
- _disposed = true;
- }
- }
- public void Dispose()
- {
- DoDispose();
- GC.SuppressFinalize(this);
- }
- }
- }
|