DeviceService.RemoteMaintain.Sync.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using Newtonsoft.Json;
  7. using WingDeviceService.Common;
  8. using WingInterfaceLibrary.DTO.LiveRoom;
  9. using WingInterfaceLibrary.Enum;
  10. using WingInterfaceLibrary.Interface;
  11. using WingInterfaceLibrary.Internal.Request;
  12. using WingInterfaceLibrary.Notifications;
  13. using WingInterfaceLibrary.Notifications.Live;
  14. using WingInterfaceLibrary.Notifications.Remote;
  15. using WingInterfaceLibrary.Request;
  16. using WingInterfaceLibrary.Request.Authentication;
  17. using WingInterfaceLibrary.Request.Device;
  18. using WingInterfaceLibrary.Request.Remote;
  19. using WingServerCommon.Interfaces.Cache;
  20. using WingServerCommon.Service;
  21. using WingInterfaceLibrary.Result.Device;
  22. using WingServerCommon.Log;
  23. using WingInterfaceLibrary.DTO.RemoteParameters;
  24. using WingInterfaceLibrary.Internal.Interface;
  25. using WingServerCommon.Config;
  26. using WingServerCommon.Mapper;
  27. using WingServerCommon.Config.Parameters;
  28. namespace WingDeviceService.Service
  29. {
  30. /// <summary>
  31. /// 远程管理
  32. /// </summary>
  33. public partial class DeviceService : JsonRpcService, IDeviceService
  34. {
  35. /// <summary>
  36. /// 转发请求给主服务器
  37. /// </summary>
  38. /// <param name="syncType"></param>
  39. /// <param name="roomId"></param>
  40. /// <param name="message"></param>
  41. /// <returns></returns>
  42. private async Task SyncToMasterAsync(SyncTypeEnum syncType, string roomId, RemoteConnectDataRequest message)
  43. {
  44. try
  45. {
  46. if (ConfigurationManager.IsDistributed)
  47. {
  48. var syncRequest = new SyncReceiveServiceDataRequest
  49. {
  50. SyncService = SyncServiceEnum.RemoteConnect,
  51. SyncType = syncType,
  52. SourceUrl = EnvironmentConfigManager.GetParammeter<StringParameter>("Gateway", "Domains").Value,
  53. ServerID = _serverID,
  54. ServiceDataJson = Newtonsoft.Json.JsonConvert.SerializeObject(message),
  55. };
  56. _masterInteractionCenterService.SyncReceiveSlaveServiceDataAsync(syncRequest);
  57. }
  58. }
  59. catch (Exception ex)
  60. {
  61. Logger.WriteLineWarn($"DeviceService SyncToMasterAsync err, roomId:{roomId}, syncType:{syncType.ToString()}, err: {ex}");
  62. }
  63. }
  64. /// <summary>
  65. /// 转发请求给主服务器
  66. /// </summary>
  67. /// <param name="syncType"></param>
  68. /// <param name="roomId"></param>
  69. /// <param name="message"></param>
  70. /// <returns></returns>
  71. private async Task SyncToMasterAsync(SyncTypeEnum syncType, string roomId, JoinInConsultationJson message)
  72. {
  73. try
  74. {
  75. if (ConfigurationManager.IsDistributed)
  76. {
  77. var syncRequest = new SyncReceiveServiceDataRequest
  78. {
  79. SyncService = SyncServiceEnum.RemoteConnect,
  80. SyncType = syncType,
  81. SourceUrl = EnvironmentConfigManager.GetParammeter<StringParameter>("Gateway", "Domains").Value,
  82. ServerID = _serverID,
  83. ServiceDataJson = Newtonsoft.Json.JsonConvert.SerializeObject(message),
  84. };
  85. _masterInteractionCenterService.SyncReceiveSlaveServiceDataAsync(syncRequest);
  86. }
  87. }
  88. catch (Exception ex)
  89. {
  90. Logger.WriteLineWarn($"DeviceService SyncToMasterAsync err, roomId:{roomId}, syncType:{syncType.ToString()}, err: {ex}");
  91. }
  92. }
  93. /// <summary>
  94. /// 转发请求给主服务器(系统设置专用)
  95. /// </summary>
  96. /// <param name="syncType"></param>
  97. /// <param name="roomId"></param>
  98. /// <param name="message"></param>
  99. /// <returns></returns>
  100. private async Task SyncRemoteDataToMasterAsync(int syncMethod, string key, string value)
  101. {
  102. try
  103. {
  104. if (ConfigurationManager.IsDistributed)
  105. {
  106. var syncRequest = new SyncReceiveServiceDataRequest
  107. {
  108. SyncService = SyncServiceEnum.RemoteConnect,
  109. SyncType = SyncTypeEnum.RemoteMaintainData,
  110. SourceUrl = EnvironmentConfigManager.GetParammeter<StringParameter>("Gateway", "Domains").Value,
  111. ServerID = _serverID,
  112. ServiceDataJson = $"{{\"Data\":[{{\"SyncMethod\":{syncMethod},\"Key\":\"{key}\",\"Value\":{value}}}]}}",
  113. };
  114. _masterInteractionCenterService.SyncReceiveSlaveServiceDataAsync(syncRequest);
  115. }
  116. }
  117. catch (Exception ex)
  118. {
  119. Logger.WriteLineWarn($"DeviceService SyncToMasterAsync err, key:{key}, value:{value}, err: {ex}");
  120. }
  121. }
  122. /// <summary>
  123. /// 转发请求给主服务器(调节参数专用)
  124. /// </summary>
  125. /// <param name="syncType"></param>
  126. /// <param name="roomId"></param>
  127. /// <param name="message"></param>
  128. /// <returns></returns>
  129. private async Task SyncControllingParameterToMasterAsync(int syncMethod, string key, CacheControllingParameter value)
  130. {
  131. try
  132. {
  133. if (ConfigurationManager.IsDistributed)
  134. {
  135. var syncRequest = new SyncReceiveServiceDataRequest
  136. {
  137. SyncService = SyncServiceEnum.RemoteConnect,
  138. SyncType = SyncTypeEnum.SyncControllingParameter,
  139. SourceUrl = EnvironmentConfigManager.GetParammeter<StringParameter>("Gateway", "Domains").Value,
  140. ServerID = _serverID,
  141. ServiceDataJson = $"{{\"Data\":[{{\"SyncMethod\":{syncMethod},\"Key\":\"{key}\",\"Value\":{JsonConvert.SerializeObject(value)}}}]}}",
  142. };
  143. _masterInteractionCenterService.SyncReceiveSlaveServiceDataAsync(syncRequest);
  144. }
  145. }
  146. catch (Exception ex)
  147. {
  148. Logger.WriteLineWarn($"DeviceService SyncToMasterAsync err, key:{key}, value:{value}, err: {ex}");
  149. }
  150. }
  151. /// <summary>
  152. /// 转发请求给主服务器(探头和应用专用)
  153. /// </summary>
  154. /// <param name="syncType"></param>
  155. /// <param name="roomId"></param>
  156. /// <param name="message"></param>
  157. /// <returns></returns>
  158. private async Task SyncProbeApplicationToMasterAsync(int syncMethod, string key, CacheControllingParameter value)
  159. {
  160. try
  161. {
  162. if (ConfigurationManager.IsDistributed)
  163. {
  164. var syncRequest = new SyncReceiveServiceDataRequest
  165. {
  166. SyncService = SyncServiceEnum.RemoteConnect,
  167. SyncType = SyncTypeEnum.ProbeApplication,
  168. SourceUrl = EnvironmentConfigManager.GetParammeter<StringParameter>("Gateway", "Domains").Value,
  169. ServerID = _serverID,
  170. ServiceDataJson = $"{{\"Data\":[{{\"SyncMethod\":{syncMethod},\"Key\":\"{key}\",\"Value\":{JsonConvert.SerializeObject(value)}}}]}}",
  171. };
  172. _masterInteractionCenterService.SyncReceiveSlaveServiceDataAsync(syncRequest);
  173. }
  174. }
  175. catch (Exception ex)
  176. {
  177. Logger.WriteLineWarn($"DeviceService SyncToMasterAsync err, key:{key}, value:{value}, err: {ex}");
  178. }
  179. }
  180. /// <summary>
  181. /// 接收到同步信息
  182. /// </summary>
  183. /// <param name="request">The request</param>
  184. /// <returns></returns>
  185. /// <show>false</show>
  186. public async Task<bool> ReceiveSyncServerMessageAsync(SyncReceiveServiceDataRequest request)
  187. {
  188. try
  189. {
  190. var res = false;
  191. Logger.WriteLineInfo($"DeviceService RemoteConnect SyncServerMessageAsync, source url:{request.SourceUrl}, syncType:{request.SyncType.ToString()}");
  192. if (request.SyncType == SyncTypeEnum.DeviceReStartPusing)
  193. {
  194. var deviceJoinInMessage = Newtonsoft.Json.JsonConvert.DeserializeObject<JoinInConsultationJson>(request.ServiceDataJson);
  195. if (deviceJoinInMessage != null)
  196. {
  197. res = await NoticeClientBeginPushLive(deviceJoinInMessage.OperatorCode);
  198. }
  199. }
  200. else if (request.SyncType == SyncTypeEnum.RemoteMaintainData)
  201. {
  202. dynamic keyValues = Newtonsoft.Json.Linq.JObject.Parse(request.ServiceDataJson);
  203. foreach (dynamic keyValue in keyValues.Data)
  204. {
  205. var key = (string)keyValue.Key;
  206. var setValue = (string)keyValue.Value;
  207. if ((int)keyValue.SyncMethod == 0)
  208. {
  209. Func<string, string, string> updateValueFactory = (a, b) => setValue;
  210. _remoteMaintainMsgs.AddOrUpdate(key, setValue, updateValueFactory);
  211. }
  212. else
  213. {
  214. _remoteMaintainMsgs.TryRemove(key, out string _);
  215. }
  216. }
  217. }
  218. else if (request.SyncType == SyncTypeEnum.ProbeApplication)
  219. {
  220. dynamic keyValues = Newtonsoft.Json.Linq.JObject.Parse(request.ServiceDataJson);
  221. foreach (dynamic keyValue in keyValues.Data)
  222. {
  223. var key = (string)keyValue.Key;
  224. dynamic setValue = keyValue.Value;
  225. if ((int)keyValue.SyncMethod == 0)
  226. {
  227. var data = new CacheControllingParameter();
  228. data.Code = setValue.Code;
  229. data.Parameter = setValue.Parameter;
  230. data.ProbeApplication = setValue.ProbeApplication;
  231. Func<string, CacheControllingParameter, CacheControllingParameter> updateValueFactory = (a, b) => data;
  232. _cacheProbeApplicationSetting.AddOrUpdate(key, data, updateValueFactory);
  233. }
  234. }
  235. }
  236. else if (request.SyncType == SyncTypeEnum.SyncControllingParameter)
  237. {
  238. dynamic keyValues = Newtonsoft.Json.Linq.JObject.Parse(request.ServiceDataJson);
  239. foreach (dynamic keyValue in keyValues.Data)
  240. {
  241. var key = (string)keyValue.Key;
  242. dynamic setValue = keyValue.Value;
  243. if ((int)keyValue.SyncMethod == 0)
  244. {
  245. var data = new CacheControllingParameter();
  246. data.Code = setValue.Code;
  247. data.Parameter = setValue.Parameter;
  248. data.ProbeApplication = setValue.ProbeApplication;
  249. Func<string, CacheControllingParameter, CacheControllingParameter> updateValueFactory = (a, b) => data;
  250. _controllingParameterDevices.AddOrUpdate(key, data, updateValueFactory);
  251. }
  252. }
  253. }
  254. else
  255. {
  256. var remoteConnectData = JsonConvert.DeserializeObject<RemoteConnectDataRequest>(request.ServiceDataJson);
  257. if (remoteConnectData != null)
  258. {
  259. switch (request.SyncType)
  260. {
  261. case SyncTypeEnum.ConsultionRemoteConnect:
  262. var controlType = ControlDeviceParameterEnum.Start;
  263. if (remoteConnectData.UserRemoteConnect.StatusEnum == ConnectStatusEnum.WaitConnect)
  264. {
  265. controlType = ControlDeviceParameterEnum.Start;
  266. }
  267. else if (remoteConnectData.UserRemoteConnect.StatusEnum == ConnectStatusEnum.WaitDisconnect)
  268. {
  269. controlType = ControlDeviceParameterEnum.End;
  270. }
  271. var applyConsultionReq = new ControlDeviceConnectRequest()
  272. {
  273. RoomCode = remoteConnectData.UserRemoteConnect.RoomId,
  274. ControlType = controlType,
  275. DeviceCode = remoteConnectData.UserRemoteConnect.DeviceCode,
  276. Token = remoteConnectData.UserRemoteConnect.UserToken,
  277. IsNeedSyn = false
  278. };
  279. res = await ConsultionApplyRemoteConnect(applyConsultionReq);
  280. break;
  281. case SyncTypeEnum.ConsultionDisConnect:
  282. var type = ControlDeviceParameterEnum.Start;
  283. if (remoteConnectData.UserRemoteConnect.StatusEnum == ConnectStatusEnum.CompleteConnect)
  284. {
  285. type = ControlDeviceParameterEnum.Start;
  286. }
  287. else if (remoteConnectData.UserRemoteConnect.StatusEnum == ConnectStatusEnum.WaitDisconnect)
  288. {
  289. type = ControlDeviceParameterEnum.End;
  290. }
  291. else if (remoteConnectData.UserRemoteConnect.StatusEnum == ConnectStatusEnum.WaitConnect)
  292. {
  293. type = ControlDeviceParameterEnum.RejectConnect;
  294. }
  295. var closeConsultionReq = new ControlDeviceResponseRequest()
  296. {
  297. UserCode = remoteConnectData.UserRemoteConnect.UserCode,
  298. UserName = "",
  299. LoginSource = remoteConnectData.UserRemoteConnect.LoginSource,
  300. ControlType = type,
  301. Token = remoteConnectData.UserRemoteConnect.DeviceToken,
  302. IsNeedSyn = false
  303. };
  304. res = await ConsultionAcceptRemoteConnnectionAsync(closeConsultionReq);
  305. break;
  306. case SyncTypeEnum.ApplyRemoteConnect:
  307. var applyReq = new ControlDeviceConnectRequest()
  308. {
  309. DeviceCode = remoteConnectData.UserRemoteConnect.DeviceCode,
  310. ControlType = ControlDeviceParameterEnum.Start,
  311. Token = remoteConnectData.UserRemoteConnect.UserToken,
  312. IsNeedSyn = false
  313. };
  314. res = await ApplyRemoteConnectionAsync(applyReq);
  315. break;
  316. case SyncTypeEnum.AcceptRemoteConnect:
  317. var acceptRemoteConnectReq = new ControlDeviceResponseRequest()
  318. {
  319. UserCode = remoteConnectData.UserRemoteConnect.UserCode,
  320. UserName = "",
  321. LoginSource = remoteConnectData.UserRemoteConnect.LoginSource,
  322. ControlType = ControlDeviceParameterEnum.Start,
  323. Token = remoteConnectData.UserRemoteConnect.DeviceToken,
  324. IsNeedSyn = false
  325. };
  326. res = await AcceptRemoteConnnectionAsync(acceptRemoteConnectReq);
  327. break;
  328. case SyncTypeEnum.RejectRemoteConnect:
  329. var rejectRemoteConnectReq = new ControlDeviceResponseRequest()
  330. {
  331. UserCode = remoteConnectData.UserRemoteConnect.UserCode,
  332. UserName = "",
  333. LoginSource = remoteConnectData.UserRemoteConnect.LoginSource,
  334. ControlType = ControlDeviceParameterEnum.RejectConnect,
  335. Token = remoteConnectData.UserRemoteConnect.DeviceToken,
  336. IsNeedSyn = false
  337. };
  338. res = await AcceptRemoteConnnectionAsync(rejectRemoteConnectReq);
  339. break;
  340. case SyncTypeEnum.ApplyDisConnect:
  341. var applyDisconnectReq = new ControlDeviceConnectRequest()
  342. {
  343. DeviceCode = remoteConnectData.UserRemoteConnect.DeviceCode,
  344. ControlType = ControlDeviceParameterEnum.End,
  345. Token = remoteConnectData.UserRemoteConnect.UserToken,
  346. IsNeedSyn = false
  347. };
  348. res = await ApplyRemoteConnectionAsync(applyDisconnectReq);
  349. break;
  350. case SyncTypeEnum.AcceptDisConnect:
  351. var acceptDisConnect = new ControlDeviceResponseRequest()
  352. {
  353. UserCode = remoteConnectData.UserRemoteConnect.UserCode,
  354. UserName = "",
  355. LoginSource = remoteConnectData.UserRemoteConnect.LoginSource,
  356. ControlType = ControlDeviceParameterEnum.End,
  357. Token = remoteConnectData.UserRemoteConnect.DeviceToken,
  358. IsNeedSyn = false
  359. };
  360. res = await AcceptRemoteConnnectionAsync(acceptDisConnect);
  361. break;
  362. case SyncTypeEnum.DeviceDisconnect:
  363. var deviceDisconnect = new RemoteConnectStautsRequest()
  364. {
  365. UserCode = remoteConnectData.UserRemoteConnect.UserCode,
  366. Token = remoteConnectData.UserRemoteConnect.DeviceToken,
  367. IsNeedSyn = false
  368. };
  369. res = await DisconnectRemoteControl(deviceDisconnect);
  370. break;
  371. case SyncTypeEnum.DeviceRemoteConnectHeartRate:
  372. var deviceRemoteConnectHeartRate = new RemoteConnectHeartRateRequest()
  373. {
  374. TransactionType = remoteConnectData.UserRemoteConnect.TransactionType,
  375. Token = remoteConnectData.UserRemoteConnect.DeviceToken,
  376. IsNeedSyn = false
  377. };
  378. res = await DeviceRemoteConnectHeartRateAsync(deviceRemoteConnectHeartRate);
  379. break;
  380. case SyncTypeEnum.UserRemoteConnectHeartRate:
  381. var userRemoteConnectHeartRate = new RemoteConnectHeartRateRequest()
  382. {
  383. TransactionType = remoteConnectData.UserRemoteConnect.TransactionType,
  384. Token = remoteConnectData.UserRemoteConnect.UserToken,
  385. IsNeedSyn = false
  386. };
  387. res = await RemoteConnectHeartRateAsync(userRemoteConnectHeartRate);
  388. break;
  389. case SyncTypeEnum.DeviceRemoteConnectOutline:
  390. var deviceRemoteConnectOutline = new DeviceRemoteConnectDTO(
  391. remoteConnectData.UserRemoteConnect.DeviceToken,
  392. remoteConnectData.UserRemoteConnect.UserToken,
  393. remoteConnectData.UserRemoteConnect.UserCode,
  394. remoteConnectData.UserRemoteConnect.DeviceCode,
  395. remoteConnectData.UserRemoteConnect.TransactionType
  396. );
  397. res = DeviceRemoteConnectOnlineState(deviceRemoteConnectOutline, false, remoteConnectData.IsManangeDisConnnect);
  398. break;
  399. case SyncTypeEnum.UserRemoteConnectOutline:
  400. var userRemoteConnectOutline = remoteConnectData.UserRemoteConnect.MappingTo<RemoteConnectDTO>();
  401. res = RemoteConnectOnlineState(userRemoteConnectOutline, false, remoteConnectData.IsManangeDisConnnect);
  402. break;
  403. case SyncTypeEnum.RestartDevice:
  404. var restartDevice = new RestartDeviceRequest()
  405. {
  406. DeviceCode = remoteConnectData.UserRemoteConnect.DeviceCode,
  407. Token = remoteConnectData.UserRemoteConnect.UserToken,
  408. IsNeedSyn = false
  409. };
  410. res = await RestartDeviceAsync(restartDevice);
  411. break;
  412. default:
  413. return res;
  414. }
  415. }
  416. }
  417. return res;
  418. }
  419. catch (Exception ex)
  420. {
  421. Logger.WriteLineWarn($"DeviceService RemoteConnect SyncServerMessageAsync err, {ex}, request data:{request.ServiceDataJson}");
  422. return false;
  423. }
  424. }
  425. }
  426. }