|
| 1 | +package axis |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/labstack/gommon/log" |
| 10 | + |
| 11 | + "github.com/eclipse/paho.mqtt.golang" |
| 12 | + "github.com/go-resty/resty" |
| 13 | +) |
| 14 | + |
| 15 | +type ( |
| 16 | + Axis struct { |
| 17 | + Options |
| 18 | + apiKey string |
| 19 | + deviceID string |
| 20 | + projectID string |
| 21 | + resty *resty.Client |
| 22 | + client mqtt.Client |
| 23 | + logger *log.Logger |
| 24 | + } |
| 25 | + |
| 26 | + Options struct { |
| 27 | + MessageHandler MessageHandler |
| 28 | + } |
| 29 | + |
| 30 | + Key struct { |
| 31 | + Value string `json:"value"` |
| 32 | + ProjectID string `json:"project_id"` |
| 33 | + } |
| 34 | + |
| 35 | + ConnectHandler func() |
| 36 | + |
| 37 | + MessageHandler func(topic string, message []byte) |
| 38 | +) |
| 39 | + |
| 40 | +func New(apiKey, deviceID string) *Axis { |
| 41 | + return NewWithOptions(apiKey, deviceID, Options{}) |
| 42 | +} |
| 43 | + |
| 44 | +func NewWithOptions(apiKey, deviceID string, options Options) (a *Axis) { |
| 45 | + a = &Axis{ |
| 46 | + apiKey: apiKey, |
| 47 | + deviceID: deviceID, |
| 48 | + resty: resty.New().SetHostURL("https://api.labstack.com").SetAuthToken(apiKey), |
| 49 | + logger: log.New("axis"), |
| 50 | + } |
| 51 | + a.Options = options |
| 52 | + return |
| 53 | +} |
| 54 | + |
| 55 | +func (a *Axis) normalizeDeviceID() string { |
| 56 | + return fmt.Sprintf("%s:%s", a.projectID, a.deviceID) |
| 57 | + |
| 58 | +} |
| 59 | + |
| 60 | +func (a *Axis) normalizeTopic(name string) string { |
| 61 | + return fmt.Sprintf("%s/%s", a.projectID, name) |
| 62 | +} |
| 63 | + |
| 64 | +func (a *Axis) denormalizeTopic(name string) string { |
| 65 | + return strings.TrimPrefix(name, a.projectID+"/") |
| 66 | +} |
| 67 | + |
| 68 | +func (a *Axis) Connect() error { |
| 69 | + return a.ConnectWithHandler(nil) |
| 70 | +} |
| 71 | + |
| 72 | +func (a *Axis) ConnectWithHandler(handler ConnectHandler) error { |
| 73 | + // Find project id |
| 74 | + key := new(Key) |
| 75 | + res, err := a.resty.R(). |
| 76 | + SetResult(key). |
| 77 | + Get("/axis/key") |
| 78 | + if err != nil || res.StatusCode() < 200 || res.StatusCode() >= 300 { |
| 79 | + return errors.New("Unable to find the project") |
| 80 | + } |
| 81 | + a.projectID = key.ProjectID |
| 82 | + |
| 83 | + // Connect |
| 84 | + o := mqtt.NewClientOptions(). |
| 85 | + AddBroker("tcp://axis.labstack.com:1883"). |
| 86 | + SetUsername(a.projectID). |
| 87 | + SetPassword(a.apiKey). |
| 88 | + SetClientID(a.normalizeDeviceID()) |
| 89 | + if handler != nil { |
| 90 | + o.OnConnect = func(_ mqtt.Client) { |
| 91 | + handler() |
| 92 | + } |
| 93 | + } |
| 94 | + a.client = mqtt.NewClient(o) |
| 95 | + t := a.client.Connect() |
| 96 | + t.Wait() |
| 97 | + return t.Error() |
| 98 | +} |
| 99 | + |
| 100 | +func (a *Axis) Publish(topic string, message interface{}) error { |
| 101 | + t := a.client.Publish(a.normalizeTopic(topic), 0, false, message) |
| 102 | + t.Wait() |
| 103 | + return t.Error() |
| 104 | +} |
| 105 | + |
| 106 | +func (a *Axis) Subscribe(topic string) error { |
| 107 | + return a.SubscribeWithHandler(topic, nil) |
| 108 | +} |
| 109 | + |
| 110 | +func (a *Axis) SubscribeWithHandler(topic string, handler MessageHandler) error { |
| 111 | + t := a.client.Subscribe(a.normalizeTopic(topic), 0, func(_ mqtt.Client, m mqtt.Message) { |
| 112 | + topic := a.denormalizeTopic(m.Topic()) |
| 113 | + if handler != nil { |
| 114 | + handler(topic, m.Payload()) |
| 115 | + } |
| 116 | + if a.MessageHandler != nil { |
| 117 | + a.MessageHandler(topic, m.Payload()) |
| 118 | + } |
| 119 | + }) |
| 120 | + t.Wait() |
| 121 | + return t.Error() |
| 122 | +} |
| 123 | + |
| 124 | +func (a *Axis) Unsubscribe(topic string) error { |
| 125 | + t := a.client.Unsubscribe(a.normalizeTopic(topic)) |
| 126 | + t.Wait() |
| 127 | + return t.Error() |
| 128 | +} |
| 129 | + |
| 130 | +func (a *Axis) Disconnect() { |
| 131 | + a.client.Disconnect(1000) |
| 132 | +} |
| 133 | + |
| 134 | +func (a *Axis) Run() { |
| 135 | + for { |
| 136 | + time.Sleep(time.Second) |
| 137 | + } |
| 138 | +} |
0 commit comments