fix:修正存储
This commit is contained in:
parent
337ee06caf
commit
6936734f7e
@ -158,10 +158,10 @@ func buildAggFrom10MinQuery(interval string) string {
|
|||||||
return `
|
return `
|
||||||
WITH base AS (
|
WITH base AS (
|
||||||
SELECT * FROM rs485_weather_10min
|
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 (
|
), g AS (
|
||||||
SELECT
|
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(temp_c_x100 * sample_count)::bigint AS w_temp,
|
||||||
SUM(humidity_pct * sample_count)::bigint AS w_hum,
|
SUM(humidity_pct * sample_count)::bigint AS w_hum,
|
||||||
SUM(pressure_hpa_x100 * sample_count)::bigint AS w_p,
|
SUM(pressure_hpa_x100 * sample_count)::bigint AS w_p,
|
||||||
|
|||||||
@ -106,14 +106,18 @@ func getDataHandler(c *gin.Context) {
|
|||||||
hexID := fmt.Sprintf("%06X", decimalNum)
|
hexID := fmt.Sprintf("%06X", decimalNum)
|
||||||
stationID := fmt.Sprintf("RS485-%s", hexID)
|
stationID := fmt.Sprintf("RS485-%s", hexID)
|
||||||
|
|
||||||
// 解析时间
|
// 解析时间(按本地CST解析,避免被当作UTC)
|
||||||
start, err := time.Parse("2006-01-02 15:04:05", startTime)
|
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 {
|
if err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的开始时间"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的开始时间"})
|
||||||
return
|
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 {
|
if err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的结束时间"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的结束时间"})
|
||||||
return
|
return
|
||||||
|
|||||||
@ -109,6 +109,13 @@ func SetupLogger() {
|
|||||||
// StartUDPServer 启动UDP服务器
|
// StartUDPServer 启动UDP服务器
|
||||||
func StartUDPServer() error {
|
func StartUDPServer() error {
|
||||||
cfg := config.GetConfig()
|
cfg := config.GetConfig()
|
||||||
|
|
||||||
|
// 初始化数据库连接
|
||||||
|
if err := model.InitDB(); err != nil {
|
||||||
|
return fmt.Errorf("初始化数据库失败: %v", err)
|
||||||
|
}
|
||||||
|
defer model.CloseDB()
|
||||||
|
|
||||||
addr := fmt.Sprintf(":%d", cfg.Server.UDPPort)
|
addr := fmt.Sprintf(":%d", cfg.Server.UDPPort)
|
||||||
conn, err := net.ListenPacket("udp", addr)
|
conn, err := net.ListenPacket("udp", addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
5
main.go
5
main.go
@ -1,5 +0,0 @@
|
|||||||
fmt.Println("请使用新的启动程序:")
|
|
||||||
fmt.Println(" ./weatherstation_launcher # 同时启动UDP和Web服务器")
|
|
||||||
fmt.Println(" ./weatherstation_launcher -web # 只启动Web服务器")
|
|
||||||
fmt.Println(" ./weatherstation_launcher -udp # 只启动UDP服务器")
|
|
||||||
}
|
|
||||||
@ -276,17 +276,23 @@
|
|||||||
// 初始化日期输入
|
// 初始化日期输入
|
||||||
function initializeDateInputs() {
|
function initializeDateInputs() {
|
||||||
const now = new Date();
|
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('startDate').value = formatDatetimeLocal(startDate);
|
||||||
document.getElementById('endDate').value = formatDatetimeLocal(endDate);
|
document.getElementById('endDate').value = formatDatetimeLocal(endDate);
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatDatetimeLocal(date) {
|
function formatDatetimeLocal(date) {
|
||||||
const offset = date.getTimezoneOffset();
|
// 直接格式化为本地时间字符串(YYYY-MM-DDTHH:mm)
|
||||||
const localDate = new Date(date.getTime() - offset * 60 * 1000);
|
const year = date.getFullYear();
|
||||||
return localDate.toISOString().slice(0, 16);
|
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 {
|
try {
|
||||||
const params = new URLSearchParams({
|
const params = new URLSearchParams({
|
||||||
station_id: selectedStation.station_id,
|
decimal_id: selectedStation.decimal_id,
|
||||||
start_time: startTime.replace('T', ' ') + ':00',
|
start_time: startTime.replace('T', ' ') + ':00',
|
||||||
end_time: endTime.replace('T', ' ') + ':00',
|
end_time: endTime.replace('T', ' ') + ':00',
|
||||||
interval: interval
|
interval: interval
|
||||||
});
|
});
|
||||||
|
|
||||||
const response = await fetch(`/api/data?${params}`);
|
const response = await fetch(`/api/data?${params}`);
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error('查询失败');
|
||||||
|
}
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
|
||||||
if (data.length === 0) {
|
if (data.length === 0) {
|
||||||
@ -387,7 +396,7 @@
|
|||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('查询历史数据失败:', error);
|
console.error('查询历史数据失败:', error);
|
||||||
alert('查询历史数据失败');
|
alert('查询历史数据失败: ' + error.message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user