3 Commits

2 changed files with 58 additions and 34 deletions

View File

@@ -438,20 +438,6 @@
<th>循环</th> <th>循环</th>
<th>操作</th> <th>操作</th>
</tr> </tr>
<tr>
<td>02-15 07:11</td>
<td>1</td>
<td>0</td>
<td>0</td>
<td><a>删除</a></td>
</tr>
<tr>
<td>2020-02-15<br>07:11:08</td>
<td>1</td>
<td>0</td>
<td>6</td>
<td><a>删除</a></td>
</tr>
</table> </table>
</div> </div>
@@ -610,23 +596,23 @@
<table class="mdl-data-table mdl-js-data-table"> <table class="mdl-data-table mdl-js-data-table">
<tr> <tr>
<th>版本</th> <th>版本</th>
<th id="info_version">v1.0.33</th> <th id="info_version"></th>
</tr> </tr>
<tr> <tr>
<td>IP</td> <td>IP</td>
<td id="info_ip">192.168.33.222</td> <td id="info_ip"></td>
</tr> </tr>
<tr> <tr>
<td>子网掩码</td> <td>子网掩码</td>
<td id="info_mask">255.255.255.0</td> <td id="info_mask"></td>
</tr> </tr>
<tr> <tr>
<td>网关</td> <td>网关</td>
<td id="info_gateway">192.168.33.1</td> <td id="info_gateway"></td>
</tr> </tr>
<tr> <tr>
<td>启动时间</td> <td>启动时间</td>
<td id="uptime">10:13:43</td> <td id="uptime"></td>
</tr> </tr>
</table> </table>
<div class="mdl-card__actions mdl-card--border" style="display: flex; align-items: center; flex-wrap: wrap;"> <div class="mdl-card__actions mdl-card--border" style="display: flex; align-items: center; flex-wrap: wrap;">
@@ -638,7 +624,7 @@
</svg> </svg>
</i> </i>
<!-- <span>2020-02-22</span> --> <!-- <span>2020-02-22</span> -->
<a class="mdl-button" id="st-date">2020-02-22</a> <a class="mdl-button" id="st-date"></a>
</div> </div>
</div> </div>

View File

