fix:修正存储

This commit is contained in:
yarnom 2025-08-22 09:13:25 +08:00
parent 337ee06caf
commit 6936734f7e
5 changed files with 33 additions and 18 deletions

View File

@ -158,10 +158,10 @@ func buildAggFrom10MinQuery(interval string) string {
return `
WITH base AS (
SELECT * FROM rs485_weather_10min
WHERE station_id = $1 AND bucket_start >= $2 AND bucket_start <= $3
WHERE station_id = $1 AND bucket_start >= $2 AND bucket_start < $3 + '` + interval + `'::interval
), g AS (
SELECT
date_trunc('hour', bucket_start) + floor(extract(epoch from (bucket_start - date_trunc('hour', bucket_start)))/extract(epoch from '` + interval + `'::interval)) * '` + interval + `'::interval AS grp,
date_trunc('` + interval + `', bucket_start) AS grp,
SUM(temp_c_x100 * sample_count)::bigint AS w_temp,
SUM(humidity_pct * sample_count)::bigint AS w_hum,
SUM(pressure_hpa_x100 * sample_count)::bigint AS w_p,

View File

@ -106,14 +106,18 @@ func getDataHandler(c *gin.Context) {
hexID := fmt.Sprintf("%06X", decimalNum)
stationID := fmt.Sprintf("RS485-%s", hexID)
// 解析时间
start, err := time.Parse("2006-01-02 15:04:05", startTime)
// 解析时间按本地CST解析避免被当作UTC
loc, _ := time.LoadLocation("Asia/Shanghai")
if loc == nil {
loc = time.FixedZone("CST", 8*3600)
}
start, err := time.ParseInLocation("2006-01-02 15:04:05", startTime, loc)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的开始时间"})
return
}
end, err := time.Parse("2006-01-02 15:04:05", endTime)
end, err := time.ParseInLocation("2006-01-02 15:04:05", endTime, loc)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的结束时间"})
return

View File

@ -109,6 +109,13 @@ func SetupLogger() {
// StartUDPServer 启动UDP服务器
func StartUDPServer() error {
cfg := config.GetConfig()
// 初始化数据库连接
if err := model.InitDB(); err != nil {
return fmt.Errorf("初始化数据库失败: %v", err)
}
defer model.CloseDB()
addr := fmt.Sprintf(":%d", cfg.Server.UDPPort)
conn, err := net.ListenPacket("udp", addr)
if err != nil {

View File

@ -1,5 +0,0 @@
fmt.Println("请使用新的启动程序:")
fmt.Println(" ./weatherstation_launcher # 同时启动UDP和Web服务器")
fmt.Println(" ./weatherstation_launcher -web # 只启动Web服务器")
fmt.Println(" ./weatherstation_launcher -udp # 只启动UDP服务器")
}

View File

@ -276,17 +276,23 @@
// 初始化日期输入
function initializeDateInputs() {
const now = new Date();
const endDate = new Date(now);
const startDate = new Date(now.getTime() - 24 * 60 * 60 * 1000); // 24小时前
// 设置为当前整点
const endDate = new Date(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours(), 0, 0);
// 24小时前的整点
const startDate = new Date(endDate.getTime() - 24 * 60 * 60 * 1000);
document.getElementById('startDate').value = formatDatetimeLocal(startDate);
document.getElementById('endDate').value = formatDatetimeLocal(endDate);
}
function formatDatetimeLocal(date) {
const offset = date.getTimezoneOffset();
const localDate = new Date(date.getTime() - offset * 60 * 1000);
return localDate.toISOString().slice(0, 16);
// 直接格式化为本地时间字符串YYYY-MM-DDTHH:mm
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
const hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
return `${year}-${month}-${day}T${hours}:${minutes}`;
}
// 加载站点列表
@ -364,13 +370,16 @@
try {
const params = new URLSearchParams({
station_id: selectedStation.station_id,
decimal_id: selectedStation.decimal_id,
start_time: startTime.replace('T', ' ') + ':00',
end_time: endTime.replace('T', ' ') + ':00',
interval: interval
});
const response = await fetch(`/api/data?${params}`);
if (!response.ok) {
throw new Error('查询失败');
}
const data = await response.json();
if (data.length === 0) {
@ -387,7 +396,7 @@
} catch (error) {
console.error('查询历史数据失败:', error);
alert('查询历史数据失败');
alert('查询历史数据失败: ' + error.message);
}
}