1
1
package hub
2
2
3
3
import (
4
+ "errors"
4
5
"fmt"
5
- "os"
6
6
"strings"
7
7
"time"
8
8
@@ -15,19 +15,20 @@ import (
15
15
type (
16
16
Hub struct {
17
17
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
22
24
}
23
25
24
26
Options struct {
25
- DeviceID string
26
27
MessageHandler MessageHandler
27
28
}
28
29
29
30
Key struct {
30
- ID string `json:"id "`
31
+ Value string `json:"value "`
31
32
ProjectID string `json:"project_id"`
32
33
}
33
34
@@ -36,58 +37,53 @@ type (
36
37
MessageHandler func (topic string , message []byte )
37
38
)
38
39
39
- func New (apiKey string ) * Hub {
40
- return NewWithOptions (apiKey , Options {})
40
+ func New (apiKey , deviceID string ) * Hub {
41
+ return NewWithOptions (apiKey , deviceID , Options {})
41
42
}
42
43
43
- func NewWithOptions (apiKey string , options Options ) (h * Hub ) {
44
+ func NewWithOptions (apiKey , deviceID string , options Options ) (h * Hub ) {
44
45
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" ),
50
50
}
51
51
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 )
65
52
return
66
53
}
67
54
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 )
70
57
71
58
}
72
59
73
60
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 )
75
62
}
76
63
77
64
func (h * Hub ) denormalizeTopic (name string ) string {
78
- return strings .TrimPrefix (name , h .key . ProjectID + "/" )
65
+ return strings .TrimPrefix (name , h .projectID + "/" )
79
66
}
80
67
81
68
func (h * Hub ) Connect () error {
82
69
return h .ConnectWithHandler (nil )
83
70
}
84
71
85
72
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
+
86
82
o := mqtt .NewClientOptions ().
87
83
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 () )
91
87
if handler != nil {
92
88
o .OnConnect = func (_ mqtt.Client ) {
93
89
handler ()
0 commit comments