123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693 |
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using Vinno.IUS.Common;
- using Vinno.IUS.Common.Log;
- using Vinno.IUS.Common.Network.Transfer;
- using Vinno.vCloud.Client.Proxy.Interfaces;
- using Vinno.vCloud.Protocol.Infrastructures;
- using Vinno.vCloud.Protocol.Initializers;
- using Vinno.vCloud.Protocol.Messages.Client.Account;
- using Vinno.vCloud.Protocol.Messages.Client.Remedical.Reports;
- using Vinno.vCloud.Protocol.Messages.Client.Remedical.TerminalDatas;
- using Vinno.vCloud.Protocol.Messages.Client.Remedical.TerminialReords;
- using Vinno.vCloud.Protocol.Messages.Client.RemoteDiagnosis;
- using Vinno.vCloud.Protocol.Messages.Common;
- using Vinno.vCloud.Protocol.Messages.Live;
- using Vinno.vCloud.Protocol.Messages.Report;
- namespace Vinno.vCloud.Client.Proxy
- {
- public class VCloudClientsProxy
- {
- private DefaultLogEngine _logEngine;
- private ConcurrentDictionary<string, IFlyinsonoClient> _vCloudClients = new ConcurrentDictionary<string, IFlyinsonoClient>();
- private string _hostUrl;
- private static VCloudClientsProxy _instance;
- /// <summary>
- /// The unique instance of app manager
- /// </summary>
- public static VCloudClientsProxy Instance => _instance ?? (_instance = new VCloudClientsProxy());
- public event EventHandler<ConversationMemberNotification> MeetingMemberNotificationArrived;//成员改变通知
- public event EventHandler<HangupConversationMemberNotification> MeetingHangupNotificationArrived;//挂断通知
- public event EventHandler<RejectConversationNotification> MeetingRejectNotificationArrived;//对方拒绝通知
- public event EventHandler<AcceptConversationNotification> MeetingAcceptNotificationArrived;//发起者收到对方接受通知
- public event EventHandler<PendingTimeoutConversationNotification> MeetingPendingMemberTimeoutNotificationArrived;//应答超时通知
- private readonly ManualResetEvent _closeEvent = new ManualResetEvent(false);
- private readonly int _sessionCheckInterval = 30 * 60 * 1000;
- /// <summary>
- /// instance intialize
- /// </summary>
- public void Initialize(string hostUrl)
- {
- _hostUrl = hostUrl;
- ClientTagsInitializer.Initialize();
- var logPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "VCloudClientsProxyLogs");
- CommonParameters.DataFolder = logPath;
- _logEngine = new DefaultLogEngine();
- Logger.RegisterEngine(_logEngine);
- StartCheckSessions();
- }
- /// <summary>
- /// Login with specified wechat token, vCloud user name and password
- /// </summary>
- /// <param name="wechatToken"></param>
- /// <param name="userName"></param>
- /// <param name="password"></param>
- /// <returns></returns>
- public LoginStatus Login(string openId, string userName, string password)
- {
- Logger.WriteLineInfo($"VCloudClient Login openId:{openId} Name:{userName}");
- var client = CreateCloudClient(openId, _hostUrl);
- var result = client.LoginByOpenId(openId, userName, password);
- if (result == LoginStatus.Success)
- {
- _vCloudClients.AddOrUpdate(openId, client, (k, c) => client);
- }
- else
- {
- CloudClientDispose(client);
- }
- return result;
- }
- private VCloudClient CreateCloudClient(string openId, string hostUrl)
- {
- Logger.WriteLineInfo($"VCloudClient CreateCloudClient openId:{openId} hostUrl:{hostUrl}");
- var client = new VCloudClient(openId, hostUrl);
- client.MeetingMemberNotificationArrived += OnMeetingMemberNotificationArrived;
- client.MeetingHangupNotificationArrived += OnMeetingHangupNotificationArrived;
- client.RejectMeetingNotificationArrived += OnMeetingRejectNotificationArrived;
- client.AcceptMeetingNotificationArrived += OnMeetingAcceptNotificationArrived;
- client.ChatPendingMemberTimeoutNotificationArrived += OnMneetingPendingMemberTimeoutNotificationArrived;
- return client;
- }
- private void CloudClientDispose(VCloudClient vCloudClient)
- {
- vCloudClient.MeetingMemberNotificationArrived -= OnMeetingMemberNotificationArrived;
- vCloudClient.MeetingHangupNotificationArrived -= OnMeetingHangupNotificationArrived;
- vCloudClient.RejectMeetingNotificationArrived -= OnMeetingRejectNotificationArrived;
- vCloudClient.AcceptMeetingNotificationArrived -= OnMeetingAcceptNotificationArrived;
- vCloudClient.ChatPendingMemberTimeoutNotificationArrived -= OnMneetingPendingMemberTimeoutNotificationArrived;
- vCloudClient.Dispose();
- }
- private void OnMeetingAcceptNotificationArrived(object sender, AcceptMeetingNotification e)
- {
- if (sender is VCloudClient vCloudClient)
- {
- MeetingAcceptNotificationArrived?.Invoke(this, new AcceptConversationNotification
- {
- OpenId = vCloudClient.OpenId,
- AcceptMeetingNotification = e
- });
- }
- }
- private void OnMeetingRejectNotificationArrived(object sender, RejectMeetingNotification e)
- {
- if (sender is VCloudClient vCloudClient)
- {
- MeetingRejectNotificationArrived?.Invoke(this, new RejectConversationNotification
- {
- OpenId = vCloudClient.OpenId,
- RejectMeetingNotification = e
- });
- }
- }
- private void OnMeetingHangupNotificationArrived(object sender, MeetingHangupNotification e)
- {
- if (sender is VCloudClient vCloudClient)
- {
- MeetingHangupNotificationArrived?.Invoke(this, new HangupConversationMemberNotification
- {
- OpenId = vCloudClient.OpenId,
- MeetingHangupNotification = e
- });
- }
- }
- private void OnMeetingMemberNotificationArrived(object sender, MeetingMemberNotification e)
- {
- if (sender is VCloudClient vCloudClient)
- {
- MeetingMemberNotificationArrived?.Invoke(this, new ConversationMemberNotification
- {
- OpenId = vCloudClient.OpenId,
- MeetingMemberNotification = e
- });
- }
- }
- private void OnMneetingPendingMemberTimeoutNotificationArrived(object sender, ChatPendingMemberTimeoutNotification e)
- {
- if (sender is VCloudClient vCloudClient)
- {
- MeetingPendingMemberTimeoutNotificationArrived?.Invoke(this, new PendingTimeoutConversationNotification()
- {
- OpenId = vCloudClient.OpenId,
- Notification = e
- });
- }
- }
- /// <summary>
- /// 开始会诊
- /// </summary>
- /// <param name="openId"></param>
- /// <param name="roomId"></param>
- /// <param name="consultationId"></param>
- /// <returns></returns>
- public ConsultationResult StartConversation(string openId, string roomId, string consultationId)
- {
- Logger.WriteLineInfo($"VCloudClient StartConversation openId:{openId} roomId:{roomId} consultationId:{consultationId}");
- var vCloudClient = GetVCloudClient(openId);
- if (vCloudClient != null)
- {
- var consultationMessage = vCloudClient.FindConsultationById(consultationId);
- vCloudClient.ArrangeConsultation(new ArrangeAppointmentRequest()
- {
- Id = consultationId,
- AppointDate = DateTime.UtcNow,
- ExpertId = consultationMessage?.ExpertId
- });
- return vCloudClient.StartConversation(roomId, consultationId);
- }
- return null;
- }
- public LiveStates CancelStartConversation(string openId, string roomId)
- {
- Logger.WriteLineInfo($"VCloudClient CancelStartConversation openId:{openId} roomId:{roomId}");
- var vCloudClient = GetVCloudClient(openId);
- if (vCloudClient != null)
- {
- return vCloudClient.CancelStartConversation(roomId);
- }
- return LiveStates.UnknowException;
- }
- public LiveStates ExitConsultation(string openId, string roomId, bool forceClosed, string terminalId, string consultationId)
- {
- Logger.WriteLineInfo($"VCloudClient ExitConsultation openId:{openId} roomId:{roomId} consultationId:consultationId");
- var vCloudClient = GetVCloudClient(openId);
- if (vCloudClient != null)
- {
- return vCloudClient.ExitConsultation(roomId, forceClosed, terminalId, consultationId);
- }
- return LiveStates.UnknowException;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="roomId"></param>
- /// <param name="initiatorId">发起者</param>
- /// <returns></returns>
- public LiveStates AcceptConsultation(string openId, string roomId, string initiatorId)
- {
- var vCloudClient = GetVCloudClient(openId);
- if (vCloudClient != null)
- {
- return vCloudClient.AcceptConsultation(roomId, FeatureSource.RemoteDiagnosis, LiveStates.OK, initiatorId);
- }
- return LiveStates.UnknowException;
- }
- public LiveStates RejectConsultation(string openId, string roomId, string initiatorId)
- {
- var vCloudClient = GetVCloudClient(openId);
- if (vCloudClient != null)
- {
- return vCloudClient.RejectConsultation(roomId, initiatorId);
- }
- return LiveStates.UnknowException;
- }
- public ConsultationResult InviteConsultationMember(string openId, string roomId, List<RecipientInfo> recipientInfos)
- {
- var vCloudClient = GetVCloudClient(openId);
- if (vCloudClient != null)
- {
- return vCloudClient.InviteConsultationMember(roomId, FeatureSource.RemoteDiagnosis, recipientInfos);
- }
- return new ConsultationResult(LiveStates.UnknowException);
- }
- /// <summary>
- /// 通过openId获取用户信息
- /// </summary>
- /// <param name="openId"></param>
- /// <returns></returns>
- public WechatUserInfo GetUserInfoByOpenId(string openId)
- {
- Logger.WriteLineInfo($"VCloudClient GetUserInfoByOpenId {openId}");
- _vCloudClients.TryGetValue(openId, out IFlyinsonoClient vCloudClient);
- if (vCloudClient != null)
- {
- var userInfo = vCloudClient.GetUserInfoByOpenId(openId);
- Logger.WriteLineInfo($"VCloudClient GetUserInfoByOpenId, client existed, openId:{openId}, name:{userInfo.UserId} {userInfo.NickName}");
- return userInfo;
- }
- var client = CreateCloudClient(openId, _hostUrl);
- var result = client.LoginByOpenId(openId);
- if (result == LoginStatus.Success)
- {
- _vCloudClients.AddOrUpdate(openId, client, (k, c) => client);
- var userInfo= client.GetUserInfoByOpenId(openId);
- Logger.WriteLineInfo($"VCloudClient GetUserInfoByOpenId, login success, openId:{openId}, name:{userInfo.UserId} {userInfo.NickName}");
- return userInfo;
- }
- else
- {
- Logger.WriteLineInfo($"VCloudClient GetUserInfoByOpenId, login failed, openId:{openId}");
- CloudClientDispose(client);
- }
- return null;
- }
- private IFlyinsonoClient GetVCloudClient(string openId)
- {
- Logger.WriteLineInfo($"VCloudClient GetVCloudClient openId:{openId}");
- _vCloudClients.TryGetValue(openId, out IFlyinsonoClient vCloudClient);
- if (vCloudClient != null)
- {
- vCloudClient.Activate();
- return vCloudClient;
- }
- else
- {
- var client = CreateCloudClient(openId, _hostUrl);
- var result = client.LoginByOpenId(openId);
- if (result == LoginStatus.Success)
- {
- _vCloudClients.AddOrUpdate(openId, client, (k, c) => client);
- return client;
- }
- else
- {
- CloudClientDispose(client);
- }
- }
- return null;
- }
- /// <summary>
- /// 获取会诊我的超声机
- /// </summary>
- /// <param name="openId"></param>
- /// <returns></returns>
- public List<TerminalInfo> GetMyTerminals(string openId)
- {
- var vCloudClient = GetVCloudClient(openId);
- if (vCloudClient != null)
- {
- return vCloudClient.GetMyTerminals();
- }
- return null;
- }
- /// <summary>
- /// 获取会诊我的医院
- /// </summary>
- /// <param name="openId"></param>
- /// <returns></returns>
- public List<OrganBaseInfoMessage> GetMyHospitals(string openId)
- {
- Logger.WriteLineInfo($"VCloudClient GetMyHospitals openId:{openId}");
- var vCloudClient = GetVCloudClient(openId);
- if (vCloudClient != null)
- {
- return vCloudClient.GetMyHospitals();
- }
- return null;
- }
- /// <summary>
- /// 查询会诊记录
- /// </summary>
- public FindNewAppointmentsResult6 FindConsultationRecords(string openId, int startIndex, int pageSize, AppointmentsFilterMessage filter)
- {
- Logger.WriteLineInfo($"VCloudClient FindConsultationRecords openId:{openId} startIndex:{startIndex} pageSize:{pageSize}");
- var vCloudClient = GetVCloudClient(openId);
- if (vCloudClient != null)
- {
- return vCloudClient.FindConsultationRecords(startIndex, pageSize, filter);
- }
- return null;
- }
- /// <summary>
- /// 创建会诊
- /// </summary>
- public string CreateConsultationRecord(string openId, CreateAppointmentRequest1 request)
- {
- Logger.WriteLineInfo($"VCloudClient CreateConsultationRecord openId:{openId}");
- var vCloudClient = GetVCloudClient(openId);
- if (vCloudClient != null)
- {
- return vCloudClient.CreateConsultationRecord(request);
- }
- return null;
- }
- /// <summary>
- /// 通过医院获取专家列表
- /// </summary>
- /// <param name=""></param>
- /// <returns></returns>
- public List<ExpertDetailMessage> FindMyExpertsByHospitalId(string openId, string hospitalId)
- {
- var vCloudClient = GetVCloudClient(openId);
- if (vCloudClient != null)
- {
- return vCloudClient.FindMyExpertsByHospitalId(hospitalId);
- }
- return null;
- }
- /// <summary>
- /// 获取检查部位
- /// </summary>
- /// <param name="openId"></param>
- /// <returns></returns>
- public List<string> GetCheckPoint(string openId)
- {
- var vCloudClient = GetVCloudClient(openId);
- if (vCloudClient != null)
- {
- return vCloudClient.GetCheckPoint();
- }
- return null;
- }
- /// <summary>
- ///通过远程记录Id查询图像
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public GetPatientRecordDatasSuccess9 GetPatientRecordImageById(string openId, GetPatientRecordDatasRequest11 request)
- {
- Logger.WriteLineInfo($"VCloudClient GetPatientRecordImageById openId:{openId}");
- var vCloudClient = GetVCloudClient(openId);
- if (vCloudClient != null)
- {
- return vCloudClient.GetPatientRecordImageById(request);
- }
- return null;
- }
- /// <summary>
- /// 通过记录Id获取报告
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public NewGetReportsSuccess5 GetReportByecordId(string openId, NewGetReportsRequest5 request)
- {
- Logger.WriteLineInfo($"VCloudClient GetReportByecordId openId:{openId}");
- var vCloudClient = GetVCloudClient(openId);
- if (vCloudClient != null)
- {
- return vCloudClient.GetReportByecordId(request);
- }
- return null;
- }
- /// <summary>
- /// 查询报告详情
- /// </summary>
- /// <param name="openId"></param>
- /// <param name="recordId"></param>
- /// <param name="reportId"></param>
- /// <returns></returns>
- public NewReportInfoMessage5 GetReportById(string openId, string recordId, string reportId)
- {
- Logger.WriteLineInfo($"VCloudClient GetReportById openId:{openId}");
- var vCloudClient = GetVCloudClient(openId);
- if (vCloudClient != null)
- {
- return vCloudClient.GetReportById(recordId, reportId);
- }
- return null;
- }
- /// <summary>
- /// 查询分时远程诊断记录
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public GetRecords11Success GetExamRecords(string openId, GetClientRecord11Request request)
- {
- Logger.WriteLineInfo($"VCloudClient GetExamRecords openId:{openId}");
- var vCloudClient = GetVCloudClient(openId);
- if (vCloudClient != null)
- {
- return vCloudClient.GetExamRecords(request);
- }
- return null;
- }
- /// <summary>
- /// Log out with specifed wechat token
- /// </summary>
- /// <param name="wechatToken"></param>
- /// <returns></returns>
- public bool Logout(string openId)
- {
- var result = _vCloudClients.TryRemove(openId, out var client);
- if (result && client != null)
- {
- client.Logout();
- CloudClientDispose(client as VCloudClient);
- return true;
- }
- return false;
- }
- /// <summary>
- /// 查询系统默认报告模板
- /// </summary>
- /// <param name="openId"></param>
- /// <param name="languageCode"></param>
- /// <returns></returns>
- public List<ReportTemplateMessage> GetReportTemplates(string openId, string languageCode)
- {
- var vCloudClient = GetVCloudClient(openId);
- if (vCloudClient != null)
- {
- return vCloudClient.GetReportTemplates(languageCode);
- }
- return new List<ReportTemplateMessage>();
- }
- /// <summary>
- /// 查询系统默认词条
- /// </summary>
- /// <param name="openId"></param>
- /// <param name="languageCode"></param>
- /// <returns></returns>
- public List<ReportTemplateMessage> GetThesaurusTemplates(string openId, string languageCode)
- {
- var vCloudClient = GetVCloudClient(openId);
- if (vCloudClient != null)
- {
- return vCloudClient.GetThesaurusTemplates(languageCode);
- }
- return new List<ReportTemplateMessage>();
- }
- /// <summary>
- /// 获取会诊信息
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public ConsultationMessage FindConsultationById(string openId, string id)
- {
- var vCloudClient = GetVCloudClient(openId);
- if (vCloudClient != null)
- {
- return vCloudClient.FindConsultationById(id);
- }
- return null;
- }
- /// <summary>
- /// 上传会诊状态
- /// </summary>
- /// <param name=""></param>
- /// <returns></returns>
- public bool UpdateRecordState(string openId, UpdateAppointmentStateRequest request)
- {
- var vCloudClient = GetVCloudClient(openId);
- if (vCloudClient != null)
- {
- return vCloudClient.UpdateRecordState(request);
- }
- return false;
- }
- /// <summary>
- /// 安排申请单
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public bool ArrangeConsultation(string openId, ArrangeAppointmentRequest request)
- {
- var vCloudClient = GetVCloudClient(openId);
- if (vCloudClient != null)
- {
- return vCloudClient.ArrangeConsultation(request);
- }
- return false;
- }
- /// <summary>
- /// 安排申请单
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public NewGetReportsSuccess5 GetConsultationReports(string openId, GetConsultationReportsRequest5 request)
- {
- var vCloudClient = GetVCloudClient(openId);
- if (vCloudClient != null)
- {
- return vCloudClient.GetConsultationReports(request);
- }
- return null;
- }
- /// <summary>
- /// 保存报告
- /// </summary>
- /// <param name="openId"></param>
- /// <param name="request"></param>
- /// <returns></returns>
- public string SaveReport(string openId, NewAddReportRequest4 request)
- {
- var vCloudClient = GetVCloudClient(openId);
- if (vCloudClient != null)
- {
- return vCloudClient.SaveReport(request);
- }
- return string.Empty;
- }
- /// <summary>
- /// Start a thread to check sessions intervally.
- /// </summary>
- private async void StartCheckSessions()
- {
- Logger.WriteLineInfo("SessionManager start check sessions.");
- await Task.Run(() =>
- {
- while (!_closeEvent.WaitOne(_sessionCheckInterval))
- {
- try
- {
- var removeSessionId = new List<string>();
- foreach (var session in _vCloudClients.Values)
- {
- session.DeActivate();
- if (session.LeftTime <= 0)
- {
- removeSessionId.Add(session.OpenId);
- }
- }
- foreach (var openId in removeSessionId)
- {
- _vCloudClients.TryRemove(openId, out IFlyinsonoClient vCloudClient);
- CloudClientDispose(vCloudClient as VCloudClient);
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"CheckSessions error:{ex.Message}");
- }
- Logger.WriteLineVerbose("Sessions checked.");
- }
- });
- }
- /// <summary>
- /// 上传会诊截图
- /// </summary>
- /// <param name="consulationId"></param>
- /// <param name="imageToken"></param>
- /// <returns></returns>
- public bool UploadConsultationImages(string openId, string consulationId, string imageToken)
- {
- var vCloudClient = GetVCloudClient(openId);
- if (vCloudClient != null)
- {
- return vCloudClient.UploadConsultationImages(consulationId, imageToken);
- }
- return false;
- }
- /// <summary>
- /// 上传会诊视频
- /// </summary>
- /// <param name="consulationId"></param>
- /// <param name="imageToken"></param>
- /// <returns></returns>
- public bool UploadConsulationVideos(string openId, string consulationId, string videoToken, string previewToken)
- {
- var vCloudClient = GetVCloudClient(openId);
- if (vCloudClient != null)
- {
- return vCloudClient.UploadConsulationVideos(consulationId, videoToken, previewToken);
- }
- return false;
- }
- public string UploadFile(string openId, byte[] fileData)
- {
- var vCloudClient = GetVCloudClient(openId);
- if (vCloudClient != null)
- {
- return vCloudClient.UploadFile(fileData);
- }
- return null;
- }
- public byte[] DownloadFile(string openId, string fileToken)
- {
- var vCloudClient = GetVCloudClient(openId);
- if (vCloudClient != null)
- {
- return vCloudClient.DownloadFile(fileToken);
- }
- return null;
- }
- /// <summary>
- /// Stop the session check thread.
- /// </summary>
- public void StopCheckSessions()
- {
- _closeEvent.Set();
- }
- }
- }
|