完成封装打印和推送功能日志方法和使用
完成添加绑定设备接口的日志便于追溯
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'package:maibu_satabot_v2/core/consts/http_api_consts.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/data/datasources/device_http_datasource.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/entities/device_entity.dart';
|
||||
|
||||
import '../../../../../core/infrastructure/logging/sentry_logger_impl.dart';
|
||||
import '../../../../../core/logging/i_logger_service.dart';
|
||||
import '../../../../../core/storage/user_storage.dart';
|
||||
import '../../../domain/entities/device_location_entity.dart';
|
||||
|
||||
@@ -77,7 +80,15 @@ class DeviceHttpDatasourceImpl implements DeviceHttpDatasource {
|
||||
|
||||
@override
|
||||
Future<int> switchDevice(String platform, String deviceId) async {
|
||||
final logger = GetIt.I<ILoggerService>() as SentryLoggerImpl;
|
||||
final token = await _getToken();
|
||||
print('用户点击switchDevice方法开始触发: $token');
|
||||
logger.logWithLevel('用户点击switchDevice方法开始触发', level: 'DEBUG');
|
||||
logger.logWithLevel(
|
||||
'用户点击switchDevice方法API 请求开始',
|
||||
level: 'INFO',
|
||||
data: {'url': 'https://serviceri.satabot.com/iot/device/switchDevice'}
|
||||
);
|
||||
var response = await dio.post(
|
||||
HttpApiConsts.switchDevice,
|
||||
data: {'platform': platform, 'deviceId': deviceId},
|
||||
@@ -88,16 +99,38 @@ class DeviceHttpDatasourceImpl implements DeviceHttpDatasource {
|
||||
},
|
||||
),
|
||||
);
|
||||
logger.logWithLevel(
|
||||
'请求详细参数',
|
||||
level: 'DEBUG',
|
||||
data: {
|
||||
'url': 'https://serviceri.satabot.com/iot/device/switchDevice',
|
||||
'platform': platform,
|
||||
'deviceId': deviceId
|
||||
}
|
||||
);
|
||||
if (response.statusCode != 200) {
|
||||
logger.logWithLevel(
|
||||
'API 请求失败',
|
||||
level: 'ERROR',
|
||||
data: {'statusCode': response.statusCode, 'data': response.data}
|
||||
);
|
||||
throw Exception('网络请求失败:${response.statusCode}');
|
||||
}
|
||||
|
||||
final responseData = response.data;
|
||||
|
||||
logger.logWithLevel(
|
||||
'API 响应成功',
|
||||
level: 'INFO',
|
||||
data: {'statusCode': response.statusCode, 'data': response.data}
|
||||
);
|
||||
if (responseData['code'] != 200 || responseData['data'] != true) {
|
||||
logger.logWithLevel(
|
||||
'API 响应失败',
|
||||
level: 'ERROR',
|
||||
data: {'statusCode': response.statusCode, 'data': response.data}
|
||||
);
|
||||
throw Exception(responseData['msg'] ?? '业务异常');
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -142,9 +175,19 @@ class DeviceHttpDatasourceImpl implements DeviceHttpDatasource {
|
||||
|
||||
@override
|
||||
Future<dynamic> getDeviceLocation(String deviceame) async {
|
||||
final logger = GetIt.I<ILoggerService>() as SentryLoggerImpl;
|
||||
// TODO: implement getDeviceLocation
|
||||
try {
|
||||
// 发起 GET 请求
|
||||
logger.logWithLevel('用户点击getDeviceLocation方法开始触发', level: 'INFO');
|
||||
logger.logWithLevel(
|
||||
'请求详细参数',
|
||||
level: 'DEBUG',
|
||||
data: {
|
||||
'url': 'https://serviceri.satabot.com/iot/device/userDevice',
|
||||
'tenantName': deviceame
|
||||
}
|
||||
);
|
||||
final response = await dio.get(
|
||||
'https://serviceri.satabot.com/iot/device/userDevice',
|
||||
queryParameters: {'tenantName': deviceame},
|
||||
@@ -159,21 +202,35 @@ class DeviceHttpDatasourceImpl implements DeviceHttpDatasource {
|
||||
// 检查响应状态码
|
||||
if (response.statusCode == 200) {
|
||||
final data = response.data;
|
||||
logger.logWithLevel(
|
||||
'API 响应接收',
|
||||
level: 'DEBUG',
|
||||
data: {'statusCode': response.statusCode, 'data': response.data}
|
||||
);
|
||||
if (data['code'] == 200) {
|
||||
//return data['data']; // 返回设备数据
|
||||
final locationData = data['data']; // 假设数据结构,测试时候如有不妥就修改结果的实例化
|
||||
logger.logWithLevel(
|
||||
'成功获取设备位置',
|
||||
level: 'INFO',
|
||||
data: {'lat': locationData['latitude'], 'lng': locationData['longitude']}
|
||||
);
|
||||
return DeviceLocationEntity(
|
||||
deviceName: locationData['deviceName'],
|
||||
latitude: locationData['latitude'],
|
||||
longitude: locationData['longitude'],
|
||||
);
|
||||
} else {
|
||||
logger.logWithLevel(data['msg'] ?? '业务异常', level: 'ERROR');
|
||||
throw Exception(data['msg'] ?? '业务异常');
|
||||
}
|
||||
} else {
|
||||
logger.logWithLevel('网络请求失败: ${response.statusCode}', level: 'ERROR');
|
||||
throw Exception('网络请求失败: ${response.statusCode}');
|
||||
}
|
||||
} catch (e) {
|
||||
logger.logWithLevel('获取设备位置失败: $e', level: 'ERROR');
|
||||
logger.captureException(e, stackTrace: e);
|
||||
throw Exception('获取设备位置失败: $e');
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user