@@ -13,11 +13,11 @@ type (
1313 Options
1414 accountID string
1515 apiKey string
16+ deviceID string
1617 client mqtt.Client
1718 }
1819
1920 Options struct {
20- DeviceID string
2121 MessageHandler MessageHandler
2222 }
2323
@@ -26,25 +26,30 @@ type (
2626 MessageHandler func (topic string , message []byte )
2727)
2828
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 {})
3131}
3232
33- func NewWithOptions (accountID , apiKey string , options Options ) * Hub {
33+ func NewWithOptions (accountID , apiKey , deviceID string , options Options ) * Hub {
3434 h := & Hub {
3535 accountID : accountID ,
3636 apiKey : apiKey ,
3737 }
38+ h .deviceID = h .normalizeDeviceID (deviceID )
3839 h .Options = options
3940 return h
4041}
4142
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 )
4445}
4546
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 + "/" )
4853}
4954
5055func (h * Hub ) Connect () error {
@@ -55,10 +60,8 @@ func (h *Hub) ConnectWithHandler(handler ConnectHandler) error {
5560 o := mqtt .NewClientOptions ().
5661 AddBroker ("tcp://hub.labstack.com:1883" ).
5762 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 )
6265 if handler != nil {
6366 o .OnConnect = func (_ mqtt.Client ) {
6467 handler ()
0 commit comments