|
@@ -1,11 +1,16 @@
|
|
|
package main
|
|
|
|
|
|
import (
|
|
|
+ "context"
|
|
|
"encoding/json"
|
|
|
"errors"
|
|
|
"sparrow/pkg/productconfig"
|
|
|
"sparrow/pkg/rpcs"
|
|
|
|
|
|
+ "github.com/opentracing/opentracing-go/ext"
|
|
|
+
|
|
|
+ "github.com/opentracing/opentracing-go"
|
|
|
+
|
|
|
"net/http"
|
|
|
|
|
|
"sparrow/pkg/models"
|
|
@@ -54,11 +59,16 @@ func done(result interface{}) Common {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+// GetDeviceInfoByKey get device info with device key
|
|
|
func GetDeviceInfoByKey(params martini.Params, req *http.Request, r render.Render) {
|
|
|
key := req.URL.Query().Get("device_key")
|
|
|
server.Log.Printf("ACTION GetDeviceInfoByKey, key:: %v", key)
|
|
|
device := &models.Device{}
|
|
|
- err := server.RPCCallByName(nil, "registry", "Registry.ValidateDevice", key, device)
|
|
|
+ span, ctx := opentracing.StartSpanFromContext(context.Background(), "GetDeviceInfoByKey")
|
|
|
+ defer span.Finish()
|
|
|
+ ext.SpanKindRPCClient.Set(span)
|
|
|
+ span.SetTag("device_key", key)
|
|
|
+ err := server.RPCCallByName(ctx, "registry", "Registry.ValidateDevice", key, device)
|
|
|
if err != nil {
|
|
|
r.JSON(http.StatusOK, renderError(ErrDeviceNotFound, err))
|
|
|
return
|
|
@@ -76,11 +86,16 @@ func GetDeviceInfoByKey(params martini.Params, req *http.Request, r render.Rende
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+// GetDeviceInfoByIdentifier get device info with device identifier
|
|
|
func GetDeviceInfoByIdentifier(urlparams martini.Params, r render.Render) {
|
|
|
identifier := urlparams["identifier"]
|
|
|
server.Log.Printf("ACTION GetDeviceInfoByIdentifier, identifier:: %v", identifier)
|
|
|
device := &models.Device{}
|
|
|
- err := server.RPCCallByName(nil, "registry", "Registry.FindDeviceByIdentifier", identifier, device)
|
|
|
+ span, ctx := opentracing.StartSpanFromContext(context.Background(), "GetDeviceInfoByIdentifier")
|
|
|
+ defer span.Finish()
|
|
|
+ ext.SpanKindRPCClient.Set(span)
|
|
|
+ span.SetTag("identifier", identifier)
|
|
|
+ err := server.RPCCallByName(ctx, "registry", "Registry.FindDeviceByIdentifier", identifier, device)
|
|
|
if err != nil {
|
|
|
r.JSON(http.StatusOK, renderError(ErrDeviceNotFound, err))
|
|
|
return
|
|
@@ -106,7 +121,12 @@ func GetDeviceCurrentStatus(device *models.Device, config *productconfig.Product
|
|
|
Id: uint64(device.ID),
|
|
|
}
|
|
|
statusreply := rpcs.ReplyGetStatus{}
|
|
|
- err := server.RPCCallByName(nil, "controller", "Controller.GetStatus", statusargs, &statusreply)
|
|
|
+ //opentracing
|
|
|
+ span, ctx := opentracing.StartSpanFromContext(context.Background(), "GetDeviceCurrentStatus")
|
|
|
+ defer span.Finish()
|
|
|
+ ext.SpanKindRPCClient.Set(span)
|
|
|
+
|
|
|
+ err := server.RPCCallByName(ctx, "controller", "Controller.GetStatus", statusargs, &statusreply)
|
|
|
if err != nil {
|
|
|
server.Log.Errorf("get devie status error: %v", err)
|
|
|
r.JSON(http.StatusOK, renderError(ErrSystemFault, err))
|
|
@@ -126,10 +146,12 @@ func GetDeviceCurrentStatus(device *models.Device, config *productconfig.Product
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+// GetDeviceLatestStatus get device latest status
|
|
|
func GetDeviceLatestStatus() {
|
|
|
|
|
|
}
|
|
|
|
|
|
+// SetDeviceStatus set device status
|
|
|
func SetDeviceStatus(device *models.Device, config *productconfig.ProductConfig,
|
|
|
urlparams martini.Params, req *http.Request, r render.Render) {
|
|
|
server.Log.Printf("ACTION GetDeviceCurrentStatus, identifier:: %v,request: %v", device.DeviceIdentifier, req.Body)
|
|
@@ -159,7 +181,12 @@ func SetDeviceStatus(device *models.Device, config *productconfig.ProductConfig,
|
|
|
Status: status,
|
|
|
}
|
|
|
statusreply := rpcs.ReplySetStatus{}
|
|
|
- err = server.RPCCallByName(nil, "controller", "Controller.SetStatus", statusargs, &statusreply)
|
|
|
+ //opentracing
|
|
|
+ span, ctx := opentracing.StartSpanFromContext(context.Background(), "SetDeviceStatus")
|
|
|
+ defer span.Finish()
|
|
|
+ ext.SpanKindRPCClient.Set(span)
|
|
|
+
|
|
|
+ err = server.RPCCallByName(ctx, "controller", "Controller.SetStatus", statusargs, &statusreply)
|
|
|
if err != nil {
|
|
|
server.Log.Errorf("set devie status error: %v", err)
|
|
|
r.JSON(http.StatusOK, renderError(ErrSystemFault, err))
|
|
@@ -171,6 +198,7 @@ func SetDeviceStatus(device *models.Device, config *productconfig.ProductConfig,
|
|
|
|
|
|
}
|
|
|
|
|
|
+// SendCommandToDevice send command to device
|
|
|
func SendCommandToDevice(device *models.Device, config *productconfig.ProductConfig,
|
|
|
urlparams martini.Params, req *http.Request, r render.Render) {
|
|
|
timeout := req.URL.Query().Get("timeout")
|
|
@@ -206,7 +234,13 @@ func SendCommandToDevice(device *models.Device, config *productconfig.ProductCon
|
|
|
Params: command.Params,
|
|
|
}
|
|
|
cmdreply := rpcs.ReplySendCommand{}
|
|
|
- err = server.RPCCallByName(nil, "controller", "Controller.SendCommand", cmdargs, &cmdreply)
|
|
|
+
|
|
|
+ //opentracing
|
|
|
+ span, ctx := opentracing.StartSpanFromContext(context.Background(), "SendCommandToDevice")
|
|
|
+ defer span.Finish()
|
|
|
+ ext.SpanKindRPCClient.Set(span)
|
|
|
+
|
|
|
+ err = server.RPCCallByName(ctx, "controller", "Controller.SendCommand", cmdargs, &cmdreply)
|
|
|
if err != nil {
|
|
|
server.Log.Errorf("send devie command error: %v", err)
|
|
|
r.JSON(http.StatusOK, renderError(ErrSystemFault, err))
|
|
@@ -218,6 +252,7 @@ func SendCommandToDevice(device *models.Device, config *productconfig.ProductCon
|
|
|
|
|
|
}
|
|
|
|
|
|
+// AddRule 增加设备规则
|
|
|
func AddRule(device *models.Device, req *http.Request, r render.Render) {
|
|
|
var ruleReq CreateRuleRequest
|
|
|
decoder := json.NewDecoder(req.Body)
|
|
@@ -235,8 +270,12 @@ func AddRule(device *models.Device, req *http.Request, r render.Render) {
|
|
|
Action: ruleReq.Action,
|
|
|
}
|
|
|
reply := &rpcs.ReplyEmptyResult{}
|
|
|
+ //opentracing
|
|
|
+ span, ctx := opentracing.StartSpanFromContext(context.Background(), "AddRule")
|
|
|
+ defer span.Finish()
|
|
|
+ ext.SpanKindRPCClient.Set(span)
|
|
|
|
|
|
- err = server.RPCCallByName(nil, "registry", "Registry.CreateRule", rule, reply)
|
|
|
+ err = server.RPCCallByName(ctx, "registry", "Registry.CreateRule", rule, reply)
|
|
|
if err != nil {
|
|
|
server.Log.Errorf("create device rule error: %v", err)
|
|
|
r.JSON(http.StatusOK, renderError(ErrSystemFault, err))
|