Explorar o código

feat: add timesync command

lijian %!s(int64=2) %!d(string=hai) anos
pai
achega
43fe49bdc8

+ 0 - 3
pkg/mqtt/connection.go

@@ -109,14 +109,11 @@ func (c *Connection) confirmPublish(MessageID uint16) {
 
 // ValidateToken validate token
 func (c *Connection) ValidateToken(token []byte) error {
-
 	err := c.Mgr.Provider.ValidateDeviceToken(c.DeviceID, token)
 	if err != nil {
 		return err
 	}
-
 	c.Token = token
-
 	return nil
 }
 

+ 1 - 1
services/mqttaccess/access.go

@@ -57,7 +57,7 @@ func (a *Access) GetStatus(args rpcs.ArgsGetStatus, reply *rpcs.ReplyGetStatus)
 }
 
 func (a *Access) SendCommand(args rpcs.ArgsSendCommand, reply *rpcs.ReplySendCommand) error {
-	server.Log.Infof("Access Send Command: %v", args)
+	server.Log.Infof("Access Send Command: %v, %v", args.DeviceId, args.Cmd)
 	cmd := &klink.CloudSend{
 		Action:      "cloudSend",
 		MsgId:       0,

+ 12 - 0
services/mqttaccess/mqtt_provider.go

@@ -7,6 +7,7 @@ import (
 	"sparrow/pkg/models"
 	"sparrow/pkg/rpcs"
 	"sparrow/pkg/server"
+	"time"
 )
 
 type MQTTProvider struct{}
@@ -42,6 +43,17 @@ func (mp *MQTTProvider) OnDeviceOnline(args rpcs.ArgsGetOnline, VendorId string)
 		return err
 	}
 
+	// 下发时间同步指令
+	var cmdArgs rpcs.ArgsSendCommand
+	cmdArgs.Cmd = "timeSync"
+	cmdArgs.DeviceId = args.Id
+	cmdArgs.Params = map[string]interface{}{
+		"value": time.Now().Unix(),
+	}
+	err = server.RPCCallByName(nil, rpcs.MQTTAccessName, "MQTTAccess.SendCommand", cmdArgs, &cReply)
+	if err != nil {
+		server.Log.Errorf("发送时间同步指令:%v", cmdArgs, err)
+	}
 	return err
 }
 func (mp *MQTTProvider) OnDeviceOffline(deviceid string, vendorId string) error {