rpc.dart 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. DeviceService get device =>
  38. findService(() => new DeviceService(currentHostAddress));
  39. LoginService get login =>
  40. findService(() => new LoginService(currentHostAddress));
  41. RemedicalService get remedical =>
  42. findService(() => new RemedicalService(currentHostAddress));
  43. UserService get user =>
  44. findService(() => new UserService(currentHostAddress));
  45. /* 服务代理设置 End */
  46. /// 设置服务主机地址
  47. void setServerHost(String address) {
  48. if (address == _defaultAddressSign) {
  49. address = defaultServerHost;
  50. }
  51. _currentHost = address;
  52. }
  53. /// 清空缓存
  54. void clearCache() => _serviceCache.clear();
  55. /// 查找Service实例
  56. T findService<T extends JsonRpcClientBase>(ServiceBuilder<T> builder) {
  57. Type serviceType = typeOf<T>();
  58. if (!_serviceCache.containsKey(serviceType)) {
  59. _serviceCache[serviceType] = builder.call();
  60. }
  61. return _serviceCache[serviceType] as T;
  62. }
  63. }