47 lines
1.8 KiB
C
47 lines
1.8 KiB
C
#ifndef MQTT_HANDLER_H
|
||
#define MQTT_HANDLER_H
|
||
|
||
#include <PubSubClient.h>
|
||
#include <WiFiClient.h>
|
||
#include <ArduinoJson.h>
|
||
#include "config.h" // 引入配置文件以获取 MQTT 设置和设备ID
|
||
|
||
// 定义机器人可能的状态枚举,与后端和需求文档对齐
|
||
enum RobotStatus {
|
||
IDLE,
|
||
MOVING,
|
||
CHARGING,
|
||
COMPLETED,
|
||
FAULTED // 或 ERROR,根据后端实际使用的枚举值
|
||
};
|
||
|
||
// 回调函数指针类型,用于通知主程序收到了特定指令
|
||
// 参数: commandType, taskId, targetSpotId (如果指令是移动)
|
||
typedef void (*CommandCallback)(const char* commandType, const char* taskId, const char* targetSpotId);
|
||
|
||
void mqtt_setup(CommandCallback cmd_callback); // MQTT 初始化,传入指令回调函数
|
||
void mqtt_loop(); // 在主 loop 中调用,保持MQTT连接和处理消息
|
||
void mqtt_publish_status_update(
|
||
RobotStatus current_status,
|
||
const char* location,
|
||
int battery_level,
|
||
const char* current_task_id, // 后端任务的ID (如果正在执行)
|
||
const char* target_spot, // 仅在MOVING状态下
|
||
const char* current_spot, // 仅在CHARGING, COMPLETED状态下
|
||
unsigned long charge_duration_seconds, // 仅在CHARGING状态下
|
||
int error_code, // 仅在FAULTED状态下
|
||
const char* error_message // 仅在FAULTED状态下
|
||
); // 发布状态更新
|
||
void mqtt_publish_ack(
|
||
const char* task_id,
|
||
bool success,
|
||
const char* message,
|
||
RobotStatus current_robot_status_at_ack,
|
||
const char* ack_context_spot_id,
|
||
const char* command_ack_chinese_value // 新增参数
|
||
); // 发布ACK消息
|
||
|
||
// 内部状态,供其他模块查询 (可选,或者通过回调传递)
|
||
RobotStatus mqtt_get_current_robot_status_commanded(); // 获取后端指令可能影响的最新状态
|
||
|
||
#endif // MQTT_HANDLER_H
|