远程遥控相关
This commit is contained in:
@@ -11,4 +11,13 @@ abstract class BindDeviceDatasource {
|
||||
int? siteId,
|
||||
int? userId,
|
||||
});
|
||||
|
||||
/// 批量解绑设备(后端 /iot/device/unbind)。
|
||||
///
|
||||
/// [type] 见 UnbindScope:1 清除负责人 / 2 清除场站 / 3 清除组织。
|
||||
/// 后端为批量接口,一次可解绑多台设备且必须指定清除层级。
|
||||
Future<void> unbindDevice({
|
||||
required List<String> deviceIds,
|
||||
required int type,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:maibu_satabot_v2/core/consts/http_api_consts.dart';
|
||||
import 'package:maibu_satabot_v2/core/di/injection.dart';
|
||||
import 'package:maibu_satabot_v2/core/app/app_user_cubit.dart';
|
||||
@@ -135,24 +138,151 @@ class BindDeviceDatasourceImpl implements BindDeviceDatasource {
|
||||
if (siteId != null) body['siteId'] = siteId;
|
||||
if (userId != null) body['userId'] = userId;
|
||||
|
||||
final response = await _dio.post(
|
||||
HttpApiConsts.bindDevice,
|
||||
data: body,
|
||||
options: Options(
|
||||
headers: {
|
||||
'Authorization': token != null ? 'Bearer $token' : '',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
),
|
||||
// 绑定接口完整请求日志:body 用分段打印,避免 debugPrint 单条过长被截断
|
||||
debugPrint('>>> [BindDevice] ===== 开始绑定 =====');
|
||||
debugPrint('>>> [BindDevice] url=${HttpApiConsts.bindDevice}');
|
||||
debugPrint(
|
||||
'>>> [BindDevice] 入参 deviceIds=$deviceIds orgId=$orgId '
|
||||
'siteId=$siteId userId=$userId',
|
||||
);
|
||||
debugPrint(
|
||||
'>>> [BindDevice] token=${token == null || token.isEmpty ? '空' : '已携带'}',
|
||||
);
|
||||
_printChunked('>>> [BindDevice] requestBody: ', _safeEncode(body));
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
throw Exception('网络请求失败: ${response.statusCode}');
|
||||
try {
|
||||
final response = await _dio.post(
|
||||
HttpApiConsts.bindDevice,
|
||||
data: body,
|
||||
options: Options(
|
||||
headers: {
|
||||
'Authorization': token != null ? 'Bearer $token' : '',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
debugPrint('>>> [BindDevice] response statusCode=${response.statusCode}');
|
||||
_printChunked(
|
||||
'>>> [BindDevice] responseBody: ',
|
||||
_safeEncode(response.data),
|
||||
);
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
throw Exception('网络请求失败: ${response.statusCode}');
|
||||
}
|
||||
|
||||
final data = response.data;
|
||||
if (data['code'] != 200) {
|
||||
debugPrint(
|
||||
'>>> [BindDevice] ❌ 业务失败 code=${data['code']} msg=${data['msg']}',
|
||||
);
|
||||
throw Exception(data['msg'] ?? '绑定设备失败');
|
||||
}
|
||||
debugPrint('>>> [BindDevice] ✅ 绑定成功');
|
||||
} on DioException catch (e) {
|
||||
debugPrint(
|
||||
'>>> [BindDevice] ❌ DioException type=${e.type} '
|
||||
'status=${e.response?.statusCode} message=${e.message}',
|
||||
);
|
||||
if (e.response?.data != null) {
|
||||
_printChunked(
|
||||
'>>> [BindDevice] errorResponseBody: ',
|
||||
_safeEncode(e.response?.data),
|
||||
);
|
||||
}
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
final data = response.data;
|
||||
if (data['code'] != 200) {
|
||||
throw Exception(data['msg'] ?? '绑定设备失败');
|
||||
@override
|
||||
Future<void> unbindDevice({
|
||||
required List<String> deviceIds,
|
||||
required int type,
|
||||
}) async {
|
||||
final token = await _getToken();
|
||||
final Map<String, dynamic> body = {'deviceIds': deviceIds, 'type': type};
|
||||
|
||||
// 解绑为不可逆数据操作,请求前后均打印完整日志便于核对 deviceIds 与 type
|
||||
debugPrint('>>> [UnbindDevice] ===== 开始解绑 =====');
|
||||
debugPrint('>>> [UnbindDevice] url=${HttpApiConsts.unbindDeviceV2}');
|
||||
debugPrint(
|
||||
'>>> [UnbindDevice] 入参 deviceIds=$deviceIds type=$type '
|
||||
'(1清除负责人 2清除场站 3清除组织)',
|
||||
);
|
||||
debugPrint(
|
||||
'>>> [UnbindDevice] token=${token == null || token.isEmpty ? '空' : '已携带'}',
|
||||
);
|
||||
_printChunked('>>> [UnbindDevice] requestBody: ', _safeEncode(body));
|
||||
|
||||
try {
|
||||
final response = await _dio.post(
|
||||
HttpApiConsts.unbindDeviceV2,
|
||||
data: body,
|
||||
options: Options(
|
||||
headers: {
|
||||
'Authorization': token != null ? 'Bearer $token' : '',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
debugPrint(
|
||||
'>>> [UnbindDevice] response statusCode=${response.statusCode}',
|
||||
);
|
||||
_printChunked(
|
||||
'>>> [UnbindDevice] responseBody: ',
|
||||
_safeEncode(response.data),
|
||||
);
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
throw Exception('网络请求失败: ${response.statusCode}');
|
||||
}
|
||||
|
||||
// 后端返回格式未固定,只在明确给出业务码时才按失败处理:
|
||||
// {code:200, data:true} 与 {code:200, msg:"..."} 两种都会当作成功
|
||||
final data = response.data;
|
||||
if (data is Map && data['code'] != null && data['code'] != 200) {
|
||||
debugPrint(
|
||||
'>>> [UnbindDevice] ❌ 业务失败 code=${data['code']} msg=${data['msg']}',
|
||||
);
|
||||
throw Exception(data['msg'] ?? '解绑设备失败');
|
||||
}
|
||||
debugPrint('>>> [UnbindDevice] ✅ 解绑成功');
|
||||
} on DioException catch (e) {
|
||||
debugPrint(
|
||||
'>>> [UnbindDevice] ❌ DioException type=${e.type} '
|
||||
'status=${e.response?.statusCode} message=${e.message}',
|
||||
);
|
||||
if (e.response?.data != null) {
|
||||
_printChunked(
|
||||
'>>> [UnbindDevice] errorResponseBody: ',
|
||||
_safeEncode(e.response?.data),
|
||||
);
|
||||
}
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
/// 长文本分段打印,避免 debugPrint 单条过长被截断
|
||||
void _printChunked(String prefix, String content) {
|
||||
const chunk = 700;
|
||||
var head = prefix;
|
||||
for (int start = 0; start < content.length; start += chunk) {
|
||||
final end =
|
||||
start + chunk < content.length ? start + chunk : content.length;
|
||||
debugPrint('$head${content.substring(start, end)}');
|
||||
head = ' ↳ ';
|
||||
}
|
||||
}
|
||||
|
||||
/// JSON 编码失败时退回 toString,保证日志本身不抛异常
|
||||
String _safeEncode(dynamic data) {
|
||||
if (data == null) return 'null';
|
||||
try {
|
||||
return jsonEncode(data);
|
||||
} catch (_) {
|
||||
return data.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,12 @@ class DeviceDataModel {
|
||||
final double? radiation;
|
||||
final bool isRobot;
|
||||
|
||||
/// 产品型号(如「割草机产品MC700」)
|
||||
final String productName;
|
||||
|
||||
/// 机型编码(如 MC700PRO)
|
||||
final String? productModel;
|
||||
|
||||
const DeviceDataModel({
|
||||
required this.deviceId,
|
||||
required this.name,
|
||||
@@ -26,6 +32,8 @@ class DeviceDataModel {
|
||||
this.voltage,
|
||||
this.radiation,
|
||||
this.isRobot = false,
|
||||
this.productName = '',
|
||||
this.productModel,
|
||||
});
|
||||
|
||||
/// 从 JSON 创建数据模型
|
||||
@@ -41,6 +49,8 @@ class DeviceDataModel {
|
||||
current: json['current']?.toDouble(),
|
||||
voltage: json['voltage']?.toDouble(),
|
||||
radiation: json['radiation']?.toDouble(),
|
||||
productName: (json['productName'] as String?) ?? '',
|
||||
productModel: json['productModel'] as String?,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -57,6 +67,8 @@ class DeviceDataModel {
|
||||
'current': current,
|
||||
'voltage': voltage,
|
||||
'radiation': radiation,
|
||||
'productName': productName,
|
||||
'productModel': productModel,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -74,6 +86,8 @@ class DeviceDataModel {
|
||||
voltage: voltage,
|
||||
radiation: radiation,
|
||||
isRobot: isRobot,
|
||||
productName: productName,
|
||||
productModel: productModel,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,4 +77,20 @@ class BindDeviceRepositoryImpl implements BindDeviceRepository {
|
||||
return Left(Failure(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Either<Failure, void>> unbindDevice({
|
||||
required List<String> deviceIds,
|
||||
required UnbindScope scope,
|
||||
}) async {
|
||||
try {
|
||||
await _datasource.unbindDevice(
|
||||
deviceIds: deviceIds,
|
||||
type: scope.value,
|
||||
);
|
||||
return const Right(null);
|
||||
} catch (e) {
|
||||
return Left(Failure(e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user