From 49aee3a2edd9df6e135bf529b250fe903021c261 Mon Sep 17 00:00:00 2001 From: fengyarnom Date: Sun, 18 May 2025 14:07:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=96=E6=B6=88=E9=99=90=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 8 ++ .idea/angle_dtu.iml | 9 +++ .idea/modules.xml | 8 ++ .idea/vcs.xml | 6 ++ http_server.go | 187 +++++++++++++++++++++++-------------------- templates/index.html | 5 +- 6 files changed, 133 insertions(+), 90 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/angle_dtu.iml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/angle_dtu.iml b/.idea/angle_dtu.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/angle_dtu.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..d8a6140 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/http_server.go b/http_server.go index 1fe378f..598b76f 100644 --- a/http_server.go +++ b/http_server.go @@ -4,21 +4,21 @@ import ( "encoding/json" "fmt" "html/template" - "time" "log" "net/http" "os" "path/filepath" "strconv" + "time" ) // 启动HTTP服务器 func StartHTTPServer(address string) error { http.HandleFunc("/", handleIndex) - + http.HandleFunc("/api/data", handleGetData) http.HandleFunc("/api/sensors", handleGetSensors) - http.HandleFunc("/api/clients", handleGetClients) + http.HandleFunc("/api/clients", handleGetClients) fmt.Printf("HTTP服务器已启动,正在监听 %s\n", address) return http.ListenAndServe(address, nil) } @@ -26,24 +26,24 @@ func StartHTTPServer(address string) error { // 处理主页 func handleIndex(w http.ResponseWriter, r *http.Request) { log.Printf("接收到主页请求: %s", r.URL.Path) - + templatePath := "templates/index.html" absPath, _ := filepath.Abs(templatePath) - + _, err := os.Stat(templatePath) if os.IsNotExist(err) { log.Printf("错误: 模板文件不存在: %s", absPath) http.Error(w, "模板文件不存在", http.StatusInternalServerError) return } - + tmpl, err := template.ParseFiles(templatePath) if err != nil { log.Printf("错误: 无法解析模板: %v", err) http.Error(w, "无法加载模板:"+err.Error(), http.StatusInternalServerError) return } - + log.Printf("模板加载成功,开始渲染") err = tmpl.Execute(w, nil) if err != nil { @@ -55,103 +55,114 @@ func handleIndex(w http.ResponseWriter, r *http.Request) { // 处理获取传感器数据的API func handleGetData(w http.ResponseWriter, r *http.Request) { - log.Printf("接收到获取数据请求: %s", r.URL.String()) - - w.Header().Set("Content-Type", "application/json") - - sensorIDStr := r.URL.Query().Get("sensor_id") - limitStr := r.URL.Query().Get("limit") - startDateStr := r.URL.Query().Get("start_date") - endDateStr := r.URL.Query().Get("end_date") - - limit := 500 - - var sensorID int - var err error - if sensorIDStr != "" && sensorIDStr != "all" { - sensorID, err = strconv.Atoi(sensorIDStr) - if err != nil { - log.Printf("错误: 无效的传感器ID: %s", sensorIDStr) - http.Error(w, "无效的传感器ID", http.StatusBadRequest) - return - } - } - - if limitStr != "" { - limit, err = strconv.Atoi(limitStr) - if err != nil || limit <= 0 { - log.Printf("错误: 无效的记录数限制: %s", limitStr) - http.Error(w, "无效的记录数限制", http.StatusBadRequest) - return - } - } - - var startDate, endDate time.Time - if startDateStr != "" { - startDate, err = time.Parse("2006-01-02T15:04", startDateStr) - if err != nil { - log.Printf("错误: 无效的开始日期: %s, %v", startDateStr, err) - http.Error(w, "无效的开始日期", http.StatusBadRequest) - return - } - } - - if endDateStr != "" { - endDate, err = time.Parse("2006-01-02T15:04", endDateStr) - if err != nil { - log.Printf("错误: 无效的结束日期: %s, %v", endDateStr, err) - http.Error(w, "无效的结束日期", http.StatusBadRequest) - return - } - } - - var data []SensorData - if sensorIDStr == "all" || sensorIDStr == "" { - data, err = GetAllSensorData(limit, startDate, endDate) - } else { - data, err = GetSensorData(sensorID, limit, startDate, endDate) - } - - if err != nil { - log.Printf("错误: 获取数据失败: %v", err) - http.Error(w, "获取数据失败:"+err.Error(), http.StatusInternalServerError) - return - } - - log.Printf("成功获取到 %d 条数据记录", len(data)) - - if err := json.NewEncoder(w).Encode(data); err != nil { - log.Printf("错误: 编码JSON失败: %v", err) - http.Error(w, "编码JSON失败:"+err.Error(), http.StatusInternalServerError) - } + log.Printf("接收到获取数据请求: %s", r.URL.String()) + + w.Header().Set("Content-Type", "application/json") + + sensorIDStr := r.URL.Query().Get("sensor_id") + limitStr := r.URL.Query().Get("limit") + startDateStr := r.URL.Query().Get("start_date") + endDateStr := r.URL.Query().Get("end_date") + + limit := 500 // 默认限制为500条数据 + noLimit := false // 是否不限制数据条数 + + var sensorID int + var err error + if sensorIDStr != "" && sensorIDStr != "all" { + sensorID, err = strconv.Atoi(sensorIDStr) + if err != nil { + log.Printf("错误: 无效的传感器ID: %s", sensorIDStr) + http.Error(w, "无效的传感器ID", http.StatusBadRequest) + return + } + } + + if limitStr != "" { + if limitStr == "all" { + noLimit = true + limit = 1000000 // 使用一个非常大的数值作为实际上的"无限制" + } else { + limit, err = strconv.Atoi(limitStr) + if err != nil || limit <= 0 { + log.Printf("错误: 无效的记录数限制: %s", limitStr) + http.Error(w, "无效的记录数限制", http.StatusBadRequest) + return + } + } + } + + var startDate, endDate time.Time + if startDateStr != "" { + startDate, err = time.Parse("2006-01-02T15:04", startDateStr) + if err != nil { + log.Printf("错误: 无效的开始日期: %s, %v", startDateStr, err) + http.Error(w, "无效的开始日期", http.StatusBadRequest) + return + } + } + + if endDateStr != "" { + endDate, err = time.Parse("2006-01-02T15:04", endDateStr) + if err != nil { + log.Printf("错误: 无效的结束日期: %s, %v", endDateStr, err) + http.Error(w, "无效的结束日期", http.StatusBadRequest) + return + } + } + + var data []SensorData + if sensorIDStr == "all" || sensorIDStr == "" { + data, err = GetAllSensorData(limit, startDate, endDate) + } else { + data, err = GetSensorData(sensorID, limit, startDate, endDate) + } + + if err != nil { + log.Printf("错误: 获取数据失败: %v", err) + http.Error(w, "获取数据失败:"+err.Error(), http.StatusInternalServerError) + return + } + + if noLimit { + log.Printf("成功获取到所有数据记录(%d条)", len(data)) + } else { + log.Printf("成功获取到 %d 条数据记录(限制:%d条)", len(data), limit) + } + + if err := json.NewEncoder(w).Encode(data); err != nil { + log.Printf("错误: 编码JSON失败: %v", err) + http.Error(w, "编码JSON失败:"+err.Error(), http.StatusInternalServerError) + } } + // 处理获取所有传感器ID的API func handleGetSensors(w http.ResponseWriter, r *http.Request) { log.Printf("接收到获取传感器列表请求") - + w.Header().Set("Content-Type", "application/json") - + sensorIDs, err := GetAllSensorIDs() if err != nil { log.Printf("错误: 获取传感器ID失败: %v", err) http.Error(w, "获取传感器ID失败:"+err.Error(), http.StatusInternalServerError) return } - + log.Printf("成功获取到 %d 个传感器ID", len(sensorIDs)) - + if err := json.NewEncoder(w).Encode(sensorIDs); err != nil { log.Printf("错误: 编码JSON失败: %v", err) http.Error(w, "编码JSON失败:"+err.Error(), http.StatusInternalServerError) } } func handleGetClients(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json") - - clients := getAllClients() - - if err := json.NewEncoder(w).Encode(clients); err != nil { - log.Printf("错误: 编码客户端信息JSON失败: %v", err) - http.Error(w, "编码JSON失败:"+err.Error(), http.StatusInternalServerError) - } + w.Header().Set("Content-Type", "application/json") + + clients := getAllClients() + + if err := json.NewEncoder(w).Encode(clients); err != nil { + log.Printf("错误: 编码客户端信息JSON失败: %v", err) + http.Error(w, "编码JSON失败:"+err.Error(), http.StatusInternalServerError) + } } diff --git a/templates/index.html b/templates/index.html index 552e4c4..391572f 100644 --- a/templates/index.html +++ b/templates/index.html @@ -159,6 +159,7 @@ + @@ -328,7 +329,7 @@ function resetFilters() { initializeDatePickers(); document.getElementById('sensorSelect').value = 'all'; - document.getElementById('limitSelect').value = '100'; + document.getElementById('limitSelect').value = '500'; loadData(); } @@ -394,7 +395,7 @@ params.push(`sensor_id=${sensorID}`); } - if (limit) { + if (limit && limit !== 'all') { params.push(`limit=${limit}`); }