123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625 |
- using JsonRpcLite.Network;
- using JsonRpcLite.Rpc;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using Vinno.IUS.Common.Log;
- using Vinno.vCloud.Common.FIS.AfterSales;
- using Vinno.vCloud.Common.FIS.FLYINSONOLogin;
- using Vinno.vCloud.Common.FIS.Helper;
- using Vinno.vCloud.Common.FIS.LiveVideos;
- using Vinno.vCloud.Common.FIS.Notification;
- using Vinno.vCloud.Common.FIS.Remedicals;
- using WingInterfaceLibrary.Enum;
- using WingInterfaceLibrary.Enum.NotificationEnum;
- using WingInterfaceLibrary.Interface;
- using WingInterfaceLibrary.Notifications;
- using WingInterfaceLibrary.Request;
- using WingInterfaceLibrary.Request.Device;
- using WingInterfaceLibrary.Result.Device;
- namespace Vinno.vCloud.Common.FIS
- {
- internal class vCloudTerminalV2 : IvCloudTerminalV2
- {
- private readonly Dictionary<TerminalFeatureType, object> _features = new Dictionary<TerminalFeatureType, object>();
- private readonly ConnectionInfo _connectionInfo;
- private readonly string _deviceName;
- private readonly string _password;
- private readonly bool _isUserDefined;
- private readonly LoginSource _loginSource;
- private readonly Platform _platform;
- private readonly string _deviceType;
- private readonly string _url;
- private readonly int _usScreenWidth;
- private readonly int _usScreenHeight;
- private readonly bool _isUseHttps;
- private string _token;
- private int _connectionCheckCycle;
- private string _prefix;
- private JsonRpcClient _client;
- private JsonRpcHttpClientEngine _clientEngine;
- private IConnectService _connectService;
- private IVinnoServerService _vinnoServerService;
- private IDeviceService _deviceService;
- private TerminalStatus _status;
- private ConnectionCheckerV2 _connectionChecker;
- private HeartRateKeeperV2 _heartRateKeeper;
- private bool _disposed;
- private int _reconnectCounter;
- private FISWebSocket _webSocket;
- /// <summary>
- /// Raised when the status is changed.
- /// </summary>
- public event EventHandler StatusChanged;
- /// <summary>
- /// Gets the unique id.
- /// </summary>
- public string UniqueId { get; private set; }
- /// <inheritdoc />
- /// <summary>
- /// Gets the status of the Terminal
- /// </summary>
- /// <remarks>
- /// </remarks>
- public TerminalStatus Status
- {
- get => _status;
- private set
- {
- if (_status != value)
- {
- _status = value;
- OnStatusChanged();
- }
- }
- }
- public string TerminalName => _deviceName;
- public string TerminalMode => _connectionInfo.DeviceMode;
- /// <summary>
- /// Gets the global remedical working folder.
- /// </summary>
- internal static string WorkingFolder { get; private set; }
- public vCloudTerminalV2(ConnectionInfo connectionInfo, bool isUseHttps = false)
- {
- _usScreenHeight = connectionInfo.USScreenHeight;
- _usScreenWidth = connectionInfo.USScreenWidth;
- _isUseHttps = isUseHttps;
- _prefix = "http://";
- if (_isUseHttps)
- {
- _prefix = "https://";
- }
- _connectionInfo = connectionInfo;
- _deviceName = connectionInfo.Account.Name;
- _password = connectionInfo.Account.Password;
- _isUserDefined = connectionInfo.Account.IsUserDefined;
- _url = connectionInfo.ServerUrl;
- _loginSource = connectionInfo.LoginSource;
- //Set the default value 10 seconds.
- _connectionCheckCycle = 10;
- _status = TerminalStatus.Offline;
- WorkingFolder = connectionInfo.FISFolder;
- if (!Directory.Exists(WorkingFolder))
- {
- Directory.CreateDirectory(WorkingFolder);
- }
- CreateJsonrpcClient();
- CreateConnectionKeeper();
- }
- /// <inheritdoc />
- /// <summary>
- /// Get the feature instance by feature enum.
- /// </summary>
- /// <typeparam name="T">The interface of the feature</typeparam>
- /// <param name="featureType">The feature type</param>
- /// <returns>The instance of the feature</returns>
- /// <remarks>
- /// If the feature doesn't exist, this method will return null.
- /// </remarks>
- public T GetFeature<T>(TerminalFeatureType featureType) where T : IFeatureV2
- {
- if (_features.ContainsKey(featureType))
- {
- var feature = _features[featureType];
- if (feature is T t)
- {
- return t;
- }
- }
- return default(T);
- }
- /// <summary>
- /// Connect to server.
- /// </summary>
- internal void Connect()
- {
- try
- {
- var connectRequest = new ConnectRequest
- {
- DeviceUniqueCode = _deviceName,
- Password = _password,
- DeviceModel = _connectionInfo.DeviceMode,
- DeviceType = _connectionInfo.DeviceType.ToString(),
- SoftwareVersion = _connectionInfo.SoftwareVersion,
- SystemVersion = _connectionInfo.USOS,
- CPUModel = _connectionInfo.USCPU,
- SystemLanguage = _connectionInfo.LanguageName,
- Description = "",
- Name = "",
- OrganizationCode = "",
- DepartmentCode = "",
- LoginSource = _connectionInfo.LoginSource,
- Platform = _connectionInfo.Platform,
- };
- ConnectResult result = JsonRpcHelper.Connect(_connectService, connectRequest);
- if (result == null)
- {
- throw new InvalidDataException("JsonRPCHelper Connect Result is null");
- }
- else
- {
- UniqueId = result.UniqueCode;
- _token = result.Token;
- HandleLoginResult();
- }
- }
- catch (Exception e)
- {
- Logger.WriteLineError($"Terminal {_deviceName} login url:{_url} failed {e}");
- Status = TerminalStatus.LoginFailed;
- }
- }
- public void Disconnect()
- {
- var status = TerminalStatus.Logoff;
- try
- {
- var tokenRequest = new TokenRequest
- {
- Token = _token,
- };
- bool result = JsonRpcHelper.Disconnect(_connectService, tokenRequest);
- if (!result)
- {
- throw new Exception($"JsonRPCHelper Disconnect Result is fail");
- }
- else if (result)
- {
- status = TerminalStatus.Logoff;
- }
- }
- catch (Exception e)
- {
- Logger.WriteLineError($"Disconnect terminal {_deviceName} error {e}");
- }
- finally
- {
- Release();
- FLYINSONOUserManager.Instance.Clear();
- Status = status;
- }
- }
- /// <summary>
- /// Update enabled feature types.
- /// </summary>
- /// <param name="enabledFeatureTypes">The enabled feature types.</param>
- public void UpdateFeatures(IEnumerable<TerminalFeatureType> enabledFeatureTypes)
- {
- var removedFeatures = _features.Keys.Where(f => !enabledFeatureTypes.Contains(f)).ToList();
- foreach (var feature in removedFeatures)
- {
- var disposable = _features[feature] as IDisposable;
- disposable?.Dispose();
- _features.Remove(feature);
- }
- foreach (var enabledFeature in enabledFeatureTypes)
- {
- if (!_features.ContainsKey(enabledFeature))
- {
- var feature = GetFeature(enabledFeature);
- _features.Add(enabledFeature, feature);
- }
- }
- }
- /// <summary>
- /// Get Device Is Encrypted Show
- /// </summary>
- /// <param name="isEncryptedShow"></param>
- /// <returns></returns>
- public bool IsEncryptedShow()
- {
- try
- {
- var tokenRequest = new TokenRequest
- {
- Token = _token,
- };
- var cacheDeviceDTO = JsonRpcHelper.GetDeviceByToken(_connectService, tokenRequest);
- if (cacheDeviceDTO == null)
- {
- throw new InvalidOperationException("JsonRPCHelper GetDeviceByToken Result is null");
- }
- else
- {
- return cacheDeviceDTO.IsEncryptedShow;
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"IsEncryptedShow Error:{ex}");
- return false;
- }
- }
- /// <summary>
- /// Set Device Is Encrypted Show
- /// </summary>
- /// <param name="isEncryptedShow"></param>
- /// <returns></returns>
- public bool SetIsEncryptedShow(bool isEncryptedShow)
- {
- try
- {
- var setDeviceIsEncryptedShowRequest = new SetDeviceIsEncryptedShowRequest
- {
- IsEncryptedShow = isEncryptedShow,
- Token = _token
- };
- bool result = JsonRpcHelper.SetDeviceIsEncryptedShow(_connectService, setDeviceIsEncryptedShowRequest);
- if (!result)
- {
- throw new Exception($"JsonRPCHelper SetDeviceIsEncryptedShowAsync Result is false");
- }
- else
- {
- return true; ;
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"SetDeviceIsEncryptedShow Error:{ex}");
- return false;
- }
- }
- public void Dispose()
- {
- if (!_disposed)
- {
- Release();
- _disposed = true;
- }
- GC.SuppressFinalize(this);
- }
- private void HandleLoginResult()
- {
- Status = TerminalStatus.Logoning;
- var tokenRequest = new TokenRequest
- {
- Token = _token
- };
- var result = JsonRpcHelper.QueryServerConfig(_deviceService, tokenRequest);
- if (result == null)
- {
- throw new InvalidDataException($"JsonRPCHelper QueryServerConfig Result is null");
- }
- else
- {
- vCloudServerConfig.Instance.IsUploadThumbnail = result.IsUploadThumbnail;
- vCloudServerConfig.Instance.PatientType = result.PatientType;
- vCloudServerConfig.Instance.HeartRateSeconds = result.HeartRateSeconds;
- vCloudServerConfig.Instance.NotificationUrl = result.NotificationUrl;
- vCloudServerConfig.Instance.MergedChannel = result.MergedChannel;
- Logger.WriteLineInfo($"vCloudServerConfig:{vCloudServerConfig.Instance}");
- if (result.ServerConfigList != null)
- {
- foreach (var kp in result.ServerConfigList)
- {
- Logger.WriteLineInfo($"ServerConfigList Key:{kp.Key},Value:{kp.Value} ");
- }
- }
- }
- if (IsEncryptedShow())
- {
- Logger.WriteLineInfo("Start SetIsEncryptedShow False");
- SetIsEncryptedShow(false);
- Logger.WriteLineInfo("SetIsEncryptedShow False Finish");
- }
- CreateHeartRateKeeper();
- CreateWebSocket();
- ReleaseFeatures();
- UpdateFeatures(_connectionInfo.EnabledFeatures);
- _reconnectCounter = 0;
- Status = TerminalStatus.Logon;
- Logger.WriteLineInfo($"{_deviceName} Login url:{_url} successed, Token:{_token}, UniqueId:{UniqueId}");
- }
- private void CreateWebSocket()
- {
- if (string.IsNullOrWhiteSpace(vCloudServerConfig.Instance.NotificationUrl))
- {
- Logger.WriteLineError($"CreateWebSocket Error Because The Notificaion Url is null");
- return;
- }
- if (_webSocket == null)
- {
- var uri = vCloudServerConfig.Instance.NotificationUrl.Replace("{0}", _token);
- uri = uri.Replace("{1}", "0");
- Logger.WriteLineInfo($"Websocket uri :{uri}");
- _webSocket = new FISWebSocket(uri);
- _webSocket.NotificationReceived += OnNoitificationReceived;
- _webSocket.Connect();
- }
- }
- private void DisposeWebSocket()
- {
- try
- {
- if (_webSocket != null)
- {
- _webSocket.NotificationReceived -= OnNoitificationReceived;
- _webSocket.Dispose();
- _webSocket = null;
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"VCloudTerminalV2 Dispose WebSocket Error:{ex}");
- }
- }
- private void OnNoitificationReceived(object sender, NotificationArgs e)
- {
- switch (e.NotificationType)
- {
- case NotificationTypeEnum.ConnectionNotification:
- Logger.WriteLineInfo($"VCloudTerminalV2 ConnectionNotification Receive");
- break;
- case NotificationTypeEnum.DisconnectNotification:
- Logger.WriteLineInfo($"VCloudTerminalV2 DisconnectNotification Receive");
- break;
- case NotificationTypeEnum.DeviceControlledParametersNotification:
- try
- {
- if (e.Params is DeviceControlledParametersNotification deviceControlledParametersNotification)
- {
- HandleDeviceControlledParametersNotification(deviceControlledParametersNotification);
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"vCloudTerminalV2 Handle DeviceControlledParametersNotification Error:{ex}");
- }
- break;
- }
- }
- private void HandleDeviceControlledParametersNotification(DeviceControlledParametersNotification deviceControlledParametersNotification)
- {
- if (deviceControlledParametersNotification.ControlType == ControlDeviceParameterEnum.Start)
- {
- Logger.WriteLineInfo($"VCloudTerminalV2 DeviceControlledParametersNotification StartControl Receive");
- var afterSales = GetFeature<IAfterSalesV2>(TerminalFeatureType.AfterSales);
- if (afterSales != null)
- {
- afterSales.UpdateAfterSalesInfo(deviceControlledParametersNotification.ControlUserCode, deviceControlledParametersNotification.ControlUserName);
- }
- else
- {
- var remoteControlRequest = new RemoteControlRequest
- {
- Token = _token,
- ControlUserCode = deviceControlledParametersNotification.ControlUserCode,
- };
- var result = JsonRpcHelper.RejectRemoteControl(_deviceService, remoteControlRequest);
- if (result)
- {
- Logger.WriteLineInfo($"VCloudTerminalV2 RejectRemoteControl Success");
- }
- else
- {
- Logger.WriteLineError($"VCloudTerminalV2 RejectRemoteControl Failed");
- }
- }
- }
- }
- private void CreateHeartRateKeeper()
- {
- if (_heartRateKeeper == null)
- {
- _heartRateKeeper = new HeartRateKeeperV2(_deviceService, _token, vCloudServerConfig.Instance.HeartRateSeconds);
- _heartRateKeeper.Start();
- }
- }
- private void DisposeHeartRateKeeper()
- {
- try
- {
- if (_heartRateKeeper != null)
- {
- _heartRateKeeper.Stop();
- _heartRateKeeper = null;
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"vCloudTerminalV2 DisposeHeartRateKeeper Error:{ex}");
- }
- }
- private void OnStatusChanged()
- {
- Logger.WriteLineInfo($"VCloudTerminalV2 Status Changed:{_status}");
- StatusChanged?.Invoke(this, EventArgs.Empty);
- }
- private void CreateConnectionKeeper()
- {
- _connectionChecker = new ConnectionCheckerV2(_vinnoServerService, _connectionCheckCycle);
- var defaultStatus = false;
- if (!_connectionChecker.Check())
- {
- Status = TerminalStatus.Offline;
- }
- else
- {
- Status = TerminalStatus.Online;
- defaultStatus = true;
- }
- if (_connectionChecker != null)//当网络异常或者Server无法访问时,_connectionChecker.Check()一般会消耗十几秒,此时用户可能已经修改服务器地址,并重新登录,此时_connectionChecker已经被Dispose
- {
- _connectionChecker.Offlined += OnOfflined;
- _connectionChecker.Start(defaultStatus);
- }
- }
- private void DisposeConnectionKeeper()
- {
- try
- {
- if (_connectionChecker != null)
- {
- _connectionChecker.Offlined -= OnOfflined;
- _connectionChecker.Stop();
- _connectionChecker = null;
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"VCloudTerminalV2 Dispose WebSocket Error:{ex}");
- }
- }
- private void OnOfflined(object sender, EventArgs e)
- {
- Release();
- Status = TerminalStatus.Offline;
- //No need to reconnect after instance release
- if (_disposed) return;
- //Reconnect
- if (_reconnectCounter < 5)
- {
- if (!string.IsNullOrEmpty(_deviceName) && !string.IsNullOrEmpty(_password))
- {
- CreateJsonrpcClient();
- CreateConnectionKeeper();
- if (Status == TerminalStatus.Online)
- {
- Status = TerminalStatus.Reconnecting;
- Connect();
- _reconnectCounter++;
- }
- }
- }
- else
- {
- Dispose();
- }
- }
- private void Release()
- {
- DisposeConnectionKeeper();
- DisposeHeartRateKeeper();
- DisposeWebSocket();
- ReleaseFeatures();
- ReleaseJsonrpcClient();
- }
- private void CreateJsonrpcClient()
- {
- try
- {
- _client = new JsonRpcClient();
- _clientEngine = new JsonRpcHttpClientEngine($"{_prefix}{_url}");
- _client.UseEngine(_clientEngine);
- _connectService = _client.CreateProxy<IConnectService>();
- _vinnoServerService = _client.CreateProxy<IVinnoServerService>();
- _deviceService = _client.CreateProxy<IDeviceService>();
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"VCloudTerminalV2 CreateJsonrpcClient Error:{ex}");
- }
- }
- private void ReleaseJsonrpcClient()
- {
- try
- {
- if (_client != null)
- {
- _client.Dispose();
- _client = null;
- _clientEngine = null;
- _connectService = null;
- _vinnoServerService = null;
- _deviceService = null;
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"VCloudTerminal V2 ReleaseJsonrpcClient Error:{ex}");
- }
- }
- private void ReleaseFeatures()
- {
- foreach (var type in _features.Keys.ToList())
- {
- var feature = _features[type];
- var disposable = feature as IDisposable;
- disposable?.Dispose();
- _features.Remove(type);
- }
- }
- private IFeatureV2 GetFeature(TerminalFeatureType terminalFeatureType)
- {
- switch (terminalFeatureType)
- {
- case TerminalFeatureType.Remedical:
- return new RemedicalV2(_token, _client);
- case TerminalFeatureType.LiveVideo:
- if (_connectionInfo.DeviceType.ToUpper() == "SONOPOST")
- {
- return new LiveVideoV2(_token, UniqueId, _usScreenWidth, _usScreenHeight, _webSocket, true, _client, _connectionInfo.DeviceMode, _deviceType, _deviceName, _connectionInfo.SoftwareVersion);
- }
- else
- {
- return new LiveVideoV2(_token, UniqueId, _usScreenWidth, _usScreenHeight, _webSocket, false, _client, _connectionInfo.DeviceMode, _deviceType, _deviceName, _connectionInfo.SoftwareVersion);
- }
- case TerminalFeatureType.AfterSales:
- return new AfterSalesV2(_token, _client, _webSocket);
- default:
- return null;
- }
- }
- }
- }
|