Skip to content

Commit 57bfbdc

Browse files
committed
Bumped version
Signed-off-by: Vishal Rana <[email protected]>
1 parent bd199f6 commit 57bfbdc

File tree

1 file changed

+31
-35
lines changed

1 file changed

+31
-35
lines changed

hub/hub.go

+31-35
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package hub
22

33
import (
4+
"errors"
45
"fmt"
5-
"os"
66
"strings"
77
"time"
88

@@ -15,19 +15,20 @@ import (
1515
type (
1616
Hub struct {
1717
Options
18-
resty *resty.Client
19-
key *Key
20-
client mqtt.Client
21-
logger *log.Logger
18+
apiKey string
19+
deviceID string
20+
projectID string
21+
resty *resty.Client
22+
client mqtt.Client
23+
logger *log.Logger
2224
}
2325

2426
Options struct {
25-
DeviceID string
2627
MessageHandler MessageHandler
2728
}
2829

2930
Key struct {
30-
ID string `json:"id"`
31+
Value string `json:"value"`
3132
ProjectID string `json:"project_id"`
3233
}
3334

@@ -36,58 +37,53 @@ type (
3637
MessageHandler func(topic string, message []byte)
3738
)
3839

39-
func New(apiKey string) *Hub {
40-
return NewWithOptions(apiKey, Options{})
40+
func New(apiKey, deviceID string) *Hub {
41+
return NewWithOptions(apiKey, deviceID, Options{})
4142
}
4243

43-
func NewWithOptions(apiKey string, options Options) (h *Hub) {
44+
func NewWithOptions(apiKey, deviceID string, options Options) (h *Hub) {
4445
h = &Hub{
45-
key: &Key{
46-
ID: apiKey,
47-
},
48-
resty: resty.New().SetHostURL("https://api.labstack.com").SetAuthToken(apiKey),
49-
logger: log.New("hub"),
46+
apiKey: apiKey,
47+
deviceID: deviceID,
48+
resty: resty.New().SetHostURL("https://api.labstack.com").SetAuthToken(apiKey),
49+
logger: log.New("hub"),
5050
}
5151
h.Options = options
52-
if h.DeviceID == "" {
53-
h.DeviceID, _ = os.Hostname()
54-
}
55-
res, err := h.resty.R().
56-
SetResult(h.key).
57-
Get("/keys")
58-
if err != nil {
59-
h.logger.Fatal(err)
60-
}
61-
if res.StatusCode() < 200 || res.StatusCode() >= 300 {
62-
h.logger.Fatal(err)
63-
}
64-
h.DeviceID = h.normalizeDeviceID(h.DeviceID)
6552
return
6653
}
6754

68-
func (h *Hub) normalizeDeviceID(id string) string {
69-
return fmt.Sprintf("%s:%s", h.key.ProjectID, id)
55+
func (h *Hub) normalizeDeviceID() string {
56+
return fmt.Sprintf("%s:%s", h.projectID, h.deviceID)
7057

7158
}
7259

7360
func (h *Hub) normalizeTopic(name string) string {
74-
return fmt.Sprintf("%s/%s", h.key.ProjectID, name)
61+
return fmt.Sprintf("%s/%s", h.projectID, name)
7562
}
7663

7764
func (h *Hub) denormalizeTopic(name string) string {
78-
return strings.TrimPrefix(name, h.key.ProjectID+"/")
65+
return strings.TrimPrefix(name, h.projectID+"/")
7966
}
8067

8168
func (h *Hub) Connect() error {
8269
return h.ConnectWithHandler(nil)
8370
}
8471

8572
func (h *Hub) ConnectWithHandler(handler ConnectHandler) error {
73+
key := new(Key)
74+
res, err := h.resty.R().
75+
SetResult(key).
76+
Get("/keys")
77+
if err != nil || res.StatusCode() < 200 || res.StatusCode() >= 300 {
78+
return errors.New("Unable to find the project")
79+
}
80+
h.projectID = key.ProjectID
81+
8682
o := mqtt.NewClientOptions().
8783
AddBroker("tcp://hub.labstack.com:1883").
88-
SetUsername(h.key.ProjectID).
89-
SetPassword(h.key.ID).
90-
SetClientID(h.DeviceID)
84+
SetUsername(h.projectID).
85+
SetPassword(h.apiKey).
86+
SetClientID(h.normalizeDeviceID())
9187
if handler != nil {
9288
o.OnConnect = func(_ mqtt.Client) {
9389
handler()

0 commit comments

Comments
 (0)