123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434 |
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Newtonsoft.Json;
- using WingDeviceService.Common;
- using WingInterfaceLibrary.DTO.LiveRoom;
- using WingInterfaceLibrary.Enum;
- using WingInterfaceLibrary.Interface;
- using WingInterfaceLibrary.Internal.Request;
- using WingInterfaceLibrary.Notifications;
- using WingInterfaceLibrary.Notifications.Live;
- using WingInterfaceLibrary.Notifications.Remote;
- using WingInterfaceLibrary.Request;
- using WingInterfaceLibrary.Request.Authentication;
- using WingInterfaceLibrary.Request.Device;
- using WingInterfaceLibrary.Request.Remote;
- using WingServerCommon.Interfaces.Cache;
- using WingServerCommon.Service;
- using WingInterfaceLibrary.Result.Device;
- using WingServerCommon.Log;
- using WingInterfaceLibrary.DTO.RemoteParameters;
- using WingInterfaceLibrary.Internal.Interface;
- using WingServerCommon.Config;
- using WingServerCommon.Mapper;
- using WingServerCommon.Config.Parameters;
- namespace WingDeviceService.Service
- {
- /// <summary>
- /// 远程管理
- /// </summary>
- public partial class DeviceService : JsonRpcService, IDeviceService
- {
- /// <summary>
- /// 转发请求给主服务器
- /// </summary>
- /// <param name="syncType"></param>
- /// <param name="roomId"></param>
- /// <param name="message"></param>
- /// <returns></returns>
- private async Task SyncToMasterAsync(SyncTypeEnum syncType, string roomId, RemoteConnectDataRequest message)
- {
- try
- {
- if (ConfigurationManager.IsDistributed)
- {
- var syncRequest = new SyncReceiveServiceDataRequest
- {
- SyncService = SyncServiceEnum.RemoteConnect,
- SyncType = syncType,
- SourceUrl = EnvironmentConfigManager.GetParammeter<StringParameter>("Gateway", "Domains").Value,
- ServerID = _serverID,
- ServiceDataJson = Newtonsoft.Json.JsonConvert.SerializeObject(message),
- };
- _masterInteractionCenterService.SyncReceiveSlaveServiceDataAsync(syncRequest);
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineWarn($"DeviceService SyncToMasterAsync err, roomId:{roomId}, syncType:{syncType.ToString()}, err: {ex}");
- }
- }
- /// <summary>
- /// 转发请求给主服务器
- /// </summary>
- /// <param name="syncType"></param>
- /// <param name="roomId"></param>
- /// <param name="message"></param>
- /// <returns></returns>
- private async Task SyncToMasterAsync(SyncTypeEnum syncType, string roomId, JoinInConsultationJson message)
- {
- try
- {
- if (ConfigurationManager.IsDistributed)
- {
- var syncRequest = new SyncReceiveServiceDataRequest
- {
- SyncService = SyncServiceEnum.RemoteConnect,
- SyncType = syncType,
- SourceUrl = EnvironmentConfigManager.GetParammeter<StringParameter>("Gateway", "Domains").Value,
- ServerID = _serverID,
- ServiceDataJson = Newtonsoft.Json.JsonConvert.SerializeObject(message),
- };
- _masterInteractionCenterService.SyncReceiveSlaveServiceDataAsync(syncRequest);
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineWarn($"DeviceService SyncToMasterAsync err, roomId:{roomId}, syncType:{syncType.ToString()}, err: {ex}");
- }
- }
- /// <summary>
- /// 转发请求给主服务器(系统设置专用)
- /// </summary>
- /// <param name="syncType"></param>
- /// <param name="roomId"></param>
- /// <param name="message"></param>
- /// <returns></returns>
- private async Task SyncRemoteDataToMasterAsync(int syncMethod, string key, string value)
- {
- try
- {
- if (ConfigurationManager.IsDistributed)
- {
- var syncRequest = new SyncReceiveServiceDataRequest
- {
- SyncService = SyncServiceEnum.RemoteConnect,
- SyncType = SyncTypeEnum.RemoteMaintainData,
- SourceUrl = EnvironmentConfigManager.GetParammeter<StringParameter>("Gateway", "Domains").Value,
- ServerID = _serverID,
- ServiceDataJson = $"{{\"Data\":[{{\"SyncMethod\":{syncMethod},\"Key\":\"{key}\",\"Value\":{value}}}]}}",
- };
- _masterInteractionCenterService.SyncReceiveSlaveServiceDataAsync(syncRequest);
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineWarn($"DeviceService SyncToMasterAsync err, key:{key}, value:{value}, err: {ex}");
- }
- }
-
- /// <summary>
- /// 转发请求给主服务器(调节参数专用)
- /// </summary>
- /// <param name="syncType"></param>
- /// <param name="roomId"></param>
- /// <param name="message"></param>
- /// <returns></returns>
- private async Task SyncControllingParameterToMasterAsync(int syncMethod, string key, CacheControllingParameter value)
- {
- try
- {
- if (ConfigurationManager.IsDistributed)
- {
- var syncRequest = new SyncReceiveServiceDataRequest
- {
- SyncService = SyncServiceEnum.RemoteConnect,
- SyncType = SyncTypeEnum.SyncControllingParameter,
- SourceUrl = EnvironmentConfigManager.GetParammeter<StringParameter>("Gateway", "Domains").Value,
- ServerID = _serverID,
- ServiceDataJson = $"{{\"Data\":[{{\"SyncMethod\":{syncMethod},\"Key\":\"{key}\",\"Value\":{JsonConvert.SerializeObject(value)}}}]}}",
- };
- _masterInteractionCenterService.SyncReceiveSlaveServiceDataAsync(syncRequest);
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineWarn($"DeviceService SyncToMasterAsync err, key:{key}, value:{value}, err: {ex}");
- }
- }
- /// <summary>
- /// 转发请求给主服务器(探头和应用专用)
- /// </summary>
- /// <param name="syncType"></param>
- /// <param name="roomId"></param>
- /// <param name="message"></param>
- /// <returns></returns>
- private async Task SyncProbeApplicationToMasterAsync(int syncMethod, string key, CacheControllingParameter value)
- {
- try
- {
- if (ConfigurationManager.IsDistributed)
- {
- var syncRequest = new SyncReceiveServiceDataRequest
- {
- SyncService = SyncServiceEnum.RemoteConnect,
- SyncType = SyncTypeEnum.ProbeApplication,
- SourceUrl = EnvironmentConfigManager.GetParammeter<StringParameter>("Gateway", "Domains").Value,
- ServerID = _serverID,
- ServiceDataJson = $"{{\"Data\":[{{\"SyncMethod\":{syncMethod},\"Key\":\"{key}\",\"Value\":{JsonConvert.SerializeObject(value)}}}]}}",
- };
- _masterInteractionCenterService.SyncReceiveSlaveServiceDataAsync(syncRequest);
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineWarn($"DeviceService SyncToMasterAsync err, key:{key}, value:{value}, err: {ex}");
- }
- }
- /// <summary>
- /// 接收到同步信息
- /// </summary>
- /// <param name="request">The request</param>
- /// <returns></returns>
- /// <show>false</show>
- public async Task<bool> ReceiveSyncServerMessageAsync(SyncReceiveServiceDataRequest request)
- {
- try
- {
- var res = false;
- Logger.WriteLineInfo($"DeviceService RemoteConnect SyncServerMessageAsync, source url:{request.SourceUrl}, syncType:{request.SyncType.ToString()}");
- if (request.SyncType == SyncTypeEnum.DeviceReStartPusing)
- {
- var deviceJoinInMessage = Newtonsoft.Json.JsonConvert.DeserializeObject<JoinInConsultationJson>(request.ServiceDataJson);
- if (deviceJoinInMessage != null)
- {
- res = await NoticeClientBeginPushLive(deviceJoinInMessage.OperatorCode);
- }
- }
- else if (request.SyncType == SyncTypeEnum.RemoteMaintainData)
- {
- dynamic keyValues = Newtonsoft.Json.Linq.JObject.Parse(request.ServiceDataJson);
- foreach (dynamic keyValue in keyValues.Data)
- {
- var key = (string)keyValue.Key;
- var setValue = (string)keyValue.Value;
- if ((int)keyValue.SyncMethod == 0)
- {
- Func<string, string, string> updateValueFactory = (a, b) => setValue;
- _remoteMaintainMsgs.AddOrUpdate(key, setValue, updateValueFactory);
- }
- else
- {
- _remoteMaintainMsgs.TryRemove(key, out string _);
- }
- }
- }
- else if (request.SyncType == SyncTypeEnum.ProbeApplication)
- {
- dynamic keyValues = Newtonsoft.Json.Linq.JObject.Parse(request.ServiceDataJson);
- foreach (dynamic keyValue in keyValues.Data)
- {
- var key = (string)keyValue.Key;
- dynamic setValue = keyValue.Value;
- if ((int)keyValue.SyncMethod == 0)
- {
- var data = new CacheControllingParameter();
- data.Code = setValue.Code;
- data.Parameter = setValue.Parameter;
- data.ProbeApplication = setValue.ProbeApplication;
- Func<string, CacheControllingParameter, CacheControllingParameter> updateValueFactory = (a, b) => data;
- _cacheProbeApplicationSetting.AddOrUpdate(key, data, updateValueFactory);
- }
- }
- }
- else if (request.SyncType == SyncTypeEnum.SyncControllingParameter)
- {
- dynamic keyValues = Newtonsoft.Json.Linq.JObject.Parse(request.ServiceDataJson);
- foreach (dynamic keyValue in keyValues.Data)
- {
- var key = (string)keyValue.Key;
- dynamic setValue = keyValue.Value;
- if ((int)keyValue.SyncMethod == 0)
- {
- var data = new CacheControllingParameter();
- data.Code = setValue.Code;
- data.Parameter = setValue.Parameter;
- data.ProbeApplication = setValue.ProbeApplication;
- Func<string, CacheControllingParameter, CacheControllingParameter> updateValueFactory = (a, b) => data;
- _controllingParameterDevices.AddOrUpdate(key, data, updateValueFactory);
- }
- }
- }
- else
- {
- var remoteConnectData = JsonConvert.DeserializeObject<RemoteConnectDataRequest>(request.ServiceDataJson);
- if (remoteConnectData != null)
- {
- switch (request.SyncType)
- {
- case SyncTypeEnum.ConsultionRemoteConnect:
- var controlType = ControlDeviceParameterEnum.Start;
- if (remoteConnectData.UserRemoteConnect.StatusEnum == ConnectStatusEnum.WaitConnect)
- {
- controlType = ControlDeviceParameterEnum.Start;
- }
- else if (remoteConnectData.UserRemoteConnect.StatusEnum == ConnectStatusEnum.WaitDisconnect)
- {
- controlType = ControlDeviceParameterEnum.End;
- }
- var applyConsultionReq = new ControlDeviceConnectRequest()
- {
- RoomCode = remoteConnectData.UserRemoteConnect.RoomId,
- ControlType = controlType,
- DeviceCode = remoteConnectData.UserRemoteConnect.DeviceCode,
- Token = remoteConnectData.UserRemoteConnect.UserToken,
- IsNeedSyn = false
- };
- res = await ConsultionApplyRemoteConnect(applyConsultionReq);
- break;
- case SyncTypeEnum.ConsultionDisConnect:
- var type = ControlDeviceParameterEnum.Start;
- if (remoteConnectData.UserRemoteConnect.StatusEnum == ConnectStatusEnum.CompleteConnect)
- {
- type = ControlDeviceParameterEnum.Start;
- }
- else if (remoteConnectData.UserRemoteConnect.StatusEnum == ConnectStatusEnum.WaitDisconnect)
- {
- type = ControlDeviceParameterEnum.End;
- }
- else if (remoteConnectData.UserRemoteConnect.StatusEnum == ConnectStatusEnum.WaitConnect)
- {
- type = ControlDeviceParameterEnum.RejectConnect;
- }
- var closeConsultionReq = new ControlDeviceResponseRequest()
- {
- UserCode = remoteConnectData.UserRemoteConnect.UserCode,
- UserName = "",
- LoginSource = remoteConnectData.UserRemoteConnect.LoginSource,
- ControlType = type,
- Token = remoteConnectData.UserRemoteConnect.DeviceToken,
- IsNeedSyn = false
- };
- res = await ConsultionAcceptRemoteConnnectionAsync(closeConsultionReq);
- break;
- case SyncTypeEnum.ApplyRemoteConnect:
- var applyReq = new ControlDeviceConnectRequest()
- {
- DeviceCode = remoteConnectData.UserRemoteConnect.DeviceCode,
- ControlType = ControlDeviceParameterEnum.Start,
- Token = remoteConnectData.UserRemoteConnect.UserToken,
- IsNeedSyn = false
- };
- res = await ApplyRemoteConnectionAsync(applyReq);
- break;
- case SyncTypeEnum.AcceptRemoteConnect:
- var acceptRemoteConnectReq = new ControlDeviceResponseRequest()
- {
- UserCode = remoteConnectData.UserRemoteConnect.UserCode,
- UserName = "",
- LoginSource = remoteConnectData.UserRemoteConnect.LoginSource,
- ControlType = ControlDeviceParameterEnum.Start,
- Token = remoteConnectData.UserRemoteConnect.DeviceToken,
- IsNeedSyn = false
- };
- res = await AcceptRemoteConnnectionAsync(acceptRemoteConnectReq);
- break;
- case SyncTypeEnum.RejectRemoteConnect:
- var rejectRemoteConnectReq = new ControlDeviceResponseRequest()
- {
- UserCode = remoteConnectData.UserRemoteConnect.UserCode,
- UserName = "",
- LoginSource = remoteConnectData.UserRemoteConnect.LoginSource,
- ControlType = ControlDeviceParameterEnum.RejectConnect,
- Token = remoteConnectData.UserRemoteConnect.DeviceToken,
- IsNeedSyn = false
- };
- res = await AcceptRemoteConnnectionAsync(rejectRemoteConnectReq);
- break;
- case SyncTypeEnum.ApplyDisConnect:
- var applyDisconnectReq = new ControlDeviceConnectRequest()
- {
- DeviceCode = remoteConnectData.UserRemoteConnect.DeviceCode,
- ControlType = ControlDeviceParameterEnum.End,
- Token = remoteConnectData.UserRemoteConnect.UserToken,
- IsNeedSyn = false
- };
- res = await ApplyRemoteConnectionAsync(applyDisconnectReq);
- break;
- case SyncTypeEnum.AcceptDisConnect:
- var acceptDisConnect = new ControlDeviceResponseRequest()
- {
- UserCode = remoteConnectData.UserRemoteConnect.UserCode,
- UserName = "",
- LoginSource = remoteConnectData.UserRemoteConnect.LoginSource,
- ControlType = ControlDeviceParameterEnum.End,
- Token = remoteConnectData.UserRemoteConnect.DeviceToken,
- IsNeedSyn = false
- };
- res = await AcceptRemoteConnnectionAsync(acceptDisConnect);
- break;
- case SyncTypeEnum.DeviceDisconnect:
- var deviceDisconnect = new RemoteConnectStautsRequest()
- {
- UserCode = remoteConnectData.UserRemoteConnect.UserCode,
- Token = remoteConnectData.UserRemoteConnect.DeviceToken,
- IsNeedSyn = false
- };
- res = await DisconnectRemoteControl(deviceDisconnect);
- break;
- case SyncTypeEnum.DeviceRemoteConnectHeartRate:
- var deviceRemoteConnectHeartRate = new RemoteConnectHeartRateRequest()
- {
- TransactionType = remoteConnectData.UserRemoteConnect.TransactionType,
- Token = remoteConnectData.UserRemoteConnect.DeviceToken,
- IsNeedSyn = false
- };
- res = await DeviceRemoteConnectHeartRateAsync(deviceRemoteConnectHeartRate);
- break;
- case SyncTypeEnum.UserRemoteConnectHeartRate:
- var userRemoteConnectHeartRate = new RemoteConnectHeartRateRequest()
- {
- TransactionType = remoteConnectData.UserRemoteConnect.TransactionType,
- Token = remoteConnectData.UserRemoteConnect.UserToken,
- IsNeedSyn = false
- };
- res = await RemoteConnectHeartRateAsync(userRemoteConnectHeartRate);
- break;
- case SyncTypeEnum.DeviceRemoteConnectOutline:
- var deviceRemoteConnectOutline = new DeviceRemoteConnectDTO(
- remoteConnectData.UserRemoteConnect.DeviceToken,
- remoteConnectData.UserRemoteConnect.UserToken,
- remoteConnectData.UserRemoteConnect.UserCode,
- remoteConnectData.UserRemoteConnect.DeviceCode,
- remoteConnectData.UserRemoteConnect.TransactionType
- );
- res = DeviceRemoteConnectOnlineState(deviceRemoteConnectOutline, false, remoteConnectData.IsManangeDisConnnect);
- break;
- case SyncTypeEnum.UserRemoteConnectOutline:
- var userRemoteConnectOutline = remoteConnectData.UserRemoteConnect.MappingTo<RemoteConnectDTO>();
- res = RemoteConnectOnlineState(userRemoteConnectOutline, false, remoteConnectData.IsManangeDisConnnect);
- break;
- case SyncTypeEnum.RestartDevice:
- var restartDevice = new RestartDeviceRequest()
- {
- DeviceCode = remoteConnectData.UserRemoteConnect.DeviceCode,
- Token = remoteConnectData.UserRemoteConnect.UserToken,
- IsNeedSyn = false
- };
- res = await RestartDeviceAsync(restartDevice);
- break;
- default:
- return res;
- }
- }
- }
- return res;
- }
- catch (Exception ex)
- {
- Logger.WriteLineWarn($"DeviceService RemoteConnect SyncServerMessageAsync err, {ex}, request data:{request.ServiceDataJson}");
- return false;
- }
- }
- }
- }
|