fix: 调整离线状态

This commit is contained in:
fengyarnom 2025-05-30 17:43:51 +08:00
parent 199575fe8d
commit bc20cdae61
2 changed files with 38 additions and 9 deletions

View File

@ -302,7 +302,14 @@ func getAllClients() []map[string]interface{} {
continue 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{}{ result = append(result, map[string]interface{}{
"address": addr, "address": addr,
@ -310,6 +317,7 @@ func getAllClients() []map[string]interface{} {
"port": client.Port, "port": client.Port,
"lastSeen": client.LastSeen, "lastSeen": client.LastSeen,
"isOnline": isOnline, "isOnline": isOnline,
"connectionStatus": connectionStatus,
"lastSeenFormatted": formatDuration(lastSeenDuration), "lastSeenFormatted": formatDuration(lastSeenDuration),
}) })
} }
@ -324,7 +332,13 @@ func formatDuration(d time.Duration) string {
} else if d < time.Hour { } else if d < time.Hour {
return fmt.Sprintf("%d分钟前", int(d.Minutes())) return fmt.Sprintf("%d分钟前", int(d.Minutes()))
} else if d < 24*time.Hour { } 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 { } else {
return fmt.Sprintf("%d天前", int(d.Hours()/24)) return fmt.Sprintf("%d天前", int(d.Hours()/24))
} }
@ -361,8 +375,8 @@ func triggerManualQuery() int {
command := "@1602301014A*0!\n" command := "@1602301014A*0!\n"
for addr, client := range clients { for addr, client := range clients {
// 检查客户端是否在线(最近10分钟内活跃 // 检查客户端是否在线(2小时内活跃与连接状态判断保持一致
if time.Since(client.LastSeen) < 10*time.Minute { if time.Since(client.LastSeen) < 2*time.Hour {
if conn, exists := clientConns[addr]; exists { if conn, exists := clientConns[addr]; exists {
if _, err := conn.Write([]byte(command)); err != nil { if _, err := conn.Write([]byte(command)); err != nil {
Logger.Printf("手动发送查询指令到客户端 %s 失败: %v", addr, err) Logger.Printf("手动发送查询指令到客户端 %s 失败: %v", addr, err)

View File

@ -137,7 +137,7 @@
<body> <body>
<div class="header"> <div class="header">
<h1>测斜仪数据</h1> <h1>测斜仪数据</h1>
<div id="connectionStatus" style="display: inline-block; padding: 5px 10px; border-radius: 4px; margin-left: 10px; background-color: red; color: white;"> <div id="connectionStatus" style="display: inline-block; padding: 5px 15px; border-radius: 4px; margin-left: 10px; background-color: red; color: white; min-width: 300px; text-align: center; cursor: pointer; font-size: 14px;">
未连接 未连接
</div> </div>
</div> </div>
@ -721,18 +721,32 @@
if (onlineClients.length > 0) { if (onlineClients.length > 0) {
statusElem.style.backgroundColor = 'green'; statusElem.style.backgroundColor = 'green';
const client = onlineClients[0]; const client = onlineClients[0];
const lastSeenTime = new Date(client.lastSeen).toLocaleString('zh-CN');
if (onlineClients.length > 1) { 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 { } else {
statusElem.textContent = `已连接: ${client.ip}:${client.port}`; statusElem.textContent = `${client.connectionStatus}: ${client.ip}:${client.port} (${client.lastSeenFormatted})`;
statusElem.title = `最后连接时间: ${lastSeenTime}`;
} }
} else { } 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 { } else {
statusElem.style.backgroundColor = 'red'; statusElem.style.backgroundColor = 'red';
statusElem.textContent = '未连接'; statusElem.textContent = '未连接';
statusElem.title = '';
} }
}) })
.catch(error => { .catch(error => {
@ -740,6 +754,7 @@
const statusElem = document.getElementById('connectionStatus'); const statusElem = document.getElementById('connectionStatus');
statusElem.style.backgroundColor = 'red'; statusElem.style.backgroundColor = 'red';
statusElem.textContent = '状态未知'; statusElem.textContent = '状态未知';
statusElem.title = '';
}); });
} }