rpc.dart 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. library fis_jsonrpc;
  2. import 'dart:collection';
  3. import 'package:fis_common/extensions/type.dart';
  4. import 'client_base.dart';
  5. import 'services/index.dart';
  6. export 'services/index.dart';
  7. typedef T ServiceBuilder<T extends JsonRpcClientBase>();
  8. /// 默认地址标识
  9. const String _defaultAddressSign = "FLYINSONO";
  10. /// JSON-RPC 代理
  11. class JsonRpcProxy {
  12. JsonRpcProxy({
  13. this.defaultServerHost = "cloud.fis.plus",
  14. this.platformHost = "platform.fis.plus",
  15. }) {
  16. _currentHost = this.defaultServerHost;
  17. }
  18. /// 默认主机地址
  19. final String defaultServerHost;
  20. /// 宿主平台代理地址
  21. final String platformHost;
  22. late String _currentHost;
  23. /// 当前服务主机地址
  24. String get currentHostAddress => "http://$_currentHost";
  25. String get defaultAddressSign => _defaultAddressSign;
  26. HashMap<Type, dynamic> _serviceCache = HashMap();
  27. static PlatformService? _platformService;
  28. /* 服务代理设置 Start */
  29. /// 平台服务
  30. PlatformService get platform {
  31. if (_platformService == null)
  32. _platformService = PlatformService("http://$platformHost");
  33. return _platformService!;
  34. }
  35. ClientLogService get clientLog =>
  36. findService(() => new ClientLogService(currentHostAddress));
  37. DepartmentService get department =>
  38. findService(() => new DepartmentService(currentHostAddress));
  39. DeviceService get device =>
  40. findService(() => new DeviceService(currentHostAddress));
  41. FrontAuthorityGroupsService get frontAuthorityGroups =>
  42. findService(() => new FrontAuthorityGroupsService(currentHostAddress));
  43. IdentityApplyService get identityApply =>
  44. findService(() => new IdentityApplyService(currentHostAddress));
  45. LoginService get login =>
  46. findService(() => new LoginService(currentHostAddress));
  47. ManagementService get management =>
  48. findService(() => new ManagementService(currentHostAddress));
  49. RemedicalService get remedical =>
  50. findService(() => new RemedicalService(currentHostAddress));
  51. UserService get user =>
  52. findService(() => new UserService(currentHostAddress));
  53. /* 服务代理设置 End */
  54. /// 设置服务主机地址
  55. void setServerHost(String address) {
  56. if (address == _defaultAddressSign) {
  57. address = defaultServerHost;
  58. }
  59. _currentHost = address;
  60. }
  61. /// 清空缓存
  62. void clearCache() => _serviceCache.clear();
  63. /// 查找Service实例
  64. T findService<T extends JsonRpcClientBase>(ServiceBuilder<T> builder) {
  65. Type serviceType = typeOf<T>();
  66. if (!_serviceCache.containsKey(serviceType)) {
  67. _serviceCache[serviceType] = builder.call();
  68. }
  69. return _serviceCache[serviceType] as T;
  70. }
  71. }