|
|
@@ -58,6 +58,8 @@ func New(db *database.DB, addr string) *Server {
|
|
|
mux.HandleFunc("/api/ble", s.handleBLE)
|
|
|
mux.HandleFunc("/api/ble/", s.handleBLEByID)
|
|
|
mux.HandleFunc("/api/health", s.handleHealth)
|
|
|
+ mux.HandleFunc("/api/status", s.handleStatus)
|
|
|
+ mux.HandleFunc("/api/history", s.handleHistory)
|
|
|
mux.HandleFunc("/api/device/config", s.handleDeviceConfig)
|
|
|
mux.HandleFunc("/api/device/config/", s.handleDeviceConfigByID)
|
|
|
mux.HandleFunc("/api/device/config/push", s.handleDeviceConfigPush)
|
|
|
@@ -119,6 +121,67 @@ func (s *Server) handleHealth(w http.ResponseWriter, r *http.Request) {
|
|
|
writeJSON(w, http.StatusOK, Response{Code: 0, Message: "ok"})
|
|
|
}
|
|
|
|
|
|
+func (s *Server) handleStatus(w http.ResponseWriter, r *http.Request) {
|
|
|
+ if r.Method != http.MethodGet {
|
|
|
+ writeJSON(w, http.StatusMethodNotAllowed, Response{Code: -1, Message: "方法不允许"})
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ status := map[string]interface{}{
|
|
|
+ "mqtt": map[string]interface{}{
|
|
|
+ "connected": false,
|
|
|
+ "broker": "",
|
|
|
+ },
|
|
|
+ "ble": map[string]interface{}{
|
|
|
+ "running": false,
|
|
|
+ "device": "",
|
|
|
+ },
|
|
|
+ }
|
|
|
+
|
|
|
+ if s.mqttClient != nil {
|
|
|
+ status["mqtt"] = map[string]interface{}{
|
|
|
+ "connected": s.mqttClient.IsConnected(),
|
|
|
+ "broker": s.mqttClient.GetBroker(),
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if s.bleStdin != nil {
|
|
|
+ bleCfg, err := s.db.GetBLEConfig()
|
|
|
+ if err == nil && bleCfg != nil {
|
|
|
+ status["ble"] = map[string]interface{}{
|
|
|
+ "running": true,
|
|
|
+ "device": bleCfg.DeviceName,
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ writeJSON(w, http.StatusOK, Response{Code: 0, Message: "ok", Data: status})
|
|
|
+}
|
|
|
+
|
|
|
+func (s *Server) handleHistory(w http.ResponseWriter, r *http.Request) {
|
|
|
+ if r.Method != http.MethodGet {
|
|
|
+ writeJSON(w, http.StatusMethodNotAllowed, Response{Code: -1, Message: "方法不允许"})
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ limitStr := r.URL.Query().Get("limit")
|
|
|
+ limit := 100
|
|
|
+ if limitStr != "" {
|
|
|
+ if l, err := strconv.Atoi(limitStr); err == nil && l > 0 {
|
|
|
+ limit = l
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ records, err := s.db.GetStatusHistory(limit)
|
|
|
+ if err != nil {
|
|
|
+ logger.Error("查询状态历史失败: %v", err)
|
|
|
+ writeJSON(w, http.StatusInternalServerError, Response{Code: -1, Message: err.Error()})
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ writeJSON(w, http.StatusOK, Response{Code: 0, Message: "ok", Data: records})
|
|
|
+}
|
|
|
+
|
|
|
func (s *Server) handleEvent(w http.ResponseWriter, r *http.Request) {
|
|
|
if r.Method != http.MethodPost {
|
|
|
writeJSON(w, http.StatusMethodNotAllowed, Response{Code: -1, Message: "方法不允许"})
|
|
|
@@ -161,6 +224,11 @@ func (s *Server) handleEvent(w http.ResponseWriter, r *http.Request) {
|
|
|
// 广播到 SSE 客户端
|
|
|
s.broadcastSSE(req.Code)
|
|
|
|
|
|
+ // 保存历史记录
|
|
|
+ if err := s.db.SaveStatusRecord(req.Code); err != nil {
|
|
|
+ logger.Error("保存状态历史失败: %v", err)
|
|
|
+ }
|
|
|
+
|
|
|
writeJSON(w, http.StatusOK, Response{Code: 0, Message: "ok"})
|
|
|
}
|
|
|
|