Browse Source

修复+inf

lijian 6 years ago
parent
commit
561a75c751

+ 45 - 6
services/apiprovider/actions.go

@@ -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))

+ 2 - 2
services/knowoapi/controllers/device.go

@@ -100,10 +100,10 @@ func (a *DeviceController) GetBannerdata() {
 		responseError(a.Ctx, ErrDatabase, err.Error())
 		return
 	}
-	if tact != 0 {
+	if tact != 0 && yact != 0 {
 		comparedWithYesterdayActive = (float64(tact) / float64(yact)) * 100
 	}
-	if tlive != 0 {
+	if tlive != 0 && ylive != 0 {
 		comparedWithYesterdayLive = (float64(tlive) / float64(ylive)) * 100
 		rateOfTodayLive = float64(tlive) / float64(totolact) * 100
 	}

+ 1 - 3
services/knowoapi/services/user.go

@@ -4,7 +4,6 @@ import (
 	"encoding/base64"
 	"sparrow/pkg/generator"
 	"sparrow/pkg/models"
-	"sparrow/pkg/server"
 	"sparrow/pkg/utils"
 	"sparrow/services/knowoapi/model"
 )
@@ -52,6 +51,7 @@ func (a userservice) Register(user *models.Reqrequest) (*models.User, error) {
 	_u.UserName = user.UserName
 	_u.Phone = user.Phone
 	_u.Email = user.Email
+	_u.UserRoleID = 1 //默认普通用户
 	vedor := &models.Vendor{
 		VendorName: user.VendorName,
 	}
@@ -66,10 +66,8 @@ func (a userservice) Register(user *models.Reqrequest) (*models.User, error) {
 func (a userservice) ModifyPassword(userid uint, new, old string) (bool, error) {
 	obytes, _ := base64.StdEncoding.DecodeString(old)
 	nbytes, _ := base64.StdEncoding.DecodeString(new)
-	server.Log.Debugf("旧密码:%s, 新密码:%s")
 	old = utils.Md5(string(obytes) + model.SignedString)
 	new = utils.Md5(string(nbytes) + model.SignedString)
-	server.Log.Debugf("旧密码:%s, 新密码:%s, 旧加密:%s,新加密:%s", string(obytes), string(nbytes), old, new)
 	return a.model.User.UpdatePassword(userid, new, old)
 }
 

BIN
tests/coaptool/coaptool


BIN
tests/device/device