update logic of html

This commit is contained in:
CSSC-WORK\murmur 2024-12-17 16:25:23 +08:00
parent 437210b6de
commit 546e6aad39
2 changed files with 64 additions and 19 deletions

View File

@ -242,6 +242,30 @@ document.addEventListener('DOMContentLoaded', function() {
const form = document.querySelector('form');
const submitBtn = form.querySelector('input[type="submit"]');
// 获取网络配置
fetch('/netconfig')
.then(response => response.json())
.then(data => {
// 填充MAC地址
document.querySelector('input[name="mac"]').value =
data.mac.map(x => x.toString(16).padStart(2,'0')).join(':');
// 填充IP地址
document.querySelector('input[name="ip"]').value =
data.ip.join('.');
// 填充子网掩码
document.querySelector('input[name="subnet"]').value =
data.sn.join('.');
// 填充网关
document.querySelector('input[name="gateway"]').value =
data.gw.join('.');
})
.catch(error => {
console.error('Failed to load network config:', error);
});
form.addEventListener('submit', function(e) {
submitBtn.value = 'Saving...';
submitBtn.classList.add('loading');
@ -387,19 +411,19 @@ document.addEventListener('DOMContentLoaded', function() {
<form method='post' action='/config'>
<div class='form-group'>
<label>MAC:</label>
<input type='text' name='mac' value='%02X:%02X:%02X:%02X:%02X:%02X'>
<input type='text' name='mac' value='#:#:#:#:#:#'>
</div>
<div class='form-group'>
<label>IP:</label>
<input type='text' name='ip' value='%d.%d.%d.%d'>
<input type='text' name='ip' value='#.#.#.#'>
</div>
<div class='form-group'>
<label>Subnet:</label>
<input type='text' name='subnet' value='%d.%d.%d.%d'>
<input type='text' name='subnet' value='#.#.#.#'>
</div>
<div class='form-group'>
<label>Gateway:</label>
<input type='text' name='gateway' value='%d.%d.%d.%d'>
<input type='text' name='gateway' value='#.#.#.#'>
</div>
<input type='submit' value='Save Configuration'>
</form>

View File

@ -69,30 +69,22 @@ static void send_chunk_end(uint8_t sn)
send_all(sn, (uint8_t*)"0\r\n\r\n", 5);
}
#define CHUNK_SIZE 512
#define CHUNK_SIZE 2048
// 处理配置页面请求
static void handle_config_page(uint8_t sn)
{
char buffer[CHUNK_SIZE];
// 先将完整的HTML内容格式化到一个临时缓冲区
char full_page[1024*15]; // 确保足够大
int total_len = sprintf(full_page, index_html,0,
gWIZNETINFO.mac[0], gWIZNETINFO.mac[1], gWIZNETINFO.mac[2],
gWIZNETINFO.mac[3], gWIZNETINFO.mac[4], gWIZNETINFO.mac[5],
gWIZNETINFO.ip[0], gWIZNETINFO.ip[1], gWIZNETINFO.ip[2], gWIZNETINFO.ip[3],
gWIZNETINFO.sn[0], gWIZNETINFO.sn[1], gWIZNETINFO.sn[2], gWIZNETINFO.sn[3],
gWIZNETINFO.gw[0], gWIZNETINFO.gw[1], gWIZNETINFO.gw[2], gWIZNETINFO.gw[3]);
// 发送响应头
send_chunked_response_header(sn);
// 分块发送内容
// 分块发送静态HTML内容
int total_len = strlen(index_html);
int sent = 0;
char buffer[CHUNK_SIZE];
while (sent < total_len) {
int chunk_len = (total_len - sent) > CHUNK_SIZE ? CHUNK_SIZE : (total_len - sent);
memcpy(buffer, full_page + sent, chunk_len);
memcpy(buffer, index_html + sent, chunk_len);
send_chunk(sn, buffer, chunk_len);
sent += chunk_len;
}
@ -254,6 +246,31 @@ static void handle_status_request(uint8_t sn)
printf("send status success!\r\n");
}
// 处理网络配置请求
static void handle_netconfig_request(uint8_t sn)
{
char json_buffer[256];
// 构建JSON响应
int len = sprintf(json_buffer,
"{"
"\"mac\":[%d,%d,%d,%d,%d,%d],"
"\"ip\":[%d,%d,%d,%d],"
"\"sn\":[%d,%d,%d,%d],"
"\"gw\":[%d,%d,%d,%d]"
"}",
gWIZNETINFO.mac[0], gWIZNETINFO.mac[1], gWIZNETINFO.mac[2],
gWIZNETINFO.mac[3], gWIZNETINFO.mac[4], gWIZNETINFO.mac[5],
gWIZNETINFO.ip[0], gWIZNETINFO.ip[1], gWIZNETINFO.ip[2], gWIZNETINFO.ip[3],
gWIZNETINFO.sn[0], gWIZNETINFO.sn[1], gWIZNETINFO.sn[2], gWIZNETINFO.sn[3],
gWIZNETINFO.gw[0], gWIZNETINFO.gw[1], gWIZNETINFO.gw[2], gWIZNETINFO.gw[3]
);
// 发送响应
send_json_response_header(sn);
send_all(sn, (uint8_t*)json_buffer, len);
}
void http_server_task(void)
{
uint8_t buffer[2048];
@ -283,7 +300,11 @@ void http_server_task(void)
if(req.method == HTTP_GET) {
if(strcmp(req.uri, "/status") == 0) {
handle_status_request(HTTP_SERVER_SOCKET);
} else {
}
else if(strcmp(req.uri, "/netconfig") == 0) {
handle_netconfig_request(HTTP_SERVER_SOCKET);
}
else {
handle_config_page(HTTP_SERVER_SOCKET);
}
}