Ver código fonte

fix: 解决消息问题

lijian 2 anos atrás
pai
commit
46b81970eb
3 arquivos alterados com 15 adições e 4 exclusões
  1. 3 0
      services/controller/controller.go
  2. 10 4
      tests/device/device.go
  3. 2 0
      tests/device/main.go

+ 3 - 0
services/controller/controller.go

@@ -105,6 +105,9 @@ func (c *Controller) Online(args rpcs.ArgsGetStatus, reply *rpcs.ReplyEmptyResul
 }
 
 func (c *Controller) Offline(args rpcs.ArgsGetStatus, reply *rpcs.ReplyEmptyResult) error {
+	if args.Id == "" || args.VendorId == "" {
+		return nil
+	}
 	data := gjson.New(nil)
 	_ = data.Set("device_id", args.Id)
 	t := time.Now()

+ 10 - 4
tests/device/device.go

@@ -1,6 +1,7 @@
 package main
 
 import (
+	"crypto/tls"
 	"encoding/hex"
 	"encoding/json"
 	"fmt"
@@ -12,7 +13,6 @@ import (
 	"sparrow/pkg/coap"
 	"sparrow/pkg/klink"
 	"sparrow/pkg/protocol"
-	"sparrow/pkg/server"
 	"sparrow/pkg/tlv"
 	"time"
 
@@ -402,20 +402,26 @@ func (d *Device) doMQTTAccess() error {
 
 	//create a ClientOptions struct setting the broker address, clientid, turn
 	//off trace output and set the default message handler
-	opts := MQTT.NewClientOptions().AddBroker("tcp://" + d.access)
+	opts := MQTT.NewClientOptions().AddBroker("ssl://" + d.access)
 	clientid := fmt.Sprintf("%x", d.id)
 	opts.SetClientID(clientid)
 	opts.SetUsername(clientid) // clientid as username
 	opts.SetPassword(hex.EncodeToString(d.token))
 	opts.SetKeepAlive(30 * time.Second)
+	// process key files
+	cert, err := tls.LoadX509KeyPair(*confCAFile, *confKeyFile)
+	if err != nil {
+		panic(err)
+	}
+
 	opts.SetDefaultPublishHandler(d.messageHandler)
-	//opts.SetTLSConfig(&tls.Config{Certificates: nil, InsecureSkipVerify: true})
+	opts.SetTLSConfig(&tls.Config{Certificates: []tls.Certificate{cert}, InsecureSkipVerify: true})
 
 	//create and start a client using the above ClientOptions
 	c := MQTT.NewClient(opts)
 	go func() {
 		if token := c.Connect(); token.Wait() && token.Error() != nil {
-			server.Log.Error(token.Error())
+			fmt.Println(err.Error())
 			return
 		}
 	}()

+ 2 - 0
tests/device/main.go

@@ -10,6 +10,8 @@ var (
 	testProductKey = flag.String("productkey", "958daf8b3a533f0d9516ac8fd17ef0cb06b439e664787a2a89608a10eeee8eb3c35c82c505d19f8a4417e530de0678fd", "product key")
 	testProtocol   = flag.String("protocol", "mqtt", "access protocol")
 	testDeviceCode = flag.String("dvc", "", "device code")
+	confCAFile     = flag.String("certfile", "", "ca-file")
+	confKeyFile    = flag.String("pkeyfile", "", "key file")
 )
 
 func main() {