退出登录+机器状态+切换设备+历史作业记录+设备信息
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
// device_hostrity_work_repository_impl.dart
|
||||
import 'dart:convert';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:fpdart/fpdart.dart';
|
||||
import 'package:maibu_satabot_v2/core/storage/user_storage.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/entities/device_run_hostrity_entity.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/errors/device_failure.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/repositories/device_hostrity_work_repository.dart';
|
||||
|
||||
class DeviceHostrityWorkRepositoryImpl implements DeviceHostrityWorkRepositoryRepository {
|
||||
final Dio client;
|
||||
final UserStorage userStorage;
|
||||
|
||||
DeviceHostrityWorkRepositoryImpl({required this.client, required this.userStorage});
|
||||
|
||||
@override
|
||||
Future<Either<DeviceFailure, List<DeviceRunStatisticsEntity>>> getDeviceHRunStatistics({
|
||||
required String deviceId,
|
||||
}) async {
|
||||
try {
|
||||
debugPrint('开始获取用户信息...');
|
||||
final user = await userStorage.getUser();
|
||||
if (user == null || user.token == null) {
|
||||
debugPrint('用户未登录或缺少 token');
|
||||
return Left(DeviceFailure.unauthorized(message: 'User not logged in or token is missing'));
|
||||
}
|
||||
final token = user.token!;
|
||||
debugPrint('用户信息获取成功,Token: $token');
|
||||
|
||||
final response = await client.get(
|
||||
'https://serviceri.satabot.com/iot/device/getDeviceRunStatistics?deviceId=$deviceId',
|
||||
options: Options(
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer $token',
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final jsonBody = response.data;
|
||||
if (jsonBody['code'] == 200) {
|
||||
final List<dynamic> data = jsonBody['data'];
|
||||
final List<DeviceRunStatisticsEntity> statistics = data
|
||||
.map((item) => DeviceRunStatisticsEntity(
|
||||
startTime: item['startTime'] as String, // 开始时间
|
||||
endTime: item['endTime'] as String, // 结束时间
|
||||
workArea: (item['workArea'] as num).toDouble(), // 工作面积
|
||||
distance: item['distance'] != null ? (item['distance'] as num).toDouble() : null, // 工作距离
|
||||
time: item['time'] as int, // 花费时间
|
||||
)).toList();
|
||||
return Right(statistics);
|
||||
} else {
|
||||
debugPrint('服务器返回错误码: ${jsonBody['code']}');
|
||||
return Left(DeviceFailure.serverError(message: '服务器返回错误码: ${jsonBody['code']}'));
|
||||
}
|
||||
} else {
|
||||
debugPrint('网络请求失败,状态码: ${response.statusCode}');
|
||||
return Left(DeviceFailure.networkError(message: '网络请求失败,状态码: ${response.statusCode}'));
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
debugPrint('Dio 请求失败: ${e.message}');
|
||||
return Left(DeviceFailure.networkError(message: e.message ?? '未知错误'));
|
||||
} catch (e) {
|
||||
debugPrint('请求过程中发生异常: $e');
|
||||
return Left(DeviceFailure.unknownError(message: e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user