远程遥控相关
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
import 'dart:convert';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import '../../../../core/consts/http_api_consts.dart';
|
||||
import '../../../../core/logging/i_logger_service.dart';
|
||||
import '../../domain/entities/device_control_mode.dart';
|
||||
|
||||
class ControlModeRemoteDatasource {
|
||||
ControlModeRemoteDatasource(this.dio, this.logger);
|
||||
final Dio dio;
|
||||
final ILoggerService logger;
|
||||
|
||||
Future<void> change(String deviceId, ControlModeTarget target) async {
|
||||
final parameters = {'deviceId': deviceId, 'controlMode': target.value};
|
||||
final requestId = DateTime.now().microsecondsSinceEpoch.toString();
|
||||
final clock = Stopwatch()..start();
|
||||
final uri = Uri.parse(HttpApiConsts.hostModeChange).replace(
|
||||
queryParameters: parameters.map((key, value) => MapEntry(key, '$value')),
|
||||
);
|
||||
void log(String stage, Map<String, Object?> details) {
|
||||
try {
|
||||
logger.logWithLevel(
|
||||
'[控制模式切换][$stage] ${jsonEncode({'requestId': requestId, 'deviceId': deviceId, 'targetMode': target.value, 'elapsedMs': clock.elapsedMilliseconds, ...details})}',
|
||||
shouldLog: true,
|
||||
);
|
||||
} catch (error) {
|
||||
debugPrint('[控制模式切换][日志输出异常] requestId=$requestId, error=$error');
|
||||
}
|
||||
}
|
||||
|
||||
log('请求', {
|
||||
'method': 'GET',
|
||||
'url': uri.toString(),
|
||||
'parameters': parameters,
|
||||
'deviceIdType': deviceId.runtimeType.toString(),
|
||||
'controlModeType': target.value.runtimeType.toString(),
|
||||
});
|
||||
try {
|
||||
final response = await dio.get(
|
||||
HttpApiConsts.hostModeChange,
|
||||
queryParameters: parameters,
|
||||
options: Options(receiveTimeout: const Duration(seconds: 10)),
|
||||
);
|
||||
final body = response.data;
|
||||
log('响应', {
|
||||
'httpStatus': response.statusCode,
|
||||
'body': body,
|
||||
'bodyType': body.runtimeType.toString(),
|
||||
if (body is Map) 'businessCode': body['code'],
|
||||
if (body is Map) 'message': body['msg'],
|
||||
});
|
||||
if (response.statusCode != 200 ||
|
||||
body is! Map ||
|
||||
'${body['code']}' != '200' ||
|
||||
body['data'] == false) {
|
||||
throw Exception(body is Map ? body['msg'] ?? '模式切换失败' : '模式切换响应格式错误');
|
||||
}
|
||||
log('结果', {'success': true, 'note': '接口成功,允许进入页面;实时模式仍以设备上报为准,不等待回推'});
|
||||
} catch (error, stack) {
|
||||
log('异常', {
|
||||
'errorType': error.runtimeType.toString(),
|
||||
'error': '$error',
|
||||
if (error is DioException) 'dioType': error.type.name,
|
||||
if (error is DioException) 'httpStatus': error.response?.statusCode,
|
||||
if (error is DioException) 'body': error.response?.data,
|
||||
'stack': '$stack',
|
||||
});
|
||||
rethrow;
|
||||
} finally {
|
||||
clock.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user