需求,按键控制逻辑更大更改完成初版

This commit is contained in:
2025-08-03 22:54:46 +08:00
parent e728a942a4
commit 1a40c6f348
7 changed files with 56 additions and 35 deletions

View File

@ -482,7 +482,7 @@ int hilink_ble_main(void)
ret = HILINK_SetNetConfigMode(HILINK_NETCONFIG_OTHER);
/* 设备按需设置,例如接入蓝牙网关时,设置广播类型标志及心跳间隔 */
unsigned char mpp[] = { 0x80, 0x60, 0xea};
unsigned char mpp[] = { 0x70, 0x30, 0x75};
ret = BLE_SetAdvNameMpp(mpp, sizeof(mpp));
if (ret != 0) {
HILINK_SAL_NOTICE("set adv name mpp failed\r\n");

View File

@ -30,7 +30,7 @@ extern "C" {
#define DEVICE_HIVERSION "1.0.0"
/* 设备固件版本号 */
#define FIRMWARE_VER "1.0.3"
#define FIRMWARE_VER "1.0.4"
/* 设备硬件版本号 */
#define HARDWARE_VER "1.0.0"
/* 设备软件版本号 */

View File

@ -84,9 +84,11 @@ static int ble_handle_master_switch(cJSON *dataItem) {
}
bool new_state = (onItem->valueint != 0);
bool old_state = g_persistent_state.master_switch;
e_printf("[BLE] 接收到总开关控制指令: %s\r\n", new_state ? "" : "");
// 调用现有的总开关控制函数
// 统一使用 update_master_switch 处理总开关逻辑
// apply_master_switch_control 中已包含智能全开逻辑
update_master_switch(new_state);
// 通过蓝牙上报状态确认

View File

@ -119,7 +119,12 @@ int handle_put_switch(const char* svc_id, const char* payload, unsigned int len)
}
cJSON* on_item = cJSON_GetObjectItem(json, "on");
update_master_switch(cJSON_GetNumberValue(on_item));
bool new_state = (bool)cJSON_GetNumberValue(on_item);
bool old_state = g_persistent_state.master_switch;
// 统一使用 update_master_switch 处理总开关逻辑
// apply_master_switch_control 中已包含智能全开逻辑
update_master_switch(new_state);
cJSON_Delete(json);
return 0;

View File

@ -13,16 +13,12 @@ void update_switch_state(int switch_id, bool state) {
return;
}
// 检查总开关是否允许操作
// 注意:如果设备未绑定,则不受总开关限制,可以自由控制
if (g_persistent_state.is_bound && !g_persistent_state.master_switch && state) {
e_printf("设备已绑定且总开关关闭,不允许开启开关%d\r\n", switch_id + 1);
return;
}
// 如果设备未绑定,记录状态变化以便调试
if (!g_persistent_state.is_bound) {
e_printf("设备未绑定,允许自由控制开关%d\r\n", switch_id + 1);
// 新逻辑:移除总开关限制,允许本地按键自由控制子开关
// 本地按键控制子开关时,如果要开启子开关且总开关关闭,则自动打开总开关
bool need_auto_master_on = false;
if (state && !g_persistent_state.master_switch) {
need_auto_master_on = true;
e_printf("子开关%d激活将自动打开总开关\r\n", switch_id + 1);
}
// 更新开关状态
@ -39,6 +35,15 @@ void update_switch_state(int switch_id, bool state) {
e_printf("开关%d 状态更新: %s\r\n",
switch_id + 1, state ? "" : "");
// 如果需要自动打开总开关
if (need_auto_master_on) {
g_persistent_state.master_switch = true;
e_printf("总开关已自动打开\r\n");
// 异步上报总开关状态变化
fast_report_master_switch_async();
}
// 立即保存状态
save_persistent_state();
@ -72,15 +77,9 @@ void update_master_switch(bool state) {
// 应用总开关控制逻辑
void apply_master_switch_control(void) {
// 如果设备未绑定,总开关不影响子开关控制
if (!g_persistent_state.is_bound) {
e_printf("设备未绑定,总开关不影响子开关控制\r\n");
return;
}
if (!g_persistent_state.master_switch) {
// 设备已绑定且总开关关闭时,强制关闭所有子开关
e_printf("设备已绑定且总开关关闭,强制关闭所有子开关\r\n");
// 总开关关闭时,强制关闭所有子开关
e_printf("总开关关闭,强制关闭所有子开关\r\n");
for (int i = 0; i < SWITCH_COUNT; i++) {
// 如果子开关之前是开着的,需要同步状态
@ -94,22 +93,37 @@ void apply_master_switch_control(void) {
set_switch_output(i, false);
set_led_output(i, LED_YELLOW);
}
} else {
// 总开关开启时,恢复各开关的原状态(不改变子开关状态)
// e_printf("总开关开启,恢复各子开关原有状态\r\n");
// for (int i = 0; i < SWITCH_COUNT; i++) {
// // 硬件状态跟随子开关的实际状态
// set_switch_output(i, g_persistent_state.switches[i].switch_on);
// set_led_output(i, g_persistent_state.switches[i].led_state ? LED_WHITE : LED_YELLOW);
// }
}
// 同步所有子开关状态到云端
if (g_persistent_state.is_bound) {
fast_report_all_switches_async();
// 同步所有子开关状态到云端
if (g_persistent_state.is_bound) {
fast_report_all_switches_async();
}
} else {
// 总开关开启时,自动开启所有子开关
e_printf("总开关开启,自动开启所有子开关\r\n");
bool any_switch_changed = false;
for (int i = 0; i < SWITCH_COUNT; i++) {
if (!g_persistent_state.switches[i].switch_on) {
g_persistent_state.switches[i].switch_on = true;
g_persistent_state.switches[i].led_state = true; // true表示白灯
// 同步硬件状态
set_switch_output(i, true);
set_led_output(i, LED_WHITE);
e_printf("自动开启子开关%d\r\n", i + 1);
any_switch_changed = true;
}
}
// 如果有子开关状态变化,同步到云端
if (any_switch_changed && g_persistent_state.is_bound) {
fast_report_all_switches_async();
}
}
e_printf("所有子开关状态已同步\r\n");
e_printf("总开关控制逻辑应用完成\r\n");
}
//====================== 按键检测与处理 ======================

Binary file not shown.

Binary file not shown.