fix: 调整最新数据的显示
This commit is contained in:
parent
bc20cdae61
commit
af3a23f300
@ -17,6 +17,7 @@ func StartHTTPServer(address string) error {
|
|||||||
http.HandleFunc("/", handleIndex)
|
http.HandleFunc("/", handleIndex)
|
||||||
|
|
||||||
http.HandleFunc("/api/data", handleGetData)
|
http.HandleFunc("/api/data", handleGetData)
|
||||||
|
http.HandleFunc("/api/latest", handleGetLatest)
|
||||||
http.HandleFunc("/api/sensors", handleGetSensors)
|
http.HandleFunc("/api/sensors", handleGetSensors)
|
||||||
http.HandleFunc("/api/clients", handleGetClients)
|
http.HandleFunc("/api/clients", handleGetClients)
|
||||||
http.HandleFunc("/api/trigger-query", handleTriggerQuery)
|
http.HandleFunc("/api/trigger-query", handleTriggerQuery)
|
||||||
@ -112,11 +113,18 @@ func handleGetData(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 特殊处理获取最新数据的请求
|
||||||
|
if limit == 1 && sensorIDStr == "" {
|
||||||
|
log.Printf("检测到获取最新数据请求 (limit=1)")
|
||||||
|
}
|
||||||
|
|
||||||
var data []SensorData
|
var data []SensorData
|
||||||
if sensorIDStr == "all" || sensorIDStr == "" {
|
if sensorIDStr == "all" || sensorIDStr == "" {
|
||||||
data, err = GetAllSensorData(limit, startDate, endDate)
|
data, err = GetAllSensorData(limit, startDate, endDate)
|
||||||
|
log.Printf("查询所有传感器数据,limit=%d, 结果数量=%d", limit, len(data))
|
||||||
} else {
|
} else {
|
||||||
data, err = GetSensorData(sensorID, limit, startDate, endDate)
|
data, err = GetSensorData(sensorID, limit, startDate, endDate)
|
||||||
|
log.Printf("查询传感器%d数据,limit=%d, 结果数量=%d", sensorID, limit, len(data))
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -125,6 +133,13 @@ func handleGetData(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 输出最新数据的详细信息(当limit=1时)
|
||||||
|
if limit == 1 && len(data) > 0 {
|
||||||
|
latest := data[0]
|
||||||
|
log.Printf("返回最新数据: ID=%d, SensorID=%d, X=%.3f, Y=%.3f, Z=%.3f, Temperature=%.1f, Timestamp=%s",
|
||||||
|
latest.ID, latest.SensorID, latest.X, latest.Y, latest.Z, latest.Temperature, latest.Timestamp.Format("2006-01-02 15:04:05"))
|
||||||
|
}
|
||||||
|
|
||||||
if noLimit {
|
if noLimit {
|
||||||
log.Printf("成功获取到所有数据记录(%d条)", len(data))
|
log.Printf("成功获取到所有数据记录(%d条)", len(data))
|
||||||
} else {
|
} else {
|
||||||
@ -192,3 +207,40 @@ func handleTriggerQuery(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.Error(w, "编码JSON失败:"+err.Error(), http.StatusInternalServerError)
|
http.Error(w, "编码JSON失败:"+err.Error(), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 处理获取最新传感器数据的API
|
||||||
|
func handleGetLatest(w http.ResponseWriter, r *http.Request) {
|
||||||
|
log.Printf("接收到获取最新数据请求")
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
// 获取最新的一条数据
|
||||||
|
data, err := GetAllSensorData(1, time.Time{}, time.Time{})
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("错误: 获取最新数据失败: %v", err)
|
||||||
|
http.Error(w, "获取最新数据失败:"+err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("查询最新数据结果数量: %d", len(data))
|
||||||
|
|
||||||
|
if len(data) == 0 {
|
||||||
|
log.Printf("数据库中没有任何数据")
|
||||||
|
// 返回空数组而不是错误
|
||||||
|
if err := json.NewEncoder(w).Encode([]SensorData{}); err != nil {
|
||||||
|
log.Printf("错误: 编码空数据JSON失败: %v", err)
|
||||||
|
http.Error(w, "编码JSON失败:"+err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 输出最新数据的详细信息
|
||||||
|
latest := data[0]
|
||||||
|
log.Printf("返回最新数据: ID=%d, SensorID=%d, X=%.3f, Y=%.3f, Z=%.3f, Temperature=%.1f, Timestamp=%s",
|
||||||
|
latest.ID, latest.SensorID, latest.X, latest.Y, latest.Z, latest.Temperature, latest.Timestamp.Format("2006-01-02 15:04:05"))
|
||||||
|
|
||||||
|
if err := json.NewEncoder(w).Encode(data); err != nil {
|
||||||
|
log.Printf("错误: 编码最新数据JSON失败: %v", err)
|
||||||
|
http.Error(w, "编码JSON失败:"+err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -760,38 +760,85 @@
|
|||||||
|
|
||||||
// 获取最新传感器数据
|
// 获取最新传感器数据
|
||||||
function fetchLatestData() {
|
function fetchLatestData() {
|
||||||
fetch('/api/data?limit=1')
|
console.log('正在获取最新传感器数据...');
|
||||||
.then(response => response.json())
|
fetch('/api/latest')
|
||||||
|
.then(response => {
|
||||||
|
console.log('获取最新数据响应状态:', response.status);
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`HTTP错误: ${response.status}`);
|
||||||
|
}
|
||||||
|
return response.json();
|
||||||
|
})
|
||||||
.then(data => {
|
.then(data => {
|
||||||
|
console.log('获取到的最新数据:', data);
|
||||||
if (data && data.length > 0) {
|
if (data && data.length > 0) {
|
||||||
const latest = data[0];
|
const latest = data[0];
|
||||||
updateLatestDataDisplay(latest);
|
updateLatestDataDisplay(latest);
|
||||||
|
console.log('最新数据显示更新完成');
|
||||||
|
} else {
|
||||||
|
console.log('数据库中没有数据');
|
||||||
|
updateLatestDataDisplayEmpty();
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
console.error('获取最新数据失败:', error);
|
console.error('获取最新数据失败:', error);
|
||||||
|
updateLatestDataDisplayError(error.message);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新最新数据显示
|
// 更新最新数据显示
|
||||||
function updateLatestDataDisplay(data) {
|
function updateLatestDataDisplay(data) {
|
||||||
const date = new Date(data.timestamp);
|
try {
|
||||||
date.setHours(date.getHours() - 8);
|
const date = new Date(data.timestamp);
|
||||||
|
date.setHours(date.getHours() - 8);
|
||||||
const formattedDate =
|
|
||||||
date.getFullYear() + '/' +
|
const formattedDate =
|
||||||
(date.getMonth() + 1).toString().padStart(2, '0') + '/' +
|
date.getFullYear() + '/' +
|
||||||
date.getDate().toString().padStart(2, '0') + ' ' +
|
(date.getMonth() + 1).toString().padStart(2, '0') + '/' +
|
||||||
date.getHours().toString().padStart(2, '0') + ':' +
|
date.getDate().toString().padStart(2, '0') + ' ' +
|
||||||
date.getMinutes().toString().padStart(2, '0') + ':' +
|
date.getHours().toString().padStart(2, '0') + ':' +
|
||||||
date.getSeconds().toString().padStart(2, '0');
|
date.getMinutes().toString().padStart(2, '0') + ':' +
|
||||||
|
date.getSeconds().toString().padStart(2, '0');
|
||||||
|
|
||||||
document.getElementById('latest-time').textContent = `(${formattedDate})`;
|
document.getElementById('latest-time').textContent = `(${formattedDate})`;
|
||||||
document.getElementById('latest-sensor-id').textContent = data.sensor_id;
|
document.getElementById('latest-sensor-id').textContent = data.sensor_id;
|
||||||
document.getElementById('latest-x').textContent = data.x.toFixed(3);
|
document.getElementById('latest-x').textContent = data.x.toFixed(3);
|
||||||
document.getElementById('latest-y').textContent = data.y.toFixed(3);
|
document.getElementById('latest-y').textContent = data.y.toFixed(3);
|
||||||
document.getElementById('latest-z').textContent = data.z.toFixed(3);
|
document.getElementById('latest-z').textContent = data.z.toFixed(3);
|
||||||
document.getElementById('latest-temperature').textContent = data.temperature.toFixed(1);
|
document.getElementById('latest-temperature').textContent = 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) {
|
||||||
|
console.error('更新最新数据显示时发生错误:', error);
|
||||||
|
updateLatestDataDisplayError('数据格式错误');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 显示空数据状态
|
||||||
|
function updateLatestDataDisplayEmpty() {
|
||||||
|
document.getElementById('latest-time').textContent = '(暂无数据)';
|
||||||
|
document.getElementById('latest-sensor-id').textContent = '--';
|
||||||
|
document.getElementById('latest-x').textContent = '--';
|
||||||
|
document.getElementById('latest-y').textContent = '--';
|
||||||
|
document.getElementById('latest-z').textContent = '--';
|
||||||
|
document.getElementById('latest-temperature').textContent = '--';
|
||||||
|
}
|
||||||
|
|
||||||
|
// 显示错误状态
|
||||||
|
function updateLatestDataDisplayError(errorMsg) {
|
||||||
|
document.getElementById('latest-time').textContent = `(错误: ${errorMsg})`;
|
||||||
|
document.getElementById('latest-sensor-id').textContent = '--';
|
||||||
|
document.getElementById('latest-x').textContent = '--';
|
||||||
|
document.getElementById('latest-y').textContent = '--';
|
||||||
|
document.getElementById('latest-z').textContent = '--';
|
||||||
|
document.getElementById('latest-temperature').textContent = '--';
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查询最新数据(触发设备查询)
|
// 查询最新数据(触发设备查询)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user