diff --git a/tcp_server.go b/tcp_server.go index 299cf13..2536cde 100644 --- a/tcp_server.go +++ b/tcp_server.go @@ -302,7 +302,14 @@ func getAllClients() []map[string]interface{} { continue } - isOnline := lastSeenDuration < 10*time.Minute + // 修改连接状态判断:2小时内为保持连接,超过2小时为掉线 + isOnline := lastSeenDuration < 2*time.Hour + var connectionStatus string + if isOnline { + connectionStatus = "保持连接" + } else { + connectionStatus = "已掉线" + } result = append(result, map[string]interface{}{ "address": addr, @@ -310,6 +317,7 @@ func getAllClients() []map[string]interface{} { "port": client.Port, "lastSeen": client.LastSeen, "isOnline": isOnline, + "connectionStatus": connectionStatus, "lastSeenFormatted": formatDuration(lastSeenDuration), }) } @@ -324,7 +332,13 @@ func formatDuration(d time.Duration) string { } else if d < time.Hour { return fmt.Sprintf("%d分钟前", int(d.Minutes())) } else if d < 24*time.Hour { - return fmt.Sprintf("%d小时前", int(d.Hours())) + hours := int(d.Hours()) + minutes := int(d.Minutes()) % 60 + if minutes == 0 { + return fmt.Sprintf("%d小时前", hours) + } else { + return fmt.Sprintf("%d小时%d分钟前", hours, minutes) + } } else { return fmt.Sprintf("%d天前", int(d.Hours()/24)) } @@ -361,8 +375,8 @@ func triggerManualQuery() int { command := "@1602301014A*0!\n" for addr, client := range clients { - // 检查客户端是否在线(最近10分钟内活跃) - if time.Since(client.LastSeen) < 10*time.Minute { + // 检查客户端是否在线(2小时内活跃,与连接状态判断保持一致) + if time.Since(client.LastSeen) < 2*time.Hour { if conn, exists := clientConns[addr]; exists { if _, err := conn.Write([]byte(command)); err != nil { Logger.Printf("手动发送查询指令到客户端 %s 失败: %v", addr, err) diff --git a/templates/index.html b/templates/index.html index 4e19bf4..58c6342 100644 --- a/templates/index.html +++ b/templates/index.html @@ -137,7 +137,7 @@

测斜仪数据

-
+
未连接
@@ -721,18 +721,32 @@ if (onlineClients.length > 0) { statusElem.style.backgroundColor = 'green'; const client = onlineClients[0]; + const lastSeenTime = new Date(client.lastSeen).toLocaleString('zh-CN'); if (onlineClients.length > 1) { - statusElem.textContent = `已连接: ${client.ip}:${client.port} (共${onlineClients.length}个设备)`; + statusElem.textContent = `${client.connectionStatus}: ${client.ip}:${client.port} (${client.lastSeenFormatted}) [共${onlineClients.length}个设备]`; + statusElem.title = `最后连接时间: ${lastSeenTime}`; } else { - statusElem.textContent = `已连接: ${client.ip}:${client.port}`; + statusElem.textContent = `${client.connectionStatus}: ${client.ip}:${client.port} (${client.lastSeenFormatted})`; + statusElem.title = `最后连接时间: ${lastSeenTime}`; } } else { - statusElem.style.backgroundColor = 'orange'; - statusElem.textContent = '设备离线'; + // 显示已掉线的设备信息 + const offlineClients = data.filter(client => !client.isOnline); + if (offlineClients.length > 0) { + statusElem.style.backgroundColor = 'orange'; + const client = offlineClients[0]; + const lastSeenTime = new Date(client.lastSeen).toLocaleString('zh-CN'); + statusElem.textContent = `${client.connectionStatus}: ${client.ip}:${client.port} (${client.lastSeenFormatted})`; + statusElem.title = `最后连接时间: ${lastSeenTime}`; + } else { + statusElem.style.backgroundColor = 'orange'; + statusElem.textContent = '设备离线'; + } } } else { statusElem.style.backgroundColor = 'red'; statusElem.textContent = '未连接'; + statusElem.title = ''; } }) .catch(error => { @@ -740,6 +754,7 @@ const statusElem = document.getElementById('connectionStatus'); statusElem.style.backgroundColor = 'red'; statusElem.textContent = '状态未知'; + statusElem.title = ''; }); }