@@ -2,54 +2,80 @@ package hub
2
2
3
3
import (
4
4
"fmt"
5
+ "os"
5
6
"strings"
6
7
"time"
7
8
8
- mqtt "github.com/eclipse/paho.mqtt.golang"
9
+ "github.com/labstack/gommon/log"
10
+
11
+ "github.com/eclipse/paho.mqtt.golang"
12
+ "github.com/go-resty/resty"
9
13
)
10
14
11
15
type (
12
16
Hub struct {
13
17
Options
14
- accountID string
15
- apiKey string
16
- deviceID string
17
- client mqtt. Client
18
+ resty * resty. Client
19
+ key * Key
20
+ client mqtt. Client
21
+ logger * log. Logger
18
22
}
19
23
20
24
Options struct {
25
+ DeviceID string
21
26
MessageHandler MessageHandler
22
27
}
23
28
29
+ Key struct {
30
+ ID string `json:"id"`
31
+ ProjectID string `json:"project_id"`
32
+ }
33
+
24
34
ConnectHandler func ()
25
35
26
36
MessageHandler func (topic string , message []byte )
27
37
)
28
38
29
- func New (accountID , apiKey , deviceID string ) * Hub {
30
- return NewWithOptions (accountID , apiKey , deviceID , Options {})
39
+ func New (apiKey string ) * Hub {
40
+ return NewWithOptions (apiKey , Options {})
31
41
}
32
42
33
- func NewWithOptions (accountID , apiKey , deviceID string , options Options ) * Hub {
34
- h := & Hub {
35
- accountID : accountID ,
36
- apiKey : apiKey ,
43
+ func NewWithOptions (apiKey string , options Options ) (h * Hub ) {
44
+ 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" ),
37
50
}
38
- h .deviceID = h .normalizeDeviceID (deviceID )
39
51
h .Options = options
40
- return h
52
+ if h .DeviceID == "" {
53
+ h .DeviceID , _ = os .Hostname ()
54
+ }
55
+ res , err := h .resty .R ().
56
+ SetResult (h .key ).
57
+ Get ("/keys/" + h .key .ID )
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
+ return
41
66
}
42
67
43
68
func (h * Hub ) normalizeDeviceID (id string ) string {
44
- return fmt .Sprintf ("%s:%s" , h .accountID , id )
69
+ return fmt .Sprintf ("%s:%s" , h .key .ProjectID , id )
70
+
45
71
}
46
72
47
73
func (h * Hub ) normalizeTopic (name string ) string {
48
- return fmt .Sprintf ("%s/%s" , h .accountID , name )
74
+ return fmt .Sprintf ("%s/%s" , h .key . ProjectID , name )
49
75
}
50
76
51
77
func (h * Hub ) denormalizeTopic (name string ) string {
52
- return strings .TrimPrefix (name , h .accountID + "/" )
78
+ return strings .TrimPrefix (name , h .key . ProjectID + "/" )
53
79
}
54
80
55
81
func (h * Hub ) Connect () error {
@@ -59,9 +85,9 @@ func (h *Hub) Connect() error {
59
85
func (h * Hub ) ConnectWithHandler (handler ConnectHandler ) error {
60
86
o := mqtt .NewClientOptions ().
61
87
AddBroker ("tcp://hub.labstack.com:1883" ).
62
- SetUsername (h .accountID ).
63
- SetPassword (h .apiKey ).
64
- SetClientID (h .deviceID )
88
+ SetUsername (h .key . ProjectID ).
89
+ SetPassword (h .key . ID ).
90
+ SetClientID (h .DeviceID )
65
91
if handler != nil {
66
92
o .OnConnect = func (_ mqtt.Client ) {
67
93
handler ()
0 commit comments