后台支持设备重命名

This commit is contained in:
Your Name
2025-03-19 22:22:17 +08:00
parent 11d3d50265
commit caffddfb43
5 changed files with 878 additions and 795 deletions

View File

@@ -163,7 +163,7 @@ static int HttpGetTc1Status(httpd_request_t *req) {
user_config->ap_name, user_config->ap_key, MQTT_SERVER, MQTT_SERVER_PORT,
MQTT_SERVER_USR, MQTT_SERVER_PWD,
VERSION, ip_status.ip, ip_status.mask, ip_status.gateway, user_config->mqtt_report_freq,
user_config->power_led_enabled, 0L,socket_names,childLockEnabled);
user_config->power_led_enabled, 0L,socket_names,childLockEnabled,sys_config->micoSystemConfig.name);
OSStatus err = kNoErr;
send_http(tc1_status, strlen(tc1_status), exit, &err);
@@ -195,7 +195,7 @@ static int HttpSetSocketStatus(httpd_request_t *req) {
static int HttpSetSocketName(httpd_request_t *req) {
OSStatus err = kNoErr;
int buf_size = 512;
int buf_size = 70;
char *buf = malloc(buf_size);
err = httpd_get_data(req, buf, buf_size);
@@ -213,6 +213,26 @@ static int HttpSetSocketName(httpd_request_t *req) {
return err;
}
static int HttpSetDeviceName(httpd_request_t *req) {
OSStatus err = kNoErr;
int buf_size = 70;
char *buf = malloc(buf_size);
err = httpd_get_data(req, buf, buf_size);
require_noerr(err, exit);
char name[64];
sscanf(buf, "%s",name);
strcpy(sys_config->micoSystemConfig.name,name);
mico_system_context_update(sys_config);
registerMqttEvents();
send_http("OK", 2, exit, &err);
exit:
if (buf) free(buf);
return err;
}
static int HttpSetChildLock(httpd_request_t *req) {
OSStatus err = kNoErr;
@@ -264,7 +284,7 @@ static int HttpGetPowerInfo(httpd_request_t *req) {
user_config->socket_names[4],
user_config->socket_names[5]);
sprintf(power_info_json, POWER_INFO_JSON, sockets, power_record.idx, PW_NUM, p_count, powers,
up_time,user_config->power_led_enabled,RelayOut()?1:0,socket_names,user_config->p_count_1_day_ago,user_config->p_count_2_days_ago,childLockEnabled);
up_time,user_config->power_led_enabled,RelayOut()?1:0,socket_names,user_config->p_count_1_day_ago,user_config->p_count_2_days_ago,childLockEnabled,sys_config->micoSystemConfig.name);
send_http(power_info_json, strlen(power_info_json), exit, &err);
if (socket_names) free(socket_names);
exit:
@@ -568,6 +588,7 @@ const struct httpd_wsgi_call g_app_handlers[] = {
{"/socketAll", HTTPD_HDR_DEFORT, 0, NULL, TotalSocketSetEnabled, NULL, NULL},
{"/socketNames", HTTPD_HDR_DEFORT, 0, NULL, HttpSetSocketName, NULL, NULL},
{"/childLock", HTTPD_HDR_DEFORT, 0, NULL, HttpSetChildLock, NULL, NULL},
{"/deviceName", HTTPD_HDR_DEFORT, 0, NULL, HttpSetDeviceName, NULL, NULL},
};
static int g_app_handlers_no = sizeof(g_app_handlers) / sizeof(struct httpd_wsgi_call);

View File

@@ -66,10 +66,11 @@
'ledEnabled':%d,\
'up_time':%ld,\
'socketNames':'%s',\
'child_lock_enabled':%d\
'child_lock_enabled':%d,\
'deviceName':'%s'\
}"
#define POWER_INFO_JSON "{'sockets':'%s','idx':%d,'len':%d,'p_count':%ld,'powers':[%s],'up_time':'%s','led_enabled':%d,'total_switch_on':%d,'socketNames':'%s','p_count_1_day_ago':%d,'p_count_2_days_ago':%d,'child_lock_enabled':%d}"
#define POWER_INFO_JSON "{'sockets':'%s','idx':%d,'len':%d,'p_count':%ld,'powers':[%s],'up_time':'%s','led_enabled':%d,'total_switch_on':%d,'socketNames':'%s','p_count_1_day_ago':%d,'p_count_2_days_ago':%d,'child_lock_enabled':%d,'deviceName':'%s'}"
int AppHttpdStart(void);

View File

@@ -25,6 +25,13 @@
<div class="demo-layout mdl-layout mdl-js-layout mdl-layout--fixed-drawer mdl-layout--fixed-header">
<header class="demo-header mdl-layout__header">
<div class="mdl-layout__header-row">
<button class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--icon edit-device-name">
<i class="material-icons">
<svg>
<use xlink:href="#icon-edit"/>
</svg>
</i>
</button>
<span class="mdl-layout-title">TC1智能插座</span>
<div class="mdl-layout-spacer"></div>
<!-- <button class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button&#45;&#45;icon"-->
@@ -795,6 +802,8 @@
var status_arr = status.sockets.split(",");
var names_arr = status.socketNames.split(",");
var switchTexts = document.querySelectorAll('.mdl-list__item-primary-content');
$(".mdl-layout-title").text(status.deviceName);
document.title=status.deviceName;
for (var i = 0; i < status_arr.length; i++) {
var langSpan = switchTexts[i+3].querySelector('.lang');
var indexSpan = switchTexts[i+3].querySelector('.socket-index');
@@ -915,6 +924,28 @@
}, index+' '+newName.trim());}
});
$(".edit-device-name").on("click", function () {
var title = $(".mdl-layout-title");
// 原名称
var oldName = title.text().trim();
// 弹窗编辑
var newName = prompt("请输入新的设备名称", oldName);
if (newName && newName.trim() !== "") {
if(newName.trim().includes(" ")){
ShowToast("名称不能包含空格");
return;
}
if(newName.trim().length>16){
ShowToast("名称不能超过16个字");
return;
}
title.text(newName.trim());
HttpPost("/deviceName", function (re) {
}, newName.trim());}
});
document.getElementById("list-switch-all").addEventListener("click", function() {
HttpPost("/socketAll", function (re) {
}, checkboxs[2].checked ? "1" : "0");});
@@ -1099,7 +1130,9 @@
}
var status_arr = power.sockets.split(",");
var names_arr = power.socketNames.split(",");
var switchTexts = document.querySelectorAll('.mdl-list__item-primary-content');
var switchTexts = document.querySelectorAll('.mdl-list__item-primary-content');
$(".mdl-layout-title").text(power.deviceName);
document.title=power.deviceName;
for (var i = 0; i < status_arr.length; i++) {
//checkboxs[i].checked = status_arr[i] == "1";
var langSpan = switchTexts[i+3].querySelector('.lang');

File diff suppressed because it is too large Load Diff

View File

@@ -543,12 +543,12 @@ void UserMqttHassAuto(char socket_id) {
"\"pl_off\":\"set socket %s %d 0\","
"\"device\":{"
"\"identifiers\":[\"tc1_%s\"],"
"\"name\":\"TC1\","
"\"name\":\"%s\","
"\"model\":\"TC1\","
"\"manufacturer\":\"PHICOMM\"}}",
user_config->socket_names[(int)socket_id], str_mac, socket_id,str_mac, socket_id, str_mac, socket_id,
str_mac,
socket_id, str_mac, socket_id, str_mac);
socket_id, str_mac, socket_id, str_mac,sys_config->micoSystemConfig.name);
UserMqttSendTopic(topic_buf, send_buf, 1);
}
if (send_buf)
@@ -574,10 +574,10 @@ void UserMqttHassAutoLed(void) {
"\"pl_off\":\"set led %s 0\","
"\"device\":{"
"\"identifiers\":[\"tc1_%s\"],"
"\"name\":\"TC1\","
"\"name\":\"%s\","
"\"model\":\"TC1\","
"\"manufacturer\":\"PHICOMM\"}}",
str_mac,str_mac,str_mac, str_mac, str_mac, str_mac);
str_mac,str_mac,str_mac, str_mac, str_mac, str_mac,sys_config->micoSystemConfig.name);
UserMqttSendTopic(topic_buf, send_buf, 1);
}
if (send_buf)
@@ -603,10 +603,10 @@ void UserMqttHassAutoChildLock(void) {
"\"pl_off\":\"set childLock %s 0\","
"\"device\":{"
"\"identifiers\":[\"tc1_%s\"],"
"\"name\":\"TC1\","
"\"name\":\"%s\","
"\"model\":\"TC1\","
"\"manufacturer\":\"PHICOMM\"}}",
str_mac,str_mac,str_mac, str_mac, str_mac, str_mac);
str_mac,str_mac,str_mac, str_mac, str_mac, str_mac,sys_config->micoSystemConfig.name);
UserMqttSendTopic(topic_buf, send_buf, 1);
}
if (send_buf)
@@ -632,10 +632,10 @@ void UserMqttHassAutoTotalSocket(void) {
"\"pl_off\":\"set total_socket %s 0\","
"\"device\":{"
"\"identifiers\":[\"tc1_%s\"],"
"\"name\":\"TC1\","
"\"name\":\"%s\","
"\"model\":\"TC1\","
"\"manufacturer\":\"PHICOMM\"}}",
str_mac, str_mac, str_mac, str_mac, str_mac, str_mac);
str_mac, str_mac, str_mac, str_mac, str_mac, str_mac,sys_config->micoSystemConfig.name);
UserMqttSendTopic(topic_buf, send_buf, 1);
}
if (send_buf)
@@ -661,10 +661,10 @@ void UserMqttHassAutoPower(void) {
"\"icon\":\"mdi:gauge\","
"\"value_template\":\"{{ value_json.power }}\",""\"device\":{"
"\"identifiers\":[\"tc1_%s\"],"
"\"name\":\"TC1\","
"\"name\":\"%s\","
"\"model\":\"TC1\","
"\"manufacturer\":\"PHICOMM\"}}",
str_mac,str_mac, str_mac, str_mac);
str_mac,str_mac, str_mac, str_mac,sys_config->micoSystemConfig.name);
UserMqttSendTopic(topic_buf, send_buf, 1);
sprintf(topic_buf, "homeassistant/sensor/%s/powerConsumption/config", str_mac);
sprintf(send_buf,
@@ -676,10 +676,10 @@ void UserMqttHassAutoPower(void) {
"\"icon\":\"mdi:fence-electric\","
"\"value_template\":\"{{ value_json.powerConsumption }}\",""\"device\":{"
"\"identifiers\":[\"tc1_%s\"],"
"\"name\":\"TC1\","
"\"name\":\"%s\","
"\"model\":\"TC1\","
"\"manufacturer\":\"PHICOMM\"}}",
str_mac, str_mac, str_mac, str_mac);
str_mac, str_mac, str_mac, str_mac,sys_config->micoSystemConfig.name);
UserMqttSendTopic(topic_buf, send_buf, 1);
sprintf(topic_buf, "homeassistant/sensor/%s/powerConsumptionToday/config", str_mac);
@@ -692,10 +692,10 @@ void UserMqttHassAutoPower(void) {
"\"icon\":\"mdi:fence-electric\","
"\"value_template\":\"{{ value_json.powerConsumptionToday }}\",""\"device\":{"
"\"identifiers\":[\"tc1_%s\"],"
"\"name\":\"TC1\","
"\"name\":\"%s\","
"\"model\":\"TC1\","
"\"manufacturer\":\"PHICOMM\"}}",
str_mac,str_mac, str_mac, str_mac);
str_mac,str_mac, str_mac, str_mac,sys_config->micoSystemConfig.name);
UserMqttSendTopic(topic_buf, send_buf, 1);
sprintf(topic_buf, "homeassistant/sensor/%s/powerConsumptionYesterday/config", str_mac);
@@ -708,10 +708,10 @@ void UserMqttHassAutoPower(void) {
"\"icon\":\"mdi:fence-electric\","
"\"value_template\":\"{{ value_json.powerConsumptionYesterday }}\",""\"device\":{"
"\"identifiers\":[\"tc1_%s\"],"
"\"name\":\"TC1\","
"\"name\":\"%s\","
"\"model\":\"TC1\","
"\"manufacturer\":\"PHICOMM\"}}",
str_mac,str_mac, str_mac, str_mac);
str_mac,str_mac, str_mac, str_mac,sys_config->micoSystemConfig.name);
UserMqttSendTopic(topic_buf, send_buf, 1);
}
if (send_buf) free(send_buf);