@@ -13,11 +13,11 @@ type (
13
13
Options
14
14
accountID string
15
15
apiKey string
16
+ deviceID string
16
17
client mqtt.Client
17
18
}
18
19
19
20
Options struct {
20
- DeviceID string
21
21
MessageHandler MessageHandler
22
22
}
23
23
@@ -26,25 +26,30 @@ type (
26
26
MessageHandler func (topic string , message []byte )
27
27
)
28
28
29
- func New (accountID , apiKey string ) * Hub {
30
- return NewWithOptions (accountID , apiKey , Options {})
29
+ func New (accountID , apiKey , deviceID string ) * Hub {
30
+ return NewWithOptions (accountID , apiKey , deviceID , Options {})
31
31
}
32
32
33
- func NewWithOptions (accountID , apiKey string , options Options ) * Hub {
33
+ func NewWithOptions (accountID , apiKey , deviceID string , options Options ) * Hub {
34
34
h := & Hub {
35
35
accountID : accountID ,
36
36
apiKey : apiKey ,
37
37
}
38
+ h .deviceID = h .normalizeDeviceID (deviceID )
38
39
h .Options = options
39
40
return h
40
41
}
41
42
42
- func (h * Hub ) normalizeTopic ( topic string ) string {
43
- return fmt .Sprintf ("%s/ %s" , h .accountID , topic )
43
+ func (h * Hub ) normalizeDeviceID ( id string ) string {
44
+ return fmt .Sprintf ("%s: %s" , h .accountID , id )
44
45
}
45
46
46
- func (h * Hub ) denormalizeTopic (topic string ) string {
47
- return strings .TrimPrefix (topic , h .accountID + "/" )
47
+ func (h * Hub ) normalizeTopic (name string ) string {
48
+ return fmt .Sprintf ("%s/%s" , h .accountID , name )
49
+ }
50
+
51
+ func (h * Hub ) denormalizeTopic (name string ) string {
52
+ return strings .TrimPrefix (name , h .accountID + "/" )
48
53
}
49
54
50
55
func (h * Hub ) Connect () error {
@@ -55,10 +60,8 @@ func (h *Hub) ConnectWithHandler(handler ConnectHandler) error {
55
60
o := mqtt .NewClientOptions ().
56
61
AddBroker ("tcp://hub.labstack.com:1883" ).
57
62
SetUsername (h .accountID ).
58
- SetPassword (h .apiKey )
59
- if h .DeviceID != "" {
60
- o .SetClientID (h .DeviceID )
61
- }
63
+ SetPassword (h .apiKey ).
64
+ SetClientID (h .deviceID )
62
65
if handler != nil {
63
66
o .OnConnect = func (_ mqtt.Client ) {
64
67
handler ()
0 commit comments