plug -> socket

This commit is contained in:
zogodo
2019-10-09 18:22:50 +08:00
parent d916d01092
commit 509eb8b812
7 changed files with 141 additions and 141 deletions

View File

@@ -41,25 +41,25 @@ void appRestoreDefault_callback(void * const user_config_data, uint32_t size)
int i, j; int i, j;
for (i = 0; i < SOCKET_NUM; i++) for (i = 0; i < SOCKET_NUM; i++)
{ {
userConfigDefault->plug[i].on = 1; userConfigDefault->socket[i].on = 1;
//插座名称 插口1-6 //插座名称 插口1-6
userConfigDefault->plug[i].name[0] = 0xe6; userConfigDefault->socket[i].name[0] = 0xe6;
userConfigDefault->plug[i].name[1] = 0x8f; userConfigDefault->socket[i].name[1] = 0x8f;
userConfigDefault->plug[i].name[2] = 0x92; userConfigDefault->socket[i].name[2] = 0x92;
userConfigDefault->plug[i].name[3] = 0xe5; userConfigDefault->socket[i].name[3] = 0xe5;
userConfigDefault->plug[i].name[4] = 0x8f; userConfigDefault->socket[i].name[4] = 0x8f;
userConfigDefault->plug[i].name[5] = 0xa3; userConfigDefault->socket[i].name[5] = 0xa3;
userConfigDefault->plug[i].name[6] = i + '1'; userConfigDefault->socket[i].name[6] = i + '1';
userConfigDefault->plug[i].name[7] = 0; userConfigDefault->socket[i].name[7] = 0;
// sprintf(userConfigDefault->plug[i].name, "插座%d", i);//编码异常 // sprintf(userConfigDefault->socket[i].name, "插座%d", i);//编码异常
for (j = 0; j < SOCKET_TIME_TASK_NUM; j++) for (j = 0; j < SOCKET_TIME_TASK_NUM; j++)
{ {
userConfigDefault->plug[i].task[j].hour = 0; userConfigDefault->socket[i].task[j].hour = 0;
userConfigDefault->plug[i].task[j].minute = 0; userConfigDefault->socket[i].task[j].minute = 0;
userConfigDefault->plug[i].task[j].repeat = 0x00; userConfigDefault->socket[i].task[j].repeat = 0x00;
userConfigDefault->plug[i].task[j].on = 0; userConfigDefault->socket[i].task[j].on = 0;
userConfigDefault->plug[i].task[j].action = 1; userConfigDefault->socket[i].task[j].action = 1;
} }
} }
// mico_system_context_update(sys_config); // mico_system_context_update(sys_config);
@@ -100,11 +100,11 @@ int application_start(void)
for (i = 0; i < Relay_NUM; i++) for (i = 0; i < Relay_NUM; i++)
{ {
MicoGpioInitialize(Relay[i], OUTPUT_PUSH_PULL); MicoGpioInitialize(Relay[i], OUTPUT_PUSH_PULL);
UserRelaySet(i, user_config->plug[i].on); UserRelaySet(i, user_config->socket[i].on);
} }
MicoSysLed(0); MicoSysLed(0);
if (user_config->version != USER_CONFIG_VERSION || user_config->plug[0].task[0].hour < 0 || user_config->plug[0].task[0].hour > 23) if (user_config->version != USER_CONFIG_VERSION || user_config->socket[0].task[0].hour < 0 || user_config->socket[0].task[0].hour > 23)
{ {
os_log("WARNGIN: user params restored!"); os_log("WARNGIN: user params restored!");
err = mico_system_context_restore(sys_config); err = mico_system_context_restore(sys_config);
@@ -136,13 +136,13 @@ int application_start(void)
os_log("version:%d",user_config->version); os_log("version:%d",user_config->version);
// for (i = 0; i < SOCKET_NUM; i++) // for (i = 0; i < SOCKET_NUM; i++)
// { // {
// os_log("plug_%d:",i); // os_log("socket_%d:",i);
// os_log("\tname:%s:",user_config->plug[i].name); // os_log("\tname:%s:",user_config->socket[i].name);
// for (j = 0; j < SOCKET_TIME_TASK_NUM; j++) // for (j = 0; j < SOCKET_TIME_TASK_NUM; j++)
// { // {
// os_log("\t\ton:%d\t %02d:%02d repeat:0x%X",user_config->plug[i].task[j].on, // os_log("\t\ton:%d\t %02d:%02d repeat:0x%X",user_config->socket[i].task[j].on,
// user_config->plug[i].task[j].hour,user_config->plug[i].task[j].minute, // user_config->socket[i].task[j].hour,user_config->socket[i].task[j].minute,
// user_config->plug[i].task[j].repeat); // user_config->socket[i].task[j].repeat);
// } // }
// } // }

View File

@@ -40,14 +40,14 @@ typedef struct
char repeat; //bit7:一次 bit6-0:周日-周一 char repeat; //bit7:一次 bit6-0:周日-周一
char action; //动作 char action; //动作
char on; //开关 char on; //开关
} user_plug_task_config_t; } user_socket_task_config_t;
typedef struct typedef struct
{ {
char name[SOCKET_NAME_LENGTH]; char name[SOCKET_NAME_LENGTH];
char on; //记录当前开关 char on; //记录当前开关
user_plug_task_config_t task[SOCKET_TIME_TASK_NUM]; user_socket_task_config_t task[SOCKET_TIME_TASK_NUM];
} user_plug_config_t; } user_socket_config_t;
//用户保存参数结构体 //用户保存参数结构体
typedef struct typedef struct
@@ -58,7 +58,7 @@ typedef struct
char mqtt_password[SETTING_MQTT_STRING_LENGTH_MAX]; //mqtt service user char mqtt_password[SETTING_MQTT_STRING_LENGTH_MAX]; //mqtt service user
// char mqtt_device_id[SETTING_MQTT_STRING_LENGTH_MAX]; //mqtt service user device name // char mqtt_device_id[SETTING_MQTT_STRING_LENGTH_MAX]; //mqtt service user device name
char version; char version;
user_plug_config_t plug[SOCKET_NUM]; user_socket_config_t socket[SOCKET_NUM];
char user[maxNameLen]; char user[maxNameLen];
WiFiEvent last_wifi_status; WiFiEvent last_wifi_status;
} user_config_t; } user_config_t;

