@@ -2,54 +2,80 @@ package hub
22
33import (
44 "fmt"
5+ "os"
56 "strings"
67 "time"
78
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"
913)
1014
1115type (
1216 Hub struct {
1317 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
1822 }
1923
2024 Options struct {
25+ DeviceID string
2126 MessageHandler MessageHandler
2227 }
2328
29+ Key struct {
30+ ID string `json:"id"`
31+ ProjectID string `json:"project_id"`
32+ }
33+
2434 ConnectHandler func ()
2535
2636 MessageHandler func (topic string , message []byte )
2737)
2838
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 {})
3141}
3242
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" ),
3750 }
38- h .deviceID = h .normalizeDeviceID (deviceID )
3951 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
4166}
4267
4368func (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+
4571}
4672
4773func (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 )
4975}
5076
5177func (h * Hub ) denormalizeTopic (name string ) string {
52- return strings .TrimPrefix (name , h .accountID + "/" )
78+ return strings .TrimPrefix (name , h .key . ProjectID + "/" )
5379}
5480
5581func (h * Hub ) Connect () error {
@@ -59,9 +85,9 @@ func (h *Hub) Connect() error {
5985func (h * Hub ) ConnectWithHandler (handler ConnectHandler ) error {
6086 o := mqtt .NewClientOptions ().
6187 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 )
6591 if handler != nil {
6692 o .OnConnect = func (_ mqtt.Client ) {
6793 handler ()
0 commit comments