Bläddra i källkod

更新获取设备状态接口

liuxiulin 1 vecka sedan
förälder
incheckning
53aa6edf9d
2 ändrade filer med 40 tillägg och 9 borttagningar
  1. 39 6
      services/apiprovider/actions.go
  2. 1 3
      services/apiprovider/response.go

+ 39 - 6
services/apiprovider/actions.go

@@ -182,7 +182,7 @@ func GetDeviceStatusByFields(device *models.Device, config *productconfig.Produc
 	}
 
 	if reply.Status == "" {
-		r.JSON(http.StatusOK, DeviceStatusFieldResponse{Data: *gjson.New("")})
+		r.JSON(http.StatusOK, DeviceStatusFieldResponse{Data: map[string]interface{}{}})
 		return
 	}
 
@@ -193,22 +193,55 @@ func GetDeviceStatusByFields(device *models.Device, config *productconfig.Produc
 		r.JSON(http.StatusOK, renderError(ErrSystemFault, err))
 		return
 	}
+	server.Log.Debugf("GetDeviceStatusByFields status keys: %v", func() []string {
+		keys := make([]string, 0, len(status))
+		for k := range status {
+			keys = append(keys, k)
+		}
+		return keys
+	}())
 
 	// 按指定字段过滤
-	j := gjson.New("")
+	filtered := make(map[string]interface{})
 	for _, field := range requestFields {
 		if val, ok := status[field]; ok {
-			_ = j.Set(field, val)
+			filtered[field] = val
 		}
 	}
 
-	r.JSON(http.StatusOK, DeviceStatusFieldResponse{Data: *j})
+	r.JSON(http.StatusOK, DeviceStatusFieldResponse{Data: filtered})
 	return
 }
 
-// GetDeviceLatestStatus get device latest status
-func GetDeviceLatestStatus() {
+// GetDeviceLatestStatus 获取设备最新状态(直接查询,不触发上报)
+func GetDeviceLatestStatus(device *models.Device, config *productconfig.ProductConfig,
+	urlparams martini.Params, r render.Render) {
+	server.Log.Printf("ACTION GetDeviceLatestStatus, identifier:: %v", device.DeviceIdentifier)
+
+	// 直接从 DeviceManager 获取存储的状态数据
+	args := rpcs.ArgsGetStatus{Id: device.DeviceIdentifier}
+	reply := rpcs.ReplyStatus{}
+	err := server.RPCCallByName(nil, rpcs.DeviceManagerName, "DeviceManager.GetDeviceStatus", args, &reply)
+	if err != nil {
+		server.Log.Errorf("获取设备状态数据失败: %v", err)
+		r.JSON(http.StatusOK, renderError(ErrSystemFault, err))
+		return
+	}
+
+	if reply.Status == "" {
+		r.JSON(http.StatusOK, DeviceStatusResponse{Data: map[string]interface{}{}})
+		return
+	}
+
+	var status map[string]interface{}
+	if err := json.Unmarshal([]byte(reply.Status), &status); err != nil {
+		server.Log.Errorf("解析设备状态数据失败: %v", err)
+		r.JSON(http.StatusOK, renderError(ErrSystemFault, err))
+		return
+	}
 
+	r.JSON(http.StatusOK, DeviceStatusResponse{Data: status})
+	return
 }
 
 // DeviceUpgrade 设备OTA升级

+ 1 - 3
services/apiprovider/response.go

@@ -1,7 +1,5 @@
 package main
 
-import "github.com/gogf/gf/encoding/gjson"
-
 // Common response fields
 type Common struct {
 	Code    int         `json:"code"`
@@ -35,5 +33,5 @@ type AppAuthDataResponse struct {
 
 type DeviceStatusFieldResponse struct {
 	Common
-	Data gjson.Json `json:"data"`
+	Data map[string]interface{} `json:"data"`
 }