View File

@@ -16,8 +16,8 @@ void user_function_set_last_time()
last_time = UpTicks(); last_time = UpTicks();
} }
bool json_plug_analysis(int udp_flag, unsigned char x, cJSON * pJsonRoot, cJSON * pJsonSend); bool json_socket_analysis(int udp_flag, unsigned char x, cJSON * pJsonRoot, cJSON * pJsonSend);
bool json_plug_task_analysis(unsigned char x, unsigned char y, cJSON * pJsonRoot, cJSON * pJsonSend); bool json_socket_task_analysis(unsigned char x, unsigned char y, cJSON * pJsonRoot, cJSON * pJsonSend);
void user_send(int udp_flag, char *s) void user_send(int udp_flag, char *s)
{ {
@@ -180,10 +180,10 @@ void user_function_cmd_received(int udp_flag, char* pusrdata)
cJSON_AddItemToObject(json_send, "setting", json_setting_send); cJSON_AddItemToObject(json_send, "setting", json_setting_send);
} }
//解析plug----------------------------------------------------------------- //解析socket-----------------------------------------------------------------
for (i = 0; i < SOCKET_NUM; i++) for (i = 0; i < SOCKET_NUM; i++)
{ {
if (json_plug_analysis(udp_flag, i, pJsonRoot, json_send)) if (json_socket_analysis(udp_flag, i, pJsonRoot, json_send))
update_user_config_flag = true; update_user_config_flag = true;
} }
@@ -214,67 +214,67 @@ void user_function_cmd_received(int udp_flag, char* pusrdata)
*udp_flag:发送udp/mqtt标志位,此处修改插座开关状态时,需要实时更新给domoticz *udp_flag:发送udp/mqtt标志位,此处修改插座开关状态时,需要实时更新给domoticz
*x:插座编号 *x:插座编号
*/ */
bool json_plug_analysis(int udp_flag, unsigned char x, cJSON * pJsonRoot, cJSON * pJsonSend) bool json_socket_analysis(int udp_flag, unsigned char x, cJSON * pJsonRoot, cJSON * pJsonSend)
{ {
if (!pJsonRoot) return false; if (!pJsonRoot) return false;
if (!pJsonSend) return false; if (!pJsonSend) return false;
char i; char i;
bool return_flag = false; bool return_flag = false;
char plug_str[] = "plug_X"; char socket_str[] = "socket_X";
plug_str[5] = x + '0'; socket_str[5] = x + '0';
cJSON *p_plug = cJSON_GetObjectItem(pJsonRoot, plug_str); cJSON *p_socket = cJSON_GetObjectItem(pJsonRoot, socket_str);
if (!p_plug) return_flag = false; if (!p_socket) return_flag = false;
cJSON *json_plug_send = cJSON_CreateObject(); cJSON *json_socket_send = cJSON_CreateObject();
//解析plug on------------------------------------------------------ //解析socket on------------------------------------------------------
if (p_plug) if (p_socket)
{ {
cJSON *p_plug_on = cJSON_GetObjectItem(p_plug, "on"); cJSON *p_socket_on = cJSON_GetObjectItem(p_socket, "on");
if (p_plug_on) if (p_socket_on)
{ {
if (cJSON_IsNumber(p_plug_on)) if (cJSON_IsNumber(p_socket_on))
{ {
UserRelaySet(x, p_plug_on->valueint); UserRelaySet(x, p_socket_on->valueint);
return_flag = true; return_flag = true;
} }
user_mqtt_send_plug_state(x); user_mqtt_send_socket_state(x);
} }
//解析plug中setting项目---------------------------------------------- //解析socket中setting项目----------------------------------------------
cJSON *p_plug_setting = cJSON_GetObjectItem(p_plug, "setting"); cJSON *p_socket_setting = cJSON_GetObjectItem(p_socket, "setting");
if (p_plug_setting) if (p_socket_setting)
{ {
cJSON *json_plug_setting_send = cJSON_CreateObject(); cJSON *json_socket_setting_send = cJSON_CreateObject();
//解析plug中setting中name---------------------------------------- //解析socket中setting中name----------------------------------------
cJSON *p_plug_setting_name = cJSON_GetObjectItem(p_plug_setting, "name"); cJSON *p_socket_setting_name = cJSON_GetObjectItem(p_socket_setting, "name");
if (p_plug_setting_name) if (p_socket_setting_name)
{ {
if (cJSON_IsString(p_plug_setting_name)) if (cJSON_IsString(p_socket_setting_name))
{ {
return_flag = true; return_flag = true;
sprintf(user_config->plug[x].name, p_plug_setting_name->valuestring); sprintf(user_config->socket[x].name, p_socket_setting_name->valuestring);
user_mqtt_hass_auto_name(x); user_mqtt_hass_auto_name(x);
} }
cJSON_AddStringToObject(json_plug_setting_send, "name", user_config->plug[x].name); cJSON_AddStringToObject(json_socket_setting_send, "name", user_config->socket[x].name);
} }
//解析plug中setting中task---------------------------------------- //解析socket中setting中task----------------------------------------
for (i = 0; i < SOCKET_TIME_TASK_NUM; i++) for (i = 0; i < SOCKET_TIME_TASK_NUM; i++)
{ {
if (json_plug_task_analysis(x, i, p_plug_setting, json_plug_setting_send)) if (json_socket_task_analysis(x, i, p_socket_setting, json_socket_setting_send))
return_flag = true; return_flag = true;
} }
cJSON_AddItemToObject(json_plug_send, "setting", json_plug_setting_send); cJSON_AddItemToObject(json_socket_send, "setting", json_socket_setting_send);
} }
} }
// cJSON *p_nvalue = cJSON_GetObjectItem(pJsonRoot, "nvalue"); // cJSON *p_nvalue = cJSON_GetObjectItem(pJsonRoot, "nvalue");
// if (p_plug || p_nvalue) // if (p_socket || p_nvalue)
cJSON_AddNumberToObject(json_plug_send, "on", user_config->plug[x].on); cJSON_AddNumberToObject(json_socket_send, "on", user_config->socket[x].on);
cJSON_AddItemToObject(pJsonSend, plug_str, json_plug_send); cJSON_AddItemToObject(pJsonSend, socket_str, json_socket_send);
return return_flag; return return_flag;
} }
@@ -282,53 +282,53 @@ bool json_plug_analysis(int udp_flag, unsigned char x, cJSON * pJsonRoot, cJSON
*解析处理定时任务json *解析处理定时任务json
*x:插座编号 y:任务编号 *x:插座编号 y:任务编号
*/ */
bool json_plug_task_analysis(unsigned char x, unsigned char y, cJSON * pJsonRoot, cJSON * pJsonSend) bool json_socket_task_analysis(unsigned char x, unsigned char y, cJSON * pJsonRoot, cJSON * pJsonSend)
{ {
if (!pJsonRoot) return false; if (!pJsonRoot) return false;
bool return_flag = false; bool return_flag = false;
char plug_task_str[] = "task_X"; char socket_task_str[] = "task_X";
plug_task_str[5] = y + '0'; socket_task_str[5] = y + '0';
cJSON *p_plug_task = cJSON_GetObjectItem(pJsonRoot, plug_task_str); cJSON *p_socket_task = cJSON_GetObjectItem(pJsonRoot, socket_task_str);
if (!p_plug_task) return false; if (!p_socket_task) return false;
cJSON *json_plug_task_send = cJSON_CreateObject(); cJSON *json_socket_task_send = cJSON_CreateObject();
cJSON *p_plug_task_hour = cJSON_GetObjectItem(p_plug_task, "hour"); cJSON *p_socket_task_hour = cJSON_GetObjectItem(p_socket_task, "hour");
cJSON *p_plug_task_minute = cJSON_GetObjectItem(p_plug_task, "minute"); cJSON *p_socket_task_minute = cJSON_GetObjectItem(p_socket_task, "minute");
cJSON *p_plug_task_repeat = cJSON_GetObjectItem(p_plug_task, "repeat"); cJSON *p_socket_task_repeat = cJSON_GetObjectItem(p_socket_task, "repeat");
cJSON *p_plug_task_action = cJSON_GetObjectItem(p_plug_task, "action"); cJSON *p_socket_task_action = cJSON_GetObjectItem(p_socket_task, "action");
cJSON *p_plug_task_on = cJSON_GetObjectItem(p_plug_task, "on"); cJSON *p_socket_task_on = cJSON_GetObjectItem(p_socket_task, "on");
if (p_plug_task_hour && p_plug_task_minute && p_plug_task_repeat && if (p_socket_task_hour && p_socket_task_minute && p_socket_task_repeat &&
p_plug_task_action p_socket_task_action
&& p_plug_task_on) && p_socket_task_on)
{ {
if (cJSON_IsNumber(p_plug_task_hour) if (cJSON_IsNumber(p_socket_task_hour)
&& cJSON_IsNumber(p_plug_task_minute) && cJSON_IsNumber(p_socket_task_minute)
&& cJSON_IsNumber(p_plug_task_repeat) && cJSON_IsNumber(p_socket_task_repeat)
&& cJSON_IsNumber(p_plug_task_action) && cJSON_IsNumber(p_socket_task_action)
&& cJSON_IsNumber(p_plug_task_on) && cJSON_IsNumber(p_socket_task_on)
) )
{ {
return_flag = true; return_flag = true;
user_config->plug[x].task[y].hour = p_plug_task_hour->valueint; user_config->socket[x].task[y].hour = p_socket_task_hour->valueint;
user_config->plug[x].task[y].minute = p_plug_task_minute->valueint; user_config->socket[x].task[y].minute = p_socket_task_minute->valueint;
user_config->plug[x].task[y].repeat = p_plug_task_repeat->valueint; user_config->socket[x].task[y].repeat = p_socket_task_repeat->valueint;
user_config->plug[x].task[y].action = p_plug_task_action->valueint; user_config->socket[x].task[y].action = p_socket_task_action->valueint;
user_config->plug[x].task[y].on = p_plug_task_on->valueint; user_config->socket[x].task[y].on = p_socket_task_on->valueint;
} }
} }
cJSON_AddNumberToObject(json_plug_task_send, "hour", user_config->plug[x].task[y].hour); cJSON_AddNumberToObject(json_socket_task_send, "hour", user_config->socket[x].task[y].hour);
cJSON_AddNumberToObject(json_plug_task_send, "minute", user_config->plug[x].task[y].minute); cJSON_AddNumberToObject(json_socket_task_send, "minute", user_config->socket[x].task[y].minute);
cJSON_AddNumberToObject(json_plug_task_send, "repeat", user_config->plug[x].task[y].repeat); cJSON_AddNumberToObject(json_socket_task_send, "repeat", user_config->socket[x].task[y].repeat);
cJSON_AddNumberToObject(json_plug_task_send, "action", user_config->plug[x].task[y].action); cJSON_AddNumberToObject(json_socket_task_send, "action", user_config->socket[x].task[y].action);
cJSON_AddNumberToObject(json_plug_task_send, "on", user_config->plug[x].task[y].on); cJSON_AddNumberToObject(json_socket_task_send, "on", user_config->socket[x].task[y].on);
cJSON_AddItemToObject(pJsonSend, plug_task_str, json_plug_task_send); cJSON_AddItemToObject(pJsonSend, socket_task_str, json_socket_task_send);
return return_flag; return return_flag;
} }

View File

@@ -23,7 +23,7 @@ bool RelayOut(void)
int i; int i;
for (i = 0; i < SOCKET_NUM; i++) for (i = 0; i < SOCKET_NUM; i++)
{ {
if (user_config->plug[i].on != 0) if (user_config->socket[i].on != 0)
{ {
return true; return true;
} }
@@ -34,12 +34,12 @@ bool RelayOut(void)
const unsigned char* GetSocketStatus() const unsigned char* GetSocketStatus()
{ {
sprintf(socket_status, "%d,%d,%d,%d,%d,%d", sprintf(socket_status, "%d,%d,%d,%d,%d,%d",
user_config->plug[0].on, user_config->socket[0].on,
user_config->plug[1].on, user_config->socket[1].on,
user_config->plug[2].on, user_config->socket[2].on,
user_config->plug[3].on, user_config->socket[3].on,
user_config->plug[4].on, user_config->socket[4].on,
user_config->plug[5].on); user_config->socket[5].on);
return (const unsigned char*)socket_status; return (const unsigned char*)socket_status;
} }
@@ -73,7 +73,7 @@ void UserRelaySet(unsigned char i, unsigned char on)
MicoGpioOutputLow(relay[i]); MicoGpioOutputLow(relay[i]);
} }
user_config->plug[i].on = on; user_config->socket[i].on = on;
if (RelayOut()) if (RelayOut())
{ {
@@ -134,7 +134,7 @@ static void KeyShortPress(void)
for (i = 0; i < SOCKET_NUM; i++) for (i = 0; i < SOCKET_NUM; i++)
{ {
user_mqtt_send_plug_state(i); user_mqtt_send_socket_state(i);
} }
} }
mico_timer_t user_key_timer; mico_timer_t user_key_timer;

View File

@@ -82,8 +82,8 @@ static OSStatus mqtt_msg_publish(Client *c, const char* topic, char qos, char re
OSStatus user_recv_handler(void *arg); OSStatus user_recv_handler(void *arg);
OSStatus user_mqtt_send_plug_state(char plug_id); OSStatus user_mqtt_send_socket_state(char socket_id);
void user_mqtt_hass_auto(char plug_id); void user_mqtt_hass_auto(char socket_id);
void user_mqtt_hass_auto_power(void); void user_mqtt_hass_auto_power(void);
bool isconnect = false; bool isconnect = false;
@@ -160,7 +160,7 @@ void user_mqtt_timer_func(void *arg)
break; break;
case 15: case 15:
if (buf1 == NULL) break; if (buf1 == NULL) break;
sprintf(buf1, "{\"mac\":\"%s\",\"version\":null,\"plug_0\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_1\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_2\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_3\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_4\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_5\":{\"on\":null,\"setting\":{\"name\":null}}}", strMac); sprintf(buf1, "{\"mac\":\"%s\",\"version\":null,\"socket_0\":{\"on\":null,\"setting\":{\"name\":null}},\"socket_1\":{\"on\":null,\"setting\":{\"name\":null}},\"socket_2\":{\"on\":null,\"setting\":{\"name\":null}},\"socket_3\":{\"on\":null,\"setting\":{\"name\":null}},\"socket_4\":{\"on\":null,\"setting\":{\"name\":null}},\"socket_5\":{\"on\":null,\"setting\":{\"name\":null}}}", strMac);
user_function_cmd_received(0, buf1); user_function_cmd_received(0, buf1);
free(buf1); free(buf1);
break; break;
@@ -504,7 +504,7 @@ OSStatus user_mqtt_send(char *arg)
} }
//更新ha开关状态 //更新ha开关状态
OSStatus user_mqtt_send_plug_state(char plug_id) OSStatus user_mqtt_send_socket_state(char socket_id)
{ {
char *send_buf = NULL; char *send_buf = NULL;
char *topic_buf = NULL; char *topic_buf = NULL;
@@ -513,8 +513,8 @@ OSStatus user_mqtt_send_plug_state(char plug_id)
OSStatus oss_status = kUnknownErr; OSStatus oss_status = kUnknownErr;
if (send_buf != NULL && topic_buf != NULL) if (send_buf != NULL && topic_buf != NULL)
{ {
sprintf(topic_buf, "homeassistant/switch/%s/plug_%d/state", strMac, (int)plug_id); sprintf(topic_buf, "homeassistant/switch/%s/socket_%d/state", strMac, (int)socket_id);
sprintf(send_buf, "{\"mac\":\"%s\",\"plug_%d\":{\"on\":%d}}", strMac, plug_id, (int)user_config->plug[(int)plug_id].on); sprintf(send_buf, "{\"mac\":\"%s\",\"socket_%d\":{\"on\":%d}}", strMac, socket_id, (int)user_config->socket[(int)socket_id].on);
oss_status = user_mqtt_send_topic(topic_buf, send_buf, 1); oss_status = user_mqtt_send_topic(topic_buf, send_buf, 1);
} }
if (send_buf) free(send_buf); if (send_buf) free(send_buf);
@@ -524,7 +524,7 @@ OSStatus user_mqtt_send_plug_state(char plug_id)
} }
//hass mqtt自动发现数据开关发送 //hass mqtt自动发现数据开关发送
void user_mqtt_hass_auto(char plug_id) void user_mqtt_hass_auto(char socket_id)
{ {
char *send_buf = NULL; char *send_buf = NULL;
char *topic_buf = NULL; char *topic_buf = NULL;
@@ -532,22 +532,22 @@ void user_mqtt_hass_auto(char plug_id)
topic_buf = malloc(128); // topic_buf = malloc(128); //
if (send_buf != NULL && topic_buf != NULL) if (send_buf != NULL && topic_buf != NULL)
{ {
sprintf(topic_buf, "homeassistant/switch/%s/plug_%d/config", strMac, plug_id); sprintf(topic_buf, "homeassistant/switch/%s/socket_%d/config", strMac, socket_id);
sprintf(send_buf, "{" sprintf(send_buf, "{"
"\"name\":\"zTC1_plug%d_%s\"," "\"name\":\"zTC1_socket%d_%s\","
"\"stat_t\":\"homeassistant/switch/%s/plug_%d/state\"," "\"stat_t\":\"homeassistant/switch/%s/socket_%d/state\","
"\"cmd_t\":\"device/ztc1/set\"," "\"cmd_t\":\"device/ztc1/set\","
"\"pl_on\":\"{\\\"mac\\\":\\\"%s\\\",\\\"plug_%d\\\":{\\\"on\\\":1}}\"," "\"pl_on\":\"{\\\"mac\\\":\\\"%s\\\",\\\"socket_%d\\\":{\\\"on\\\":1}}\","
"\"pl_off\":\"{\\\"mac\\\":\\\"%s\\\",\\\"plug_%d\\\":{\\\"on\\\":0}}\"" "\"pl_off\":\"{\\\"mac\\\":\\\"%s\\\",\\\"socket_%d\\\":{\\\"on\\\":0}}\""
"}", "}",
plug_id, strMac + 8, strMac, plug_id, strMac, plug_id, strMac, plug_id); socket_id, strMac + 8, strMac, socket_id, strMac, socket_id, strMac, socket_id);
user_mqtt_send_topic(topic_buf, send_buf, 1); user_mqtt_send_topic(topic_buf, send_buf, 1);
} }
if (send_buf) free(send_buf); if (send_buf) free(send_buf);
if (topic_buf) free(topic_buf); if (topic_buf) free(topic_buf);
} }
void user_mqtt_hass_auto_name(char plug_id) void user_mqtt_hass_auto_name(char socket_id)
{ {
char *send_buf = NULL; char *send_buf = NULL;
char *topic_buf = NULL; char *topic_buf = NULL;
@@ -555,15 +555,15 @@ void user_mqtt_hass_auto_name(char plug_id)
topic_buf = (char *) malloc(64); topic_buf = (char *) malloc(64);
if (send_buf != NULL && topic_buf != NULL) if (send_buf != NULL && topic_buf != NULL)
{ {
sprintf(topic_buf, "homeassistant/switch/%s/plug_%d/config", strMac, plug_id); sprintf(topic_buf, "homeassistant/switch/%s/socket_%d/config", strMac, socket_id);
sprintf(send_buf, "{" sprintf(send_buf, "{"
"\"name\":\"%s\"," "\"name\":\"%s\","
"\"stat_t\":\"homeassistant/switch/%s/plug_%d/state\"," "\"stat_t\":\"homeassistant/switch/%s/socket_%d/state\","
"\"cmd_t\":\"device/ztc1/set\"," "\"cmd_t\":\"device/ztc1/set\","
"\"pl_on\":\"{\\\"mac\\\":\\\"%s\\\",\\\"plug_%d\\\":{\\\"on\\\":1}}\"," "\"pl_on\":\"{\\\"mac\\\":\\\"%s\\\",\\\"socket_%d\\\":{\\\"on\\\":1}}\","
"\"pl_off\":\"{\\\"mac\\\":\\\"%s\\\",\\\"plug_%d\\\":{\\\"on\\\":0}}\"" "\"pl_off\":\"{\\\"mac\\\":\\\"%s\\\",\\\"socket_%d\\\":{\\\"on\\\":0}}\""
"}", "}",
user_config->plug[(int)plug_id].name, strMac, plug_id, strMac, plug_id, strMac, plug_id); user_config->socket[(int)socket_id].name, strMac, socket_id, strMac, socket_id, strMac, socket_id);
user_mqtt_send_topic(topic_buf, send_buf, 0); user_mqtt_send_topic(topic_buf, send_buf, 0);
} }
if (send_buf) if (send_buf)

View File

@@ -14,9 +14,9 @@
extern OSStatus user_mqtt_init(void); extern OSStatus user_mqtt_init(void);
extern OSStatus user_mqtt_send(char *arg); extern OSStatus user_mqtt_send(char *arg);
extern bool user_mqtt_isconnect(void); extern bool user_mqtt_isconnect(void);
extern OSStatus user_mqtt_send_plug_state(char plug_id); extern OSStatus user_mqtt_send_socket_state(char socket_id);
extern void user_mqtt_hass_auto(char plug_id); extern void user_mqtt_hass_auto(char socket_id);
extern void user_mqtt_hass_auto_name(char plug_id); extern void user_mqtt_hass_auto_name(char socket_id);
extern void user_mqtt_hass_power(void); extern void user_mqtt_hass_power(void);
extern void user_mqtt_hass_auto_power(void); extern void user_mqtt_hass_auto_power(void);
extern void user_mqtt_hass_auto_power_name(void); extern void user_mqtt_hass_auto_power_name(void);

View File

@@ -180,26 +180,26 @@ void rtc_thread(mico_thread_arg_t arg)
{ {
for (j = 0; j < SOCKET_TIME_TASK_NUM; j++) for (j = 0; j < SOCKET_TIME_TASK_NUM; j++)
{ {
if (user_config->plug[i].task[j].on != 0) if (user_config->socket[i].task[j].on != 0)
{ {
char repeat = user_config->plug[i].task[j].repeat; char repeat = user_config->socket[i].task[j].repeat;
if ( //符合条件则改变继电器状态: 秒为0 时分符合设定值, 重复符合设定值 if ( //符合条件则改变继电器状态: 秒为0 时分符合设定值, 重复符合设定值
rtc_time.sec == 0 && rtc_time.min == user_config->plug[i].task[j].minute rtc_time.sec == 0 && rtc_time.min == user_config->socket[i].task[j].minute
&& rtc_time.hr == user_config->plug[i].task[j].hour && rtc_time.hr == user_config->socket[i].task[j].hour
&& ((repeat == 0x00) || repeat & (1 << (rtc_time.weekday - 1))) && ((repeat == 0x00) || repeat & (1 << (rtc_time.weekday - 1)))
) )
{ {
if (user_config->plug[i].on != user_config->plug[i].task[j].action) if (user_config->socket[i].on != user_config->socket[i].task[j].action)
{ {
UserRelaySet(i, user_config->plug[i].task[j].action); UserRelaySet(i, user_config->socket[i].task[j].action);
update_user_config_flag = 1; update_user_config_flag = 1;
user_mqtt_send_plug_state(i); user_mqtt_send_socket_state(i);
} }
if (repeat == 0x00) if (repeat == 0x00)
{ {
task_flag[i] = j; task_flag[i] = j;
user_config->plug[i].task[j].on = 0; user_config->socket[i].task[j].on = 0;
update_user_config_flag = 1; update_user_config_flag = 1;
} }
} }
@@ -219,31 +219,31 @@ void rtc_thread(mico_thread_arg_t arg)
for (i = 0; i < SOCKET_NUM; i++) for (i = 0; i < SOCKET_NUM; i++)
{ {
char strTemp1[] = "plug_X"; char strTemp1[] = "socket_X";
strTemp1[5] = i + '0'; strTemp1[5] = i + '0';
cJSON *json_send_plug = cJSON_CreateObject(); cJSON *json_send_socket = cJSON_CreateObject();
cJSON_AddNumberToObject(json_send_plug, "on", user_config->plug[i].on); cJSON_AddNumberToObject(json_send_socket, "on", user_config->socket[i].on);
if (task_flag[i] >= 0) if (task_flag[i] >= 0)
{ {
cJSON *json_send_plug_setting = cJSON_CreateObject(); cJSON *json_send_socket_setting = cJSON_CreateObject();
j = task_flag[i]; j = task_flag[i];
char strTemp2[] = "task_X"; char strTemp2[] = "task_X";
strTemp2[5] = j + '0'; strTemp2[5] = j + '0';
cJSON *json_send_plug_task = cJSON_CreateObject(); cJSON *json_send_socket_task = cJSON_CreateObject();
cJSON_AddNumberToObject(json_send_plug_task, "hour", user_config->plug[i].task[j].hour); cJSON_AddNumberToObject(json_send_socket_task, "hour", user_config->socket[i].task[j].hour);
cJSON_AddNumberToObject(json_send_plug_task, "minute", user_config->plug[i].task[j].minute); cJSON_AddNumberToObject(json_send_socket_task, "minute", user_config->socket[i].task[j].minute);
cJSON_AddNumberToObject(json_send_plug_task, "repeat", user_config->plug[i].task[j].repeat); cJSON_AddNumberToObject(json_send_socket_task, "repeat", user_config->socket[i].task[j].repeat);
cJSON_AddNumberToObject(json_send_plug_task, "action", user_config->plug[i].task[j].action); cJSON_AddNumberToObject(json_send_socket_task, "action", user_config->socket[i].task[j].action);
cJSON_AddNumberToObject(json_send_plug_task, "on", user_config->plug[i].task[j].on); cJSON_AddNumberToObject(json_send_socket_task, "on", user_config->socket[i].task[j].on);
cJSON_AddItemToObject(json_send_plug_setting, strTemp2, json_send_plug_task); cJSON_AddItemToObject(json_send_socket_setting, strTemp2, json_send_socket_task);
cJSON_AddItemToObject(json_send_plug, "setting", json_send_plug_setting); cJSON_AddItemToObject(json_send_socket, "setting", json_send_socket_setting);
task_flag[i] = -1; task_flag[i] = -1;
} }
cJSON_AddItemToObject(json_send, strTemp1, json_send_plug); cJSON_AddItemToObject(json_send, strTemp1, json_send_socket);
} }
char *json_str = cJSON_Print(json_send); char *json_str = cJSON_Print(json_send);