-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathappliance_service.go
160 lines (135 loc) · 5.5 KB
/
appliance_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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package natureremo
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/url"
"strings"
)
// ApplianceService provides interface of Nature Remo APIs which are related to appliances.
type ApplianceService interface {
// Detect detects air conditioners by an infrared ray signal.
Detect(ctx context.Context, ir *IRSignal) ([]*DetectedAirCon, error)
// GetAll gets all appliances.
GetAll(ctx context.Context) ([]*Appliance, error)
// New creates new appliance and links to specified device.
New(ctx context.Context, device *Device, nickname, image string) (*Appliance, error)
// NewWithModel creates new appliance with specified model.
NewWithModel(ctx context.Context, device *Device, nickname, image string, model *ApplianceModel) (*Appliance, error)
// ReOrder arranges appliances by given orders.
ReOrder(ctx context.Context, appliances []*Appliance) error
// Delete deletes specified appliance.
Delete(ctx context.Context, appliance *Appliance) error
// Update updates specified appliance.
Update(ctx context.Context, appliance *Appliance) (*Appliance, error)
// UpdateAirConSettings updates air conditioner settings of specified appliance.
UpdateAirConSettings(ctx context.Context, appliance *Appliance, settings *AirConSettings) error
// SendTVSignal sends TV infrared signal.
SendTVSignal(ctx context.Context, appliance *Appliance, buttonName string) (*TVState, error)
// SendLightSignal sends light infrared signal.
SendLightSignal(ctx context.Context, appliance *Appliance, buttonName string) (*LightState, error)
}
type applianceService struct {
cli *Client
}
func (s *applianceService) Detect(ctx context.Context, ir *IRSignal) ([]*DetectedAirCon, error) {
data := url.Values{}
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(ir); err != nil {
return nil, fmt.Errorf("cannot encode IRSignal %v: %w", ir, err)
}
data.Set("message", buf.String())
var aircons []*DetectedAirCon
if err := s.cli.postForm(ctx, "detectappliance", data, &aircons); err != nil {
return nil, fmt.Errorf("POST detectappliance failed with %#v: %w", ir, err)
}
return aircons, nil
}
func (s *applianceService) GetAll(ctx context.Context) ([]*Appliance, error) {
var as []*Appliance
if err := s.cli.get(ctx, "appliances", nil, &as); err != nil {
return nil, fmt.Errorf("GET appliances failed: %w", err)
}
return as, nil
}
func (s *applianceService) New(ctx context.Context, device *Device, nickname, image string) (*Appliance, error) {
return s.NewWithModel(ctx, device, nickname, image, nil)
}
func (s *applianceService) NewWithModel(ctx context.Context, device *Device, nickname, image string, model *ApplianceModel) (*Appliance, error) {
data := url.Values{}
data.Set("nickname", nickname)
if model != nil {
data.Set("model", model.ID)
}
data.Set("device", device.ID)
data.Set("image", image)
var a Appliance
if err := s.cli.postForm(ctx, "appliances", data, &a); err != nil {
return nil, fmt.Errorf("POST appliances failed with %#v: %w", data, err)
}
return &a, nil
}
func (s *applianceService) ReOrder(ctx context.Context, appliances []*Appliance) error {
ids := make([]string, 0, len(appliances))
for i := range appliances {
ids = append(ids, appliances[i].ID)
}
data := url.Values{}
data.Set("appliances", strings.Join(ids, ","))
if err := s.cli.postForm(ctx, "appliance_orders", data, nil); err != nil {
return fmt.Errorf("POST appliance_orders failed with %#v: %w", data, err)
}
return nil
}
func (s *applianceService) Delete(ctx context.Context, appliance *Appliance) error {
path := fmt.Sprintf("appliances/%s/delete", appliance.ID)
if err := s.cli.post(ctx, path, nil); err != nil {
return fmt.Errorf("POST %s: %w", path, err)
}
return nil
}
func (s *applianceService) Update(ctx context.Context, appliance *Appliance) (*Appliance, error) {
path := fmt.Sprintf("appliances/%s", appliance.ID)
data := url.Values{}
data.Set("image", appliance.Image)
data.Set("nickname", appliance.Nickname)
var a Appliance
if err := s.cli.postForm(ctx, path, data, &a); err != nil {
return nil, fmt.Errorf("POST %s with %#v: %w", path, data, err)
}
return &a, nil
}
func (s *applianceService) UpdateAirConSettings(ctx context.Context, appliance *Appliance, settings *AirConSettings) error {
path := fmt.Sprintf("appliances/%s/aircon_settings", appliance.ID)
data := url.Values{}
data.Set("temperature", settings.Temperature)
data.Set("operation_mode", settings.OperationMode.StringValue())
data.Set("air_volume", settings.AirVolume.StringValue())
data.Set("air_direction", settings.AirDirection.StringValue())
data.Set("button", settings.Button.StringValue())
if err := s.cli.postForm(ctx, path, data, nil); err != nil {
return fmt.Errorf("POST %s with %#v: %w", path, data, err)
}
return nil
}
func (s *applianceService) SendTVSignal(ctx context.Context, appliance *Appliance, buttonName string) (*TVState, error) {
path := fmt.Sprintf("appliances/%s/tv", appliance.ID)
data := url.Values{}
data.Set("button", buttonName)
var status TVState
if err := s.cli.postForm(ctx, path, data, &status); err != nil {
return nil, fmt.Errorf("POST %s with %#v: %w", path, data, err)
}
return &status, nil
}
func (s *applianceService) SendLightSignal(ctx context.Context, appliance *Appliance, buttonName string) (*LightState, error) {
path := fmt.Sprintf("appliances/%s/light", appliance.ID)
data := url.Values{}
data.Set("button", buttonName)
var status LightState
if err := s.cli.postForm(ctx, path, data, &status); err != nil {
return nil, fmt.Errorf("POST %s with %#v: %w", path, data, err)
}
return &status, nil
}