Files
ai_chat_assistant/lib/services/command_service.dart

127 lines
4.4 KiB
Dart
Raw Permalink Normal View History

import 'package:ai_chat_core/ai_chat_core.dart';
2025-08-12 13:36:42 +08:00
/// 命令处理回调函数定义
typedef CommandCallback = Future<(bool, Map<String, dynamic>? params)> Function(
VehicleCommandType type, Map<String, dynamic>? params);
/// 命令处理器类 - 负责处理来自AI的车辆控制命令(使用回调的方式交给App去实现具体的车控逻辑)
2025-08-12 13:36:42 +08:00
class CommandService {
/// 保存主应用注册的回调函数
static CommandCallback? onCommandReceived;
/// 注册命令处理回调
static void registerCallback(CommandCallback callback) {
onCommandReceived = callback;
}
/// 执行车控命令
/// 返回命令执行是否成功
static Future<(bool, Map<String, dynamic>? params)> executeCommand(
VehicleCommandType type,
{Map<String, dynamic>? params}) async {
if (onCommandReceived != null) {
try {
return await onCommandReceived!(type, params);
} catch (e) {
2025-09-30 14:48:12 +08:00
Logger.e('执行命令出错: $e');
2025-08-12 13:36:42 +08:00
}
} else {
2025-09-30 14:48:12 +08:00
Logger.w('警告: 命令处理回调未注册');
2025-08-12 13:36:42 +08:00
}
return (false, null);
}
static bool checkState(
VehicleCommandType type, Map<String, dynamic>? params) {
// 获取车辆状态信息
VehicleStatusInfo vehicleStatusInfo = VehicleStatusInfo();
switch (type) {
// 车辆开锁
case VehicleCommandType.lock:
return vehicleStatusInfo.leftFrontLockState &&
vehicleStatusInfo.rightFrontLockState &&
vehicleStatusInfo.leftRearLockState &&
vehicleStatusInfo.rightRearLockState;
// 车辆关锁
case VehicleCommandType.unlock:
return !(vehicleStatusInfo.leftFrontLockState &&
vehicleStatusInfo.rightFrontLockState &&
vehicleStatusInfo.leftRearLockState &&
vehicleStatusInfo.rightRearLockState);
// 打开窗户
case VehicleCommandType.openWindow:
return vehicleStatusInfo.leftFrontWindowState &&
vehicleStatusInfo.rightFrontWindowState &&
vehicleStatusInfo.leftRearWindowState &&
vehicleStatusInfo.rightRearWindowState;
// 关闭窗户
case VehicleCommandType.closeWindow:
return !(vehicleStatusInfo.leftFrontWindowState &&
vehicleStatusInfo.rightFrontWindowState &&
vehicleStatusInfo.leftRearWindowState &&
vehicleStatusInfo.rightRearWindowState);
// 预约空调
case VehicleCommandType.appointAC:
// todo
return false;
// 打开空调
case VehicleCommandType.openAC:
return vehicleStatusInfo.acState && vehicleStatusInfo.acSwitch;
// 关闭空调
case VehicleCommandType.closeAC:
return !vehicleStatusInfo.acState && !vehicleStatusInfo.acSwitch;
// 极速降温
case VehicleCommandType.coolSharply:
// todo
return false;
// 一键备车
case VehicleCommandType.prepareCar:
return false;
// 一键融雪
case VehicleCommandType.meltSnow:
// todo
return false;
// 打开后备箱
case VehicleCommandType.openTrunk:
return vehicleStatusInfo.trunkState;
// 关闭后备箱
case VehicleCommandType.closeTrunk:
return !vehicleStatusInfo.trunkState;
// 车辆鸣笛
case VehicleCommandType.honk:
// todo
return false;
// 寻找车辆
case VehicleCommandType.locateCar:
// todo
return false;
// 修改空调温度
case VehicleCommandType.changeACTemp:
return vehicleStatusInfo.acState &&
vehicleStatusInfo.acSwitch &&
vehicleStatusInfo.acTemp == params?['temperature'];
// 开启方向盘加热
case VehicleCommandType.openWheelHeat:
return vehicleStatusInfo.wheelHeat;
// 关闭方向盘加热
case VehicleCommandType.closeWheelHeat:
return !vehicleStatusInfo.wheelHeat;
// 开启主座椅加热
case VehicleCommandType.openMainSeatHeat:
return vehicleStatusInfo.mainSeatHeat;
// 关闭主座椅加热
case VehicleCommandType.closeMainSeatHeat:
return !vehicleStatusInfo.mainSeatHeat;
// 开启副座椅加热
case VehicleCommandType.openMinorSeatHeat:
return vehicleStatusInfo.minorSeatHeat;
// 关闭副座椅加热
case VehicleCommandType.closeMinorSeatHeat:
return !vehicleStatusInfo.minorSeatHeat;
default:
return false;
}
}
}