device.dart 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import 'dart:convert';
  2. import 'package:fis_common/event/event_type.dart';
  3. import 'package:fis_jsonrpc/rpc.dart';
  4. import 'package:get/get.dart';
  5. import 'package:package_info_plus/package_info_plus.dart';
  6. import 'package:platform_device_id/platform_device_id.dart';
  7. import 'package:vitalapp/consts/devices.dart';
  8. import 'package:vitalapp/global.dart';
  9. import 'package:vitalapp/managers/interfaces/application.dart';
  10. import 'package:vitalapp/rpc.dart';
  11. import 'package:vitalapp/store/store.dart';
  12. import 'package:vnote_device_plugin/consts/types.dart';
  13. import 'package:vitalapp/architecture/storage/text_storage.dart';
  14. import 'package:vitalapp/managers/interfaces/models/device.dart';
  15. import 'package:fis_common/logger/logger.dart';
  16. import 'interfaces/device.dart';
  17. import 'interfaces/system_setting.dart';
  18. class DeviceManager extends IDeviceManager {
  19. /// 在线状态变更事件
  20. final FEventHandler<String> onScanCode = FEventHandler<String>();
  21. final _typeConvertMap = <String, String>{
  22. "Temp": DeviceTypes.TEMP,
  23. "Weight": DeviceTypes.WEIGHT,
  24. "SpO2": DeviceTypes.SPO2,
  25. "NIBP": DeviceTypes.NIBP,
  26. "GLU": DeviceTypes.SUGAR,
  27. "Urine": DeviceTypes.URINE,
  28. "ICReader": DeviceTypes.IC_READER,
  29. "ECG": DeviceTypes.HEART,
  30. "TwelveHeart": DeviceTypes.TWELVEHEART,
  31. "WHB": DeviceTypes.WAIST,
  32. "WEIGHTHEIGHT": DeviceTypes.WEIGHTHEIGHT,
  33. };
  34. final _storage = TextStorage(fileName: "devices.cfg");
  35. final _deviceStorage = TextStorage(fileName: "devices.setting.cfg");
  36. List<DeviceModel>? _cacheList;
  37. @override
  38. Future<List<String>> getAccessTypes() async {
  39. List<String> settingTypes = [];
  40. if (kIsOnline) {
  41. settingTypes =
  42. await Get.find<ISystemSettingManager>().getEnableDeviceKeysAsync();
  43. _deviceStorage.save(jsonEncode(settingTypes));
  44. } else {
  45. String? settingTypesString = await _deviceStorage.read();
  46. settingTypes =
  47. jsonDecode(settingTypesString ?? '').cast<String>().toList();
  48. }
  49. final types = settingTypes
  50. .map((e) => _typeConvertMap[e])
  51. .where((e) => e != null)
  52. .map((e) => e!)
  53. .toList();
  54. return types;
  55. }
  56. Future<List<String>> getCacheAccessTypes() async {
  57. List<String> settingTypes = [];
  58. String? settingTypesString = await _deviceStorage.read();
  59. settingTypes = jsonDecode(settingTypesString ?? '').cast<String>().toList();
  60. final types = settingTypes
  61. .map((e) => _typeConvertMap[e])
  62. .where((e) => e != null)
  63. .map((e) => e!)
  64. .toList();
  65. return types;
  66. }
  67. @override
  68. Future<void> addDevice(DeviceModel model) async {
  69. final source = await getDeviceList();
  70. final list = List<DeviceModel>.from(source);
  71. list.removeWhere((e) => e.type == model.type);
  72. list.add(model);
  73. saveDeviceList(list);
  74. }
  75. @override
  76. Future<void> removeDeviceByType(String type) async {
  77. final source = await getDeviceList();
  78. final list = List<DeviceModel>.from(source);
  79. list.removeWhere((e) => e.type == type);
  80. saveDeviceList(list);
  81. }
  82. @override
  83. Future<List<DeviceModel>> getDeviceList() async {
  84. if (_cacheList != null) return _cacheList!;
  85. final jsonText = await _storage.read();
  86. if (jsonText == null) {
  87. _cacheList = [];
  88. } else {
  89. final List<dynamic> mapList = jsonDecode(jsonText);
  90. _cacheList = mapList.map((e) => DeviceModel.fromJson(e)).toList();
  91. }
  92. return _cacheList!;
  93. }
  94. @override
  95. Future<void> saveDeviceList(List<DeviceModel> deviceList) async {
  96. _cacheList = deviceList;
  97. final jsonText = jsonEncode(deviceList);
  98. await _storage.save(jsonText);
  99. }
  100. @override
  101. Future<void> reportTablet() async {
  102. try {
  103. final appManager = Get.find<IApplicationManager>();
  104. // TODO: 考虑到登录因素过多,暂时不限制
  105. // final localAccount = await appManager.getProperty("account");
  106. // if (Store.user.account == localAccount) {
  107. // return;
  108. // }
  109. final androidId = await PlatformDeviceId.getDeviceId;
  110. // TODO: 太长了
  111. // final deviceId =
  112. // "2${const Uuid().v5(Uuid.NAMESPACE_OID, androidId).replaceAll("-", "")}";
  113. final deviceId = androidId ?? "";
  114. final androidInfo = await PlatformDeviceId.deviceInfoPlugin.androidInfo;
  115. final osVersion = "Android ${androidInfo.version.release}";
  116. PackageInfo packageInfo = await PackageInfo.fromPlatform();
  117. final request = ReportingDeviceInfoRequest(
  118. token: Store.user.token,
  119. serialNumber: deviceId,
  120. model: androidInfo.model,
  121. softwareVersion: packageInfo.version,
  122. oSVersion: osVersion,
  123. );
  124. final result = await rpc.vitalDevice.reportingDeviceInfoAsync(request);
  125. if (result) {
  126. await appManager.setProperties({
  127. "account": Store.user.account,
  128. "deviceId": deviceId,
  129. });
  130. }
  131. logger.i("DeviceManager reportTablet result: $result.");
  132. } catch (e) {
  133. logger.e("DeviceManager reportTablet error", e);
  134. }
  135. }
  136. Future<bool> reportAddDevice(DeviceModel model) async {
  137. try {
  138. if (kIsOnline) {
  139. final deviceId = await PlatformDeviceId.getDeviceId;
  140. final deviceKey4Server = DeviceConsts.transType2ServerKey(model.type);
  141. final request = CreatePeripheralDeviceRequest(
  142. token: Store.user.token,
  143. deviceCode: deviceId,
  144. enable: true,
  145. key: deviceKey4Server,
  146. name: model.productName,
  147. );
  148. final result =
  149. await rpc.vitalDevice.createPeripheralDeviceAsync(request);
  150. return result;
  151. } else {
  152. //
  153. return true;
  154. }
  155. } catch (e) {
  156. return false;
  157. }
  158. }
  159. }