rpc.dart 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. LoginService get login =>
  59. findService(() => new LoginService(currentHostAddress));
  60. OrganizationService get organization =>
  61. findService(() => new OrganizationService(currentHostAddress));
  62. PatientService get patient =>
  63. findService(() => new PatientService(currentHostAddress));
  64. PaymentService get payment =>
  65. findService(() => new PaymentService(currentHostAddress));
  66. PositionService get position =>
  67. findService(() => new PositionService(currentHostAddress));
  68. RankService get rank =>
  69. findService(() => new RankService(currentHostAddress));
  70. RecordInfoService get recordInfo =>
  71. findService(() => new RecordInfoService(currentHostAddress));
  72. RegionService get region =>
  73. findService(() => new RegionService(currentHostAddress));
  74. RemedicalService get remedical =>
  75. findService(() => new RemedicalService(currentHostAddress));
  76. ReportService get report =>
  77. findService(() => new ReportService(currentHostAddress));
  78. RoleService get role =>
  79. findService(() => new RoleService(currentHostAddress));
  80. SMSService get sMS =>
  81. findService(() => new SMSService(currentHostAddress));
  82. StorageService get storage =>
  83. findService(() => new StorageService(currentHostAddress));
  84. UpgradeService get upgrade =>
  85. findService(() => new UpgradeService(currentHostAddress));
  86. UserService get user =>
  87. findService(() => new UserService(currentHostAddress));
  88. VinnoServerService get vinnoServer =>
  89. findService(() => new VinnoServerService(currentHostAddress));
  90. /* 服务代理设置 End */
  91. /// 设置服务主机地址
  92. void setServerHost(String address) {
  93. logger.i('JsonRpcProxy setServerHost :' + address);
  94. _currentHost = address;
  95. }
  96. /// 添加拦截器
  97. void addInterceptor(JsonRpcInterceptor interceptor) =>
  98. jsonRpcInterceptHost.addInterceptor(interceptor);
  99. /// 清空缓存
  100. void clearCache() => _serviceCache.clear();
  101. /// 查找Service实例
  102. T findService<T extends JsonRpcClientBase>(ServiceBuilder<T> builder) {
  103. Type serviceType = typeOf<T>();
  104. if (!_serviceCache.containsKey(serviceType)) {
  105. _serviceCache[serviceType] = builder.call();
  106. }
  107. return _serviceCache[serviceType] as T;
  108. }
  109. }