|
@@ -4,12 +4,13 @@ import (
|
|
|
"context"
|
|
"context"
|
|
|
"encoding/json"
|
|
"encoding/json"
|
|
|
"errors"
|
|
"errors"
|
|
|
- "github.com/gogf/gf/encoding/gjson"
|
|
|
|
|
- "github.com/opentracing/opentracing-go/ext"
|
|
|
|
|
"sparrow/pkg/productconfig"
|
|
"sparrow/pkg/productconfig"
|
|
|
"sparrow/pkg/rpcs"
|
|
"sparrow/pkg/rpcs"
|
|
|
"strings"
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
+ "github.com/gogf/gf/encoding/gjson"
|
|
|
|
|
+ "github.com/opentracing/opentracing-go/ext"
|
|
|
|
|
+
|
|
|
"github.com/opentracing/opentracing-go"
|
|
"github.com/opentracing/opentracing-go"
|
|
|
|
|
|
|
|
"net/http"
|
|
"net/http"
|
|
@@ -116,72 +117,92 @@ func GetDeviceCurrentStatus(device *models.Device, config *productconfig.Product
|
|
|
urlparams martini.Params, r render.Render) {
|
|
urlparams martini.Params, r render.Render) {
|
|
|
server.Log.Printf("ACTION GetDeviceCurrentStatus, identifier:: %v", device.DeviceIdentifier)
|
|
server.Log.Printf("ACTION GetDeviceCurrentStatus, identifier:: %v", device.DeviceIdentifier)
|
|
|
|
|
|
|
|
- statusargs := rpcs.ArgsGetStatus{
|
|
|
|
|
- Id: device.DeviceIdentifier,
|
|
|
|
|
|
|
+ // 1. 触发设备上报状态(下发指令,不等待返回)
|
|
|
|
|
+ triggerArgs := rpcs.ArgsGetStatus{Id: device.DeviceIdentifier}
|
|
|
|
|
+ triggerReply := rpcs.ReplyGetStatus{}
|
|
|
|
|
+ err := server.RPCCallByName(context.Background(), rpcs.ControllerName, "Controller.GetStatus", triggerArgs, &triggerReply)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ server.Log.Errorf("trigger device status error: %v", err)
|
|
|
}
|
|
}
|
|
|
- statusreply := rpcs.ReplyGetStatus{}
|
|
|
|
|
|
|
|
|
|
- err := server.RPCCallByName(context.Background(), rpcs.ControllerName, "Controller.GetStatus", statusargs, &statusreply)
|
|
|
|
|
|
|
+ // 2. 从 DeviceManager 获取实际存储的状态数据
|
|
|
|
|
+ args := rpcs.ArgsGetStatus{Id: device.DeviceIdentifier}
|
|
|
|
|
+ reply := rpcs.ReplyStatus{}
|
|
|
|
|
+ err = server.RPCCallByName(nil, rpcs.DeviceManagerName, "DeviceManager.GetDeviceStatus", args, &reply)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- server.Log.Errorf("get device status error: %v", err)
|
|
|
|
|
|
|
+ server.Log.Errorf("获取设备状态数据失败: %v", err)
|
|
|
r.JSON(http.StatusOK, renderError(ErrSystemFault, err))
|
|
r.JSON(http.StatusOK, renderError(ErrSystemFault, err))
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- status, err := config.StatusToMap(statusreply.Status)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- r.JSON(http.StatusOK, renderError(ErrWrongRequestFormat, err))
|
|
|
|
|
|
|
+ if reply.Status == "" {
|
|
|
|
|
+ r.JSON(http.StatusOK, DeviceStatusResponse{Data: map[string]interface{}{}})
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
- result := DeviceStatusResponse{
|
|
|
|
|
- Data: status,
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // 3. 解析 JSON 字符串
|
|
|
|
|
+ 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, result)
|
|
|
|
|
|
|
+ r.JSON(http.StatusOK, DeviceStatusResponse{Data: status})
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func GetDeviceStatusByFields(device *models.Device, config *productconfig.ProductConfig,
|
|
func GetDeviceStatusByFields(device *models.Device, config *productconfig.ProductConfig,
|
|
|
urlparams martini.Params, req *http.Request, r render.Render) {
|
|
urlparams martini.Params, req *http.Request, r render.Render) {
|
|
|
- server.Log.Printf("ACTION GetDeviceCurrentStatus, identifier:: %v", device.DeviceIdentifier)
|
|
|
|
|
|
|
+ server.Log.Printf("ACTION GetDeviceStatusByFields, identifier:: %v", device.DeviceIdentifier)
|
|
|
|
|
|
|
|
fieldsParam := req.URL.Query().Get("fields")
|
|
fieldsParam := req.URL.Query().Get("fields")
|
|
|
if fieldsParam == "" {
|
|
if fieldsParam == "" {
|
|
|
r.JSON(http.StatusOK, renderError(ErrWrongQueryFormat, errBadRequestString))
|
|
r.JSON(http.StatusOK, renderError(ErrWrongQueryFormat, errBadRequestString))
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- // 解析字段列表
|
|
|
|
|
requestFields := strings.Split(fieldsParam, ",")
|
|
requestFields := strings.Split(fieldsParam, ",")
|
|
|
|
|
|
|
|
- statusargs := rpcs.ArgsGetStatus{
|
|
|
|
|
- Id: device.DeviceIdentifier,
|
|
|
|
|
|
|
+ // 触发设备上报状态
|
|
|
|
|
+ triggerArgs := rpcs.ArgsGetStatus{Id: device.DeviceIdentifier}
|
|
|
|
|
+ triggerReply := rpcs.ReplyGetStatus{}
|
|
|
|
|
+ err := server.RPCCallByName(context.Background(), rpcs.ControllerName, "Controller.GetStatus", triggerArgs, &triggerReply)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ server.Log.Errorf("trigger device status error: %v", err)
|
|
|
}
|
|
}
|
|
|
- statusreply := rpcs.ReplyGetStatus{}
|
|
|
|
|
|
|
|
|
|
- err := server.RPCCallByName(context.Background(), rpcs.ControllerName, "Controller.GetStatus", statusargs, &statusreply)
|
|
|
|
|
|
|
+ // 从 DeviceManager 获取实际存储的状态数据
|
|
|
|
|
+ args := rpcs.ArgsGetStatus{Id: device.DeviceIdentifier}
|
|
|
|
|
+ reply := rpcs.ReplyStatus{}
|
|
|
|
|
+ err = server.RPCCallByName(nil, rpcs.DeviceManagerName, "DeviceManager.GetDeviceStatus", args, &reply)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- server.Log.Errorf("get device status error: %v", err)
|
|
|
|
|
|
|
+ server.Log.Errorf("获取设备状态数据失败: %v", err)
|
|
|
r.JSON(http.StatusOK, renderError(ErrSystemFault, err))
|
|
r.JSON(http.StatusOK, renderError(ErrSystemFault, err))
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- status, err := config.StatusToMap(statusreply.Status)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- r.JSON(http.StatusOK, renderError(ErrWrongRequestFormat, err))
|
|
|
|
|
|
|
+ if reply.Status == "" {
|
|
|
|
|
+ r.JSON(http.StatusOK, DeviceStatusFieldResponse{Data: *gjson.New("")})
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- j := gjson.New("")
|
|
|
|
|
- for _, v := range requestFields {
|
|
|
|
|
- _ = j.Set(v, status[v])
|
|
|
|
|
|
|
+ // 解析 JSON 字符串
|
|
|
|
|
+ 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
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- result := DeviceStatusFieldResponse{
|
|
|
|
|
- Data: *j,
|
|
|
|
|
|
|
+ // 按指定字段过滤
|
|
|
|
|
+ j := gjson.New("")
|
|
|
|
|
+ for _, field := range requestFields {
|
|
|
|
|
+ if val, ok := status[field]; ok {
|
|
|
|
|
+ _ = j.Set(field, val)
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- r.JSON(http.StatusOK, result)
|
|
|
|
|
|
|
+ r.JSON(http.StatusOK, DeviceStatusFieldResponse{Data: *j})
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
|