-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsignal_service.go
107 lines (89 loc) · 3.18 KB
/
signal_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
package natureremo
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/url"
"strings"
)
// SignalService provides interface of Nature Remo APIs which are related to signals.
type SignalService interface {
// GetAll gets all signals which related to specified appliance.
GetAll(ctx context.Context, appliance *Appliance) ([]*Signal, error)
// New creates new signal and links to specified appliance.
New(ctx context.Context, appliance *Appliance, ir *IRSignal, name, image string) (*Signal, error)
// ReOrder arranges signals by given orders.
ReOrder(ctx context.Context, appliance *Appliance, signals []*Signal) error
// Update updates specified signal.
Update(ctx context.Context, signal *Signal) (*Signal, error)
// Delete deletes specified signal.
Delete(ctx context.Context, signal *Signal) error
// Send sends specified signal.
Send(ctx context.Context, signal *Signal) error
}
type signalService struct {
cli *Client
}
func (s *signalService) GetAll(ctx context.Context, appliance *Appliance) ([]*Signal, error) {
path := fmt.Sprintf("appliances/%s/signals", appliance.ID)
var ss []*Signal
if err := s.cli.get(ctx, path, nil, &ss); err != nil {
return nil, fmt.Errorf("GET %s failed: %w", path, err)
}
return ss, nil
}
func (s *signalService) New(ctx context.Context, appliance *Appliance, ir *IRSignal, name, image string) (*Signal, error) {
path := fmt.Sprintf("appliances/%s/signals", appliance.ID)
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())
data.Set("name", name)
data.Set("image", image)
var sig Signal
if err := s.cli.postForm(ctx, path, data, &sig); err != nil {
return nil, fmt.Errorf("POST %s with %#v: %w", path, data, err)
}
return &sig, nil
}
func (s *signalService) ReOrder(ctx context.Context, appliance *Appliance, signals []*Signal) error {
path := fmt.Sprintf("appliances/%s/signal_orders", appliance.ID)
ids := make([]string, 0, len(signals))
for i := range signals {
ids = append(ids, signals[i].ID)
}
data := url.Values{}
data.Set("signals", strings.Join(ids, ","))
if err := s.cli.postForm(ctx, path, data, nil); err != nil {
return fmt.Errorf("POST %s failed with %#v: %w", path, data, err)
}
return nil
}
func (s *signalService) Update(ctx context.Context, signal *Signal) (*Signal, error) {
path := fmt.Sprintf("signals/%s", signal.ID)
data := url.Values{}
data.Set("name", signal.Name)
data.Set("image", signal.Image)
var sig Signal
if err := s.cli.postForm(ctx, path, data, &sig); err != nil {
return nil, fmt.Errorf("POST %s failed with %#v: %w", path, signal, err)
}
return &sig, nil
}
func (s *signalService) Delete(ctx context.Context, signal *Signal) error {
path := fmt.Sprintf("signals/%s/delete", signal.ID)
if err := s.cli.post(ctx, path, nil); err != nil {
return fmt.Errorf("POST %s failed: %w", path, err)
}
return nil
}
func (s *signalService) Send(ctx context.Context, signal *Signal) error {
path := fmt.Sprintf("signals/%s/send", signal.ID)
if err := s.cli.post(ctx, path, nil); err != nil {
return fmt.Errorf("POST %s failed: %w", path, err)
}
return nil
}