后台以及ha mqtt增加总开关功能

This commit is contained in:
nhkefus
2025-03-17 10:40:45 +08:00
parent a3e3639cf9
commit 7113b361e8
6 changed files with 105 additions and 6 deletions

View File

@@ -74,6 +74,7 @@ void UserMqttTimerFunc(void *arg)
{
case 0:
UserMqttHassAutoLed();
UserMqttHassAutoTotalSocket();
break;
case 1:
case 2:
@@ -412,6 +413,13 @@ void ProcessHaCmd(char* cmd)
}
UserMqttSendLedState();
mico_system_context_update(sys_config);
}else if(strcmp(cmd, "set total_socket") == ' '){
int on;
sscanf(cmd, "set total_socket %s %d", mac, &on);
if (strcmp(mac, str_mac)) return;
mqtt_log("set total_socket on[%d]", on);
UserRelaySetAll(on);
UserMqttSendTotalSocketState();
}
}
@@ -474,6 +482,23 @@ OSStatus UserMqttSendSocketState(char socket_id)
return oss_status;
}
OSStatus UserMqttSendTotalSocketState()
{
char *send_buf = malloc(64);
char *topic_buf = malloc(64);
OSStatus oss_status = kUnknownErr;
if (send_buf != NULL && topic_buf != NULL)
{
sprintf(topic_buf, "homeassistant/switch/%s/total_socket/state", str_mac);
sprintf(send_buf, "set total_socket %s %d", str_mac, RelayOut()?1:0);
oss_status = UserMqttSendTopic(topic_buf, send_buf, 1);
}
if (send_buf) free(send_buf);
if (topic_buf) free(topic_buf);
return oss_status;
}
OSStatus UserMqttSendLedState(void)
{
char *send_buf = malloc(64);
@@ -552,6 +577,36 @@ void UserMqttHassAutoLed(void)
if (topic_buf)
free(topic_buf);
}
void UserMqttHassAutoTotalSocket(void)
{
char *send_buf = NULL;
char *topic_buf = NULL;
send_buf = (char *) malloc(1024);
topic_buf = (char *) malloc(64);
if (send_buf != NULL && topic_buf != NULL)
{
sprintf(topic_buf, "homeassistant/switch/%s/total_socket/config", str_mac);
sprintf(send_buf,
"{\"name\":\"TC1_%s_TotalSocket\","
"\"uniq_id\":\"%s_total_socket\","
"\"stat_t\":\"homeassistant/switch/%s/total_socket/state\","
"\"cmd_t\":\"device/ztc1/set\","
"\"pl_on\":\"set total_socket %s 1\","
"\"pl_off\":\"set total_socket %s 0\","
"\"device\":{"
"\"identifiers\":[\"tc1_%s\"],"
"\"name\":\"TC1_%s\","
"\"model\":\"TC1\","
"\"manufacturer\":\"PHICOMM\"}}",
str_mac+8, str_mac, str_mac, str_mac,str_mac, str_mac, str_mac);
UserMqttSendTopic(topic_buf, send_buf, 1);
}
if (send_buf)
free(send_buf);
if (topic_buf)
free(topic_buf);
}
//hass mqtt鑷姩鍙戠幇鏁版嵁鍔熺巼鍙戦<E98D99><E688A6>
void UserMqttHassAutoPower(void)
{

View File

@@ -31,6 +31,8 @@ extern OSStatus UserMqttSendSocketState(char socket_id);
extern OSStatus UserMqttSendLedState(void);
extern OSStatus UserMqttSendTotalSocketState(void);
extern void UserMqttHassAuto(char socket_id);
extern void UserMqttHassPower(void);