From a5d382493acbc2b7f346cfd09ca953829a872535 Mon Sep 17 00:00:00 2001 From: yarnom Date: Sun, 24 Aug 2025 15:18:03 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BF=AE=E5=A4=8D=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/weatherstation/main.go | 8 +++-- internal/forecast/caiyun.go | 13 +++++++- internal/tools/backfill.go | 17 ++++++++++ templates/index.html | 64 ++++++++++++++++++++++++------------- 4 files changed, 77 insertions(+), 25 deletions(-) diff --git a/cmd/weatherstation/main.go b/cmd/weatherstation/main.go index 5f48d81..c5832ae 100644 --- a/cmd/weatherstation/main.go +++ b/cmd/weatherstation/main.go @@ -111,11 +111,15 @@ func main() { if *bfFrom == "" || *bfTo == "" { log.Fatalln("backfill 需要提供 --from 与 --to 时间") } - fromT, err := time.Parse("2006-01-02 15:04:05", *bfFrom) + loc, _ := time.LoadLocation("Asia/Shanghai") + if loc == nil { + loc = time.FixedZone("CST", 8*3600) + } + fromT, err := time.ParseInLocation("2006-01-02 15:04:05", *bfFrom, loc) if err != nil { log.Fatalf("解析from失败: %v", err) } - toT, err := time.Parse("2006-01-02 15:04:05", *bfTo) + toT, err := time.ParseInLocation("2006-01-02 15:04:05", *bfTo, loc) if err != nil { log.Fatalf("解析to失败: %v", err) } diff --git a/internal/forecast/caiyun.go b/internal/forecast/caiyun.go index 980d3dc..fca76bc 100644 --- a/internal/forecast/caiyun.go +++ b/internal/forecast/caiyun.go @@ -7,6 +7,7 @@ import ( "fmt" "io" "log" + "math" "net/http" "time" @@ -137,7 +138,17 @@ func RunCaiyunFetch(ctx context.Context, token string) error { if ft, err := time.ParseInLocation("2006-01-02T15:04-07:00", p.Datetime, loc); err == nil { v := table[ft] v.rain = p.Value - v.prob = p.Probability * 100.0 + // 直接使用API返回的概率值,只进行范围限制 + prob := p.Probability + // 四舍五入并确保在0-100范围内 + prob = math.Round(prob) + if prob < 0 { + prob = 0 + } + if prob > 100 { + prob = 100 + } + v.prob = prob table[ft] = v } } diff --git a/internal/tools/backfill.go b/internal/tools/backfill.go index 1d473ee..785589b 100644 --- a/internal/tools/backfill.go +++ b/internal/tools/backfill.go @@ -138,6 +138,23 @@ func RunBackfill10Min(ctx context.Context, opts BackfillOptions) error { // 雨量增量:按时间比例切分到跨越的各个桶,避免边界全部被计入后一桶 if rf.Valid { curr := rf.Float64 + + // 若该站点的上一条样本未知(窗口首条),尝试读取窗口前一条样本作为种子,避免首桶丢雨 + if math.IsNaN(prevTotal) || prevTS.IsZero() { + var seedTS time.Time + var seedTotal sql.NullFloat64 + if err := db.QueryRowContext(ctx, ` + SELECT timestamp, rainfall + FROM rs485_weather_data + WHERE station_id = $1 AND timestamp < $2 + ORDER BY timestamp DESC + LIMIT 1 + `, stationID, ts).Scan(&seedTS, &seedTotal); err == nil && seedTotal.Valid { + prevTotal = seedTotal.Float64 + prevTS = seedTS + } + } + if !math.IsNaN(prevTotal) && !prevTS.IsZero() { // 计算增量(带回绕) inc := 0.0 diff --git a/templates/index.html b/templates/index.html index 35d4bf2..09fea99 100644 --- a/templates/index.html +++ b/templates/index.html @@ -8,7 +8,7 @@ - + - +

{{.Title}}

-
-
-
- 设备列表 - × +
+
+
+
设备列表
+ ×
-
+
在线设备: {{.OnlineDevices}} 个 | - 总设备: 0 + 总设备: 0
@@ -543,7 +563,7 @@ - +