feat: 添加 console_user_cmds 组件,提供用户命令支持
This commit is contained in:
214
components/console_user_cmds/console_user_cmds.c
Normal file
214
components/console_user_cmds/console_user_cmds.c
Normal file
@@ -0,0 +1,214 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "console_simple_init.h"
|
||||
#include "console_user_cmds.h"
|
||||
#include "i2c_master_messager.h"
|
||||
#include "io_device_control.h"
|
||||
#include "wifi-connect.h"
|
||||
|
||||
static const char *wifi_status_to_str(wifi_connect_status_t status)
|
||||
{
|
||||
switch (status) {
|
||||
case WIFI_CONNECT_STATUS_IDLE:
|
||||
return "idle";
|
||||
case WIFI_CONNECT_STATUS_PROVISIONING:
|
||||
return "provisioning";
|
||||
case WIFI_CONNECT_STATUS_CONNECTING:
|
||||
return "connecting";
|
||||
case WIFI_CONNECT_STATUS_CONNECTED:
|
||||
return "connected";
|
||||
case WIFI_CONNECT_STATUS_FAILED:
|
||||
return "failed";
|
||||
case WIFI_CONNECT_STATUS_TIMEOUT:
|
||||
return "timeout";
|
||||
default:
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
// hello: 最小可用命令,用于验证 console 链路是否正常。
|
||||
static int cmd_hello(int argc, char **argv)
|
||||
{
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
printf("hello from BotanicalBuddy\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// sensor: 读取一次传感器缓存数据并打印,便于快速排查现场状态。
|
||||
static int cmd_sensor(int argc, char **argv)
|
||||
{
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
|
||||
i2c_master_messager_data_t data = {0};
|
||||
esp_err_t ret = i2c_master_messager_get_data(&data);
|
||||
if (ret != ESP_OK) {
|
||||
printf("sensor read failed: %s\n", esp_err_to_name(ret));
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (data.bh1750.valid) {
|
||||
printf("BH1750: lux=%.1f, ts=%lld ms\n",
|
||||
data.bh1750.lux,
|
||||
(long long)data.bh1750.last_update_ms);
|
||||
} else {
|
||||
printf("BH1750: invalid, err=%s\n", esp_err_to_name(data.bh1750.last_error));
|
||||
}
|
||||
|
||||
if (data.aht30.valid) {
|
||||
printf("AHT30: temp=%.1f C, hum=%.1f %%, ts=%lld ms\n",
|
||||
data.aht30.temperature_c,
|
||||
data.aht30.humidity_rh,
|
||||
(long long)data.aht30.last_update_ms);
|
||||
} else {
|
||||
printf("AHT30: invalid, err=%s\n", esp_err_to_name(data.aht30.last_error));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool parse_on_off_arg(const char *arg, bool *on)
|
||||
{
|
||||
if (strcmp(arg, "on") == 0 || strcmp(arg, "1") == 0) {
|
||||
*on = true;
|
||||
return true;
|
||||
}
|
||||
if (strcmp(arg, "off") == 0 || strcmp(arg, "0") == 0) {
|
||||
*on = false;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// pump: 控制水泵开关,参数支持 on/off 或 1/0。
|
||||
static int cmd_pump(int argc, char **argv)
|
||||
{
|
||||
if (argc < 2) {
|
||||
printf("usage: pump <on|off>\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool on = false;
|
||||
if (!parse_on_off_arg(argv[1], &on)) {
|
||||
printf("invalid arg: %s\n", argv[1]);
|
||||
printf("usage: pump <on|off>\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
esp_err_t ret = io_device_control_set_pump(on);
|
||||
if (ret != ESP_OK) {
|
||||
printf("set pump failed: %s\n", esp_err_to_name(ret));
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("pump: %s\n", on ? "on" : "off");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// light: 控制补光灯开关,参数支持 on/off 或 1/0。
|
||||
static int cmd_light(int argc, char **argv)
|
||||
{
|
||||
if (argc < 2) {
|
||||
printf("usage: light <on|off>\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool on = false;
|
||||
if (!parse_on_off_arg(argv[1], &on)) {
|
||||
printf("invalid arg: %s\n", argv[1]);
|
||||
printf("usage: light <on|off>\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
esp_err_t ret = io_device_control_set_light(on);
|
||||
if (ret != ESP_OK) {
|
||||
printf("set light failed: %s\n", esp_err_to_name(ret));
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("light: %s\n", on ? "on" : "off");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// wifi: 查询或控制配网状态,支持 status/start/stop/clear 子命令。
|
||||
static int cmd_wifi(int argc, char **argv)
|
||||
{
|
||||
if (argc < 2 || strcmp(argv[1], "status") == 0) {
|
||||
wifi_connect_config_t cfg = {0};
|
||||
esp_err_t cfg_ret = wifi_connect_get_config(&cfg);
|
||||
printf("wifi status: %s\n", wifi_status_to_str(wifi_connect_get_status()));
|
||||
if (cfg_ret == ESP_OK && cfg.has_config) {
|
||||
printf("saved ssid: %s\n", cfg.ssid);
|
||||
} else {
|
||||
printf("saved config: none\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (strcmp(argv[1], "start") == 0) {
|
||||
esp_err_t ret = wifi_connect_start();
|
||||
if (ret != ESP_OK) {
|
||||
printf("wifi start failed: %s\n", esp_err_to_name(ret));
|
||||
return 1;
|
||||
}
|
||||
printf("wifi start requested\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (strcmp(argv[1], "stop") == 0) {
|
||||
esp_err_t ret = wifi_connect_stop();
|
||||
if (ret != ESP_OK) {
|
||||
printf("wifi stop failed: %s\n", esp_err_to_name(ret));
|
||||
return 1;
|
||||
}
|
||||
printf("wifi stop requested\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (strcmp(argv[1], "clear") == 0) {
|
||||
esp_err_t ret = wifi_connect_clear_config();
|
||||
if (ret != ESP_OK) {
|
||||
printf("wifi clear failed: %s\n", esp_err_to_name(ret));
|
||||
return 1;
|
||||
}
|
||||
printf("wifi config cleared\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
printf("usage: wifi <status|start|stop|clear>\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
esp_err_t console_user_cmds_register(void)
|
||||
{
|
||||
esp_err_t ret = ESP_OK;
|
||||
|
||||
ret = console_cmd_user_register("hello", cmd_hello);
|
||||
if (ret != ESP_OK) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = console_cmd_user_register("sensor", cmd_sensor);
|
||||
if (ret != ESP_OK) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = console_cmd_user_register("pump", cmd_pump);
|
||||
if (ret != ESP_OK) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = console_cmd_user_register("light", cmd_light);
|
||||
if (ret != ESP_OK) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = console_cmd_user_register("wifi", cmd_wifi);
|
||||
if (ret != ESP_OK) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
Reference in New Issue
Block a user