-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.go
50 lines (42 loc) · 1001 Bytes
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package aqara
import (
"encoding/json"
"errors"
"net"
)
// Get one of the interface's unicast IP address
func getInterfaceAddr(iface string) (net.IP, error) {
dev, err := net.InterfaceByName(iface)
if err != nil {
return nil, err
}
devAddr, err := dev.Addrs()
if err != nil {
return nil, err
}
if len(devAddr) == 0 {
return nil, errors.New("interface has no address")
}
return devAddr[0].(*net.IPNet).IP, nil
}
func parseBuffer(buffer []byte, gateway *Gateway) (*ReportMessage, error) {
var message internalReportMessage
if err := json.Unmarshal(buffer, &message); err != nil {
return nil, err
}
if gateway != nil && message.Token != "" {
gateway.token = message.Token
}
var data map[string]interface{}
if err := json.Unmarshal([]byte(message.Data), &data); err != nil {
return nil, err
}
if val, ok := data["error"]; ok {
return nil, errors.New(val.(string))
}
return &ReportMessage{
Model: message.Model,
Sid: message.Sid,
Data: data,
}, nil
}