@@ -108,10 +108,23 @@ OSStatus UserMqttDeInit(void) {
return err; return err;
} }
void clear_mqtt_msg_send_queue(void) {
if(mqtt_msg_send_queue == NULL){
return;
}
void *msg = NULL;
while (mico_rtos_is_queue_empty(&mqtt_msg_send_queue) == false) {
if (mico_rtos_pop_from_queue(&mqtt_msg_send_queue, &msg, 0) == kNoErr) {
if (msg) free(msg); // 释放消息内存,避免泄漏
}
}
}
/* Application entrance */ /* Application entrance */
OSStatus UserMqttInit(void) { OSStatus UserMqttInit(void) {
OSStatus err = kNoErr; OSStatus err = kNoErr;
if(mqtt_msg_send_queue != NULL)
return err;
sprintf(topic_set, MQTT_CLIENT_SUB_TOPIC1); sprintf(topic_set, MQTT_CLIENT_SUB_TOPIC1);
sprintf(topic_state, MQTT_CLIENT_PUB_TOPIC, str_mac); sprintf(topic_state, MQTT_CLIENT_PUB_TOPIC, str_mac);
//TODO size:0x800 //TODO size:0x800
@@ -146,13 +159,32 @@ OSStatus UserMqttInit(void) {
static OSStatus UserMqttClientRelease(Client *c, Network *n) { static OSStatus UserMqttClientRelease(Client *c, Network *n) {
OSStatus err = kNoErr; OSStatus err = kNoErr;
if (c->isconnected) MQTTDisconnect(c); if (c == NULL || n == NULL) return kParamErr;
n->disconnect(n); // close connection if (c->isconnected) {
MQTTDisconnect(c);
c->isconnected = 0;
}
if (MQTT_SUCCESS != MQTTClientDeinit(c)) { mqtt_log("MQTTClientDeinit failed!"); if (c->buf) {
free(c->buf);
c->buf = NULL;
}
if (c->readbuf) {
free(c->readbuf);
c->readbuf = NULL;
}
if (n->disconnect) {
n->disconnect(n);
}
if (MQTT_SUCCESS != MQTTClientDeinit(c)) {
mqtt_log("MQTTClientDeinit failed!");
err = kDeletedErr; err = kDeletedErr;
} }
return err; return err;
} }
@@ -187,6 +219,9 @@ static OSStatus MqttMsgPublish(Client *c, const char *topic, char qos, char reta
} }
void registerMqttEvents(void) { void registerMqttEvents(void) {
if(timer_status !=0){
mico_stop_timer(&timer_handle);
}
timer_status = 0; timer_status = 0;
mico_start_timer(&timer_handle); mico_start_timer(&timer_handle);
} }
@@ -212,6 +247,8 @@ void MqttClientThread(mico_thread_arg_t arg) {
/* create msg send queue event fd */ /* create msg send queue event fd */
msg_send_event_fd = mico_create_event_fd(mqtt_msg_send_queue); msg_send_event_fd = mico_create_event_fd(mqtt_msg_send_queue);
mico_init_timer(&timer_handle, 150, UserMqttTimerFunc, &arg);
require_action(msg_send_event_fd >= 0, exit, require_action(msg_send_event_fd >= 0, exit,
mqtt_log("ERROR: create msg send queue event fd failed!!!")); mqtt_log("ERROR: create msg send queue event fd failed!!!"));
mqtt_thread_should_exit = false; mqtt_thread_should_exit = false;
@@ -238,7 +275,8 @@ void MqttClientThread(mico_thread_arg_t arg) {
if (rc == MQTT_SUCCESS) break; if (rc == MQTT_SUCCESS) break;
//mqtt_log("ERROR: MQTT network connect err=%d, reconnect after 3s...", rc); //mqtt_log("ERROR: MQTT network connect err=%d, reconnect after 3s...", rc);
}mqtt_log("MQTT network connect success!"); }
mqtt_log("MQTT network connect success!");
/* 2. init mqtt client */ /* 2. init mqtt client */
//c.heartbeat_retry_max = 2; //c.heartbeat_retry_max = 2;
@@ -259,7 +297,7 @@ void MqttClientThread(mico_thread_arg_t arg) {
rc = MQTTConnect(&c, &connectData); rc = MQTTConnect(&c, &connectData);
require_noerr_string(rc, MQTT_reconnect, "ERROR: MQTT client connect err."); require_noerr_string(rc, MQTT_reconnect, "ERROR: MQTT client connect err.");
mqtt_log("MQTT client connect success!"); mqtt_log("MQTT client connect success, result: %d ", rc);
UserLedSet(RelayOut() && user_config->power_led_enabled); UserLedSet(RelayOut() && user_config->power_led_enabled);
@@ -279,7 +317,6 @@ void MqttClientThread(mico_thread_arg_t arg) {
UserMqttSendTotalSocketState(); UserMqttSendTotalSocketState();
UserMqttSendChildLockState(); UserMqttSendChildLockState();
mico_init_timer(&timer_handle, 150, UserMqttTimerFunc, &arg);
registerMqttEvents(); registerMqttEvents();
/* 5. client loop for recv msg && keepalive */ /* 5. client loop for recv msg && keepalive */
while (!mqtt_thread_should_exit) { while (!mqtt_thread_should_exit) {
@@ -308,13 +345,12 @@ void MqttClientThread(mico_thread_arg_t arg) {
err = MqttMsgPublish(&c, p_send_msg->topic, p_send_msg->qos, p_send_msg->retained, err = MqttMsgPublish(&c, p_send_msg->topic, p_send_msg->qos, p_send_msg->retained,
(const unsigned char *) p_send_msg->data, (const unsigned char *) p_send_msg->data,
p_send_msg->datalen); p_send_msg->datalen);
free(p_send_msg);
p_send_msg = NULL;
require_noerr_string(err, MQTT_reconnect, "ERROR: MQTT publish data err"); require_noerr_string(err, MQTT_reconnect, "ERROR: MQTT publish data err");
//mqtt_log("MQTT publish data success! send_topic=[%s], msg=[%ld].", p_send_msg->topic, p_send_msg->datalen); //mqtt_log("MQTT publish data success! send_topic=[%s], msg=[%ld].", p_send_msg->topic, p_send_msg->datalen);
no_mqtt_msg_exchange = false; no_mqtt_msg_exchange = false;
free(p_send_msg);
p_send_msg = NULL;
} }
} }
@@ -330,7 +366,7 @@ void MqttClientThread(mico_thread_arg_t arg) {
mqtt_log("Disconnect MQTT client, and reconnect after 5s, reason: mqtt_rc = %d, err = %d", rc, err); mqtt_log("Disconnect MQTT client, and reconnect after 5s, reason: mqtt_rc = %d, err = %d", rc, err);
timer_status = 100; timer_status = 100;
clear_mqtt_msg_send_queue();
UserMqttClientRelease(&c, &n); UserMqttClientRelease(&c, &n);
isconnect = false; isconnect = false;
UserLedSet(-1); UserLedSet(-1);
@@ -435,7 +471,9 @@ void ProcessHaCmd(char *cmd) {
childLockEnabled = on; childLockEnabled = on;
UserMqttSendChildLockState(); UserMqttSendChildLockState();
mico_system_context_update(sys_config); mico_system_context_update(sys_config);
}else if (strcmp(cmd, "reboot") == 0) { }else if (strcmp(cmd, "reboot") == ' ') {
sscanf(cmd, "reboot %s", mac);
if (strcmp(mac, str_mac)) return;
MicoSystemReboot(); // 立即重启设备 MicoSystemReboot(); // 立即重启设备
} }
} }
@@ -590,13 +628,13 @@ void UserMqttHassAutoRebootButton(void) {
"\"uniq_id\":\"tc1_%s_reboot\"," "\"uniq_id\":\"tc1_%s_reboot\","
"\"object_id\":\"tc1_%s_reboot\"," "\"object_id\":\"tc1_%s_reboot\","
"\"cmd_t\":\"device/ztc1/set\"," "\"cmd_t\":\"device/ztc1/set\","
"\"pl_prs\":\"reboot\"," "\"pl_prs\":\"reboot %s\","
"\"device\":{" "\"device\":{"
"\"identifiers\":[\"tc1_%s\"]," "\"identifiers\":[\"tc1_%s\"],"
"\"name\":\"%s\"," "\"name\":\"%s\","
"\"model\":\"TC1\"," "\"model\":\"TC1\","
"\"manufacturer\":\"PHICOMM\"}}", "\"manufacturer\":\"PHICOMM\"}}",
str_mac,str_mac,str_mac, sys_config->micoSystemConfig.name); str_mac,str_mac,str_mac,str_mac, sys_config->micoSystemConfig.name);
UserMqttSendTopic(topic_buf, send_buf, 1); UserMqttSendTopic(topic_buf, send_buf, 1);
} }
if (send_buf) free(send_buf); if (send_buf) free(send_buf);