rpc.dart 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. library fisjsonrpc;
  2. import 'dart:collection';
  3. import 'package:fiscommon/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(platformHost);
  33. return _platformService!;
  34. }
  35. /// 登录服务
  36. LoginService get login =>
  37. findService(() => new LoginService(currentHostAddress));
  38. /// 诊断服务
  39. RemedicalService get remedical =>
  40. findService(() => new RemedicalService(currentHostAddress));
  41. /// 用户服务
  42. UserService get user =>
  43. findService(() => new UserService(currentHostAddress));
  44. /* 服务代理设置 End */
  45. /// 设置服务主机地址
  46. void setServerHost(String address) {
  47. if (address == _defaultAddressSign) {
  48. address = defaultServerHost;
  49. }
  50. _currentHost = address;
  51. }
  52. /// 清空缓存
  53. void clearCache() => _serviceCache.clear();
  54. /// 查找Service实例
  55. T findService<T extends JsonRpcClientBase>(ServiceBuilder<T> builder) {
  56. Type serviceType = typeOf<T>();
  57. if (!_serviceCache.containsKey(serviceType)) {
  58. _serviceCache[serviceType] = builder.call();
  59. }
  60. return _serviceCache[serviceType] as T;
  61. }
  62. }