device.dart 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. };
  30. final _storage = TextStorage(fileName: "devices.cfg");
  31. final _deviceStorage = TextStorage(fileName: "devices.setting.cfg");
  32. List<DeviceModel>? _cacheList;
  33. @override
  34. Future<List<String>> getAccessTypes() async {
  35. List<String> settingTypes = [];
  36. if (kIsOnline) {
  37. settingTypes =
  38. await Get.find<ISystemSettingManager>().getEnableDeviceKeysAsync();
  39. _deviceStorage.save(jsonEncode(settingTypes));
  40. } else {
  41. String? settingTypesString = await _deviceStorage.read();
  42. settingTypes =
  43. jsonDecode(settingTypesString ?? '').cast<String>().toList();
  44. }
  45. final types = settingTypes
  46. .map((e) => _typeConvertMap[e])
  47. .where((e) => e != null)
  48. .map((e) => e!)
  49. .toList();
  50. return types;
  51. }
  52. Future<List<String>> getCacheAccessTypes() async {
  53. List<String> settingTypes = [];
  54. String? settingTypesString = await _deviceStorage.read();
  55. settingTypes = jsonDecode(settingTypesString ?? '').cast<String>().toList();
  56. final types = settingTypes
  57. .map((e) => _typeConvertMap[e])
  58. .where((e) => e != null)
  59. .map((e) => e!)
  60. .toList();
  61. return types;
  62. }
  63. @override
  64. Future<void> addDevice(DeviceModel model) async {
  65. final source = await getDeviceList();
  66. final list = List<DeviceModel>.from(source);
  67. list.removeWhere((e) => e.type == model.type);
  68. list.add(model);
  69. saveDeviceList(list);
  70. }
  71. @override
  72. Future<void> removeDeviceByType(String type) async {
  73. final source = await getDeviceList();
  74. final list = List<DeviceModel>.from(source);
  75. list.removeWhere((e) => e.type == type);
  76. saveDeviceList(list);
  77. }
  78. @override
  79. Future<List<DeviceModel>> getDeviceList() async {
  80. if (_cacheList != null) return _cacheList!;
  81. final jsonText = await _storage.read();
  82. if (jsonText == null) {
  83. _cacheList = [];
  84. } else {
  85. final List<dynamic> mapList = jsonDecode(jsonText);
  86. _cacheList = mapList.map((e) => DeviceModel.fromJson(e)).toList();
  87. }
  88. return _cacheList!;
  89. }
  90. @override
  91. Future<void> saveDeviceList(List<DeviceModel> deviceList) async {
  92. _cacheList = deviceList;
  93. final jsonText = jsonEncode(deviceList);
  94. await _storage.save(jsonText);
  95. }
  96. @override
  97. Future<void> reportTablet() async {
  98. try {
  99. final appManager = Get.find<IApplicationManager>();
  100. // TODO: 考虑到登录因素过多,暂时不限制
  101. // final localAccount = await appManager.getProperty("account");
  102. // if (Store.user.account == localAccount) {
  103. // return;
  104. // }
  105. final androidId = await PlatformDeviceId.getDeviceId;
  106. // TODO: 太长了
  107. // final deviceId =
  108. // "2${const Uuid().v5(Uuid.NAMESPACE_OID, androidId).replaceAll("-", "")}";
  109. final deviceId = androidId ?? "";
  110. final androidInfo = await PlatformDeviceId.deviceInfoPlugin.androidInfo;
  111. final osVersion = "Android ${androidInfo.version.release}";
  112. PackageInfo packageInfo = await PackageInfo.fromPlatform();
  113. final request = ReportingDeviceInfoRequest(
  114. token: Store.user.token,
  115. serialNumber: deviceId,
  116. model: androidInfo.model,
  117. softwareVersion: packageInfo.version,
  118. oSVersion: osVersion,
  119. );
  120. final result = await rpc.device.reportingDeviceInfoAsync(request);
  121. if (result) {
  122. await appManager.setProperties({
  123. "account": Store.user.account,
  124. "deviceId": deviceId,
  125. });
  126. }
  127. logger.i("DeviceManager reportTablet result: $result.");
  128. } catch (e) {
  129. logger.e("DeviceManager reportTablet error", e);
  130. }
  131. }
  132. Future<bool> reportAddDevice(DeviceModel model) async {
  133. try {
  134. if (kIsOnline) {
  135. final deviceId = await PlatformDeviceId.getDeviceId;
  136. final deviceKey4Server = DeviceConsts.transType2ServerKey(model.type);
  137. final request = CreatePeripheralDeviceRequest(
  138. token: Store.user.token,
  139. deviceCode: deviceId,
  140. enable: true,
  141. key: deviceKey4Server,
  142. name: model.productName,
  143. );
  144. final result = await rpc.device.createPeripheralDeviceAsync(request);
  145. return result;
  146. } else {
  147. //
  148. return true;
  149. }
  150. } catch (e) {
  151. return false;
  152. }
  153. }
  154. }