rpc.dart 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. String get currentHostAddress => "http://$_currentHost";
  29. HashMap<Type, dynamic> _serviceCache = HashMap();
  30. static PlatformService? _platformService;
  31. /* 服务代理设置 Start */
  32. /// 平台服务
  33. PlatformService get platform {
  34. if (_platformService == null)
  35. _platformService = PlatformService("http://$platformHost");
  36. return _platformService!;
  37. }
  38. LiveConsultationService get liveConsultation =>
  39. findService(() => new LiveConsultationService(currentHostAddress));
  40. AIDiagnosisService get aIDiagnosis =>
  41. findService(() => new AIDiagnosisService(currentHostAddress));
  42. ASRService get aSR =>
  43. findService(() => new ASRService(currentHostAddress));
  44. AuthenticationService get authentication =>
  45. findService(() => new AuthenticationService(currentHostAddress));
  46. ConnectService get connect =>
  47. findService(() => new ConnectService(currentHostAddress));
  48. DeployPlatformService get deployPlatform =>
  49. findService(() => new DeployPlatformService(currentHostAddress));
  50. DeviceService get device =>
  51. findService(() => new DeviceService(currentHostAddress));
  52. EducationService get education =>
  53. findService(() => new EducationService(currentHostAddress));
  54. EmailService get email =>
  55. findService(() => new EmailService(currentHostAddress));
  56. IdentityApplyService get identityApply =>
  57. findService(() => new IdentityApplyService(currentHostAddress));
  58. LabService get lab =>
  59. findService(() => new LabService(currentHostAddress));
  60. LoginService get login =>
  61. findService(() => new LoginService(currentHostAddress));
  62. OrganizationService get organization =>
  63. findService(() => new OrganizationService(currentHostAddress));
  64. PatientService get patient =>
  65. findService(() => new PatientService(currentHostAddress));
  66. PaymentService get payment =>
  67. findService(() => new PaymentService(currentHostAddress));
  68. PositionService get position =>
  69. findService(() => new PositionService(currentHostAddress));
  70. RankService get rank =>
  71. findService(() => new RankService(currentHostAddress));
  72. RecordInfoService get recordInfo =>
  73. findService(() => new RecordInfoService(currentHostAddress));
  74. RegionService get region =>
  75. findService(() => new RegionService(currentHostAddress));
  76. RemedicalService get remedical =>
  77. findService(() => new RemedicalService(currentHostAddress));
  78. ReportService get report =>
  79. findService(() => new ReportService(currentHostAddress));
  80. RoleService get role =>
  81. findService(() => new RoleService(currentHostAddress));
  82. SMSService get sMS =>
  83. findService(() => new SMSService(currentHostAddress));
  84. StorageService get storage =>
  85. findService(() => new StorageService(currentHostAddress));
  86. UpgradeService get upgrade =>
  87. findService(() => new UpgradeService(currentHostAddress));
  88. UserService get user =>
  89. findService(() => new UserService(currentHostAddress));
  90. VinnoServerService get vinnoServer =>
  91. findService(() => new VinnoServerService(currentHostAddress));
  92. /* 服务代理设置 End */
  93. /// 设置服务主机地址
  94. void setServerHost(String address) {
  95. logger.i('JsonRpcProxy setServerHost :' + address);
  96. _currentHost = address;
  97. }
  98. /// 添加拦截器
  99. void addInterceptor(JsonRpcInterceptor interceptor) =>
  100. jsonRpcInterceptHost.addInterceptor(interceptor);
  101. /// 清空缓存
  102. void clearCache() => _serviceCache.clear();
  103. /// 查找Service实例
  104. T findService<T extends JsonRpcClientBase>(ServiceBuilder<T> builder) {
  105. Type serviceType = typeOf<T>();
  106. if (!_serviceCache.containsKey(serviceType)) {
  107. _serviceCache[serviceType] = builder.call();
  108. }
  109. return _serviceCache[serviceType] as T;
  110. }
  111. }