fix: 调整最新数据的显示
This commit is contained in:
parent
af3a23f300
commit
186d0efd37
@ -16,6 +16,7 @@ type ClientInfo struct {
|
|||||||
IP string // IP地址
|
IP string // IP地址
|
||||||
Port string // 端口
|
Port string // 端口
|
||||||
LastSeen time.Time // 最后活跃时间
|
LastSeen time.Time // 最后活跃时间
|
||||||
|
IsConnected bool // 是否当前连接
|
||||||
}
|
}
|
||||||
|
|
||||||
// 客户端列表(使用互斥锁保护的映射)
|
// 客户端列表(使用互斥锁保护的映射)
|
||||||
@ -260,6 +261,7 @@ func addClient(addr string) {
|
|||||||
IP: host,
|
IP: host,
|
||||||
Port: port,
|
Port: port,
|
||||||
LastSeen: time.Now(),
|
LastSeen: time.Now(),
|
||||||
|
IsConnected: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger.Printf("添加新客户端: %s", addr)
|
Logger.Printf("添加新客户端: %s", addr)
|
||||||
@ -281,8 +283,9 @@ func removeClient(addr string) {
|
|||||||
defer clientsMutex.Unlock()
|
defer clientsMutex.Unlock()
|
||||||
|
|
||||||
if client, exists := clients[addr]; exists {
|
if client, exists := clients[addr]; exists {
|
||||||
client.LastSeen = time.Now()
|
// 标记为断开连接,不更新LastSeen时间
|
||||||
Logger.Printf("客户端标记为断开连接: %s", addr)
|
client.IsConnected = false
|
||||||
|
Logger.Printf("客户端断开连接: %s", addr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -297,18 +300,21 @@ func getAllClients() []map[string]interface{} {
|
|||||||
for addr, client := range clients {
|
for addr, client := range clients {
|
||||||
lastSeenDuration := now.Sub(client.LastSeen)
|
lastSeenDuration := now.Sub(client.LastSeen)
|
||||||
|
|
||||||
|
// 清理24小时前的记录
|
||||||
if lastSeenDuration > 24*time.Hour {
|
if lastSeenDuration > 24*time.Hour {
|
||||||
delete(clients, addr)
|
delete(clients, addr)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// 修改连接状态判断:2小时内为保持连接,超过2小时为掉线
|
// 连接状态判断:当前连接且2小时内活跃为在线
|
||||||
isOnline := lastSeenDuration < 2*time.Hour
|
isOnline := client.IsConnected && lastSeenDuration < 2*time.Hour
|
||||||
var connectionStatus string
|
var connectionStatus string
|
||||||
if isOnline {
|
if isOnline {
|
||||||
connectionStatus = "保持连接"
|
connectionStatus = "保持连接"
|
||||||
|
} else if client.IsConnected {
|
||||||
|
connectionStatus = "连接超时"
|
||||||
} else {
|
} else {
|
||||||
connectionStatus = "已掉线"
|
connectionStatus = "已断开"
|
||||||
}
|
}
|
||||||
|
|
||||||
result = append(result, map[string]interface{}{
|
result = append(result, map[string]interface{}{
|
||||||
@ -375,17 +381,21 @@ func triggerManualQuery() int {
|
|||||||
command := "@1602301014A*0!\n"
|
command := "@1602301014A*0!\n"
|
||||||
|
|
||||||
for addr, client := range clients {
|
for addr, client := range clients {
|
||||||
// 检查客户端是否在线(2小时内活跃,与连接状态判断保持一致)
|
// 检查客户端是否在线(连接状态且2小时内活跃)
|
||||||
if time.Since(client.LastSeen) < 2*time.Hour {
|
if client.IsConnected && 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)
|
||||||
// 连接可能已断开,从映射中移除
|
// 连接可能已断开,从映射中移除并标记为断开
|
||||||
delete(clientConns, addr)
|
delete(clientConns, addr)
|
||||||
|
client.IsConnected = false
|
||||||
} else {
|
} else {
|
||||||
TCPDataLogger.Printf("手动发送查询指令到客户端 %s: %s", addr, strings.TrimSpace(command))
|
TCPDataLogger.Printf("手动发送查询指令到客户端 %s: %s", addr, strings.TrimSpace(command))
|
||||||
sentCount++
|
sentCount++
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// 没有连接记录,标记为断开
|
||||||
|
client.IsConnected = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -760,37 +760,59 @@
|
|||||||
|
|
||||||
// 获取最新传感器数据
|
// 获取最新传感器数据
|
||||||
function fetchLatestData() {
|
function fetchLatestData() {
|
||||||
console.log('正在获取最新传感器数据...');
|
console.log('=== 开始获取最新传感器数据 ===');
|
||||||
fetch('/api/latest')
|
fetch('/api/latest')
|
||||||
.then(response => {
|
.then(response => {
|
||||||
console.log('获取最新数据响应状态:', response.status);
|
console.log('API响应状态:', response.status, response.statusText);
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`HTTP错误: ${response.status}`);
|
throw new Error(`HTTP错误: ${response.status}`);
|
||||||
}
|
}
|
||||||
return response.json();
|
return response.json();
|
||||||
})
|
})
|
||||||
.then(data => {
|
.then(data => {
|
||||||
console.log('获取到的最新数据:', data);
|
console.log('=== API返回的原始数据 ===');
|
||||||
if (data && data.length > 0) {
|
console.log('数据类型:', typeof data);
|
||||||
|
console.log('数据内容:', data);
|
||||||
|
console.log('是否为数组:', Array.isArray(data));
|
||||||
|
console.log('数组长度:', data ? data.length : 'null');
|
||||||
|
|
||||||
|
if (data && Array.isArray(data) && data.length > 0) {
|
||||||
const latest = data[0];
|
const latest = data[0];
|
||||||
|
console.log('=== 最新数据详情 ===');
|
||||||
|
console.log('latest对象:', latest);
|
||||||
|
console.log('sensor_id:', latest.sensor_id);
|
||||||
|
console.log('x:', latest.x);
|
||||||
|
console.log('y:', latest.y);
|
||||||
|
console.log('z:', latest.z);
|
||||||
|
console.log('temperature:', latest.temperature);
|
||||||
|
console.log('timestamp:', latest.timestamp);
|
||||||
|
|
||||||
updateLatestDataDisplay(latest);
|
updateLatestDataDisplay(latest);
|
||||||
console.log('最新数据显示更新完成');
|
console.log('=== 更新显示完成 ===');
|
||||||
} else {
|
} else {
|
||||||
console.log('数据库中没有数据');
|
console.log('=== 数据为空,显示空状态 ===');
|
||||||
updateLatestDataDisplayEmpty();
|
updateLatestDataDisplayEmpty();
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
console.error('获取最新数据失败:', error);
|
console.error('=== 获取最新数据出错 ===');
|
||||||
|
console.error('错误详情:', error);
|
||||||
updateLatestDataDisplayError(error.message);
|
updateLatestDataDisplayError(error.message);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新最新数据显示
|
// 更新最新数据显示
|
||||||
function updateLatestDataDisplay(data) {
|
function updateLatestDataDisplay(data) {
|
||||||
|
console.log('=== 开始更新最新数据显示 ===');
|
||||||
|
console.log('输入数据:', data);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const date = new Date(data.timestamp);
|
const date = new Date(data.timestamp);
|
||||||
|
console.log('解析时间戳:', data.timestamp);
|
||||||
|
console.log('原始时间:', date.toISOString());
|
||||||
|
|
||||||
date.setHours(date.getHours() - 8);
|
date.setHours(date.getHours() - 8);
|
||||||
|
console.log('调整后时间:', date.toISOString());
|
||||||
|
|
||||||
const formattedDate =
|
const formattedDate =
|
||||||
date.getFullYear() + '/' +
|
date.getFullYear() + '/' +
|
||||||
@ -800,23 +822,44 @@
|
|||||||
date.getMinutes().toString().padStart(2, '0') + ':' +
|
date.getMinutes().toString().padStart(2, '0') + ':' +
|
||||||
date.getSeconds().toString().padStart(2, '0');
|
date.getSeconds().toString().padStart(2, '0');
|
||||||
|
|
||||||
document.getElementById('latest-time').textContent = `(${formattedDate})`;
|
console.log('格式化时间:', formattedDate);
|
||||||
document.getElementById('latest-sensor-id').textContent = data.sensor_id;
|
|
||||||
document.getElementById('latest-x').textContent = data.x.toFixed(3);
|
// 检查DOM元素是否存在
|
||||||
document.getElementById('latest-y').textContent = data.y.toFixed(3);
|
const timeElem = document.getElementById('latest-time');
|
||||||
document.getElementById('latest-z').textContent = data.z.toFixed(3);
|
const sensorIdElem = document.getElementById('latest-sensor-id');
|
||||||
document.getElementById('latest-temperature').textContent = data.temperature.toFixed(1);
|
const xElem = document.getElementById('latest-x');
|
||||||
|
const yElem = document.getElementById('latest-y');
|
||||||
|
const zElem = document.getElementById('latest-z');
|
||||||
|
const tempElem = document.getElementById('latest-temperature');
|
||||||
|
|
||||||
|
console.log('DOM元素检查:');
|
||||||
|
console.log('- latest-time:', timeElem ? '存在' : '不存在');
|
||||||
|
console.log('- latest-sensor-id:', sensorIdElem ? '存在' : '不存在');
|
||||||
|
console.log('- latest-x:', xElem ? '存在' : '不存在');
|
||||||
|
console.log('- latest-y:', yElem ? '存在' : '不存在');
|
||||||
|
console.log('- latest-z:', zElem ? '存在' : '不存在');
|
||||||
|
console.log('- latest-temperature:', tempElem ? '存在' : '不存在');
|
||||||
|
|
||||||
|
if (timeElem) timeElem.textContent = `(${formattedDate})`;
|
||||||
|
if (sensorIdElem) sensorIdElem.textContent = data.sensor_id;
|
||||||
|
if (xElem) xElem.textContent = data.x.toFixed(3);
|
||||||
|
if (yElem) yElem.textContent = data.y.toFixed(3);
|
||||||
|
if (zElem) zElem.textContent = data.z.toFixed(3);
|
||||||
|
if (tempElem) tempElem.textContent = data.temperature.toFixed(1);
|
||||||
|
|
||||||
|
console.log('=== 数据更新成功 ===');
|
||||||
|
console.log('更新的值:');
|
||||||
|
console.log('- 时间:', formattedDate);
|
||||||
|
console.log('- 传感器ID:', data.sensor_id);
|
||||||
|
console.log('- X:', data.x.toFixed(3));
|
||||||
|
console.log('- Y:', data.y.toFixed(3));
|
||||||
|
console.log('- Z:', data.z.toFixed(3));
|
||||||
|
console.log('- 温度:', data.temperature.toFixed(1));
|
||||||
|
|
||||||
console.log('最新数据显示更新成功:', {
|
|
||||||
sensor_id: data.sensor_id,
|
|
||||||
time: formattedDate,
|
|
||||||
x: data.x,
|
|
||||||
y: data.y,
|
|
||||||
z: data.z,
|
|
||||||
temperature: data.temperature
|
|
||||||
});
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('更新最新数据显示时发生错误:', error);
|
console.error('=== 更新最新数据显示时发生错误 ===');
|
||||||
|
console.error('错误详情:', error);
|
||||||
|
console.error('错误堆栈:', error.stack);
|
||||||
updateLatestDataDisplayError('数据格式错误');
|
updateLatestDataDisplayError('数据格式错误');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user