/
client.go
89 lines (69 loc) · 2.04 KB
/
client.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package awair
import (
"fmt"
"github.com/arcticfoxnv/awair_api"
"github.com/patrickmn/go-cache"
"log"
"sync"
"time"
)
const (
DEVICES_KEY = "devices"
DEVICE_LATEST_KEY_FORMAT = "device-latest-%s-%d"
)
type Client struct {
client *awair_api.Client
clientCacheLock *sync.RWMutex
clientCache *cache.Cache
}
func NewClient(accessToken string, cacheTTL time.Duration, options ...awair_api.Option) *Client {
cli := &Client{
client: awair_api.NewClient(accessToken, options...),
clientCacheLock: new(sync.RWMutex),
}
cli.SetCacheTTL(cacheTTL)
return cli
}
func (c *Client) SetCacheTTL(ttl time.Duration) {
c.clientCacheLock.Lock()
defer c.clientCacheLock.Unlock()
if c.clientCache != nil {
c.clientCache.Flush()
}
c.clientCache = cache.New(ttl, 10*time.Minute)
}
func (c *Client) GetDeviceAPIUsage(deviceType string, deviceId int) (*awair_api.DeviceUsage, error) {
return c.client.DeviceAPIUsage(deviceType, deviceId)
}
func (c *Client) GetDeviceList() (*awair_api.DeviceList, error) {
c.clientCacheLock.RLock()
defer c.clientCacheLock.RUnlock()
if data, found := c.clientCache.Get(DEVICES_KEY); found {
return data.(*awair_api.DeviceList), nil
}
log.Printf("Fetching device list")
devices, err := c.client.Devices()
if err != nil {
return nil, err
}
c.clientCache.Set(DEVICES_KEY, devices, cache.DefaultExpiration)
return devices, nil
}
func (c *Client) GetLatestData(deviceType string, deviceId int) (*awair_api.DeviceDataList, error) {
c.clientCacheLock.RLock()
defer c.clientCacheLock.RUnlock()
cacheKey := fmt.Sprintf(DEVICE_LATEST_KEY_FORMAT, deviceType, deviceId)
if data, found := c.clientCache.Get(cacheKey); found {
return data.(*awair_api.DeviceDataList), nil
}
log.Printf("Fetching data for %s-%d", deviceType, deviceId)
data, err := c.client.UserLatestAirData(deviceType, deviceId)
if err != nil {
return nil, err
}
c.clientCache.Set(cacheKey, data, cache.DefaultExpiration)
return data, nil
}
func (c *Client) GetUserInfo() (*awair_api.User, error) {
return c.client.UserInfo()
}