rpc.dart 3.9 KB

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