123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- library fis_jsonrpc;
- import 'dart:collection';
- import 'package:fis_common/extensions/type.dart';
- import 'client_base.dart';
- import 'services/index.dart';
- export 'services/index.dart';
- typedef T ServiceBuilder<T extends JsonRpcClientBase>();
- /// 默认地址标识
- const String _defaultAddressSign = "FLYINSONO";
- /// JSON-RPC 代理
- class JsonRpcProxy {
- JsonRpcProxy({
- this.defaultServerHost = "cloud.fis.plus",
- this.platformHost = "platform.fis.plus",
- }) {
- _currentHost = this.defaultServerHost;
- }
- /// 默认主机地址
- final String defaultServerHost;
- /// 宿主平台代理地址
- final String platformHost;
- late String _currentHost;
- /// 当前服务主机地址
- String get currentHostAddress => "http://$_currentHost";
- String get defaultAddressSign => _defaultAddressSign;
- HashMap<Type, dynamic> _serviceCache = HashMap();
- static PlatformService? _platformService;
- /* 服务代理设置 Start */
- /// 平台服务
- PlatformService get platform {
- if (_platformService == null)
- _platformService = PlatformService("http://$platformHost");
- return _platformService!;
- }
- ClientLogService get clientLog =>
- findService(() => new ClientLogService(currentHostAddress));
- DeviceService get device =>
- findService(() => new DeviceService(currentHostAddress));
- LoginService get login =>
- findService(() => new LoginService(currentHostAddress));
- RemedicalService get remedical =>
- findService(() => new RemedicalService(currentHostAddress));
- UserService get user =>
- findService(() => new UserService(currentHostAddress));
- /* 服务代理设置 End */
- /// 设置服务主机地址
- void setServerHost(String address) {
- if (address == _defaultAddressSign) {
- address = defaultServerHost;
- }
- _currentHost = address;
- }
- /// 清空缓存
- void clearCache() => _serviceCache.clear();
- /// 查找Service实例
- T findService<T extends JsonRpcClientBase>(ServiceBuilder<T> builder) {
- Type serviceType = typeOf<T>();
- if (!_serviceCache.containsKey(serviceType)) {
- _serviceCache[serviceType] = builder.call();
- }
- return _serviceCache[serviceType] as T;
- }
- }
|