-
Notifications
You must be signed in to change notification settings - Fork 14
/
device_service.go
84 lines (67 loc) · 2.51 KB
/
device_service.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
package natureremo
import (
"context"
"fmt"
"net/url"
"strconv"
)
// DeviceService provides interface of Nature Remo APIs which are related to devices.
type DeviceService interface {
// GetAll gets all information of devices which are related with user.
GetAll(ctx context.Context) ([]*Device, error)
// Update updates device information which exclude temperature offset and humidity offset.
Update(ctx context.Context, device *Device) (*Device, error)
// Delete deletes specified device.
Delete(ctx context.Context, device *Device) error
// UpdateTemperatureOffset updates temperature offset of specified device.
UpdateTemperatureOffset(ctx context.Context, device *Device) (*Device, error)
// UpdateHumidityOffset updates humidity offset of specified device.
UpdateHumidityOffset(ctx context.Context, device *Device) (*Device, error)
}
type deviceService struct {
cli *Client
}
func (s *deviceService) GetAll(ctx context.Context) ([]*Device, error) {
var ds []*Device
if err := s.cli.get(ctx, "devices", nil, &ds); err != nil {
return nil, fmt.Errorf("GET deviecs failed: %w", err)
}
return ds, nil
}
func (s *deviceService) Update(ctx context.Context, device *Device) (*Device, error) {
path := fmt.Sprintf("devices/%s", device.ID)
data := url.Values{}
data.Set("name", device.Name)
var d Device
if err := s.cli.postForm(ctx, path, data, &d); err != nil {
return nil, fmt.Errorf("POST %s with %#v: %w", path, data, err)
}
return &d, nil
}
func (s *deviceService) Delete(ctx context.Context, device *Device) error {
path := fmt.Sprintf("devices/%s/delete", device.ID)
if err := s.cli.post(ctx, path, nil); err != nil {
return fmt.Errorf("POST %s failed: %w", path, err)
}
return nil
}
func (s *deviceService) UpdateTemperatureOffset(ctx context.Context, device *Device) (*Device, error) {
path := fmt.Sprintf("devices/%s/temperature_offset", device.ID)
data := url.Values{}
data.Set("offset", strconv.FormatInt(device.TemperatureOffset, 10))
var d Device
if err := s.cli.postForm(ctx, path, data, &d); err != nil {
return nil, fmt.Errorf("POST %s with %#v: %w", path, data, err)
}
return &d, nil
}
func (s *deviceService) UpdateHumidityOffset(ctx context.Context, device *Device) (*Device, error) {
path := fmt.Sprintf("devices/%s/humidity_offset", device.ID)
data := url.Values{}
data.Set("offset", strconv.FormatInt(device.HumidityOffset, 10))
var d Device
if err := s.cli.postForm(ctx, path, data, &d); err != nil {
return nil, fmt.Errorf("POST %s with %#v: %w", path, data, err)
}
return &d, nil
}