rpc.dart 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. library fis_jsonrpc;
  2. import 'dart:collection';
  3. import 'package:fis_common/extensions/type.dart';
  4. import 'package:fis_common/logger/logger.dart';
  5. import 'client_base.dart';
  6. import 'interceptor.dart';
  7. import 'services/index.dart';
  8. export 'services/index.dart';
  9. export 'request.dart';
  10. export 'exception.dart';
  11. export 'interceptor.dart';
  12. export 'notifications/index.dart';
  13. typedef T ServiceBuilder<T extends JsonRpcClientBase>();
  14. const C_SHELL_RPC_DEFAULT_HOST = 'platform.fis.plus';
  15. /// JSON-RPC 代理
  16. class JsonRpcProxy {
  17. JsonRpcProxy({
  18. String? host,
  19. this.platformHost = C_SHELL_RPC_DEFAULT_HOST,
  20. }) {
  21. _currentHost = host ?? "unknown";
  22. }
  23. /// 宿主平台代理地址
  24. final String platformHost;
  25. /// 服务主机地址
  26. late String _currentHost;
  27. /// 服务主机协议
  28. late String _currentProtocol;
  29. /// 当前服务主机地址
  30. String get currentHostAddress => "$_currentProtocol://$_currentHost";
  31. HashMap<Type, dynamic> _serviceCache = HashMap();
  32. static PlatformService? _platformService;
  33. /* 服务代理设置 Start */
  34. /// 平台服务
  35. PlatformService get platform {
  36. if (_platformService == null)
  37. _platformService = PlatformService("http://$platformHost");
  38. return _platformService!;
  39. }
  40. LiveConsultationService get liveConsultation =>
  41. findService(() => new LiveConsultationService(currentHostAddress));
  42. AIDiagnosisService get aIDiagnosis =>
  43. findService(() => new AIDiagnosisService(currentHostAddress));
  44. AppletAPIService get appletAPI =>
  45. findService(() => new AppletAPIService(currentHostAddress));
  46. ASRService get aSR =>
  47. findService(() => new ASRService(currentHostAddress));
  48. AuthenticationService get authentication =>
  49. findService(() => new AuthenticationService(currentHostAddress));
  50. ChatMessageService get chatMessage =>
  51. findService(() => new ChatMessageService(currentHostAddress));
  52. ConnectService get connect =>
  53. findService(() => new ConnectService(currentHostAddress));
  54. DeployPlatformService get deployPlatform =>
  55. findService(() => new DeployPlatformService(currentHostAddress));
  56. DeviceService get device =>
  57. findService(() => new DeviceService(currentHostAddress));
  58. EducationService get education =>
  59. findService(() => new EducationService(currentHostAddress));
  60. EmailService get email =>
  61. findService(() => new EmailService(currentHostAddress));
  62. IdentityApplyService get identityApply =>
  63. findService(() => new IdentityApplyService(currentHostAddress));
  64. LabService get lab =>
  65. findService(() => new LabService(currentHostAddress));
  66. LoginService get login =>
  67. findService(() => new LoginService(currentHostAddress));
  68. OrganizationService get organization =>
  69. findService(() => new OrganizationService(currentHostAddress));
  70. PatientService get patient =>
  71. findService(() => new PatientService(currentHostAddress));
  72. PaymentService get payment =>
  73. findService(() => new PaymentService(currentHostAddress));
  74. PositionService get position =>
  75. findService(() => new PositionService(currentHostAddress));
  76. RankService get rank =>
  77. findService(() => new RankService(currentHostAddress));
  78. RecordInfoService get recordInfo =>
  79. findService(() => new RecordInfoService(currentHostAddress));
  80. RegionService get region =>
  81. findService(() => new RegionService(currentHostAddress));
  82. RemedicalService get remedical =>
  83. findService(() => new RemedicalService(currentHostAddress));
  84. ReportService get report =>
  85. findService(() => new ReportService(currentHostAddress));
  86. RoleService get role =>
  87. findService(() => new RoleService(currentHostAddress));
  88. SMSService get sMS =>
  89. findService(() => new SMSService(currentHostAddress));
  90. StorageService get storage =>
  91. findService(() => new StorageService(currentHostAddress));
  92. UpgradeService get upgrade =>
  93. findService(() => new UpgradeService(currentHostAddress));
  94. UserService get user =>
  95. findService(() => new UserService(currentHostAddress));
  96. VinnoServerService get vinnoServer =>
  97. findService(() => new VinnoServerService(currentHostAddress));
  98. /* 服务代理设置 End */
  99. /// 设置服务主机地址
  100. void setServerHost(String address, [bool useSSL = false]) {
  101. logger.i('JsonRpcProxy setServerHost :' + address);
  102. _currentProtocol = useSSL ? "https" : "http";
  103. _currentHost = address;
  104. }
  105. /// 添加拦截器
  106. void addInterceptor(JsonRpcInterceptor interceptor) =>
  107. jsonRpcInterceptHost.addInterceptor(interceptor);
  108. /// 清空缓存
  109. void clearCache() => _serviceCache.clear();
  110. /// 查找Service实例
  111. T findService<T extends JsonRpcClientBase>(ServiceBuilder<T> builder) {
  112. Type serviceType = typeOf<T>();
  113. if (!_serviceCache.containsKey(serviceType)) {
  114. _serviceCache[serviceType] = builder.call();
  115. }
  116. return _serviceCache[serviceType] as T;
  117. }
  118. }