123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623 |
- using System.ComponentModel;
- using System;
- using System.Collections.Concurrent;
- using System.Threading.Tasks;
- using WingServerCommon.Log;
- using WingInterfaceLibrary.Interface;
- using WingInterfaceLibrary.Enum;
- using System.Collections.Generic;
- using WingServerCommon.Config;
- using WingServerCommon.Config.Parameters;
- namespace WingDeviceService.Common
- {
- internal class RemoteConnectManager
- {
- private ConcurrentDictionary<string, RemoteConnectDTO> _remoteConnects = new ConcurrentDictionary<string, RemoteConnectDTO>();
- private ConcurrentDictionary<string, DeviceRemoteConnectDTO> _deviceRemoteConnects = new ConcurrentDictionary<string, DeviceRemoteConnectDTO>();
- private Func<RemoteConnectDTO, bool, bool, bool> OnRemoteConnectOnlineState;
- private Func<DeviceRemoteConnectDTO, bool, bool, bool> OnDeviceRemoteConnectOnlineState;
- private int _rateSeconds => EnvironmentConfigManager.GetParammeter<IntParameter>("Device", "RemoteControlHeartRateSeconds").Value;
- private IAuthenticationService _authenticationService;
- public RemoteConnectManager(Func<RemoteConnectDTO, bool, bool, bool> setOnlineState, Func<DeviceRemoteConnectDTO, bool, bool, bool> deviceSetOnlineState, IAuthenticationService _authService)
- {
- _authenticationService = _authService;
- OnRemoteConnectOnlineState = setOnlineState;
- OnDeviceRemoteConnectOnlineState = deviceSetOnlineState;
- StartCheckClients();
- }
- /// <summary>
- /// 加入检测集合
- /// </summary>
- /// <param name="key"></param>
- /// <param name="userToken"></param>
- /// <param name="deviceCode"></param>
- /// <param name="roomId"></param>
- public void AddOrUpdate(string key, string userToken, string deviceToken, string deviceCode, string userCode, string roomId, ConnectStatusEnum statusEnum, LoginSource loginSource, TransactionTypeEnum transactionType = TransactionTypeEnum.AfterSales)
- {
- _remoteConnects.AddOrUpdate(key, new RemoteConnectDTO(userToken, deviceToken, deviceCode, userCode, roomId, statusEnum, transactionType, loginSource), (k, v) =>
- {
- v.Activate();
- v.StatusEnum = statusEnum;
- v.DeviceToken = deviceToken;
- v.LoginSource = loginSource;
- v.UserToken = userToken;
- v.IsNeedSyn = v.IsNeedSyn;
- return v;
- });
- }
- /// <summary>
- /// 加入检测集合
- /// </summary>
- /// <param name="key"></param>
- /// <param name="userToken"></param>
- /// <param name="deviceCode"></param>
- /// <param name="roomId"></param>
- public void AddOrUpdateHeart(string key, bool isNeedSyn, string userToken, string deviceToken, string deviceCode, string userCode, string roomId, ConnectStatusEnum statusEnum, LoginSource loginSource, TransactionTypeEnum transactionType = TransactionTypeEnum.AfterSales)
- {
- _remoteConnects.AddOrUpdate(key, new RemoteConnectDTO(isNeedSyn, userToken, deviceToken, deviceCode, userCode, roomId, statusEnum, transactionType, loginSource), (k, v) =>
- {
- v.Activate();
- v.StatusEnum = statusEnum;
- v.DeviceToken = deviceToken;
- v.LoginSource = loginSource;
- v.UserToken = userToken;
- v.IsNeedSyn = isNeedSyn;
- return v;
- });
- }
- /// <summary>
- /// 加入检测集合
- /// </summary>
- /// <param name="key"></param>
- /// <param name="userToken"></param>
- /// <param name="deviceCode"></param>
- /// <param name="roomId"></param>
- public void DeviceAddOrUpdate(string key, string deviceToken, string userToken, string userCode, string deviceCode, TransactionTypeEnum transactionType)
- {
- _deviceRemoteConnects.AddOrUpdate(key, new DeviceRemoteConnectDTO(deviceToken, userToken, userCode, deviceCode, transactionType), (k, v) =>
- {
- v.UserToken = userToken;
- v.DeviceToken = deviceToken;
- v.IsNeedSyn = v.IsNeedSyn;
- v.Activate();
- return v;
- });
- }
- /// <summary>
- /// 加入检测集合
- /// </summary>
- /// <param name="key"></param>
- /// <param name="userToken"></param>
- /// <param name="deviceCode"></param>
- /// <param name="roomId"></param>
- public void DeviceAddOrUpdateHeart(string key, bool isNeedSyn, string deviceToken, string userToken, string userCode, string deviceCode, TransactionTypeEnum transactionType)
- {
- _deviceRemoteConnects.AddOrUpdate(key, new DeviceRemoteConnectDTO(isNeedSyn, deviceToken, userToken, userCode, deviceCode, transactionType), (k, v) =>
- {
- v.UserToken = userToken;
- v.DeviceToken = deviceToken;
- v.IsNeedSyn = isNeedSyn;
- v.Activate();
- return v;
- });
- }
- /// <summary>
- /// 是否存在token
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public TransactionTypeEnum GetConnectTransactionType(string key)
- {
- TransactionTypeEnum transactionType = TransactionTypeEnum.AfterSales;
- if (_remoteConnects?.Count > 0)
- {
- var res = _remoteConnects.ContainsKey(key);
- if (res)
- {
- transactionType = _remoteConnects[key].TransactionType;
- }
- }
- return transactionType;
- }
- /// <summary>
- /// 获取
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public string GetRoomId(string key)
- {
- string result = "";
- if (_remoteConnects?.Count > 0)
- {
- var res = _remoteConnects.ContainsKey(key);
- if (res)
- {
- result = _remoteConnects[key]?.RoomId ?? string.Empty;
- }
- }
- return result;
- }
- /// <summary>
- /// 获取
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public bool GetExistDeviceRoomId(string key)
- {
- bool res = false;
- if (_deviceRemoteConnects?.Count > 0)
- {
- res = _deviceRemoteConnects.ContainsKey(key);
- }
- return res;
- }
- /// <summary>
- /// 获取
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public RemoteConnectDTO GetRemoteConnectDTO(string key)
- {
- RemoteConnectDTO result = null;
- if (_remoteConnects?.Count > 0)
- {
- var res = _remoteConnects.ContainsKey(key);
- if (res)
- {
- result = _remoteConnects[key];
- }
- }
- return result;
- }
- /// <summary>
- /// 获取
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public DeviceRemoteConnectDTO GetDeviceRemoteConnectDTO(string key)
- {
- DeviceRemoteConnectDTO result = null;
- if (_deviceRemoteConnects?.Count > 0)
- {
- var res = _deviceRemoteConnects.ContainsKey(key);
- if (res)
- {
- result = _deviceRemoteConnects[key];
- }
- }
- return result;
- }
- /// <summary>
- /// 根据用户token查询所有的连接状态
- /// </summary>
- /// <param name="userToken"></param>
- /// <returns></returns>
- public List<RemoteConnectDTO> GetAllRemoteConnectByUserToken(string userToken)
- {
- var result = new List<RemoteConnectDTO>();
- if (_remoteConnects?.Count > 0)
- {
- foreach (var item in _remoteConnects.Values)
- {
- if (item.UserToken == userToken)
- {
- result.Add(item);
- }
- }
- }
- return result;
- }
- /// <summary>
- /// 根据deviceCode查询所有的连接状态
- /// </summary>
- /// <param name="userToken"></param>
- /// <returns></returns>
- public List<DeviceRemoteConnectDTO> GetAllRemoteConnectByDeviceToken(string deviceToken)
- {
- var result = new List<DeviceRemoteConnectDTO>();
- if (_deviceRemoteConnects?.Count > 0)
- {
- foreach (var item in _deviceRemoteConnects.Values)
- {
- if (item.DeviceToken == deviceToken)
- {
- result.Add(item);
- }
- }
- }
- return result;
- }
- public IList<RemoteConnectDTO> GetAllRemoteConnectList(string deviceCode)
- {
- var dataList = new List<RemoteConnectDTO>();
- if (_remoteConnects?.Count > 0)
- {
- foreach (var item in _remoteConnects.Values)
- {
- if (item.DeviceCode == deviceCode)
- {
- dataList.Add(item);
- }
- }
- }
- return dataList;
- }
- public IList<DeviceRemoteConnectDTO> GetAllDeviceRemoteConnectList(string deviceCode)
- {
- var dataList = new List<DeviceRemoteConnectDTO>();
- if (_deviceRemoteConnects?.Count > 0)
- {
- foreach (var item in _deviceRemoteConnects.Values)
- {
- if (item.DeviceCode == deviceCode)
- {
- dataList.Add(item);
- }
- }
- }
- return dataList;
- }
- public RemoteConnectDTO GetDeviceRemoteConnectInfo(string deviceCode)
- {
- RemoteConnectDTO info = null;
- if (_remoteConnects?.Count > 0)
- {
- foreach (var item in _remoteConnects.Values)
- {
- if (item.DeviceCode == deviceCode)
- {
- info = item;
- break;
- }
- }
- }
- return info;
- }
- /// <summary>
- /// 查询设备是否空闲
- /// </summary>
- /// <param name="userToken"></param>
- /// <param name="deviceCode"></param>
- /// <returns>是否被他人专用</returns>
- public int IsIdle(string userToken, string deviceCode, TransactionTypeEnum type = TransactionTypeEnum.AfterSales)
- {
- int res = 0;
- if (_remoteConnects?.Count > 0)
- {
- RemoteConnectDTO checkItem = null;
- foreach (var item in _remoteConnects.Values)
- {
- if (item.DeviceCode == deviceCode)
- {
- checkItem = item;
- break;
- }
- }
- if (checkItem != null)
- {
- //找到,判断usertoken是否一样, 如果不一样,则表示不是同一个人,则为他人已连接
- if (checkItem.UserToken != userToken)
- {
- //判断如果是同一个人,不同端
- var checkItemReq = new WingInterfaceLibrary.Request.TokenRequest() { Token = checkItem.UserToken };
- var checkItemToken = _authenticationService.GetTokenAsync(checkItemReq).Result;
- var userReq = new WingInterfaceLibrary.Request.TokenRequest() { Token = userToken };
- var userTokenInfo = _authenticationService.GetTokenAsync(userReq).Result;
- //这两个一样,表示不同端登录
- if (checkItemToken.ClientId == userTokenInfo.ClientId)
- {
- res = 2;
- }
- else
- {
- res = 1;
- }
- }
- //如果是同一个人,但是针对不同的模块也是不行的,比如正在会诊调参,是不能进行远程连接的
- else if (checkItem.UserToken == userToken && checkItem.TransactionType != type)
- {
- res = 1;
- }
- }
- }
- return res;
- }
- /// <summary>
- /// 尝试关闭
- /// </summary>
- /// <param name="key"></param>
- public bool Close(string key)
- {
- if (_remoteConnects.ContainsKey(key))
- {
- var res = _remoteConnects.TryRemove(key, out _);
- return res;
- }
- else
- {
- return false;
- }
- }
- /// <summary>
- /// 尝试关闭
- /// </summary>
- /// <param name="key"></param>
- public bool DeviceClose(string key)
- {
- if (_deviceRemoteConnects.ContainsKey(key))
- {
- var res = _deviceRemoteConnects.TryRemove(key, out _);
- return res;
- }
- else
- {
- return false;
- }
- }
- /// <summary>
- /// 设备心跳有效期验证
- /// </summary>
- private void StartCheckClients()
- {
- Logger.WriteLineInfo($"RemoteConnectManager start check clients");
- Task.Run(async () =>
- {
- while (true)
- {
- await Task.Delay(1 * 1000 * _rateSeconds);
- foreach (var token in _remoteConnects.Keys)
- {
- var remoteConnectDTO = _remoteConnects[token];
- remoteConnectDTO.DeActivate();
- try
- {
- if (remoteConnectDTO.LeftTime < 0)
- {
- Logger.WriteLineInfo($"RemoteConnectManager StartCheckClients, user is not online, Token:{ remoteConnectDTO.UserToken }, DeviceCode:{ remoteConnectDTO.DeviceCode }");
- OnRemoteConnectOnlineState?.Invoke(remoteConnectDTO, remoteConnectDTO.IsNeedSyn, false);
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineWarn($"check remote connect token {token} err, {ex}");
- }
- }
- foreach (var token in _deviceRemoteConnects.Keys)
- {
- var deviceRemoteConnectDTO = _deviceRemoteConnects[token];
- deviceRemoteConnectDTO.DeActivate();
- try
- {
- if (deviceRemoteConnectDTO.LeftTime < 0)
- {
- Logger.WriteLineInfo($"RemoteConnectManager StartCheckClients, device is not online, Token:{ deviceRemoteConnectDTO.DeviceToken }, UserCode:{ deviceRemoteConnectDTO.UserCode }");
- OnDeviceRemoteConnectOnlineState?.Invoke(deviceRemoteConnectDTO, deviceRemoteConnectDTO.IsNeedSyn, false);
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineWarn($"check remote connect token {token} err, {ex}");
- }
- }
- }
- });
- Logger.WriteLineInfo($"RemoteConnectManager finished");
- }
- }
- /// <summary>
- /// 远程连接管理实体
- /// </summary>
- internal class RemoteConnectDTO
- {
- /// <summary>
- /// 有参构造
- /// </summary>
- public RemoteConnectDTO(bool isNeedSyn, string userToken, string deviceToken, string deviceCode, string userCode, string roomId, ConnectStatusEnum statusEnum, TransactionTypeEnum transactionType, LoginSource loginSource)
- {
- UserToken = userToken;
- DeviceToken = deviceToken;
- DeviceCode = deviceCode;
- UserCode = userCode;
- RoomId = roomId;
- TransactionType = transactionType;
- StatusEnum = statusEnum;
- LoginSource = loginSource;
- IsNeedSyn = isNeedSyn;
- }
- /// <summary>
- /// 有参构造
- /// </summary>
- public RemoteConnectDTO(string userToken, string deviceToken, string deviceCode, string userCode, string roomId, ConnectStatusEnum statusEnum, TransactionTypeEnum transactionType, LoginSource loginSource)
- {
- UserToken = userToken;
- DeviceToken = deviceToken;
- DeviceCode = deviceCode;
- UserCode = userCode;
- RoomId = roomId;
- TransactionType = transactionType;
- StatusEnum = statusEnum;
- LoginSource = loginSource;
- }
- /// <summary>
- /// 类型
- /// </summary>
- public TransactionTypeEnum TransactionType { get; set; }
- /// <summary>
- /// 状态
- /// </summary>
- public ConnectStatusEnum StatusEnum { get; set; }
- /// <summary>
- /// 用户Token
- /// </summary>
- public string UserToken { get; set; }
- /// <summary>
- /// 设备Token
- /// </summary>
- public string DeviceToken { get; set; }
- /// <summary>
- /// 用户code
- /// </summary>
- public string UserCode { get; set; }
- /// <summary>
- /// 房间id
- /// </summary>
- public string RoomId { get; set; }
- /// <summary>
- /// 设备code
- /// </summary>
- public string DeviceCode { get; set; }
- /// <summary>
- /// 是否需要同步数据处理
- /// </summary>
- public bool IsNeedSyn { get; set; }
- /// <summary>
- /// 用户来源
- /// </summary>
- /// <value></value>
- public LoginSource LoginSource { get; set; }
- private volatile int _livingTime = 0;
- private readonly int _clientLifeCycle = 3; //3次无心跳就默认断开
- /// <summary>
- /// 离开次数
- /// </summary>
- public int LeftTime => _clientLifeCycle - _livingTime;
- /// <summary>
- /// Reset the living time.
- /// </summary>
- public virtual void Activate()
- {
- _livingTime = 0;
- }
-
- /// <summary>
- /// Deactive the session.
- /// </summary>
- public virtual void DeActivate()
- {
- _livingTime++;
- }
- }
- /// <summary>
- /// 设备远程连接管理实体
- /// </summary>
- internal class DeviceRemoteConnectDTO
- {
- /// <summary>
- /// 有参构造
- /// </summary>
- public DeviceRemoteConnectDTO(bool isNeedSyn, string deviceToken, string userToken, string userCode, string deviceCode, TransactionTypeEnum transactionType)
- {
- DeviceToken = deviceToken;
- UserToken = userToken;
- UserCode = userCode;
- DeviceCode = deviceCode;
- TransactionType = transactionType;
- IsNeedSyn = isNeedSyn;
- }
- /// <summary>
- /// 有参构造
- /// </summary>
- public DeviceRemoteConnectDTO(string deviceToken, string userToken, string userCode, string deviceCode, TransactionTypeEnum transactionType)
- {
- DeviceToken = deviceToken;
- UserToken = userToken;
- UserCode = userCode;
- DeviceCode = deviceCode;
- TransactionType = transactionType;
- }
- /// <summary>
- /// 类型
- /// </summary>
- public TransactionTypeEnum TransactionType { get; set; }
- /// <summary>
- /// 设备Token
- /// </summary>
- public string DeviceToken { get; set; }
- /// <summary>
- /// 用户Token
- /// </summary>
- public string UserToken { get; set; }
-
- /// <summary>
- /// 设备code
- /// </summary>
- public string DeviceCode { get; set; }
- /// <summary>
- /// 设备code
- /// </summary>
- public string UserCode { get; set; }
-
- /// <summary>
- /// 是否需要同步数据处理
- /// </summary>
- public bool IsNeedSyn { get; set; }
- private volatile int _livingTime = 0;
- private readonly int _clientLifeCycle = 3; //3次无心跳就默认断开
- /// <summary>
- /// 离开次数
- /// </summary>
- public int LeftTime => _clientLifeCycle - _livingTime;
- /// <summary>
- /// Reset the living time.
- /// </summary>
- public virtual void Activate()
- {
- _livingTime = 0;
- }
- /// <summary>
- /// Deactive the session.
- /// </summary>
- public virtual void DeActivate()
- {
- _livingTime++;
- }
- }
- }
|