-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.go
160 lines (148 loc) · 3.61 KB
/
device.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 miio
import (
"context"
"encoding/json"
"github.com/uole/miio/types"
"strconv"
)
type Device struct {
address string
token string
conn *Conn
}
func (device *Device) ID() int {
if device.conn == nil {
return 0
}
return int(device.conn.deviceID)
}
// Dial dial device
func (device *Device) Dial(ctx context.Context) (err error) {
return device.conn.Dial(ctx, device.address, device.token)
}
// Info get device information
func (device *Device) Info(ctx context.Context) (deviceInfo *types.DeviceInfo, err error) {
var (
res *CommandResponse
)
if res, err = device.conn.Execute(ctx, NewCommandRequest("miIO.info", nil)); err == nil {
deviceInfo = &types.DeviceInfo{}
err = res.Decode(deviceInfo)
}
return
}
// GetAttributes get device attributes
func (device *Device) GetAttributes(ctx context.Context, attrs ...string) (values []*types.DeviceAttribute, err error) {
var (
res *CommandResponse
)
if res, err = device.conn.Execute(ctx, NewCommandRequest("get_prop", attrs)); err == nil {
values = make([]*types.DeviceAttribute, 0, len(attrs))
ss := make([]string, 0)
if err = res.Decode(&ss); err == nil {
if len(ss) == len(attrs) {
for i := 0; i < len(attrs); i++ {
values = append(values, &types.DeviceAttribute{
Attribute: attrs[i],
Value: ss[i],
})
}
}
}
}
return
}
// GetProperties get device properties
func (device *Device) GetProperties(ctx context.Context, ps ...*types.DeviceProperty) (err error) {
var (
res *CommandResponse
)
for _, p := range ps {
if p.DID == "" {
p.DID = strconv.FormatUint(uint64(device.conn.deviceID), 10)
}
}
if res, err = device.conn.Execute(ctx, NewCommandRequest("get_properties", ps)); err != nil {
return
}
items := make([]*types.DeviceProperty, 0)
if err = json.Unmarshal(res.Result, &items); err != nil {
return
}
for _, row := range items {
for _, p := range ps {
if p.SIID == row.SIID && p.PIID == row.PIID {
p.Value = row.Value
p.Code = row.Code
break
}
}
}
return
}
// SetProperties set device properties
func (device *Device) SetProperties(ctx context.Context, ps ...*types.DeviceProperty) (err error) {
var (
res *CommandResponse
)
for _, p := range ps {
if p.DID == "" {
p.DID = strconv.FormatUint(uint64(device.conn.deviceID), 10)
}
}
if res, err = device.conn.Execute(ctx, NewCommandRequest("set_properties", ps)); err == nil {
return
}
items := make([]*types.DeviceProperty, 0)
if err = json.Unmarshal(res.Result, &items); err != nil {
return
}
for _, row := range items {
for _, p := range ps {
if p.SIID == row.SIID && p.PIID == row.PIID {
if row.Value != nil {
p.Value = row.Value
}
p.Code = row.Code
break
}
}
}
return
}
// Action execute an action
func (device *Device) Action(ctx context.Context, action *types.DeviceAction) (buf []byte, err error) {
var (
res *CommandResponse
)
if action.DID == "" {
action.DID = strconv.Itoa(int(device.conn.deviceID))
}
if res, err = device.conn.Execute(ctx, NewCommandRequest("action", action)); err != nil {
return
}
buf = res.Result
return
}
// Execute execute command
func (device *Device) Execute(ctx context.Context, method string, args ...string) (buf []byte, err error) {
var (
res *CommandResponse
)
if res, err = device.conn.Execute(ctx, NewCommandRequest(method, args)); err != nil {
return
}
buf = res.Result
return
}
// Close stop device connection
func (device *Device) Close() (err error) {
return device.conn.Close()
}
func NewDevice(address string, token string) *Device {
return &Device{
address: address,
token: token,
conn: &Conn{},
}
}