rpc.dart 2.7 KB

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