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
}
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)

View File

@ -137,7 +137,7 @@
<body>
<div class="header">
<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>
@ -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 {
// 显示已掉线的设备信息
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 = '';
});
}