device.dart 5.4 KB

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